Skip to content

Commit

Permalink
Culebra: Added output option
Browse files Browse the repository at this point in the history
- Fixed triple quote escaping
  • Loading branch information
dtmilano committed Apr 23, 2013
1 parent 9e3ca3a commit e2462b5
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions AndroidViewClient/tools/culebra
Expand Up @@ -14,7 +14,7 @@ ___________________/ /__/ /__/ /__/ /________________________________
| / \ / \ / \ / \ \___ | / \ / \ / \ / \ \___
|/ \_/ \_/ \_/ \ o \ |/ \_/ \_/ \_/ \ o \
\_____/--< \_____/--<
@author: diego @author: Diego Torres Milano
@author: Jennifer E. Swofford (ascii art snake) @author: Jennifer E. Swofford (ascii art snake)
''' '''


Expand Down Expand Up @@ -60,17 +60,20 @@ UNIT_TEST = 'unit-test'
USE_JAR = 'use-jar' USE_JAR = 'use-jar'
USE_DICTIONARY = 'use-dictionary' USE_DICTIONARY = 'use-dictionary'
AUTO_REGEXPS = 'auto-regexps' AUTO_REGEXPS = 'auto-regexps'
OUTPUT = 'output'


USAGE = 'usage: %s [OPTION]... [serialno]' USAGE = 'usage: %s [OPTION]... [serialno]'
# -u,-s,-p,-v eaten by monkeyrunner # -u,-s,-p,-v eaten by monkeyrunner
SHORT_OPTS = 'HVIFSi:t:d:rCUj:D:R:' SHORT_OPTS = 'HVIFSi:t:d:rCUj:D:R:o:'


LONG_OPTS = [HELP, VERBOSE, IGNORE_SECURE_DEVICE, FORCE_VIEW_SERVER_USE, DO_NOT_START_VIEW_SERVER, LONG_OPTS = [HELP, VERBOSE, IGNORE_SECURE_DEVICE, FORCE_VIEW_SERVER_USE, DO_NOT_START_VIEW_SERVER,
FIND_VIEWS_BY_ID + '=', FIND_VIEWS_WITH_TEXT + '=', FIND_VIEWS_WITH_CONTENT_DESCRIPTION + '=', FIND_VIEWS_BY_ID + '=', FIND_VIEWS_WITH_TEXT + '=', FIND_VIEWS_WITH_CONTENT_DESCRIPTION + '=',
USE_REGEXPS, VERBOSE_COMMENTS, UNIT_TEST, USE_REGEXPS, VERBOSE_COMMENTS, UNIT_TEST,
USE_JAR + '=', USE_DICTIONARY + '=', AUTO_REGEXPS + '='] USE_JAR + '=', USE_DICTIONARY + '=', AUTO_REGEXPS + '=',
OUTPUT + '=']
LONG_OPTS_ARG = {FIND_VIEWS_BY_ID: 'BOOL', FIND_VIEWS_WITH_TEXT: 'BOOL', FIND_VIEWS_WITH_CONTENT_DESCRIPTION: 'BOOL', LONG_OPTS_ARG = {FIND_VIEWS_BY_ID: 'BOOL', FIND_VIEWS_WITH_TEXT: 'BOOL', FIND_VIEWS_WITH_CONTENT_DESCRIPTION: 'BOOL',
USE_JAR: 'BOOL', USE_DICTIONARY: 'BOOL', AUTO_REGEXPS: 'LIST'} USE_JAR: 'BOOL', USE_DICTIONARY: 'BOOL', AUTO_REGEXPS: 'LIST',
OUTPUT: 'STR'}
OPTS_HELP = { OPTS_HELP = {
'H': 'prints this help', 'H': 'prints this help',
'i': 'whether to use findViewById() in script', 'i': 'whether to use findViewById() in script',
Expand All @@ -81,6 +84,7 @@ OPTS_HELP = {
'j': 'use jar and appropriate shebang to run script', 'j': 'use jar and appropriate shebang to run script',
'D': 'use a dictionary to store the Views found', 'D': 'use a dictionary to store the Views found',
'R': 'auto regexps (i.e. clock)', 'R': 'auto regexps (i.e. clock)',
'o': 'output filename',
} }
ID_RE = re.compile('id/([^/]*)(/(\d+))?') ID_RE = re.compile('id/([^/]*)(/(\d+))?')
AUTO_REGEXPS_RES = {'clock': re.compile('[012]\d:[0-5]\d')} AUTO_REGEXPS_RES = {'clock': re.compile('[012]\d:[0-5]\d')}
Expand Down Expand Up @@ -184,6 +188,9 @@ def printFindViewWithText(view, useregexp):
text = view.getText() text = view.getText()
if text: if text:
var = variableNameFromId(id) var = variableNameFromId(id)
if text.find("\n") > 0:
# 2 quotes + 1 quote = 3 quotes
text = "''%s''" % text
if useregexp: if useregexp:
text = "re.compile('%s')" % text text = "re.compile('%s')" % text
else: else:
Expand Down Expand Up @@ -263,7 +270,7 @@ kwargs1 = {VERBOSE: False, 'ignoresecuredevice': False}
kwargs2 = {'forceviewserveruse': False, 'startviewserver': True} kwargs2 = {'forceviewserveruse': False, 'startviewserver': True}
options = {FIND_VIEWS_BY_ID: True, FIND_VIEWS_WITH_TEXT: False, FIND_VIEWS_WITH_CONTENT_DESCRIPTION: False, options = {FIND_VIEWS_BY_ID: True, FIND_VIEWS_WITH_TEXT: False, FIND_VIEWS_WITH_CONTENT_DESCRIPTION: False,
USE_REGEXPS: False, VERBOSE_COMMENTS: False, USE_REGEXPS: False, VERBOSE_COMMENTS: False,
UNIT_TEST: False, USE_JAR: True, USE_DICTIONARY: False, AUTO_REGEXPS: None} UNIT_TEST: False, USE_JAR: True, USE_DICTIONARY: False, AUTO_REGEXPS: None, OUTPUT: None}
transform = traverseAndPrint transform = traverseAndPrint
for o, a in optlist: for o, a in optlist:
o = o.strip('-') o = o.strip('-')
Expand Down Expand Up @@ -300,6 +307,8 @@ for o, a in optlist:
if r not in AUTO_REGEXPS_RES: if r not in AUTO_REGEXPS_RES:
error("invalid auto regexp: %s\n" % (r)) error("invalid auto regexp: %s\n" % (r))
usage() usage()
elif o in ['o', OUTPUT]:
options[OUTPUT] = a


if not (options[FIND_VIEWS_BY_ID] or options[FIND_VIEWS_WITH_TEXT]): if not (options[FIND_VIEWS_BY_ID] or options[FIND_VIEWS_WITH_TEXT]):
if not options[VERBOSE_COMMENTS]: if not options[VERBOSE_COMMENTS]:
Expand All @@ -309,6 +318,12 @@ if not (options[FIND_VIEWS_BY_ID] or options[FIND_VIEWS_WITH_TEXT]):


device, serialno = ViewClient.connectToDeviceOrExit(**kwargs1) device, serialno = ViewClient.connectToDeviceOrExit(**kwargs1)
vc = ViewClient(device, serialno, **kwargs2) vc = ViewClient(device, serialno, **kwargs2)
if options[OUTPUT]:
sys.stdout = open(options[OUTPUT], 'w')
import stat
st = os.stat(options[OUTPUT])
os.chmod(options[OUTPUT], st.st_mode | stat.S_IEXEC)

print '''%s print '''%s
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
\'\'\' \'\'\'
Expand Down

0 comments on commit e2462b5

Please sign in to comment.