-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpointSim.py
85 lines (69 loc) · 2.72 KB
/
endpointSim.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
import web
import sys
import importlib
import unittest, doctest
from operator import itemgetter #sorting urls
programID = "Endpoint Simulator v1.0"
urls = ('/(.*)', 'base')
def prependPackageToClassnames(urls, packageName):
"""Take web.py formatted url/endpoint tuples and prepend
a packageName to each of the endpoint classnames, e.g.
('/Test/(.*)', 'test') becomes ('/Test/(.*)', 'Test.test')
>>> prependPackageToClassnames(('/Test/(.*)', 'test'), 'PACKAGE')
('/Test/(.*)', 'PACKAGE.test')
>>> prependPackageToClassnames(('/Test/(.*)', 'test', '/Test/Test2', 'test2'), 'PACKAGE')
('/Test/(.*)', 'PACKAGE.test', '/Test/Test2', 'PACKAGE.test2')"""
newList = []
for url, endpoint in zip(urls[::2], urls[1::2]):
newList += [url, "{}.{}".format(packageName, endpoint)]
return tuple(newList)
def importAllEndpointFiles(endpointDefinitions, urls):
""" Import all python files from endpointDefinitions and
add their urls to the presented ones.
>>> importAllEndpointFiles(['Test'], ('/existing/(.*)', 'existing'))
Attempting to import Test
Imported Test.py presenting the endpoints ('/Test/(.*)', 'Test.test')
('/Test/(.*)', 'Test.test', '/existing/(.*)', 'existing')"""
completeUrlList = ()
for endpointFile in endpointDefinitions:
print "Attempting to import {}".format(endpointFile)
try:
importlib.import_module(endpointFile)
urlList = sys.modules[endpointFile].urls
urlList = prependPackageToClassnames(urlList, endpointFile)
completeUrlList += urlList
print "Imported {}.py presenting the endpoints {}".format(endpointFile, urlList)
except ImportError as ex:
print "Cannot import {}: {}".format(endpointFile, ex)
completeUrlList += urls
return completeUrlList
def sortURLs(urls):
""" Order URLs by how specific they are so that they
are matched in the correct order, ordered by granualty,
most fine-grained first
>>> sortURLs(('/(.*)','base', '/a/(.*)','sub', '/a/b/(.*)','subsub'))
('/a/b/(.*)', 'subsub', '/a/(.*)', 'sub', '/(.*)', 'base')"""
kv = zip(urls[::2], urls[1::2])
s = sorted(kv, key=itemgetter(0), reverse=True)
sortedUrls = tuple(x for pair in s for x in pair)
return sortedUrls
allUrls = importAllEndpointFiles(sys.argv[2:], urls)
allUrlsSorted = sortURLs(allUrls)
print "{} - Providing Endpoints :- \n{}".format(programID, allUrlsSorted)
app = web.application(allUrls, locals())
################################
# Endpoint Definition Classes #
################################
class base:
def GET(self, name):
return programID
################
# Entry Point #
################
if __name__ == "__main__":
import doctest
failed, attempted = doctest.testmod()
if failed > 0:
print "Script terminated: {} of {} tests failed.".format(failed, attempted)
sys.exit()
app.run()