-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloader.py
213 lines (174 loc) · 5.8 KB
/
loader.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
from app import workspace
import yaml
import pandas as pd
import blaze as blaze
import operator
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, show
from bokeh.resources import INLINE, CDN
import bokeh
import io
class CIOLoader(yaml.Loader):
pass
def uri_to_value( obj, uri ):
"""
path to resolve in yaml manifests
data['foo'].bar - /foo/_bar
"""
key = uri.lstrip('/')
if '/' in uri:
key,uri = uri.split('/',1)
else:
uri = None
if key.startswith( '_' ):
obj = getattr( obj, key[1:] )
else:
obj = obj[ key ]
if not uri:
return obj
else:
return uri_to_value( obj, uri )
def yaml_to_args( obj ):
"""
convert list and dict to list of key and value
"""
if isinstance( obj, list ):
args = []
for arg in obj:
for key in arg:
args.append((key,arg[key]))
return args
elif isinstance( obj, dict ):
return [(key, obj[key]) for key in obj ]
else:
return obj
def resolve_pointer( workspace, obj ):
"""
"""
if isinstance( obj, list ):
for index, value in enumerate(obj):
if hasattr( value, 'decode'):
value = value.decode('utf-8')
if isinstance( value, str ) and value.startswith('/'):
obj[index] = uri_to_value( workspace, value.lstrip('/') )
else:
obj[index] = resolve_pointer( workspace, value )
elif isinstance( obj, dict ):
for key in obj:
value = obj[key]
if hasattr( value, 'decode'):
value = value.decode('utf-8')
if isinstance( value, str ) and value.startswith('/'):
obj[key] = uri_to_value( workspace, value.lstrip('/') )
else:
obj[key] = resolve_pointer( workspace, value )
return obj
def blaze_constructor(loader, node):
"""
Execute blaze expression
"""
global workspace
obj = loader.construct_mapping(node, deep=True)
obj = resolve_pointer( workspace, obj )
blaze_method, args = yaml_to_args( obj )[0]
if blaze_method == 'odo':
args[1] = type( args[1] )
if isinstance( args[-1], dict ):
# If kwargs are provided
return getattr( blaze, blaze_method )( *args[:-1], **args[-1])
# evaluate args
return getattr( blaze, blaze_method)( *args )
def compute_constructor(loader, node):
"""
Add a compute operation to a blaze constructor
"""
return blaze.compute( blaze_constructor(loader, node) )
CIOLoader.add_constructor("!blaze", blaze_constructor)
CIOLoader.add_constructor("!compute", compute_constructor)
def operator_constructor(loader, node):
"""
Compute operations using built operator module
"""
global workspace
obj = loader.construct_mapping(node, deep=True)
obj = resolve_pointer( workspace, obj )
operation, arg = yaml_to_args( obj )[0]
return getattr( operator, operation )( *arg )
CIOLoader.add_constructor("!operator", operator_constructor)
def resolve_constructor(loader, node):
"""
Resolve the value of an object with path constructors
"""
global workspace
arg = loader.construct_mapping(node, deep=True)
return resolve_pointer( workspace, arg )
CIOLoader.add_constructor("!resolve", resolve_constructor)
def bokeh_constructor( loader, node ):
"""
build a bokeh plot
"""
global workspace
args = loader.construct_mapping(node, deep=True)
args = resolve_pointer( workspace, args )
source = None
if not 'figure' in args:
args['figure'] = {}
args['figure'] = resolve_pointer( workspace, args['figure'] )
if 'source' in args:
source = blaze.odo( args['source'], ColumnDataSource )
p = figure( **args['figure'] )
for glyph, kwargs in yaml_to_args(args['glyphs']):
if source:
kwargs['source'] = source
getattr( p, glyph )( **kwargs )
return p
CIOLoader.add_constructor("!bokeh", bokeh_constructor)
def widget_constructor( loader, node ):
"""
Append bokeh widgets
"""
global workspace
obj = loader.construct_mapping(node, deep=True)
obj = resolve_pointer( workspace, obj )
operation, arg = yaml_to_args( obj )[0]
if isinstance( arg[-1], dict ):
# If kwargs are provided
return getattr( bokeh.models.widgets, operation )( *arg[:-1], **arg[-1])
return getattr( bokeh.models.widgets, operation )( *arg )
CIOLoader.add_constructor("!widgets", widget_constructor)
def io_constructor( loader, node ):
"""
Append bokeh widgets
"""
global workspace
obj = loader.construct_mapping(node, deep=True)
obj = resolve_pointer( workspace, obj )
operation, arg = yaml_to_args( obj )[0]
if isinstance( arg[-1], dict ):
# If kwargs are provided
return getattr( bokeh.io, operation )( *arg[:-1], **arg[-1])
return getattr( bokeh.io, operation )( *arg )
CIOLoader.add_constructor("!io", io_constructor)
def parse_blaze_manifest_with_loader( filename ):
global workspace
if filename.startswith('---'):
for block in filename.lstrip('---').split('---'):
workspace = parse_blaze_manifest_with_loader( block )
else:
stream = yaml.load( filename, Loader=CIOLoader )
for key in stream:
value = stream[key]
workspace[key] = value
return workspace
def parse_bokeh_manifest_with_loader( filename ):
global workspace
if filename.startswith('---'):
tabs = []
for block in filename.lstrip('---').split('---'):
print(block,tabs)
tabs.append( parse_bokeh_manifest_with_loader( block ) )
return bokeh.models.widgets.Tabs( tabs = tabs )
else:
return yaml.load( filename, Loader=CIOLoader )
def __init__():
pass