|
1 | | -import sys |
2 | 1 | from collections import namedtuple |
3 | 2 |
|
4 | 3 | from ua_parser import user_agent_parser |
5 | | - |
6 | | - |
7 | | -PY2 = sys.version_info[0] == 2 |
8 | | -PY3 = sys.version_info[0] == 3 |
9 | | - |
10 | | -if PY3: |
11 | | - string_types = str |
12 | | -else: |
13 | | - string_types = basestring |
| 4 | +from .compat import string_types |
14 | 5 |
|
15 | 6 |
|
16 | 7 | MOBILE_DEVICE_FAMILIES = ( |
|
74 | 65 | ) |
75 | 66 |
|
76 | 67 |
|
| 68 | +def verify_attribute(attribute): |
| 69 | + if isinstance(attribute, string_types) and attribute.isdigit(): |
| 70 | + return int(attribute) |
| 71 | + |
| 72 | + return attribute |
| 73 | + |
| 74 | + |
77 | 75 | def parse_version(major=None, minor=None, patch=None, patch_minor=None): |
78 | 76 | # Returns version number tuple, attributes will be integer if they're numbers |
79 | | - if major is not None and isinstance(major, string_types): |
80 | | - major = int(major) if major.isdigit() else major |
81 | | - if minor is not None and isinstance(minor, string_types): |
82 | | - minor = int(minor) if minor.isdigit() else minor |
83 | | - if patch is not None and isinstance(patch, string_types): |
84 | | - patch = int(patch) if patch.isdigit() else patch |
85 | | - if patch_minor is not None and isinstance(patch_minor, string_types): |
86 | | - patch_minor = int(patch_minor) if patch_minor.isdigit() else patch_minor |
87 | | - if patch_minor: |
88 | | - return (major, minor, patch, patch_minor) |
89 | | - elif patch: |
90 | | - return (major, minor, patch) |
91 | | - elif minor: |
92 | | - return (major, minor) |
93 | | - elif major: |
94 | | - return (major,) |
95 | | - else: |
96 | | - return tuple() |
| 77 | + major = verify_attribute(major) |
| 78 | + minor = verify_attribute(minor) |
| 79 | + patch = verify_attribute(patch) |
| 80 | + patch_minor = verify_attribute(patch_minor) |
| 81 | + |
| 82 | + return tuple(filter(None, (major, minor, patch, patch_minor))) |
97 | 83 |
|
98 | 84 |
|
99 | 85 | Browser = namedtuple('Browser', ['family', 'version', 'version_string']) |
|
0 commit comments