commit a33028d8e4708e14da7b23e0b5c2c87c60650d5e Author: aagbsn aagbsn@extc.org Date: Sun Jul 7 20:08:43 2013 +0200
Organize handlers and api for each component --- oonib/deck/api.py | 10 +++++++ oonib/deck/handlers.py | 31 +++++++++++++++++++++ oonib/input/api.py | 10 +++++++ oonib/input/handlers.py | 35 +++++++++++++++++++++++ oonib/policy/api.py | 40 ++++----------------------- oonib/policy/handlers.py | 69 ++-------------------------------------------- 6 files changed, 95 insertions(+), 100 deletions(-)
diff --git a/oonib/deck/api.py b/oonib/deck/api.py new file mode 100644 index 0000000..db6fb66 --- /dev/null +++ b/oonib/deck/api.py @@ -0,0 +1,10 @@ +from cyclone import web +from oonib.deck import handlers +from oonib import config + +deckAPI = [ + (r"/deck", handlers.DeckListHandler), + (r"/deck/([a-z0-9]{40})$", handlers.DeckDescHandler), + (r"/deck/([a-z0-9]{40})/file$", web.StaticFileHandler, {"path": + config.main.deck_dir}), +] diff --git a/oonib/deck/handlers.py b/oonib/deck/handlers.py new file mode 100644 index 0000000..f37609a --- /dev/null +++ b/oonib/deck/handlers.py @@ -0,0 +1,31 @@ +class DeckDescHandler(web.RequestHandler): + def get(self, deckID): + bn = os.path.basename(deckID) + try: + f = open(os.path.join(config.main.deck_dir, bn)) + a = {} + deckDesc = yaml.safe_load(f) + a['id'] = deckID + a['name'] = deckDesc['name'] + a['description'] = deckDesc['description'] + self.write(json.dumps(a)) + except IOError: + log.err("Deck %s missing" % deckID) + except KeyError: + log.err("Deck %s missing required keys!" % deckID) + +class DeckListHandler(web.RequestHandler): + def get(self): + if not config.main.deck_dir: return + path = os.path.abspath(config.main.deck_dir) + "/*" + decknames = map(os.path.basename, glob.iglob(path)) + deckList = [] + for deckname in decknames: + f = open(os.path.join(config.main.deck_dir, deckname)) + d = yaml.safe_load(f) + deckList.append({'id': deckname,'name': d['name'], + 'description': d['description']}) + f.close() + self.write(json.dumps(deckList)) + + diff --git a/oonib/input/api.py b/oonib/input/api.py new file mode 100644 index 0000000..4c397ab --- /dev/null +++ b/oonib/input/api.py @@ -0,0 +1,10 @@ +from cyclone import web +from oonib.input import handlers +from oonib import config + +inputAPI = [ + (r"/input", handlers.InputListHandler), + (r"/input/([a-z0-9]{40})", handlers.InputDescHandler), + (r"/input/([a-z0-9]{40})/file$", web.StaticFileHandler, {"path": + config.main.input_dir}), +] diff --git a/oonib/input/handlers.py b/oonib/input/handlers.py new file mode 100644 index 0000000..ed8008e --- /dev/null +++ b/oonib/input/handlers.py @@ -0,0 +1,35 @@ +from oonib import config +from cyclone import web +import json +import os +import yaml + +class InputDescHandler(web.RequestHandler): + def get(self, inputID): + #XXX return the input descriptor + # see oonib.md in ooni-spec + bn = os.path.basename(inputID) + ".desc" + try: + f = open(os.path.join(config.main.input_dir, bn)) + a = {} + inputDesc = yaml.safe_load(f) + a['id'] = inputID + a['name'] = inputDesc['name'] + a['description'] = inputDesc['description'] + self.write(json.dumps(a)) + except Exception: + log.err("No Input Descriptor found for id %s" % inputID) + +class InputListHandler(web.RequestHandler): + def get(self): + if not config.main.input_dir: return + path = os.path.abspath(config.main.input_dir) + "/*" + inputnames = map(os.path.basename, glob.iglob(path)) + inputList = [] + for inputname in inputnames: + f = open(os.path.join(config.main.input_dir, deckname)) + d = yaml.safe_load(f) + inputList.append({'id': inputname,'name': d['name'], + 'description': d['description']}) + f.close() + self.write(json.dumps(inputList)) diff --git a/oonib/policy/api.py b/oonib/policy/api.py index 577d988..0f6f023 100644 --- a/oonib/policy/api.py +++ b/oonib/policy/api.py @@ -1,40 +1,12 @@ -""" -/report - -/pcap - -This is the async pcap reporting system. It requires the client to have created -a report already, but can work independently from test progress. - -""" -import random -import string -import json -import re -import os - -from twisted.internet import reactor, defer - from cyclone import web - -from oonib import otime -from oonib import randomStr - +from oonib.policy import handlers from oonib import config -from oonib.policy import DeckListHandler, InputListHandler, NetTestListHandler
#XXX: if policy is configured policyAPI = [ - (r"/deck", DeckListHandler), - (r"/input", InputListHandler), - (r"/deck/([a-z0-9]{40})$", DeckDescHandler), - (r"/deck/([a-z0-9]{40})/file$", web.StaticFileHandler, {"path": - config.main.deck_dir}), - (r"/input/([a-z0-9]{40})/file$", web.StaticFileHandler, {"path": - config.main.input_dir}), - (r"/input([a-z0-9]{40}$", InputDescHandler), - (r"/policy/nettest", NetTestListHandler), - (r"/policy/nettest/([a-z0-9]+/py$", web.StaticFileHandler, {"path": - config.main.nettest_dir}), - (r"/policy/input", InputListHandler + (r"/policy/nettest", handlers.NetTestPolicyHandler), + #XXX: add nettest handler + #(r"/policy/nettest/([a-z0-9]+)/py$", web.StaticFileHandler, {"path": + # config.main.nettest_dir}), + (r"/policy/input", handlers.InputPolicyHandler), ] diff --git a/oonib/policy/handlers.py b/oonib/policy/handlers.py index 567da2c..2b55fb7 100644 --- a/oonib/policy/handlers.py +++ b/oonib/policy/handlers.py @@ -4,75 +4,12 @@ import json import os import yaml
-class DeckDescHandler(web.RequestHandler): - def get(self, deckID): - bn = os.path.basename(deckID) - try: - f = open(os.path.join(config.main.deck_dir, bn)) - a = {} - deckDesc = yaml.safe_load(f) - a['id'] = deckID - a['name'] = deckDesc['name'] - a['description'] = deckDesc['description'] - self.write(json.dumps(a)) - except IOError: - log.err("Deck %s missing" % deckID) - except KeyError: - log.err("Deck %s missing required keys!" % deckID) - -class DeckListHandler(web.RequestHandler): - def get(self): - if not config.main.deck_dir: return - path = os.path.abspath(config.main.deck_dir) + "/*" - decknames = map(os.path.basename, glob.iglob(path)) - deckList = [] - for deckname in decknames: - f = open(os.path.join(config.main.deck_dir, deckname)) - d = yaml.safe_load(f) - deckList.append({'id': deckname,'name': d['name'], - 'description': d['description']}) - f.close() - self.write(json.dumps(deckList)) - -class InputDescHandler(web.RequestHandler): - def get(self, inputID): - #XXX return the input descriptor - # see oonib.md in ooni-spec - bn = os.path.basename(inputID) + ".desc" - try: - f = open(os.path.join(config.main.input_dir, bn)) - a = {} - inputDesc = yaml.safe_load(f) - a['id'] = inputID - a['name'] = inputDesc['name'] - a['description'] = inputDesc['description'] - self.write(json.dumps(a)) - except Exception: - log.err("No Input Descriptor found for id %s" % inputID) - -class InputListHandler(web.RequestHandler): - def get(self): - if not config.main.input_dir: return - path = os.path.abspath(config.main.input_dir) + "/*" - inputnames = map(os.path.basename, glob.iglob(path)) - inputList = [] - for inputname in inputnames: - f = open(os.path.join(config.main.input_dir, deckname)) - d = yaml.safe_load(f) - inputList.append({'id': inputname,'name': d['name'], - 'description': d['description']}) - f.close() - self.write(json.dumps(inputList)) - -class NetTestListHandler(web.RequestHandler): +class NetTestPolicyHandler(web.RequestHandler): def get(self): #XXX: returns a list of accepted NetTests pass
-class HelperListHandler(web.RequestHandler): +class InputPolicyHandler(web.RequestHandler): def get(self): - #XXX: get the list of the running handlers - #'id' - #'description' - #'address' pass + #XXX return list of input ids
tor-commits@lists.torproject.org