forked from WRI-Cities/payanam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaunch.py
123 lines (103 loc) · 4.68 KB
/
launch.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
'''
Combined App
start date: 1.1.19
author: Nikhil VJ
Created for work on TSRTC Hyderabad bus routes
'''
print('\nPayanam')
print('Starting up the program, loading dependencies, please wait...\n\n')
import datetime
import json, xmltodict
import os, sys, webbrowser, signal # signal: for catching Ctrl+C and exiting gracefully.
import shutil # for copying etc
import time, datetime
from collections import OrderedDict
from math import sin, cos, sqrt, atan2, radians
import zipfile, zlib
import uuid
import pandas as pd
import tornado.ioloop, tornado.web
from tornado import escape
# for fuzzy search
import jellyfish as jf
import requests, platform # used to log user stats
requests.packages.urllib3.disable_warnings() # suppress warning messages like "InsecureRequestWarning: Unverified HTTPS request is being made." from https://stackoverflow.com/a/44850849/4355695
import gtfs_common as gtfsC
# setting constants
root = os.path.dirname(__file__) # needed for tornado and all other paths, prog should work even if called from other working directory.
startPage = 'index.html' # change the starting page as per needs.
routesFolder = os.path.join(root,'routes')
lockFolder = os.path.join(root,'locked-routes')
timeFolder = os.path.join(root,'timings')
logFolder = os.path.join(root,'logs')
configFolder = os.path.join(root,'config')
reportsFolder = os.path.join(root,'reports')
databankFolder = os.path.join(root,'databank')
backupsFolder = os.path.join(root,'backups')
uploadFolder = os.path.join(root,'uploads')
configFile = 'config.json'
accessFile = 'access.csv'
accessRanking = ["VIEW","MAPPER","DATAENTRY","REVIEW","ADMIN"]
# paths were you must not tread.. see "MyStaticFileHandler" class in apy.py
forbiddenPaths = []
forbiddenEndings = ['.py','access.csv']
debugMode = False # using this flag at various places to do or not do things based on whether we're in development or production
try:
configRules = json.load(open(os.path.join(configFolder,configFile)), object_pairs_hook=OrderedDict)
# hey gotta load this is as OrderedDict
except FileNotFoundError:
configRules = {}
# create folders if they don't exist
for folder in [routesFolder, lockFolder, logFolder, configFolder, reportsFolder, databankFolder, backupsFolder, uploadFolder]:
os.makedirs(folder, exist_ok=True)
exec(open(os.path.join(root,"functions.py"), encoding='utf8').read())
exec(open(os.path.join(root,"api.py"), encoding='utf8').read())
logmessage('Loaded dependencies, starting program.')
def make_app():
return tornado.web.Application([
(r"/API/loadJsonsList", loadJsonsList),
(r"/API/loadJson", loadJson),
(r"/API/GPXUpload", GPXUpload),
(r"/API/routeSuggest", routeSuggest),
(r"/API/routeLock", routeLock),
(r"/API/bulkSuggest", bulkSuggest),
(r"/API/keyCheck", keyCheck),
# (r"/API/catchJumpers", catchJumpers),
(r"/API/routeEntry", routeEntry),
(r"/API/reconcile", reconcile),
(r"/API/getRouteLine", getRouteLine),
(r"/API/removeAutoMappingAPI", removeAutoMappingAPI),
(r"/API/timings", timings),
(r"/API/unLock", unLock),
(r"/API/depotsList", depotsList),
(r"/API/importGTFS", importGTFS),
#(r"/API/allStops", allStops),
# (r"/(.*)", tornado.web.StaticFileHandler, {"path": root, "default_filename": startPage})
(r"/(.*)", MyStaticFileHandler, {"path": root, "default_filename": startPage})
])
# for catching Ctrl+C and exiting gracefully. From https://nattster.wordpress.com/2013/06/05/catch-kill-signal-in-python/
def signal_term_handler(signal, frame):
# to do: Make this work in windows, ra!
logmessage('\nClosing Program.\nThank you for using this program. Feedback : nikhil.js [at] gmail.com')
sys.exit(0)
if __name__ == "__main__":
signal.signal(signal.SIGINT, signal_term_handler)
app = make_app()
portnum = 5050
while True: # loop to increment the port number till we find one that isn't occupied
try:
port = int(os.environ.get("PORT", portnum))
app.listen(port)
break
except OSError:
portnum += 1
if portnum > 9999:
logmessage('Can\'t launch as no port number from 5000 through 9999 is free.')
sys.exit()
thisURL = "http://localhost:" + str(port)
if configRules.get('openBrowser') :
webbrowser.open(thisURL)
logmessage("\n\nOpen {} in your Web Browser if you don't see it opening automatically in 5 seconds.\n\nNote: If this is through docker, then it's not going to auto-open in browser, don't wait.".format(thisURL))
else:
logmessage(f"\n\nOpen {thisURL} in your Web Browser")
tornado.ioloop.IOLoop.current().start()