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

Skip to content

Pre-compile yaml file into py files #39

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 9 commits into from
Dec 22, 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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
*.pyc
*.egg-info/
.eggs/
.cache/
.tox/
build/
dist/
tmp/
regexes.yaml
regexes.json
_regexes.py
1 change: 0 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
include README.md
include ua_parser/regexes.json
22 changes: 8 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
all: prep test
all: test

prep:
#git submodule update --init
#sudo apt-get install python-yaml

test:
@#test ! -d tmp && mkdir tmp
@export PYTHONPATH=tmp && python setup.py develop -d tmp
test: clean
@mkdir -p tmp
@PYTHONPATH=tmp python setup.py develop -d tmp
@# run all tests
@python ua_parser/user_agent_parser_test.py
@PYTHONPATH=tmp python ua_parser/user_agent_parser_test.py
@# run a single test
@#python ua_parser/user_agent_parser_test.py ParseTest.testStringsDeviceBrandModel
@#PYTHONPATH=tmp python ua_parser/user_agent_parser_test.py ParseTest.testStringsDeviceBrandModel

clean:
@rm -f ua_parser/user_agent_parser.pyc\
ua_parser/regexes.yaml\
ua_parser/regexes.json
@find . -name '*.pyc' -delete
@rm -rf tmp\
ua_parser.egg-info\
dist\
build

.PHONY: all prep test clean
.PHONY: all test clean
131 changes: 106 additions & 25 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,123 @@
#!/usr/bin/env python
import os
from distutils import log
from distutils.core import Command
from setuptools import setup
from setuptools.command.develop import develop as _develop
from setuptools.command.sdist import sdist as _sdist


def install_regexes():
print('Copying regexes.yaml to package directory...')
import os
cwd = os.path.abspath(os.path.dirname(__file__))
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)
def check_output(*args, **kwargs):
from subprocess import Popen
proc = Popen(*args, **kwargs)
output, _ = proc.communicate()
rv = proc.poll()
assert rv == 0, output

print('Converting regexes.yaml to regexes.json...')
import json
import yaml
json_dest = os.path.join(cwd, 'ua_parser', 'regexes.json')
with open(yaml_src, 'rb') as fp:
regexes = yaml.safe_load(fp)
with open(json_dest, "w") as f:
json.dump(regexes, f)

class build_regexes(Command):
description = 'build supporting regular expressions from uap-core'
user_options = [
('work-path=', 'w',
'The working directory for source files. Defaults to .'),
]

def initialize_options(self):
self.work_path = None

def finalize_options(self):
if self.work_path is None:
self.work_path = os.path.realpath(os.path.join(os.path.dirname(__file__)))

def run(self):
work_path = self.work_path
if os.path.exists(os.path.join(work_path, '.git')):
log.info('initializing git submodules')
check_output(['git', 'submodule', 'init'], cwd=work_path)
check_output(['git', 'submodule', 'update'], cwd=work_path)

yaml_src = os.path.join(work_path, 'uap-core', 'regexes.yaml')
if not os.path.exists(yaml_src):
raise RuntimeError(
'Unable to find regexes.yaml, should be at %r' % yaml_src)

def force_bytes(text):
if text is None:
return text
return text.encode('utf8')

import yaml
py_dest = os.path.join(work_path, 'ua_parser', '_regexes.py')

log.info('Compiling regexes.yaml -> _regexes.py')
with open(yaml_src, 'rb') as fp:
regexes = yaml.safe_load(fp)
with open(py_dest, 'wb') as fp:
fp.write(b'############################################\n')
fp.write(b'# NOTICE: This file is autogenerated from #\n')
fp.write(b'# regexes.yaml. Do not edit by hand, #\n')
fp.write(b'# instead, re-run `setup.py build_regexes` #\n')
fp.write(b'############################################\n')
fp.write(b'\n')
fp.write(b'from __future__ import absolute_import\n')
fp.write(b'from .user_agent_parser import (\n')
fp.write(b' UserAgentParser, DeviceParser, OSParser,\n')
fp.write(b')\n')
fp.write(b'\n')
fp.write(b'__all__ = (\n')
fp.write(b' \'USER_AGENT_PARSERS\', \'DEVICE_PARSERS\', \'OS_PARSERS\',\n')
fp.write(b')\n')
fp.write(b'\n')
fp.write(b'USER_AGENT_PARSERS = [\n')
for device_parser in regexes['user_agent_parsers']:
fp.write(b' UserAgentParser(\n')
fp.write(force_bytes(' %r,\n' % device_parser['regex']))
fp.write(force_bytes(' %r,\n' % device_parser.get('family_replacement')))
fp.write(force_bytes(' %r,\n' % device_parser.get('v1_replacement')))
fp.write(force_bytes(' %r,\n' % device_parser.get('v2_replacement')))
fp.write(b' ),\n')
fp.write(b']\n')
fp.write(b'\n')
fp.write(b'DEVICE_PARSERS = [\n')
for device_parser in regexes['device_parsers']:
fp.write(b' DeviceParser(\n')
fp.write(force_bytes(' %r,\n' % device_parser['regex']))
fp.write(force_bytes(' %r,\n' % device_parser.get('regex_flag')))
fp.write(force_bytes(' %r,\n' % device_parser.get('device_replacement')))
fp.write(force_bytes(' %r,\n' % device_parser.get('brand_replacement')))
fp.write(force_bytes(' %r,\n' % device_parser.get('model_replacement')))
fp.write(b' ),\n')
fp.write(b']\n')
fp.write(b'\n')
fp.write(b'OS_PARSERS = [\n')
for device_parser in regexes['os_parsers']:
fp.write(b' OSParser(\n')
fp.write(force_bytes(' %r,\n' % device_parser['regex']))
fp.write(force_bytes(' %r,\n' % device_parser.get('os_replacement')))
fp.write(force_bytes(' %r,\n' % device_parser.get('os_v1_replacement')))
fp.write(force_bytes(' %r,\n' % device_parser.get('os_v2_replacement')))
fp.write(force_bytes(' %r,\n' % device_parser.get('os_v3_replacement')))
fp.write(force_bytes(' %r,\n' % device_parser.get('os_v4_replacement')))
fp.write(b' ),\n')
fp.write(b']\n')
fp.write(b'\n')


class develop(_develop):
def run(self):
install_regexes()
self.run_command('build_regexes')
_develop.run(self)


class sdist(_sdist):
def run(self):
install_regexes()
_sdist.run(self)
sub_commands = _sdist.sub_commands + [('build_regexes', None)]


cmdclass = {
'develop': develop,
'sdist': sdist,
'build_regexes': build_regexes,
}


setup(
Expand All @@ -47,13 +132,9 @@ def run(self):
zip_safe=False,
url='https://github.com/ua-parser/uap-python',
include_package_data=True,
package_data={'ua_parser': ['regexes.json']},
setup_requires=['pyyaml'],
install_requires=[],
cmdclass={
'develop': develop,
'sdist': sdist,
},
cmdclass=cmdclass,
classifiers=[
'Development Status :: 4 - Beta',
'Environment :: Web Environment',
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ envlist = py26, py27, pypy, py31, py32, py33, py34, py35, docs, pep8, py3pep8
deps =
pyyaml
commands =
python setup.py develop
python ua_parser/user_agent_parser_test.py
Loading