Skip to content

Commit

Permalink
Merge pull request #329 from giantpune/python_3.21_regex_fix
Browse files Browse the repository at this point in the history
add 'r' in front of some regex strings.
  • Loading branch information
dtmilano authored Sep 27, 2024
2 parents 8738d3a + cf491c7 commit 5a6fa78
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
26 changes: 13 additions & 13 deletions src/com/dtmilano/android/adb/adbclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ def shell(self, _cmd=None, _convertOutputToString=True):
def getRestrictedScreen(self):
''' Gets C{mRestrictedScreen} values from dumpsys. This is a method to obtain display dimensions '''

rsRE = re.compile('\s*mRestrictedScreen=\((?P<x>\d+),(?P<y>\d+)\) (?P<w>\d+)x(?P<h>\d+)')
rsRE = re.compile(r'\s*mRestrictedScreen=\((?P<x>\d+),(?P<y>\d+)\) (?P<w>\d+)x(?P<h>\d+)')
for line in self.shell('dumpsys window').splitlines():
m = rsRE.match(line)
if m:
Expand Down Expand Up @@ -670,7 +670,7 @@ def getPhysicalDisplayInfo(self):
''' Gets C{mPhysicalDisplayInfo} values from dumpsys. This is a method to obtain display dimensions and density'''

self.__checkTransport()
phyDispRE = re.compile('Physical size: (?P<width>\d+)x(?P<height>\d+).*Physical density: (?P<density>\d+)',
phyDispRE = re.compile(r'Physical size: (?P<width>\d+)x(?P<height>\d+).*Physical density: (?P<density>\d+)',
re.DOTALL)
m = phyDispRE.search(self.shell('wm size; wm density'))
if m:
Expand All @@ -682,7 +682,7 @@ def getPhysicalDisplayInfo(self):
return displayInfo

phyDispRE = re.compile(
'.*PhysicalDisplayInfo{(?P<width>\d+) x (?P<height>\d+), .*, density (?P<density>[\d.]+).*')
r'.*PhysicalDisplayInfo{(?P<width>\d+) x (?P<height>\d+), .*, density (?P<density>[\d.]+).*')
for line in self.shell('dumpsys display').splitlines():
m = phyDispRE.search(line, 0)
if m:
Expand All @@ -695,9 +695,9 @@ def getPhysicalDisplayInfo(self):
return displayInfo

# This could also be mSystem or mOverscanScreen
phyDispRE = re.compile('\s*mUnrestrictedScreen=\((?P<x>\d+),(?P<y>\d+)\) (?P<width>\d+)x(?P<height>\d+)')
phyDispRE = re.compile(r'\s*mUnrestrictedScreen=\((?P<x>\d+),(?P<y>\d+)\) (?P<width>\d+)x(?P<height>\d+)')
# This is known to work on older versions (i.e. API 10) where mrestrictedScreen is not available
dispWHRE = re.compile('\s*DisplayWidth=(?P<width>\d+) *DisplayHeight=(?P<height>\d+)')
dispWHRE = re.compile(r'\s*DisplayWidth=(?P<width>\d+) *DisplayHeight=(?P<height>\d+)')
for line in self.shell('dumpsys window').splitlines():
m = phyDispRE.search(line, 0)
if not m:
Expand Down Expand Up @@ -743,7 +743,7 @@ def __getDisplayOrientation(self, key, strip=True):
return displayInfo['orientation']
# Fallback method to obtain the orientation
# See https://github.com/dtmilano/AndroidViewClient/issues/128
surfaceOrientationRE = re.compile('SurfaceOrientation:\s+(\d+)')
surfaceOrientationRE = re.compile(r'SurfaceOrientation:\s+(\d+)')
output = self.shell('dumpsys input')
m = surfaceOrientationRE.search(output)
if m:
Expand Down Expand Up @@ -1338,21 +1338,21 @@ def getWindows(self):
dww = self.shell('dumpsys window windows')
if DEBUG_WINDOWS: print(dww, file=sys.stderr)
lines = dww.splitlines()
widRE = re.compile('^ *Window #%s Window\{%s (u\d+ )?%s?.*\}:' %
widRE = re.compile(r'^ *Window #%s Window\{%s (u\d+ )?%s?.*\}:' %
(_nd('num'), _nh('winId'), _ns('activity', greedy=True)))
currentFocusRE = re.compile('^ mCurrentFocus=Window\{%s .*' % _nh('winId'))
currentFocusRE = re.compile(r'^ mCurrentFocus=Window\{%s .*' % _nh('winId'))
viewVisibilityRE = re.compile(' mViewVisibility=0x%s ' % _nh('visibility'))
# This is for 4.0.4 API-15
containingFrameRE = re.compile('^ *mContainingFrame=\[%s,%s\]\[%s,%s\] mParentFrame=\[%s,%s\]\[%s,%s\]' %
containingFrameRE = re.compile(r'^ *mContainingFrame=\[%s,%s\]\[%s,%s\] mParentFrame=\[%s,%s\]\[%s,%s\]' %
(_nd('cx'), _nd('cy'), _nd('cw'), _nd('ch'), _nd('px'), _nd('py'), _nd('pw'),
_nd('ph')))
contentFrameRE = re.compile('^ *mContentFrame=\[%s,%s\]\[%s,%s\] mVisibleFrame=\[%s,%s\]\[%s,%s\]' %
contentFrameRE = re.compile(r'^ *mContentFrame=\[%s,%s\]\[%s,%s\] mVisibleFrame=\[%s,%s\]\[%s,%s\]' %
(_nd('x'), _nd('y'), _nd('w'), _nd('h'), _nd('vx'), _nd('vy'), _nd('vx1'),
_nd('vy1')))
# This is for 4.1 API-16
framesRE = re.compile('^ *Frames: containing=\[%s,%s\]\[%s,%s\] parent=\[%s,%s\]\[%s,%s\]' %
framesRE = re.compile(r'^ *Frames: containing=\[%s,%s\]\[%s,%s\] parent=\[%s,%s\]\[%s,%s\]' %
(_nd('cx'), _nd('cy'), _nd('cw'), _nd('ch'), _nd('px'), _nd('py'), _nd('pw'), _nd('ph')))
contentRE = re.compile('^ *content=\[%s,%s\]\[%s,%s\] visible=\[%s,%s\]\[%s,%s\]' %
contentRE = re.compile(r'^ *content=\[%s,%s\]\[%s,%s\] visible=\[%s,%s\]\[%s,%s\]' %
(_nd('x'), _nd('y'), _nd('w'), _nd('h'), _nd('vx'), _nd('vy'), _nd('vx1'), _nd('vy1')))
policyVisibilityRE = re.compile('mPolicyVisibility=%s ' % _ns('policyVisibility', greedy=True))

Expand Down Expand Up @@ -1463,7 +1463,7 @@ def getFocusedWindowName(self):

def getTopActivityNameAndPid(self):
dat = self.shell('dumpsys activity top')
activityRE = re.compile('\s*ACTIVITY ([A-Za-z0-9_.]+)/([A-Za-z0-9_.\$]+) \w+ pid=(\d+)')
activityRE = re.compile(r'\s*ACTIVITY ([A-Za-z0-9_.]+)/([A-Za-z0-9_.\$]+) \w+ pid=(\d+)')
m = activityRE.findall(dat)
if len(m) > 0:
return m[-1]
Expand Down
32 changes: 16 additions & 16 deletions src/com/dtmilano/android/viewclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@
GONE = 0x8

RegexType = type(re.compile(''))
IP_RE = re.compile('^(\d{1,3}\.){3}\d{1,3}$')
ID_RE = re.compile('id/([^/]*)(/(\d+))?')
IP_RE = re.compile(r'^(\d{1,3}\.){3}\d{1,3}$')
ID_RE = re.compile(r'id/([^/]*)(/(\d+))?')
IP_DOMAIN_NAME_PORT_REGEX = r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' \
r'localhost|' \
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' \
Expand Down Expand Up @@ -872,21 +872,21 @@ def __dumpWindowsInformation(self, debug=False):
dww = self.device.shell('dumpsys window windows')
if DEBUG_WINDOWS or debug: print(dww, file=sys.stderr)
lines = dww.splitlines()
widRE = re.compile('^ *Window #%s Window\{%s (u\d+ )?%s?.*\}:' %
widRE = re.compile(r'^ *Window #%s Window\{%s (u\d+ )?%s?.*\}:' %
(_nd('num'), _nh('winId'), _ns('activity', greedy=True)))
currentFocusRE = re.compile('^ mCurrentFocus=Window\{%s .*' % _nh('winId'))
currentFocusRE = re.compile(r'^ mCurrentFocus=Window\{%s .*' % _nh('winId'))
viewVisibilityRE = re.compile(' mViewVisibility=0x%s ' % _nh('visibility'))
# This is for 4.0.4 API-15
containingFrameRE = re.compile('^ *mContainingFrame=\[%s,%s\]\[%s,%s\] mParentFrame=\[%s,%s\]\[%s,%s\]' %
containingFrameRE = re.compile(r'^ *mContainingFrame=\[%s,%s\]\[%s,%s\] mParentFrame=\[%s,%s\]\[%s,%s\]' %
(_nd('cx'), _nd('cy'), _nd('cw'), _nd('ch'), _nd('px'), _nd('py'), _nd('pw'),
_nd('ph')))
contentFrameRE = re.compile('^ *mContentFrame=\[%s,%s\]\[%s,%s\] mVisibleFrame=\[%s,%s\]\[%s,%s\]' %
contentFrameRE = re.compile(r'^ *mContentFrame=\[%s,%s\]\[%s,%s\] mVisibleFrame=\[%s,%s\]\[%s,%s\]' %
(_nd('x'), _nd('y'), _nd('w'), _nd('h'), _nd('vx'), _nd('vy'), _nd('vx1'),
_nd('vy1')))
# This is for 4.1 API-16
framesRE = re.compile('^ *Frames: containing=\[%s,%s\]\[%s,%s\] parent=\[%s,%s\]\[%s,%s\]' %
framesRE = re.compile(r'^ *Frames: containing=\[%s,%s\]\[%s,%s\] parent=\[%s,%s\]\[%s,%s\]' %
(_nd('cx'), _nd('cy'), _nd('cw'), _nd('ch'), _nd('px'), _nd('py'), _nd('pw'), _nd('ph')))
contentRE = re.compile('^ *content=\[%s,%s\]\[%s,%s\] visible=\[%s,%s\]\[%s,%s\]' %
contentRE = re.compile(r'^ *content=\[%s,%s\]\[%s,%s\] visible=\[%s,%s\]\[%s,%s\]' %
(_nd('x'), _nd('y'), _nd('w'), _nd('h'), _nd('vx'), _nd('vy'), _nd('vx1'), _nd('vy1')))
policyVisibilityRE = re.compile('mPolicyVisibility=%s ' % _ns('policyVisibility', greedy=True))

Expand Down Expand Up @@ -1237,7 +1237,7 @@ def __tinyStr__(self):
# __str = str("View[", 'utf-8', 'replace')
__str = "View["
if "class" in self.map:
__str += " class=" + re.sub('.*\.', '', self.map['class'])
__str += " class=" + re.sub(r'.*\.', '', self.map['class'])
__str += " id=%s" % self.getId()
__str += " ]"

Expand Down Expand Up @@ -2394,7 +2394,7 @@ def StartElement(self, name, attributes):
elif name == 'node':
# Instantiate an Element object
attributes['uniqueId'] = 'id/no_id/%d' % self.idCount
bounds = re.split('[\][,]', attributes['bounds'])
bounds = re.split(r'[\][,]', attributes['bounds'])
attributes['bounds'] = ((int(bounds[1]), int(bounds[2])), (int(bounds[4]), int(bounds[5])))
if DEBUG_BOUNDS:
print("bounds=", attributes['bounds'], file=sys.stderr)
Expand Down Expand Up @@ -3224,8 +3224,8 @@ def __splitAttrs(self, strArgs):
s2 = s1.replace(' ', WS)
strArgs = strArgs.replace(s1, s2, 1)

idRE = re.compile("(?P<viewId>id/\S+)")
attrRE = re.compile('%s(?P<parens>\(\))?=%s,(?P<val>[^ ]*)' % (_ns('attr'), _nd('len')), flags=re.DOTALL)
idRE = re.compile(r"(?P<viewId>id/\S+)")
attrRE = re.compile(r'%s(?P<parens>\(\))?=%s,(?P<val>[^ ]*)' % (_ns('attr'), _nd('len')), flags=re.DOTALL)
hashRE = re.compile('%s@%s' % (_ns('class'), _nh('oid')))

attrs = {}
Expand Down Expand Up @@ -3269,7 +3269,7 @@ def __splitAttrs(self, strArgs):
# sometimes the view ids are not unique, so let's generate a unique id here
i = 1
while True:
newId = re.sub('/\d+$', '', viewId) + '/%d' % i
newId = re.sub(r'/\d+$', '', viewId) + '/%d' % i
if not newId in self.viewsById:
break
i += 1
Expand Down Expand Up @@ -3579,13 +3579,13 @@ def dump(self, window=-1, sleep=1):
if self.ignoreUiAutomatorKilled:
if DEBUG_RECEIVED:
print("ignoring UiAutomator Killed", file=sys.stderr)
killedRE = re.compile('</hierarchy>[\n\S]*Killed', re.MULTILINE)
killedRE = re.compile(r'</hierarchy>[\n\S]*Killed', re.MULTILINE)
if killedRE.search(received):
received = re.sub(killedRE, '</hierarchy>', received)
elif DEBUG_RECEIVED:
print("UiAutomator Killed: NOT FOUND!")
# It seems that API18 uiautomator spits this message to stdout
dumpedToDevTtyRE = re.compile('</hierarchy>[\n\S]*UI hierchary dumped to: /dev/tty.*', re.MULTILINE)
dumpedToDevTtyRE = re.compile(r'</hierarchy>[\n\S]*UI hierchary dumped to: /dev/tty.*', re.MULTILINE)
if dumpedToDevTtyRE.search(received):
received = re.sub(dumpedToDevTtyRE, '</hierarchy>', received)
if DEBUG_RECEIVED:
Expand All @@ -3595,7 +3595,7 @@ def dump(self, window=-1, sleep=1):
received = received.replace(
'WARNING: linker: libdvm.so has text relocations. This is wasting memory and is a security risk. Please fix.\r\n',
'')
if re.search('\[: not found', received):
if re.search(r'\[: not found', received):
raise RuntimeError('''ERROR: Some emulator images (i.e. android 4.1.2 API 16 generic_x86) does not include the '[' command.
While UiAutomator back-end might be supported 'uiautomator' command fails.
You should force ViewServer back-end.''')
Expand Down

0 comments on commit 5a6fa78

Please sign in to comment.