Skip to content

Commit 0923907

Browse files
author
Troy Melhase
committed
Added support for module epilogue lines. Closes natural#6.
1 parent 9b71c0a commit 0923907

2 files changed

Lines changed: 36 additions & 4 deletions

File tree

lib/defaultconfig.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@
4141
## in method declarations. set to 0 to disable.
4242
minIndentParams = 5
4343

44+
## Note about modulePreamble and moduleEpilogue:
45+
##
46+
## Items in both of these lists may be strings or callables. If
47+
## they're callables, they should take a single argument. The Module
48+
## object will be passed as this single argument in the case of
49+
## callables.
50+
4451
## lines written at the beginning of each generated module. this value
4552
## is cumulative, so when user-defined configuration modules specify
4653
## this value, those lines are written after these.
@@ -50,6 +57,12 @@
5057
'',
5158
]
5259

60+
## lines written at the end of each generated module. this value is
61+
## cumulative, so user-defined configuration modules may specify
62+
## additional values.
63+
moduleEpilogue = [
64+
]
65+
5366
## regular expression substitutions performed on source after
5467
## generation but before final output. this value is cumulative, so
5568
## user-defined configuration modules can add augment these with their

lib/sourcetypes.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ def __init__(self, parent=None, name=None):
102102
self.bases = []
103103
self.modifiers = set()
104104
self.preamble = []
105+
self.epilogue = []
105106
self.lines = []
106107
self.type = None
107108
self.variables = set()
@@ -341,6 +342,11 @@ def setName(self, name):
341342
self.name = name
342343

343344
def fixSwitch(self, block):
345+
""" fixes the first clause in an generated switch statement
346+
347+
@param block Statement instance and child of this block
348+
@return None
349+
"""
344350
lines = self.lines
345351
if (not block in lines) or (block.name != 'if'):
346352
return
@@ -380,15 +386,28 @@ def writeTo(self, output, indent):
380386
@param indent indentation level of this block
381387
@return None
382388
"""
383-
for preambles in self.config.all('modulePreamble', []):
384-
for line in preambles:
389+
self.writeExtraLines('modulePreamble', output)
390+
output.write('\n')
391+
Source.writeTo(self, output, indent)
392+
self.writeExtraLines('moduleEpilogue', output)
393+
394+
def writeExtraLines(self, name, output):
395+
""" writes an sequence of lines given a config attribute name
396+
397+
Lines may be callable. Refer to the defaultconfig module for
398+
details.
399+
400+
@param name configuration module attribute
401+
@param output writable file-like object
402+
@return None
403+
"""
404+
for lines in self.config.all(name, []):
405+
for line in lines:
385406
try:
386407
line = line(self)
387408
except (TypeError, ):
388409
pass
389410
output.write('%s\n' % (line, ))
390-
output.write('\n')
391-
Source.writeTo(self, output, indent)
392411

393412

394413
class Class(Source):

0 commit comments

Comments
 (0)