1 culebra: Fixed regexp escaping · codediving/AndroidViewClient@7047b0a · GitHub
Skip to content

Commit 7047b0a

Browse files
committed
culebra: Fixed regexp escaping
- Version 5.3.1 - Added auto-regexps help
1 parent 68d3f85 commit 7047b0a

1 file changed

Lines changed: 34 additions & 22 deletions

File tree

AndroidViewClient/tools/culebra

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ ___________________/ /__/ /__/ /__/ /________________________________
1313
| / \ / \ / \ / \ \___
1414
|/ \_/ \_/ \_/ \ o \
1515
\_____/--<
16+
1617
@author: Diego Torres Milano
1718
@author: Jennifer E. Swofford (ascii art snake)
1819
1920
'''
2021

21-
__version__ = '5.3.0'
22+
__version__ = '5.3.1'
2223

2324
import re
2425
import sys
@@ -86,7 +87,7 @@ OPTS_HELP = {
8687
'j': 'use jar and appropriate shebang to run script (deprecated)',
8788
'D': 'use a dictionary to store the Views found',
8889
'K': 'dictionary keys from: id, text, content-description',
89-
'R': 'auto regexps (i.e. clock)',
90+
'R': 'auto regexps (i.e. clock), implies -r. help list options',
9091
'a': 'starts Activity before dump',
9192
'o': 'output filename',
9293
'A': 'interactive',
@@ -115,10 +116,10 @@ def shortAndLongOptions():
115116
if len(short_opts) != len(LONG_OPTS):
116117
raise Exception('There is a mismatch between short and long options')
117118
t = tuple(short_opts) + tuple(LONG_OPTS)
118-
l2 = len(t)/2
119+
l2 = len(t) / 2
119120
sl = []
120121
for i in range(l2):
121-
sl.append((t[i], t[i+l2]))
122+
sl.append((t[i], t[i + l2]))
122123
return sl
123124

124125
def usage(exitVal=1):
@@ -141,8 +142,15 @@ def help():
141142
print >> sys.stderr, o
142143
sys.exit(0)
143144

145+
def autoRegexpsHelp():
146+
print >> sys.stderr, "Available %s options:" % AUTO_REGEXPS
147+
for r in AUTO_REGEXPS_RES:
148+
print >> sys.stderr, "\t%s: %s" % (r, AUTO_REGEXPS_RES[r].pattern)
149+
print >> sys.stderr
150+
sys.exit(0)
151+
144152
def error(msg, fatal=False):
145-
print >>sys.stderr, "%s: ERROR: %s" % (progname, msg)
153+
print >> sys.stderr, "%s: ERROR: %s" % (progname, msg)
146154
if fatal:
147155
sys.exit(1)
148156

@@ -217,12 +225,12 @@ def printFindViewWithText(view, useregexp):
217225
# 2 quotes + 1 quote = 3 quotes
218226
text = "''%s''" % text
219227
if useregexp:
220-
text = "re.compile(%c'''%s''')" % (u, text)
228+
text = "re.compile(%c'%s)" % (u, text)
221229
else:
222230
text = "%c'%s'" % (u, text)
223231
print '%s = vc.findViewWithTextOrRaise(%s)' % (var, text)
224232
elif kwargs1[VERBOSE]:
225-
warnings.warn('View with id=%s has no text' % view.getUniqueId())
233+
warnings.warn('View with id=%s has no text' % view.getUniqueId())
226234

227235
def printFindViewWithContentDescription(view, useregexp):
228236
'''
@@ -247,7 +255,7 @@ def printFindViewWithContentDescription(view, useregexp):
247255
contentDescription = "u'''%s'''" % contentDescription
248256
print '%s = %svc.findViewWithContentDescriptionOrRaise(%s)' % (var, prefix, contentDescription)
249257
elif kwargs1[VERBOSE]:
250-
warnings.warn('View with id=%s has no content-description' % view.getUniqueId())
258+
warnings.warn('View with id=%s has no content-description' % view.getUniqueId())
251259

252260
def printFindViewById(view):
253261
'''
@@ -323,7 +331,7 @@ options = {FIND_VIEWS_BY_ID: True, FIND_VIEWS_WITH_TEXT: False, FIND_VIEWS_WITH_
323331
USE_REGEXPS: False, VERBOSE_COMMENTS: False,
324332
UNIT_TEST: False, USE_JAR: False, USE_DICTIONARY: False, DICTIONARY_KEYS_FROM: 'id',
325333
AUTO_REGEXPS: None, START_ACTIVITY: None, OUTPUT: None, INTERACTIVE: False,
326-
WINDOW: -1, APPEND_TO_SYS_PATH: False}
334+
WINDOW:-1, APPEND_TO_SYS_PATH: False}
327335
transform = traverseAndPrint
328336
for o, a in optlist:
329337
o = o.strip('-')
@@ -352,7 +360,7 @@ for o, a in optlist:
352360
elif o in ['C', VERBOSE_COMMENTS]:
353361
options[VERBOSE_COMMENTS] = True
354362
elif o in ['U', UNIT_TEST]:
355-
#warnings.warn('Not implemented yet: %s' % o)
363+
# warnings.warn('Not implemented yet: %s' % o)
356364
options[UNIT_TEST] = True
357365
elif o in ['j', USE_JAR]:
358366
options[USE_JAR] = str2bool(a)
@@ -363,9 +371,13 @@ for o, a in optlist:
363371
elif o in ['R', AUTO_REGEXPS]:
364372
options[AUTO_REGEXPS] = a.split(',')
365373
for r in options[AUTO_REGEXPS]:
374+
if r == 'help':
375+
autoRegexpsHelp()
366376
if r not in AUTO_REGEXPS_RES:
367377
error("invalid auto regexp: %s\n" % (r))
368378
usage()
379+
# AUTO_REGEPXS implies USE_REGEXPS
380+
options[USE_REGEXPS] = True
369381
elif o in ['a', START_ACTIVITY]:
370382
options[START_ACTIVITY] = a
371383
elif o in ['o', OUTPUT]:
@@ -437,12 +449,12 @@ class CulebraTests(unittest.TestCase):
437449
438450
kwargs1 = None
439451
kwargs2 = None
440-
452+
441453
@classmethod
442454
def setUpClass(cls):
443455
cls.kwargs1 = %s
444456
cls.kwargs2 = %s
445-
457+
446458
def setUp(self):
447459
self.device, self.serialno = ViewClient.connectToDeviceOrExit(**CulebraTests.kwargs1)
448460
''' % (kwargs1, kwargs2),
@@ -451,7 +463,7 @@ class CulebraTests(unittest.TestCase):
451463
print '''\
452464
self.device.startActivity(component='%s')
453465
''' % options[START_ACTIVITY],
454-
466+
455467
print '''\
456468
self.vc = ViewClient(self.device, self.serialno, **CulebraTests.kwargs2)
457469
@@ -460,22 +472,22 @@ class CulebraTests(unittest.TestCase):
460472
461473
def preconditions(self):
462474
return True
463-
475+
464476
def testSomething(self):
465477
if not self.preconditions():
466478
self.fail('Preconditions failed')
467479
self.vc.dump(%s)
468480
''' % (getWindowOption())
469-
481+
470482
if options[USE_DICTIONARY]:
471483
print '''\
472484
self.views = dict()'''
473-
485+
474486
vc.dump(window=options[WINDOW])
475487
indent = ' ' * 7
476488
prefix = 'self.'
477489
vc.traverse(transform=transform)
478-
490+
479491
print '''
480492
@staticmethod
481493
def main():
@@ -490,21 +502,21 @@ else:
490502
kwargs1 = %s
491503
device, serialno = ViewClient.connectToDeviceOrExit(**kwargs1)
492504
''' % kwargs1,
493-
505+
494506
if options[START_ACTIVITY]:
495507
print '''\
496508
device.startActivity(component='%s')
497509
''' % options[START_ACTIVITY],
498-
510+
499511
print '''\
500512
kwargs2 = %s
501513
vc = ViewClient(device, serialno, **kwargs2)
502514
vc.dump(window=%s)
503515
''' % (kwargs2, getWindowOption())
504-
516+
505517
if options[USE_DICTIONARY]:
506518
print '''views = dict()'''
507-
519+
508520
vc.dump(window=options[WINDOW])
509521
vc.traverse(transform=transform)
510522

@@ -517,7 +529,7 @@ if options[INTERACTIVE]:
517529
s.sendall("RECORD EVENTS START\n")
518530
fin = open("/dev/tty")
519531
while True:
520-
print >>sys.stderr, "Reading events..."
532+
print >> sys.stderr, "Reading events..."
521533
data = s.recv(1024)
522534
code = ViewClient.excerpt(data)
523535
exec code

0 commit comments

Comments
 (0)