Skip to content

Commit a36eefe

Browse files
committed
Merge pull request #41 from pnegahdar/master
Fixed
2 parents a7e43fd + 7a8d6e7 commit a36eefe

File tree

271 files changed

+2971
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

271 files changed

+2971
-0
lines changed

render_docs.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import glob2
2+
import os
3+
import json
4+
from mako.template import Template
5+
from mako.lookup import TemplateLookup
6+
7+
8+
def delete_by_file_type(file_type):
9+
for each in glob2.glob('./scenarios/**/{}'.format(file_type)):
10+
os.remove(each)
11+
12+
13+
def create_definitions(path):
14+
with open(path, 'r') as py_mako:
15+
content = py_mako.read()
16+
try:
17+
def_part = content.split("% if mode == 'definition':")[1] \
18+
.split('% else:')[0]
19+
except IndexError:
20+
print "WARNING {} skipped".format(path)
21+
def_part = ''
22+
def_part = def_part.strip()
23+
path_to_write = os.path.dirname(path)
24+
with open(os.path.join(path_to_write, 'definition.mako'),
25+
'w+') as write_file:
26+
write_file.write(def_part)
27+
return def_part
28+
29+
30+
31+
def create_request(path):
32+
with open(path, 'r') as py_mako:
33+
content = py_mako.read()
34+
try:
35+
request_part = content.split("% else:")[1] \
36+
.split('% endif')[0]
37+
except IndexError:
38+
print "WARNING {} skipped".format(path)
39+
request_part = ''
40+
request_part = request_part.strip()
41+
path_to_write = os.path.dirname(path)
42+
with open(os.path.join(path_to_write, 'request.mako'),'w+') as write_file:
43+
write_file.write(request_part)
44+
return request_part
45+
46+
47+
48+
def run_fix():
49+
for path in glob2.glob('./scenarios/**/python.mako'):
50+
def_part = create_definitions(path)
51+
req_part = create_request(path)
52+
os.rename(path, os.path.join(os.path.dirname(path), 'original.mako'))
53+
54+
55+
def fix_main_mako():
56+
with open('./scenarios/_main.mako', 'r') as file_name:
57+
data = file_name.read()
58+
new_data= data.replace('ctx.', '')
59+
with open('./scenarios/_main.mako', 'w+b') as file_name:
60+
file_name.write(new_data)
61+
62+
63+
64+
def render_exec():
65+
data = json.load(open('scenario.cache','r'))
66+
lookup = TemplateLookup(directories=['./scenarios'])
67+
for path in glob2.glob('./scenarios/**/original.mako'):
68+
event_name = path.split('/')[-2]
69+
template = Template(filename=path, lookup=lookup,)
70+
try:
71+
request = data[event_name].get('request', {})
72+
payload = request.get('payload')
73+
text = template.render(api_key=data['api_key'],
74+
request=request, payload=payload).strip()
75+
with open(os.path.join(os.path.dirname(path),
76+
'{}.py'.format(event_name)),
77+
'w+'
78+
) as write_to:
79+
write_to.write(text)
80+
except KeyError:
81+
print "WARN: Skipped {} since {} not in scenario.cache".format(
82+
path, event_name)
83+
84+
def render_rest():
85+
for path in glob2.glob('./scenarios/**/*.py'):
86+
dir = os.path.dirname(path)
87+
with open(os.path.join(dir, 'python.mako'), 'w+b') as wfile:
88+
top = open(os.path.join(dir, 'definition.mako'),'r').read()
89+
bottom = open(path).read()
90+
body = "% if mode == 'definition':\n\n{}".format(top) + "\n% " \
91+
"else:\n" + bottom + "\n\n% endif"
92+
wfile.write(body)
93+
94+
95+
96+
97+
98+
if __name__ == "__main__":
99+
print "Setting up"
100+
delete_by_file_type('curl.mako')
101+
delete_by_file_type('__init__.py')
102+
delete_by_file_type('metadata.py')
103+
delete_by_file_type('php.mako')
104+
delete_by_file_type('ruby.mako')
105+
fix_main_mako()
106+
print "Fixing"
107+
run_fix()
108+
print "Making Executables"
109+
render_exec()
110+
print "Rendering new mako files"
111+
render_rest()
112+
113+

scenario.cache

Lines changed: 529 additions & 0 deletions
Large diffs are not rendered by default.

scenarios/_main.mako

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
<%def name="recursive_expand(dikt, delimiter='\\')">
2+
<%
3+
if not dikt:
4+
raise StopIteration
5+
6+
keys = sorted(dikt.keys(), key=lambda x: isinstance(dikt[x], dict))
7+
try:
8+
last = keys[-1]
9+
except IndexError:
10+
last = keys[0]
11+
12+
lines = []
13+
for k in keys:
14+
if isinstance(dikt[k], dict):
15+
for subk, v, slash in recursive_expand(dikt[k], delimiter):
16+
lines.append(('{0}[{1}]'.format(k, subk), v, slash))
17+
else:
18+
lines.append((k, dikt[k], '' if k is last else delimiter))
19+
return lines
20+
%>
21+
</%def>
22+
23+
## http://stackoverflow.com/a/6701741/133514
24+
<%def name="route_for_endpoint(endpoint, select=None)">
25+
<%
26+
return context['Endpoint'](ctx, endpoint, select)
27+
%>
28+
</%def>
29+
30+
<%def name="interpolate_uri(uri, **kwargs)">
31+
<%
32+
# this basically parses :bank_account_id to a string with {bank_account_id}
33+
# for much easier interpolation.
34+
import re
35+
# note that it shouldn't start with a digit!
36+
_uri = re.sub('(:([^\d]\w+))+', r'{\2}', uri)
37+
return _uri.format(**kwargs)
38+
%>
39+
</%def>
40+
41+
<%def name="make_endpoint(endpoint_name, select=None)">
42+
<%
43+
return context['Endpoint'](ctx, endpoint_name, select)
44+
%>
45+
</%def>
46+
47+
<%def name="php_boilerplate()">
48+
## i'm putting this between an interpolation because pycharm's introspection
49+
## correctly detects that this is an unclosed tag and warns me..annoying.
50+
${"<?php"}
51+
52+
require(__DIR__ . '/vendor/autoload.php');
53+
54+
Httpful\Bootstrap::init();
55+
RESTful\Bootstrap::init();
56+
Balanced\Bootstrap::init();
57+
58+
%if api_location:
59+
Balanced\Settings::configure("${api_location}", "${api_key}");
60+
%else:
61+
Balanced\Settings::$api_key = "${api_key}";
62+
%endif
63+
64+
</%def>
65+
66+
<%def name="python_boilerplate()">
67+
import balanced
68+
69+
%if api_location:
70+
balanced.config.root_uri = "${api_location}"
71+
%endif
72+
balanced.configure("${api_key}")
73+
74+
</%def>
75+
76+
<%def name="ruby_boilerplate()">
77+
require 'balanced'
78+
%if api_location:
79+
<%
80+
import urlparse
81+
parsed_url = urlparse.urlparse(api_location)
82+
_root_url = parsed_url.netloc
83+
if ':' in _root_url:
84+
_root_url, _, _ = parsed_url.netloc.partition(':')
85+
86+
%>
87+
options = {
88+
:scheme => 'http',
89+
:host => '${_root_url}',
90+
:port => 5000,
91+
}
92+
Balanced.configure('${api_key}', options)
93+
%else:
94+
Balanced.configure('${api_key}')
95+
%endif
96+
</%def>
97+
98+
99+
<%def name="curl_show_template(endpoint_name)">
100+
% if mode == 'definition':
101+
<%
102+
ep = Endpoint(ctx, endpoint_name, select='shortest')
103+
%>
104+
${ep.method} ${ep.url}
105+
% else:
106+
<%
107+
slash = '\\'
108+
%>
109+
curl ${Endpoint.qualify_uri(ctx, request['uri'])} ${slash}
110+
-u ${api_key}:
111+
% endif
112+
</%def>
113+
114+
115+
<%def name="curl_create_template(endpoint_name, uri='uri', ep=None)">
116+
<%
117+
ep = Endpoint(ctx, endpoint_name, select='shortest')
118+
%>
119+
%if mode == 'definition':
120+
${ep.method} ${ep.url}
121+
%elif mode == 'request':
122+
<%
123+
slash = '\\'
124+
%>
125+
curl ${Endpoint.qualify_uri(ctx, request[uri])} ${slash}
126+
-u ${api_key}: ${slash}
127+
%if 'payload' in request:
128+
%for k, v, slash in recursive_expand(request['payload']):
129+
-d "${k}=${v}" ${slash}
130+
%endfor
131+
%else:
132+
-X POST
133+
%endif
134+
%endif
135+
</%def>
136+
137+
<%def name="curl_update_template(endpoint_name, uri='uri', ep=None)">
138+
<%
139+
ep = Endpoint(ctx, endpoint_name, select='shortest')
140+
%>
141+
%if mode == 'definition':
142+
${ep.method} ${ep.url}
143+
%elif mode == 'request':
144+
<%
145+
slash = '\\'
146+
%>
147+
curl ${Endpoint.qualify_uri(ctx, request[uri])} ${slash}
148+
-u ${api_key}: ${slash}
149+
-X PUT ${slash}
150+
%for k, v, slash in recursive_expand(request['payload']):
151+
-d "${k}=${v}" ${slash}
152+
%endfor
153+
%endif
154+
</%def>
155+
156+
157+
<%def name="curl_list_template(endpoint_name, limit=2, ep=None)">
158+
<%
159+
ep = Endpoint(ctx, endpoint_name, select='shortest')
160+
%>
161+
% if request == 'definition':
162+
${ep.method} ${ep.url}
163+
%elif mode == 'request':
164+
<%
165+
slash = '\\'
166+
%>
167+
curl ${Endpoint.qualify_uri(ctx, request['uri'], limit=limit)} ${slash}
168+
-u ${api_key}:
169+
% endif
170+
</%def>
171+
172+
<%def name="curl_delete_template(endpoint_name, ep=None)">
173+
<%
174+
ep = Endpoint(ctx, endpoint_name)
175+
%>
176+
% if mode == 'definition':
177+
${ep.method} ${ep.url}
178+
% else:
179+
<%
180+
slash = '\\'
181+
%>
182+
curl ${Endpoint.qualify_uri(ctx, request['uri'])} ${slash}
183+
-u ${api_key}: ${slash}
184+
-X DELETE
185+
% endif
186+
</%def>
187+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import balanced
2+
3+
balanced.configure("46c08048cd8811e2acae026ba7c1aba6")
4+
5+
6+
account = balanced.Account.find('/v1/marketplaces/TEST-MP29J5STPtZVvnjAFndM0N62/accounts/AC1SUaFl5mNtKGOA3LeO2IxN')
7+
account.add_card('/v1/marketplaces/TEST-MP29J5STPtZVvnjAFndM0N62/cards/CC1UxG1UwXYFcslMhp4wq2Mg')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
balanced.Account.add_card
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
% if mode == 'definition':
2+
3+
balanced.Account.add_card
4+
% else:
5+
import balanced
6+
7+
balanced.configure("46c08048cd8811e2acae026ba7c1aba6")
8+
9+
10+
account = balanced.Account.find('/v1/marketplaces/TEST-MP29J5STPtZVvnjAFndM0N62/accounts/AC1SUaFl5mNtKGOA3LeO2IxN')
11+
account.add_card('/v1/marketplaces/TEST-MP29J5STPtZVvnjAFndM0N62/cards/CC1UxG1UwXYFcslMhp4wq2Mg')
12+
13+
% endif
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import balanced
2+
3+
balanced.configure("46c08048cd8811e2acae026ba7c1aba6")
4+
5+
6+
account = balanced.Account.find('/v1/marketplaces/TEST-MP29J5STPtZVvnjAFndM0N62/accounts/AC1SUaFl5mNtKGOA3LeO2IxN')
7+
account.add_card('/v1/marketplaces/TEST-MP29J5STPtZVvnjAFndM0N62/cards/CC1UxG1UwXYFcslMhp4wq2Mg')
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import balanced
2+
3+
balanced.configure("46c08048cd8811e2acae026ba7c1aba6")
4+
5+
6+
account = balanced.Account.find('/v1/marketplaces/TEST-MP29J5STPtZVvnjAFndM0N62/accounts/AC41WWE1V0nZtw5J8BicNwnB')
7+
account.debit(amount=1000, hold_uri='/v1/marketplaces/TEST-MP29J5STPtZVvnjAFndM0N62/holds/HL4b3zuvRlumVNFMeKl0h5Pk')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
balanced.Account.debit()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
% if mode == 'definition':
2+
3+
balanced.Account.debit()
4+
% else:
5+
import balanced
6+
7+
balanced.configure("46c08048cd8811e2acae026ba7c1aba6")
8+
9+
10+
account = balanced.Account.find('/v1/marketplaces/TEST-MP29J5STPtZVvnjAFndM0N62/accounts/AC41WWE1V0nZtw5J8BicNwnB')
11+
account.debit(amount=1000, hold_uri='/v1/marketplaces/TEST-MP29J5STPtZVvnjAFndM0N62/holds/HL4b3zuvRlumVNFMeKl0h5Pk')
12+
13+
% endif
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import balanced
2+
3+
balanced.configure("46c08048cd8811e2acae026ba7c1aba6")
4+
5+
6+
account = balanced.Account.find('/v1/marketplaces/TEST-MP29J5STPtZVvnjAFndM0N62/accounts/AC41WWE1V0nZtw5J8BicNwnB')
7+
account.debit(amount=1000, hold_uri='/v1/marketplaces/TEST-MP29J5STPtZVvnjAFndM0N62/holds/HL4b3zuvRlumVNFMeKl0h5Pk')
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import balanced
2+
3+
balanced.configure("46c08048cd8811e2acae026ba7c1aba6")
4+
5+
6+
account = balanced.Account().save()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
balanced.Account(...).save()

scenarios/account_create/python.mako

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
% if mode == 'definition':
2+
3+
balanced.Account(...).save()
4+
% else:
5+
import balanced
6+
7+
balanced.configure("46c08048cd8811e2acae026ba7c1aba6")
8+
9+
10+
account = balanced.Account().save()
11+
12+
% endif

0 commit comments

Comments
 (0)