1+ # -*- coding: utf-8 -*-
2+ """
3+ Copyright (C) 2012-2022 Diego Torres Milano
4+ Created on Nov 7, 2022
5+
6+ Licensed under the Apache License, Version 2.0 (the "License");
7+ you may not use this file except in compliance with the License.
8+ You may obtain a copy of the License at
9+
10+ http://www.apache.org/licenses/LICENSE-2.0
11+
12+ Unless required by applicable law or agreed to in writing, software
13+ distributed under the License is distributed on an "AS IS" BASIS,
14+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+ See the License for the specific language governing permissions and
16+ limitations under the License.
17+
18+ @author: Diego Torres Milano
19+ """
20+
121import sys
222from collections import OrderedDict
323
424from culebratester_client import Selector
525from culebratester_client .rest import ApiException
626
727from com .dtmilano .android .distance import levenshtein_distance
28+ from com .dtmilano .android .uiautomator .utils import window_hierarchy_to_selector_list
829
930#
1031# https://en.wikipedia.org/wiki/Kato_(The_Green_Hornet)
1132#
1233
1334DEBUG = False
1435
15- ANYTHING = 'Pattern:^.*$'
16-
1736
1837class Kato :
1938 def __init__ (self ):
@@ -25,11 +44,13 @@ def __init__(self):
2544def kato (func ):
2645 """
2746 Kato decorator.
28- @param func:the function to invoke
29- @type func:
30- @return: the wrapper
31- @rtype:
47+
48+ :param func: the function to invoke
49+ :type func: function
50+ :return: the wrapper
51+ :rtype:
3252 """
53+
3354 def wrapper (* args , ** kwargs ):
3455 try :
3556 return func (* args , ** kwargs )
@@ -57,18 +78,18 @@ def find_me_the_selectors(e: ApiException, *args, **kwargs):
5778 print ('find_me_the_selectors' , args , kwargs , file = sys .stderr )
5879 helper = args [0 ].uiAutomatorHelper
5980 msg = ''
60- _d = dict ()
6181 if helper .kato .enabled :
6282 if e .status == 404 :
6383 distance = kwargs ['distance_func' ]
6484 mapper = kwargs ['distance_func_argument_mapper' ]
65- helper .kato .selectors = list (map (lambda oid : helper .ui_object2 .dump (oid ),
66- map (lambda obj_ref : obj_ref .oid ,
67- helper .ui_device .find_objects (body = {'text' : ANYTHING }))))
68- for n , s in enumerate (filter (lambda _s : helper .ui_device .has_object (body = _s ), helper .kato .selectors )):
85+ selector = Selector (** kwargs ['body' ])
86+ helper .kato .selectors = window_hierarchy_to_selector_list (
87+ helper .ui_device .dump_window_hierarchy (_format = 'JSON' ))
88+ _d = dict ()
89+ for n , s in enumerate (helper .kato .selectors ):
6990 if n == 0 :
7091 msg += 'Kato: selector distances:\n '
71- d = distance (mapper (Selector ( ** kwargs [ 'body' ]) ), mapper (s ))
92+ d = distance (mapper (selector ), mapper (s ))
7293 _d [d ] = s
7394 msg += f'{ d } -> { s } \n '
7495 helper .kato .distances = OrderedDict (sorted (_d .items (), reverse = False ))
0 commit comments