Browse Source

Transformation de encoding.open_read en un 'context manager'

pull/69/head
Louis 10 years ago
parent
commit
607e064bfb
  1. 5
      patacrep/content/include.py
  2. 8
      patacrep/encoding.py
  3. 6
      patacrep/index.py
  4. 3
      patacrep/latex/__init__.py
  5. 6
      patacrep/songbook.py
  6. 7
      patacrep/templates.py

5
patacrep/content/include.py

@ -48,15 +48,12 @@ def parse(keyword, config, argument, contentlist):
filepath = load_from_datadirs(path, config)
content_file = None
try:
content_file = encoding.open_read(filepath, 'r')
with encoding.open_read(filepath, 'r') as content_file:
new_content = json.load(content_file)
except Exception as error: # pylint: disable=broad-except
LOGGER.error(error)
LOGGER.error("Error while loading file '{}'.".format(filepath))
sys.exit(1)
finally:
if content_file:
content_file.close()
config["datadir"].append(os.path.abspath(os.path.dirname(filepath)))
new_contentlist += process_content(new_content, config)

8
patacrep/encoding.py

@ -5,17 +5,21 @@
import codecs
import chardet
import logging
import contextlib
LOGGER = logging.getLogger(__name__)
@contextlib.contextmanager
def open_read(filename, mode='r'):
"""Open a file for reading, guessing the right encoding.
Return a fileobject, reading unicode strings.
"""
return codecs.open(
with codecs.open(
filename,
mode=mode,
encoding=chardet.detect(open(filename, 'rb').read())['encoding'],
errors='replace',
)
) as fileobject:
yield fileobject

6
patacrep/index.py

@ -29,13 +29,9 @@ def process_sxd(filename):
"""
data = []
index_file = None
try:
index_file = encoding.open_read(filename, 'r')
with encoding.open_read(filename, 'r') as index_file:
for line in index_file:
data.append(line.strip())
finally:
if index_file:
index_file.close()
i = 1
idx = Index(data[0])

3
patacrep/latex/__init__.py

@ -15,6 +15,7 @@ def parsesong(path):
"""Return a dictonary of data read from the latex file `path`.
"""
data = syntax_parsesong(encoding.open_read(path).read(), path)
with encoding.open_read(path) as songfile:
data = syntax_parsesong(songfile.read(), path)
data['@path'] = path
return data

6
patacrep/songbook.py

@ -100,17 +100,13 @@ def main():
basename = os.path.basename(songbook_path)[:-3]
songbook_file = None
try:
songbook_file = encoding.open_read(songbook_path)
with encoding.open_read(songbook_path) as songbook_file:
songbook = json.load(songbook_file)
except Exception as error: # pylint: disable=broad-except
LOGGER.error(error)
LOGGER.error("Error while loading file '{}'.".format(songbook_path))
sys.exit(1)
finally:
if songbook_file:
songbook_file.close()
# Gathering datadirs
datadirs = []

7
patacrep/templates.py

@ -186,10 +186,8 @@ class TexRenderer(object):
"""
subvariables = {}
template_file = None
templatename = self.texenv.get_template(template).filename
try:
template_file = encoding.open_read(templatename, 'r')
with encoding.open_read(templatename, 'r') as template_file:
content = template_file.read()
subtemplates = list(find_templates(self.texenv.parse(content)))
match = re.findall(_VARIABLE_REGEXP, content)
@ -209,9 +207,6 @@ class TexRenderer(object):
jsonstring=var,
)
)
finally:
if template_file:
template_file.close()
return (subvariables, subtemplates)

Loading…
Cancel
Save