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

Skip to content

Commit 707a357

Browse files
committed
Merge pull request ua-parser#39 from ua-parser/compile
Pre-compile yaml file into py files
2 parents aae1341 + c8b8cbf commit 707a357

File tree

7 files changed

+219
-155
lines changed

7 files changed

+219
-155
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
*.pyc
22
*.egg-info/
33
.eggs/
4+
.cache/
5+
.tox/
46
build/
57
dist/
68
tmp/
79
regexes.yaml
8-
regexes.json
10+
_regexes.py

MANIFEST.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
include README.md
2-
include ua_parser/regexes.json

Makefile

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
1-
all: prep test
1+
all: test
22

3-
prep:
4-
#git submodule update --init
5-
#sudo apt-get install python-yaml
6-
7-
test:
8-
@#test ! -d tmp && mkdir tmp
9-
@export PYTHONPATH=tmp && python setup.py develop -d tmp
3+
test: clean
4+
@mkdir -p tmp
5+
@PYTHONPATH=tmp python setup.py develop -d tmp
106
@# run all tests
11-
@python ua_parser/user_agent_parser_test.py
7+
@PYTHONPATH=tmp python ua_parser/user_agent_parser_test.py
128
@# run a single test
13-
@#python ua_parser/user_agent_parser_test.py ParseTest.testStringsDeviceBrandModel
9+
@#PYTHONPATH=tmp python ua_parser/user_agent_parser_test.py ParseTest.testStringsDeviceBrandModel
1410

1511
clean:
16-
@rm -f ua_parser/user_agent_parser.pyc\
17-
ua_parser/regexes.yaml\
18-
ua_parser/regexes.json
12+
@find . -name '*.pyc' -delete
1913
@rm -rf tmp\
2014
ua_parser.egg-info\
2115
dist\
2216
build
2317

24-
.PHONY: all prep test clean
18+
.PHONY: all test clean

setup.py

Lines changed: 106 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,123 @@
11
#!/usr/bin/env python
2+
import os
3+
from distutils import log
4+
from distutils.core import Command
25
from setuptools import setup
36
from setuptools.command.develop import develop as _develop
47
from setuptools.command.sdist import sdist as _sdist
58

69

7-
def install_regexes():
8-
print('Copying regexes.yaml to package directory...')
9-
import os
10-
cwd = os.path.abspath(os.path.dirname(__file__))
11-
yaml_src = os.path.join(cwd, 'uap-core', 'regexes.yaml')
12-
if not os.path.exists(yaml_src):
13-
raise RuntimeError(
14-
'Unable to find regexes.yaml, should be at %r' % yaml_src)
10+
def check_output(*args, **kwargs):
11+
from subprocess import Popen
12+
proc = Popen(*args, **kwargs)
13+
output, _ = proc.communicate()
14+
rv = proc.poll()
15+
assert rv == 0, output
1516

16-
print('Converting regexes.yaml to regexes.json...')
17-
import json
18-
import yaml
19-
json_dest = os.path.join(cwd, 'ua_parser', 'regexes.json')
20-
with open(yaml_src, 'rb') as fp:
21-
regexes = yaml.safe_load(fp)
22-
with open(json_dest, "w") as f:
23-
json.dump(regexes, f)
17+
18+
class build_regexes(Command):
19+
description = 'build supporting regular expressions from uap-core'
20+
user_options = [
21+
('work-path=', 'w',
22+
'The working directory for source files. Defaults to .'),
23+
]
24+
25+
def initialize_options(self):
26+
self.work_path = None
27+
28+
def finalize_options(self):
29+
if self.work_path is None:
30+
self.work_path = os.path.realpath(os.path.join(os.path.dirname(__file__)))
31+
32+
def run(self):
33+
work_path = self.work_path
34+
if os.path.exists(os.path.join(work_path, '.git')):
35+
log.info('initializing git submodules')
36+
check_output(['git', 'submodule', 'init'], cwd=work_path)
37+
check_output(['git', 'submodule', 'update'], cwd=work_path)
38+
39+
yaml_src = os.path.join(work_path, 'uap-core', 'regexes.yaml')
40+
if not os.path.exists(yaml_src):
41+
raise RuntimeError(
42+
'Unable to find regexes.yaml, should be at %r' % yaml_src)
43+
44+
def force_bytes(text):
45+
if text is None:
46+
return text
47+
return text.encode('utf8')
48+
49+
import yaml
50+
py_dest = os.path.join(work_path, 'ua_parser', '_regexes.py')
51+
52+
log.info('Compiling regexes.yaml -> _regexes.py')
53+
with open(yaml_src, 'rb') as fp:
54+
regexes = yaml.safe_load(fp)
55+
with open(py_dest, 'wb') as fp:
56+
fp.write(b'############################################\n')
57+
fp.write(b'# NOTICE: This file is autogenerated from #\n')
58+
fp.write(b'# regexes.yaml. Do not edit by hand, #\n')
59+
fp.write(b'# instead, re-run `setup.py build_regexes` #\n')
60+
fp.write(b'############################################\n')
61+
fp.write(b'\n')
62+
fp.write(b'from __future__ import absolute_import\n')
63+
fp.write(b'from .user_agent_parser import (\n')
64+
fp.write(b' UserAgentParser, DeviceParser, OSParser,\n')
65+
fp.write(b')\n')
66+
fp.write(b'\n')
67+
fp.write(b'__all__ = (\n')
68+
fp.write(b' \'USER_AGENT_PARSERS\', \'DEVICE_PARSERS\', \'OS_PARSERS\',\n')
69+
fp.write(b')\n')
70+
fp.write(b'\n')
71+
fp.write(b'USER_AGENT_PARSERS = [\n')
72+
for device_parser in regexes['user_agent_parsers']:
73+
fp.write(b' UserAgentParser(\n')
74+
fp.write(force_bytes(' %r,\n' % device_parser['regex']))
75+
fp.write(force_bytes(' %r,\n' % device_parser.get('family_replacement')))
76+
fp.write(force_bytes(' %r,\n' % device_parser.get('v1_replacement')))
77+
fp.write(force_bytes(' %r,\n' % device_parser.get('v2_replacement')))
78+
fp.write(b' ),\n')
79+
fp.write(b']\n')
80+
fp.write(b'\n')
81+
fp.write(b'DEVICE_PARSERS = [\n')
82+
for device_parser in regexes['device_parsers']:
83+
fp.write(b' DeviceParser(\n')
84+
fp.write(force_bytes(' %r,\n' % device_parser['regex']))
85+
fp.write(force_bytes(' %r,\n' % device_parser.get('regex_flag')))
86+
fp.write(force_bytes(' %r,\n' % device_parser.get('device_replacement')))
87+
fp.write(force_bytes(' %r,\n' % device_parser.get('brand_replacement')))
88+
fp.write(force_bytes(' %r,\n' % device_parser.get('model_replacement')))
89+
fp.write(b' ),\n')
90+
fp.write(b']\n')
91+
fp.write(b'\n')
92+
fp.write(b'OS_PARSERS = [\n')
93+
for device_parser in regexes['os_parsers']:
94+
fp.write(b' OSParser(\n')
95+
fp.write(force_bytes(' %r,\n' % device_parser['regex']))
96+
fp.write(force_bytes(' %r,\n' % device_parser.get('os_replacement')))
97+
fp.write(force_bytes(' %r,\n' % device_parser.get('os_v1_replacement')))
98+
fp.write(force_bytes(' %r,\n' % device_parser.get('os_v2_replacement')))
99+
fp.write(force_bytes(' %r,\n' % device_parser.get('os_v3_replacement')))
100+
fp.write(force_bytes(' %r,\n' % device_parser.get('os_v4_replacement')))
101+
fp.write(b' ),\n')
102+
fp.write(b']\n')
103+
fp.write(b'\n')
24104

25105

26106
class develop(_develop):
27107
def run(self):
28-
install_regexes()
108+
self.run_command('build_regexes')
29109
_develop.run(self)
30110

31111

32112
class sdist(_sdist):
33-
def run(self):
34-
install_regexes()
35-
_sdist.run(self)
113+
sub_commands = _sdist.sub_commands + [('build_regexes', None)]
114+
115+
116+
cmdclass = {
117+
'develop': develop,
118+
'sdist': sdist,
119+
'build_regexes': build_regexes,
120+
}
36121

37122

38123
setup(
@@ -47,13 +132,9 @@ def run(self):
47132
zip_safe=False,
48133
url='https://github.com/ua-parser/uap-python',
49134
include_package_data=True,
50-
package_data={'ua_parser': ['regexes.json']},
51135
setup_requires=['pyyaml'],
52136
install_requires=[],
53-
cmdclass={
54-
'develop': develop,
55-
'sdist': sdist,
56-
},
137+
cmdclass=cmdclass,
57138
classifiers=[
58139
'Development Status :: 4 - Beta',
59140
'Environment :: Web Environment',

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ envlist = py26, py27, pypy, py31, py32, py33, py34, py35, docs, pep8, py3pep8
55
deps =
66
pyyaml
77
commands =
8+
python setup.py develop
89
python ua_parser/user_agent_parser_test.py

0 commit comments

Comments
 (0)