Commit 0301ebf1 authored by Stefan Behnel's avatar Stefan Behnel

clean up utility code parsing, encourage commenting in utility code files by...

clean up utility code parsing, encourage commenting in utility code files by stripping comments on writing
parent 51d6b865
...@@ -55,9 +55,11 @@ class UtilityCodeBase(object): ...@@ -55,9 +55,11 @@ class UtilityCodeBase(object):
@classmethod @classmethod
def _add_utility(cls, utility, type, lines, begin_lineno): def _add_utility(cls, utility, type, lines, begin_lineno):
if utility: if utility is None:
# Remember line numbers as least until after templating return
code = '\n' * begin_lineno + ''.join(lines)
# remember correct line numbers as least until after templating
code = '\n' * begin_lineno + '\n'.join(lines)
if type == 'Proto': if type == 'Proto':
utility[0] = code utility[0] = code
...@@ -71,54 +73,50 @@ class UtilityCodeBase(object): ...@@ -71,54 +73,50 @@ class UtilityCodeBase(object):
return utilities return utilities
filename = os.path.join(get_utility_dir(), path) filename = os.path.join(get_utility_dir(), path)
_, ext = os.path.splitext(path) _, ext = os.path.splitext(path)
if ext in ('.pyx', '.py', '.pxd', '.pxi'): if ext in ('.pyx', '.py', '.pxd', '.pxi'):
comment = '#' comment = '#'
replace_comments = re.compile(r'^\s*#.*').sub
else: else:
comment = '/' comment = '/'
replace_comments = re.compile(r'^\s*//.*|^\s*/\*[^*]*\*/').sub
regex = r'%s{5,30}\s*((\w|\.)+)\s*%s{5,30}' % (comment, comment) match_header = re.compile(r'%s{5,30}\s*((\w|\.)+)\s*%s{5,30}' % (comment, comment)).match
utilities = {}
lines = []
utility = type = None
begin_lineno = 0
f = Utils.open_source_file(filename, encoding='UTF-8') f = Utils.open_source_file(filename, encoding='UTF-8')
try: try:
all_lines = f.readlines() # py23 all_lines = f.readlines()
finally: finally:
f.close() f.close()
utilities = {}
lines = []
utility = type = None
begin_lineno = 0
for lineno, line in enumerate(all_lines): for lineno, line in enumerate(all_lines):
m = re.search(regex, line) m = match_header(line)
if m: if m:
cls._add_utility(utility, type, lines, begin_lineno) cls._add_utility(utility, type, lines, begin_lineno)
begin_lineno = lineno + 1 begin_lineno = lineno + 1
lines = []
name = m.group(1) name = m.group(1)
if name.endswith(".proto"): if name.endswith(".proto"):
name = name[:-6] name = name[:-6]
type = 'Proto' type = 'Proto'
else: else:
type = 'Code' type = 'Code'
utility = utilities.setdefault(name, [None, None]) utility = utilities.setdefault(name, [None, None])
utilities[name] = utility
lines = []
else: else:
lines.append(line) lines.append(replace_comments('', line).rstrip())
if not utility: if utility is None:
raise ValueError("Empty utility code file") raise ValueError("Empty utility code file")
# Don't forget to add the last utility code # Don't forget to add the last utility code
cls._add_utility(utility, type, lines, begin_lineno) cls._add_utility(utility, type, lines, begin_lineno)
f.close()
cls._utility_cache[path] = utilities cls._utility_cache[path] = utilities
return utilities return utilities
...@@ -204,6 +202,14 @@ class UtilityCodeBase(object): ...@@ -204,6 +202,14 @@ class UtilityCodeBase(object):
return s % context return s % context
def format_code(self, code_string, replace_empty_lines=re.compile(r'\n\n+').sub):
"""
Format a code section for output.
"""
if code_string:
code_string = replace_empty_lines('\n', code_string.strip()) + '\n\n'
return code_string
def __str__(self): def __str__(self):
return "<%s(%s)" % (type(self).__name__, self.name) return "<%s(%s)" % (type(self).__name__, self.name)
...@@ -288,19 +294,19 @@ class UtilityCode(UtilityCodeBase): ...@@ -288,19 +294,19 @@ class UtilityCode(UtilityCodeBase):
for dependency in self.requires: for dependency in self.requires:
output.use_utility_code(dependency) output.use_utility_code(dependency)
if self.proto: if self.proto:
output[self.proto_block].put(self.proto) output[self.proto_block].put(self.format_code(self.proto))
if self.impl: if self.impl:
output['utility_code_def'].put(self.impl) output['utility_code_def'].put(self.format_code(self.impl))
if self.init: if self.init:
writer = output['init_globals'] writer = output['init_globals']
if isinstance(self.init, basestring): if isinstance(self.init, basestring):
writer.put(self.init) writer.put(self.format_code(self.init))
else: else:
self.init(writer, output.module_pos) self.init(writer, output.module_pos)
if self.cleanup and Options.generate_cleanup_code: if self.cleanup and Options.generate_cleanup_code:
writer = output['cleanup_globals'] writer = output['cleanup_globals']
if isinstance(self.cleanup, basestring): if isinstance(self.cleanup, basestring):
writer.put(self.cleanup) writer.put(self.format_code(self.cleanup))
else: else:
self.cleanup(writer, output.module_pos) self.cleanup(writer, output.module_pos)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment