@@ -100,8 +100,8 @@ def __main__():
100
100
# Generate the code!
101
101
102
102
# Imports go first
103
- print ("package com.alphasights.kotlintwilio" , file = output )
104
- print ("\n " .join (imports ) + "\n " , file = output )
103
+ print (u "package com.alphasights.kotlintwilio" , file = output )
104
+ print (u "\n " .join (imports ) + u "\n " , file = output )
105
105
106
106
# Boilerplate code and type aliases
107
107
print (fmt_preamble (), file = output )
@@ -110,14 +110,14 @@ def __main__():
110
110
print (fmt_dsl_definition (package_layout_code ), file = output )
111
111
112
112
# The extenions
113
- print ("\n " .join (extension_code ), file = output )
113
+ print (u "\n " .join (extension_code ), file = output )
114
114
115
115
# Finally, a list of types that have been declared as a valid child,
116
116
# but have not had code generated.
117
117
missing = list (set (acceptors ) - set (constructors ))
118
118
missing .sort ()
119
- print ("" , file = output )
120
- print (f "/** MISSING: { missing } */" , file = output )
119
+ print (u "" , file = output )
120
+ print (u "/** MISSING: {missing} */". format ( missing = missing ) , file = output )
121
121
122
122
reformatted = refmt_kotlin (output .getvalue ())
123
123
output_file .write (reformatted )
@@ -134,41 +134,41 @@ def importer(typespec):
134
134
package = typespec .package .strip ()
135
135
sep = "." if package else ""
136
136
name = typespec .name
137
- return f "import com.twilio.twiml.{ package } { sep } { name } "
137
+ return u "import com.twilio.twiml.{package}{sep}{name}". format ( package = package , sep = sep , name = name )
138
138
139
139
def fun_constructor (typespec , declare , consume ):
140
140
''' Build a constructor function for the given class '''
141
141
sep = ", " if declare else ""
142
- return f """
143
- inline fun { camel ( typespec .name ) } ({ declare } { sep } f: Takes<{ typespec .dsl_builder } > = {{}}): { typespec .qualified } = { typespec .dsl_builder } ({ consume } ).apply(f).build()
144
- """ .strip ()
142
+ return u """
143
+ inline fun {typespec.camel_name }({declare}{sep}f: Takes<{typespec.dsl_builder}> = {{}}): {typespec.qualified} = {typespec.dsl_builder}({consume}).apply(f).build()
144
+ """ .format ( typespec = typespec , declare = declare , sep = sep , consume = consume ). strip ()
145
145
146
146
def fun_class_marked_constructor (declare , consume ):
147
147
''' Build a constructor function for the given class '''
148
- return f """
148
+ return u """
149
149
constructor ({declare}) : super({consume})
150
- """ .strip ()
150
+ """ .format ( declare = declare , consume = consume ). strip ()
151
151
152
152
def fun_extension (typespec , acceptor , declare , consume ):
153
153
''' Build an extension to construct a child under the given parent class '''
154
154
sep = ", " if declare else ""
155
- return f """
156
- inline fun { acceptor .qualified } .Builder.{ camel ( typespec .name ) } ({ declare } { sep } f: Takes<{ typespec .dsl_builder } > = {{}}): { acceptor .qualified } .Builder = this.{ camel ( typespec .name ) } ({ typespec .dsl_constructor } ({ consume } { sep } f))
157
- """ .strip ()
155
+ return u """
156
+ inline fun {acceptor.qualified}.Builder.{typespec.camel_name }({declare}{sep}f: Takes<{typespec.dsl_builder}> = {{}}): {acceptor.qualified}.Builder = this.{typespec.camel_name }({typespec.dsl_constructor}({consume}{sep}f))
157
+ """ .format ( acceptor = acceptor , typespec = typespec , declare = declare , sep = sep , consume = consume ). strip ()
158
158
159
159
def class_marked_with_constructors (typespec , constructors ):
160
160
''' Build a DSL-marked class to ensure TwiML verbs are appropriately restricted. '''
161
161
162
162
constructor_code = "\n " .join (fun_class_marked_constructor (i , j ) for (i , j ) in constructors )
163
163
164
- return f """@TwimlMarker class { typespec .name } : { typespec .qualified } .Builder {{
164
+ return u """@TwimlMarker class {typespec.name} : {typespec.qualified}.Builder {{
165
165
{constructor_code}
166
166
}}
167
- """
167
+ """ . format ( typespec = typespec , constructor_code = constructor_code )
168
168
169
169
def fmt_preamble ():
170
170
''' The code after the import lines and before the DSL definition '''
171
- return """
171
+ return u """
172
172
typealias Takes<F> = F.() -> Unit
173
173
@DslMarker annotation class TwimlMarker
174
174
""" .strip ()
@@ -181,21 +181,21 @@ def fmt_dsl_definition(package_layout):
181
181
code = "\n " .join (code_bits )
182
182
if package :
183
183
pp = package .title ()
184
- value = f """
184
+ value = """
185
185
object {pp} {{
186
186
{code}
187
187
}}
188
- """ .strip () + "\n "
188
+ """ .format ( pp = pp , code = code ). strip () + "\n "
189
189
else :
190
- value = f """{ code } \n """
190
+ value = """{code}\n """ . format ( code = code )
191
191
lines .append (value )
192
192
c = "\n " .join (lines ).strip ()
193
193
194
- return f """
194
+ return u """
195
195
object DSLTwiML {{
196
196
{c}
197
197
}}
198
- """
198
+ """ . format ( c = c )
199
199
200
200
def constructor_argstrings (args ):
201
201
''' Produce the argstrings for the type. Args is a list of strings formatted
@@ -207,8 +207,8 @@ def constructor_argstrings(args):
207
207
consume_argstrings = []
208
208
for arg in args :
209
209
argname , argtype = arg .split (":" )
210
- declare_argstrings .append (f "{ argname } : { argtype } " )
211
- consume_argstrings .append (f "{ argname } " )
210
+ declare_argstrings .append ("{argname}: {argtype}" . format ( argname = argname , argtype = argtype ) )
211
+ consume_argstrings .append ("{argname}" . format ( argname = argname ) )
212
212
213
213
declare_argstring = ", " .join (declare_argstrings )
214
214
consume_argstring = ", " .join (consume_argstrings )
@@ -252,19 +252,23 @@ class TypeSpec(namedtuple("TypeSpec", ("package", "name"))):
252
252
@property
253
253
def qualified (self ):
254
254
sep = "." if self .package else ""
255
- return f "com.twilio.twiml.{ self .package } { sep } { self .name } "
255
+ return "com.twilio.twiml.{self.package}{sep}{self.name}" . format ( self = self , sep = sep )
256
256
257
257
@property
258
258
def dsl_constructor (self ):
259
259
p = self .package .title ()
260
260
sep = "." if p else ""
261
- return f "DSLTwiML.{ p } { sep } { camel ( self .name ) } "
261
+ return "DSLTwiML.{p}{sep}{self.camel_name}" . format ( p = p , sep = sep , self = self )
262
262
263
263
@property
264
264
def dsl_builder (self ):
265
265
p = self .package .title ()
266
266
sep = "." if p else ""
267
- return f"DSLTwiML.{ p } { sep } { self .name } "
267
+ return "DSLTwiML.{p}{sep}{self.name}" .format (p = p , sep = sep , self = self )
268
+
269
+ @property
270
+ def camel_name (self ):
271
+ return camel (self .name )
268
272
269
273
if __name__ == "__main__" :
270
274
__main__ ()
0 commit comments