-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
executable file
·67 lines (53 loc) · 1.45 KB
/
server.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
import argparse
import cherrypy
import json
import os
from data.fetch_codes import write_codes, get_codes
location = "./data/"
filename = "industry-codes.json"
path = os.path.join(location, filename)
data = {}
reload_on_start = False
def reload_data(check_if_exists=True):
global data
global reload_on_start
if not os.path.isfile(path) or not check_if_exists:
write_codes(get_codes(), location)
reload_on_start = True
with open('./data/industry-codes.json') as f:
data = json.load(f)
reload_data()
class CODE:
exposed = True
code = None
@cherrypy.tools.json_out()
def GET(self, id=None):
if id == None:
return self.code
elif id in self.code:
return self.code[id]
else:
return {}
class NAICS(CODE):
code = data['naics_codes']
class SIC(CODE):
code = data['sic_codes']
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--reload', action='store_true')
if parser.parse_args().reload and not reload_on_start:
reload_data(check_if_exists=False)
cherrypy.tree.mount(
NAICS(), '/naics',
{'/':
{'request.dispatch': cherrypy.dispatch.MethodDispatcher()}
}
)
cherrypy.tree.mount(
SIC(), '/sic',
{'/':
{'request.dispatch': cherrypy.dispatch.MethodDispatcher()}
}
)
cherrypy.engine.start()
cherrypy.engine.block()