Commit c212735c authored by Stefan Behnel's avatar Stefan Behnel

modernise code a bit

parent 78c47d4a
...@@ -18,6 +18,8 @@ import operator ...@@ -18,6 +18,8 @@ import operator
import textwrap import textwrap
from string import Template from string import Template
from functools import partial from functools import partial
from contextlib import closing
from collections import defaultdict
try: try:
import hashlib import hashlib
...@@ -164,15 +166,12 @@ class UtilityCodeBase(object): ...@@ -164,15 +166,12 @@ class UtilityCodeBase(object):
{'C': comment}).match {'C': comment}).match
match_type = re.compile('(.+)[.](proto|impl|init|cleanup)$').match match_type = re.compile('(.+)[.](proto|impl|init|cleanup)$').match
f = Utils.open_source_file(filename, encoding='UTF-8') with closing(Utils.open_source_file(filename, encoding='UTF-8')) as f:
try:
all_lines = f.readlines() all_lines = f.readlines()
finally:
f.close()
utilities = {} utilities = defaultdict(lambda: [None, None, {}])
lines = [] lines = []
tags = {} tags = defaultdict(set)
utility = type = None utility = type = None
begin_lineno = 0 begin_lineno = 0
...@@ -192,10 +191,10 @@ class UtilityCodeBase(object): ...@@ -192,10 +191,10 @@ class UtilityCodeBase(object):
name, type = mtype.groups() name, type = mtype.groups()
else: else:
type = 'impl' type = 'impl'
utility = utilities.setdefault(name, [None, None, {}]) utility = utilities[name]
else: else:
tags.setdefault(m.group('tag'), set()).add(m.group('value')) tags[m.group('tag')].add(m.group('value'))
lines.append('') # keep line number correct lines.append('') # keep line number correct
else: else:
lines.append(rstrip(strip_comments(line))) lines.append(rstrip(strip_comments(line)))
...@@ -205,6 +204,7 @@ class UtilityCodeBase(object): ...@@ -205,6 +204,7 @@ class UtilityCodeBase(object):
# 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, tags) cls._add_utility(utility, type, lines, begin_lineno, tags)
utilities = dict(utilities) # un-defaultdict-ify
cls._utility_cache[path] = utilities cls._utility_cache[path] = utilities
return utilities return utilities
...@@ -230,11 +230,10 @@ class UtilityCodeBase(object): ...@@ -230,11 +230,10 @@ class UtilityCodeBase(object):
global __loader__ global __loader__
loader = __loader__ loader = __loader__
archive = loader.archive archive = loader.archive
fileobj = zipfile.ZipFile(archive) with closing(zipfile.ZipFile(archive)) as fileobj:
listing = [ os.path.basename(name) listing = [ os.path.basename(name)
for name in fileobj.namelist() for name in fileobj.namelist()
if os.path.join(archive, name).startswith(utility_dir)] if os.path.join(archive, name).startswith(utility_dir)]
fileobj.close()
files = [ os.path.join(utility_dir, filename) files = [ os.path.join(utility_dir, filename)
for filename in listing for filename in listing
if filename.startswith(prefix) ] if filename.startswith(prefix) ]
...@@ -1607,11 +1606,8 @@ class CCodeWriter(object): ...@@ -1607,11 +1606,8 @@ class CCodeWriter(object):
path = os.path.join(include_dir, include_file) path = os.path.join(include_dir, include_file)
if not os.path.exists(path): if not os.path.exists(path):
tmp_path = '%s.tmp%s' % (path, os.getpid()) tmp_path = '%s.tmp%s' % (path, os.getpid())
f = Utils.open_new_file(tmp_path) with closing(Utils.open_new_file(tmp_path)) as f:
try:
f.write(code) f.write(code)
finally:
f.close()
os.rename(tmp_path, path) os.rename(tmp_path, path)
code = '#include "%s"\n' % path code = '#include "%s"\n' % path
self.put(code) self.put(code)
......
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