diff options
| author | blob8108 <blob8108@gmail.com> | 2014-01-05 23:30:08 +0000 |
|---|---|---|
| committer | blob8108 <blob8108@gmail.com> | 2014-01-05 23:30:08 +0000 |
| commit | 830d200bd1e1002d6f0356b22963e1caa516cd80 (patch) | |
| tree | 0322765f20e32873e36e12adb7459cbad3094ef6 | |
| parent | 192bc9617b09fc584980191c14b02f6bdae2e583 (diff) | |
| download | blockext-830d200bd1e1002d6f0356b22963e1caa516cd80.tar.gz blockext-830d200bd1e1002d6f0356b22963e1caa516cd80.zip | |
Variable filenames for handlers
Fixes #2
| -rw-r--r-- | blockext/__init__.py | 25 | ||||
| -rw-r--r-- | blockext/scratch.py | 2 | ||||
| -rw-r--r-- | blockext/snap.py | 2 | ||||
| -rw-r--r-- | example.py | 2 |
4 files changed, 21 insertions, 10 deletions
diff --git a/blockext/__init__.py b/blockext/__init__.py index 776d447..e507915 100644 --- a/blockext/__init__.py +++ b/blockext/__init__.py @@ -50,12 +50,13 @@ class ThreadedHTTPServer(ThreadingMixIn, HTTPServer): class Blockext(BaseHTTPRequestHandler): - # HTTPServer makes one instance of this for each request. + # HTTPServer makes one Blockext instance for each request. blocks = {} - handlers = {} + _handlers = {} menus = {} name = "A blockext extension" + filename = "extension" port = 8080 requests = {} @@ -65,6 +66,14 @@ class Blockext(BaseHTTPRequestHandler): return return BaseHTTPRequestHandler.log_message(self, format, *args) + @classmethod + def handlers(cls): + handlers = {} + for name, func in cls._handlers.items(): + name = name.format(**vars(cls)) + handlers[name] = func + return handlers + def do_GET(self): is_browser = "text/html" in self.headers.get("Accept", "") mime_type = "text/plain" @@ -74,7 +83,7 @@ class Blockext(BaseHTTPRequestHandler): name = path[0] args = path[1:] - func = self.handlers.get(name, self.blocks.get(name, None)) + func = self.handlers().get(name, self.blocks.get(name, None)) if func: if isinstance(func, Block): response = func(*args) @@ -119,7 +128,7 @@ class Blockext(BaseHTTPRequestHandler): @classmethod def register_handler(cls, name, func, hidden=False, display=None): - cls.handlers[name] = func + cls._handlers[name] = func func.is_hidden = hidden func.display = display @@ -153,8 +162,9 @@ def index(is_browser=False): html += "<h1>{name}</h1>".format(name=escape(Blockext.name)) html += "<ul>" - for name in sorted(Blockext.handlers): - func = Blockext.handlers[name] + handlers = Blockext.handlers() + for name in sorted(handlers): + func = handlers[name] if func.is_hidden: continue html += '<li><a href="/{path}">{display}</a>'.format( path=escape(name), @@ -299,7 +309,7 @@ reporter = _shape("reporter") predicate = _shape("predicate") -def run(name="", port=8080): +def run(name="", filename="extension", port=8080): blocking_reporters = [b.text for b in Blockext.blocks.values() if b.shape != "command" and not all(shape[0] in "md" for shape in b.arg_shapes)] @@ -309,6 +319,7 @@ def run(name="", port=8080): print("") Blockext.name = name + Blockext.filename = filename Blockext.port = port server = ThreadedHTTPServer(('localhost', port), Blockext) print('Listening on {}'.format(port)) diff --git a/blockext/scratch.py b/blockext/scratch.py index e296e8e..917d37a 100644 --- a/blockext/scratch.py +++ b/blockext/scratch.py @@ -23,7 +23,7 @@ BLOCK_SHAPES = { "predicate": "b", } -@handler("scratch_extension.s2e", display="Download Scratch 2.0 extension") +@handler("scratch_{filename}.s2e", display="Download Scratch 2.0 extension") def generate_s2e(): extension = { "extensionName": Blockext.name, diff --git a/blockext/snap.py b/blockext/snap.py index 0210e0d..b18a9c0 100644 --- a/blockext/snap.py +++ b/blockext/snap.py @@ -20,7 +20,7 @@ def pretty(stuff): xml = xml.dom.minidom.parseString(ElementTree.tostring(stuff)) return xml.toprettyxml() -@handler("snap_extension.xml", display="Download Snap! blocks") +@handler("snap_{filename}.xml", display="Download Snap! blocks") def generate_s2e(): root = Element("blocks", { "app": "Snap! 4.0, http://snap.berkeley.edu", @@ -92,5 +92,5 @@ def move(steps): """ if __name__ == "__main__": - blockext.run("Fancy Spaceship", 1234) + blockext.run("Fancy Spaceship", "spaceship", 1234) |
