-
Notifications
You must be signed in to change notification settings - Fork 4
/
__init__.py
46 lines (36 loc) · 1.35 KB
/
__init__.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
import sys
import json
from easycli import SubCommand, Argument
class MockupServer(SubCommand):
__command__ = 'mockupserver'
__help__ = 'Generates a mockup from YAML file.'
__arguments__ = [
Argument(
'story',
metavar='YAML',
help='A story file'
),
]
def __call__(self, args):
from nanohttp import Controller, HTTPNotFound, context
from ..authoring import Story
with open(args.story) as story_file:
story = Story.load(story_file)
class RootMockupController(Controller):
def __init__(self):
self.stories = []
def add_story(self, story):
self.stories.append(story)
def server(self, call):
for k, v in call.response.headers:
context.response_headers.add_header(k, v)
yield call.response.text.encode()
def __call__(self, *remaining_paths):
calls = [story.base_call] + story.calls
for call in calls:
url = call.url.replace(':', '')
if set(url.strip('/').split('/')) == set(remaining_paths) :
return self.server(call)
raise HTTPNotFound()
from nanohttp import quickstart
quickstart(RootMockupController())