2222DEL_METHOD = 'delete'
2323
2424NESTED_TYPE_MARKER = 'is_nested'
25+ SPACES_PER_TAB = 4
2526
2627# ==============================================================================
2728## @name Filters
@@ -51,9 +52,23 @@ def unindent(s):
5152# tabs: 1 tabs is 4 spaces
5253def unindent_first_by (tabs ):
5354 def unindent_inner (s ):
54- return re .sub ("^ {1,%i}" % ( tabs * 4 ), '' , s )
55+ return re_linestart .sub (' ' * tabs * SPACES_PER_TAB , s )
5556 return unindent_inner
5657
58+ # tabs: 1 tabs is 4 spaces
59+ def indent_all_but_first_by (tabs ):
60+ def indent_inner (s ):
61+ try :
62+ i = s .index ('\n ' )
63+ except ValueError :
64+ f = s
65+ p = ''
66+ else :
67+ f = s [:i + 1 ]
68+ p = s [i + 1 :]
69+ return f + re_linestart .sub (' ' * (tabs * SPACES_PER_TAB ), p )
70+ return indent_inner
71+
5772# add 4 spaces to the beginning of a line.
5873# useful if you have defs embedded in an unindent block - they need to counteract.
5974# It's a bit itchy, but logical
@@ -119,6 +134,10 @@ def split_camelcase_s(s):
119134 s1 = re .sub ('(.)([A-Z][a-z]+)' , r'\1 \2' , s )
120135 return re .sub ('([a-z0-9])([A-Z])' , r'\1 \2' , s1 ).lower ()
121136
137+ def camel_to_under (s ):
138+ s1 = re .sub ('(.)([A-Z][a-z]+)' , r'\1_\2' , s )
139+ return re .sub ('([a-z0-9])([A-Z])' , r'\1_\2' , s1 ).lower ()
140+
122141## -- End Natural Language Utilities -- @}
123142
124143
@@ -137,6 +156,7 @@ def nested_type_name(sn, pn):
137156
138157# Make properties which are reserved keywords usable
139158def mangle_ident (n ):
159+ n = camel_to_under (n )
140160 if n == 'type' :
141161 return n + '_'
142162 return n
@@ -193,6 +213,14 @@ def is_nested_type_property(t):
193213def is_nested_type (s ):
194214 return NESTED_TYPE_MARKER in s
195215
216+ # convert a rust-type to something that would be taken as input of a function
217+ # even though our storage type is different
218+ def activity_input_type (p ):
219+ n = activity_rust_type (p , allow_optionals = False )
220+ if n == 'String' :
221+ n = 'str'
222+ return '&%s' % n
223+
196224# return an iterator yielding fake-schemas that identify a nested type
197225def iter_nested_types (schemas ):
198226 for s in schemas .values ():
@@ -243,6 +271,14 @@ def activity_split(fqan):
243271 assert len (t ) == 3
244272 return t [1 :]
245273
274+ # Shorthand to get a type from parameters of activities
275+ def activity_rust_type (p , allow_optionals = True ):
276+ return to_rust_type (None , p .name , p , allow_optionals = allow_optionals )
277+
278+ # the inverse of activity-split, but needs to know the 'name' of the API
279+ def to_fqan (name , resource , method ):
280+ return '%s.%s.%s' % (name , resource , method )
281+
246282# videos -> Video
247283def activity_name_to_type_name (an ):
248284 return an .capitalize ()[:- 1 ]
@@ -251,6 +287,33 @@ def activity_name_to_type_name(an):
251287def iter_acitivities (c ):
252288 return ((activity_split (an ) + [a ]) for an , a in c .fqan_map .iteritems ())
253289
290+ # return a list of parameter structures of all params of the given method dict
291+ # apply a prune filter to restrict the set of returned parameters.
292+ # The order will always be: partOrder + alpha
293+ def method_params (m , required = None , location = None ):
294+ res = list ()
295+ po = m .get ('parameterOrder' , [])
296+ for pn , p in m .parameters .iteritems ():
297+ if required is not None and p .get ('required' , False ) != required :
298+ continue
299+ if location is not None and p .get ('location' , '' ) != location :
300+ continue
301+ np = p .copy ()
302+ np ['name' ] = pn
303+ try :
304+ # po = ['part', 'foo']
305+ # part_prio = 2 - 0 = 2
306+ # foo_prio = 2 - 1 = 1
307+ # default = 0
308+ prio = len (po ) - po .index (pn )
309+ except ValueError :
310+ prio = 0
311+ np ['priority' ] = prio
312+ res .append (np )
313+ # end for each parameter
314+ return sorted (res , key = lambda p : (p .priority , p .name ), reverse = True )
315+
316+
254317## -- End Activity Utilities -- @}
255318
256319
@@ -322,3 +385,14 @@ def mb_type(r, m):
322385
323386def hub_type (canonicalName ):
324387 return canonical_type_name (canonicalName )
388+
389+ # return e + d[n] + e + ' ' or ''
390+ def get_word (d , n , e = '' ):
391+ if n in d :
392+ v = e + d [n ] + e
393+ if not v .endswith (' ' ):
394+ v += ' '
395+ return v
396+ else :
397+ return ''
398+
0 commit comments