-
Notifications
You must be signed in to change notification settings - Fork 3
/
setup.py
209 lines (187 loc) · 7.23 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# coding: utf-8
import csv
import sqlite3
import subprocess
from setuptools import setup
from setuptools.command.install import install
from distutils import log
class cahoots_install(install):
"""
Extension of setuptools install class for post-install scripting
"""
def prep_location_data(self):
"""Copies dist database and extracts location data"""
location_install_cmds = [
'cp {0}cahoots/parsers/location/data/location.sqlite.dist {0}cahoots/parsers/location/data/location.sqlite',
'bzip2 -d -k {0}cahoots/parsers/location/data/city.txt.bz2',
'bzip2 -d -k {0}cahoots/parsers/location/data/country.csv.bz2',
'bzip2 -d -k {0}cahoots/parsers/location/data/street_suffix.csv.bz2',
'bzip2 -d -k {0}cahoots/parsers/location/data/landmark.csv.bz2',
]
for command in location_install_cmds:
subprocess.call(command.format(self.install_lib), shell=True)
def import_city_data(self, cursor):
"""Imports city data"""
city_file = self.install_lib+'cahoots/parsers/location/data/city.txt'
city_csv = csv.DictReader(
open(city_file, 'r'),
delimiter='\t',
quotechar='"',
fieldnames=['country', 'postal_code', 'city', 'state1', 'state2', 'province1', 'province2', 'community1', 'community2', 'latitude', 'longitude', 'coord_accuracy']
)
cursor.executemany(
"INSERT INTO city (country, postal_code, city, state1, state2, province1, province2, community1, community2, latitude, longitude, coord_accuracy) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);",
[(i['country'], i['postal_code'], i['city'], i['state1'], i['state2'], i['province1'], i['province2'], i['community1'], i['community2'], i['latitude'], i['longitude'], i['coord_accuracy'])
for i in city_csv]
)
def import_country_data(self, cursor):
"""Imports country data"""
country_file = self.install_lib+'cahoots/parsers/location/data/country.csv'
country_csv = csv.DictReader(
open(country_file, 'r'),
delimiter=',',
quotechar='"',
fieldnames=['abbreviation', 'name']
)
cursor.executemany(
"INSERT INTO country (abbreviation, name) VALUES (?, ?);",
[(i['abbreviation'], i['name'])
for i in country_csv]
)
def import_street_suffix_data(self, cursor):
"""Imports street_suffix data"""
suffix_file = self.install_lib+'cahoots/parsers/location/data/street_suffix.csv'
suffix_csv = csv.DictReader(
open(suffix_file, 'r'),
delimiter=',',
quotechar='"',
fieldnames=['suffix_name']
)
cursor.executemany(
"INSERT INTO street_suffix (suffix_name) VALUES (?);",
[(i['suffix_name'],)
for i in suffix_csv]
)
def import_landmark_data(self, cursor):
"""Imports landmark data"""
landmark_file = self.install_lib+'cahoots/parsers/location/data/landmark.csv'
landmark_csv = csv.DictReader(
open(landmark_file, 'r'),
delimiter=',',
quotechar='"',
fieldnames=['resource', 'address', 'city', 'county', 'state', 'country']
)
cursor.executemany(
"INSERT INTO landmark (resource, address, city, county, state, country) VALUES (?, ?, ?, ?, ?, ?);",
[(i['resource'], i['address'], i['city'], i['county'], i['state'], i['country'])
for i in landmark_csv]
)
def cleanup_location_data(self):
"""Deletes temp files"""
location_cleanup_cmds = [
'rm {0}cahoots/parsers/location/data/city.txt',
'rm {0}cahoots/parsers/location/data/country.csv',
'rm {0}cahoots/parsers/location/data/street_suffix.csv',
'rm {0}cahoots/parsers/location/data/landmark.csv',
]
for command in location_cleanup_cmds:
subprocess.call(command.format(self.install_lib), shell=True)
def run(self):
"""Runs the install and post-install actions"""
install.run(self)
log.info("Preparing/extracting location database and data for import...")
self.prep_location_data()
log.info("Importing location data into location database...")
database = sqlite3.connect(self.install_lib+'cahoots/parsers/location/data/location.sqlite')
database.text_factory = str
cursor = database.cursor()
log.info("Importing city data...")
self.import_city_data(cursor)
log.info("Importing country data...")
self.import_country_data(cursor)
log.info("Importing street suffix data...")
self.import_street_suffix_data(cursor)
log.info("Importing landmark data...")
self.import_landmark_data(cursor)
database.commit()
database.close()
log.info('Cleaning up location import temporary files...')
self.cleanup_location_data()
log.info('Done!')
setup (
cmdclass = {
'install': cahoots_install
},
name = 'Cahoots',
version = '0.5.2',
url = 'https://github.com/SerenitySoftwareLLC/cahoots',
maintainer='Serenity Software',
maintainer_email = 'hello@serenitysoftware.io',
description = 'A Text Comprehension Engine in Python.',
long_description = open('README.rst', 'r').read(),
license = 'MIT',
classifiers = [
'Development Status :: 4 - Beta',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Topic :: Utilities',
'Operating System :: MacOS',
'Operating System :: POSIX :: Linux',
],
install_requires = [
'watchdog',
'flask',
'mako',
'dateutils',
'pyyaml',
'pygments',
'pyparsing',
'phonenumbers',
'simplejson',
'SereneRegistry',
'LatLon23',
'simplebayes',
'validate_email',
],
packages = [
'cahoots',
'cahoots.confidence',
'cahoots.confidence.normalizers',
'cahoots.parsers',
'cahoots.parsers.location',
'cahoots.parsers.measurement',
'cahoots.parsers.programming',
'cahootserver',
],
package_data = {
'cahoots': [
# Cahoots Data
'data/*',
# Location
'parsers/location/data/*.sql',
'parsers/location/data/*.bz2',
'parsers/location/data/location.sqlite.dist',
'parsers/location/data/LICENSE',
# Programming
'parsers/programming/languages/*',
'parsers/programming/LICENSES/*',
'parsers/programming/trainers.zip',
# Measurement
'parsers/measurement/units/*',
],
'cahootserver': [
'static/*',
'templates/*',
],
},
entry_points={
'console_scripts': [
'cahootserver = cahootserver.server:launch_server',
],
},
)
# python ./setup.py sdist upload