Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fixed various lint issues #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def install_regexes():
yaml_src = os.path.join(cwd, 'uap-core', 'regexes.yaml')
if not os.path.exists(yaml_src):
raise RuntimeError(
'Unable to find regexes.yaml, should be at %r' % yaml_src)
'Unable to find regexes.yaml, should be at %r' % yaml_src)

print('Converting regexes.yaml to regexes.json...')
import json
Expand Down
38 changes: 21 additions & 17 deletions ua_parser/user_agent_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ def __init__(self, pattern, regex_flag=None, device_replacement=None, brand_repl
"""
self.pattern = pattern
if regex_flag == 'i':
self.user_agent_re = re.compile(self.pattern, re.IGNORECASE)
self.user_agent_re = re.compile(self.pattern, re.IGNORECASE)
else:
self.user_agent_re = re.compile(self.pattern)
self.user_agent_re = re.compile(self.pattern)
self.device_replacement = device_replacement
self.brand_replacement = brand_replacement
self.model_replacement = model_replacement
Expand All @@ -175,11 +175,11 @@ def MatchSpans(self, user_agent_string):

def MultiReplace(self, string, match):
def _repl(m):
index = int(m.group(1)) - 1
group = match.groups()
if index < len(group):
return group[index]
return ''
index = int(m.group(1)) - 1
group = match.groups()
if index < len(group):
return group[index]
return ''

_string = re.sub(r'\$(\d)', _repl, string)
_string = re.sub(r'^\s+|\s+$', '', _string)
Expand Down Expand Up @@ -261,8 +261,10 @@ def ParseUserAgent(user_agent_string, **jsParseBits):
# Override for Chrome Frame IFF Chrome is enabled.
if 'js_user_agent_string' in jsParseBits:
js_user_agent_string = jsParseBits['js_user_agent_string']
if (js_user_agent_string and js_user_agent_string.find('Chrome/') > -1 and
user_agent_string.find('chromeframe') > -1):
if (
js_user_agent_string and js_user_agent_string.find('Chrome/') > -1 and
user_agent_string.find('chromeframe') > -1
):
jsOverride = {}
jsOverride = ParseUserAgent(js_user_agent_string)
family = 'Chrome Frame (%s %s)' % (family, v1)
Expand Down Expand Up @@ -314,7 +316,7 @@ def ParseDevice(user_agent_string):
if device:
break

if device == None:
if device is None:
device = 'Other'

return {
Expand Down Expand Up @@ -381,8 +383,10 @@ def ParseWithJSOverrides(user_agent_string,
break

# Override for Chrome Frame IFF Chrome is enabled.
if (js_user_agent_string and js_user_agent_string.find('Chrome/') > -1 and
user_agent_string.find('chromeframe') > -1):
if (
js_user_agent_string and js_user_agent_string.find('Chrome/') > -1 and
user_agent_string.find('chromeframe') > -1
):
family = 'Chrome Frame (%s %s)' % (family, v1)
ua_dict = ParseUserAgent(js_user_agent_string)
v1 = ua_dict['major']
Expand Down Expand Up @@ -438,11 +442,11 @@ def GetFilters(user_agent_string, js_user_agent_string=None,
"""
filters = {}
filterdict = {
'js_user_agent_string': js_user_agent_string,
'js_user_agent_family': js_user_agent_family,
'js_user_agent_v1': js_user_agent_v1,
'js_user_agent_v2': js_user_agent_v2,
'js_user_agent_v3': js_user_agent_v3
'js_user_agent_string': js_user_agent_string,
'js_user_agent_family': js_user_agent_family,
'js_user_agent_v1': js_user_agent_v1,
'js_user_agent_v2': js_user_agent_v2,
'js_user_agent_v3': js_user_agent_v3
}
for key, value in filterdict.items():
if value is not None and value != '':
Expand Down
117 changes: 61 additions & 56 deletions ua_parser/user_agent_parser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,31 +72,31 @@ def testPGTSStrings(self):
def testParseAll(self):
user_agent_string = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; fr; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5,gzip(gfe),gzip(gfe)'
expected = {
'device': {
'family': 'Other',
'brand': None,
'model': None
},
'os': {
'family': 'Mac OS X',
'major': '10',
'minor': '4',
'patch': None,
'patch_minor': None
},
'user_agent': {
'family': 'Firefox',
'major': '3',
'minor': '5',
'patch': '5'
},
'string': user_agent_string
'device': {
'family': 'Other',
'brand': None,
'model': None
},
'os': {
'family': 'Mac OS X',
'major': '10',
'minor': '4',
'patch': None,
'patch_minor': None
},
'user_agent': {
'family': 'Firefox',
'major': '3',
'minor': '5',
'patch': '5'
},
'string': user_agent_string
}

result = user_agent_parser.Parse(user_agent_string)
self.assertEqual(result, expected,
self.assertEqual(
result, expected,
"UA: {0}\n expected<{1}> != actual<{2}>".format(user_agent_string, expected, result))

# Make a YAML file for manual comparsion with pgts_browser_list-orig.yaml
def makePGTSComparisonYAML(self):
import codecs
Expand Down Expand Up @@ -146,11 +146,12 @@ def runUserAgentTestsFromYAML(self, file_name):

result = {}
result = user_agent_parser.ParseUserAgent(user_agent_string, **kwds)
self.assertEqual(result, expected,
"UA: {0}\n expected<{1}, {2}, {3}, {4}> != actual<{5}, {6}, {7}, {8}>".format(\
user_agent_string,
expected['family'], expected['major'], expected['minor'], expected['patch'],
result['family'], result['major'], result['minor'], result['patch']))
self.assertEqual(
result, expected,
"UA: {0}\n expected<{1}, {2}, {3}, {4}> != actual<{5}, {6}, {7}, {8}>".format(
user_agent_string,
expected['family'], expected['major'], expected['minor'], expected['patch'],
result['family'], result['major'], result['minor'], result['patch']))

def runOSTestsFromYAML(self, file_name):
yamlFile = open(os.path.join(TEST_RESOURCES_DIR, file_name))
Expand All @@ -166,27 +167,28 @@ def runOSTestsFromYAML(self, file_name):

# The expected results
expected = {
'family': test_case['family'],
'major': test_case['major'],
'minor': test_case['minor'],
'patch': test_case['patch'],
'patch_minor': test_case['patch_minor']
'family': test_case['family'],
'major': test_case['major'],
'minor': test_case['minor'],
'patch': test_case['patch'],
'patch_minor': test_case['patch_minor']
}

result = user_agent_parser.ParseOS(user_agent_string, **kwds)
self.assertEqual(result, expected,
"UA: {0}\n expected<{1} {2} {3} {4} {5}> != actual<{6} {7} {8} {9} {10}>".format(\
user_agent_string,
expected['family'],
expected['major'],
expected['minor'],
expected['patch'],
expected['patch_minor'],
result['family'],
result['major'],
result['minor'],
result['patch'],
result['patch_minor']))
self.assertEqual(
result, expected,
"UA: {0}\n expected<{1} {2} {3} {4} {5}> != actual<{6} {7} {8} {9} {10}>".format(
user_agent_string,
expected['family'],
expected['major'],
expected['minor'],
expected['patch'],
expected['patch_minor'],
result['family'],
result['major'],
result['minor'],
result['patch'],
result['patch_minor']))

def runDeviceTestsFromYAML(self, file_name):
yamlFile = open(os.path.join(TEST_RESOURCES_DIR, file_name))
Expand All @@ -202,13 +204,14 @@ def runDeviceTestsFromYAML(self, file_name):

# The expected results
expected = {
'family': test_case['family'],
'brand': test_case['brand'],
'model': test_case['model']
'family': test_case['family'],
'brand': test_case['brand'],
'model': test_case['model']
}

result = user_agent_parser.ParseDevice(user_agent_string, **kwds)
self.assertEqual(result, expected,
self.assertEqual(
result, expected,
"UA: {0}\n expected<{1} {2} {3}> != actual<{4} {5} {6}>".format(
user_agent_string,
expected['family'],
Expand All @@ -223,23 +226,25 @@ class GetFiltersTest(unittest.TestCase):
def testGetFiltersNoMatchesGiveEmptyDict(self):
user_agent_string = 'foo'
filters = user_agent_parser.GetFilters(
user_agent_string, js_user_agent_string=None)
user_agent_string, js_user_agent_string=None)
self.assertEqual({}, filters)

def testGetFiltersJsUaPassedThrough(self):
user_agent_string = 'foo'
filters = user_agent_parser.GetFilters(
user_agent_string, js_user_agent_string='bar')
user_agent_string, js_user_agent_string='bar')
self.assertEqual({'js_user_agent_string': 'bar'}, filters)

def testGetFiltersJsUserAgentFamilyAndVersions(self):
user_agent_string = ('Mozilla/4.0 (compatible; MSIE 8.0; '
'Windows NT 5.1; Trident/4.0; GTB6; .NET CLR 2.0.50727; '
'.NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)')
user_agent_string = (
'Mozilla/4.0 (compatible; MSIE 8.0; '
'Windows NT 5.1; Trident/4.0; GTB6; .NET CLR 2.0.50727; '
'.NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)')
filters = user_agent_parser.GetFilters(
user_agent_string, js_user_agent_string='bar',
js_user_agent_family='foo')
self.assertEqual({'js_user_agent_string': 'bar',
user_agent_string, js_user_agent_string='bar',
js_user_agent_family='foo')
self.assertEqual({
'js_user_agent_string': 'bar',
'js_user_agent_family': 'foo'}, filters)


Expand Down