Files
linux/debian/lib/python/debian_linux/utils.py
Ben Hutchings af0098b10a debian/bin, debian/lib/python: Fix most errors reported by pycodestyle
Fix coding style violations reported by pycodestyle.  This is
mostly a matter of reformatting code, particularly to eliminate
over-long lines.  I also rename one variable ("l" is considered
visually ambiguous) and change a bare "except" to explicitly
catch all exceptions.

There are three types of error or warning remaining:

- debian/bin/...: E402 module level import not at top of file
  Scripts in debian/bin need to modify the import path before
  importing from debian/lib/python.
- E127 continuation line over-indented for visual indent
  This seems to be a false positive.  pycodestyle doesn't seem to be
  happy with any level of indent (including 0) on a continuation line
  in a "with" statement.
- debian/lib/python/debian_linux/debian.py:15:2: W291 trailing whitespace
  This is a false positive.  The trailing spaces are in a long
  string and are intentional.
2018-10-01 21:41:23 +01:00

98 lines
2.6 KiB
Python

import codecs
import os
import re
import textwrap
class Templates(object):
def __init__(self, dirs=["debian/templates"]):
self.dirs = dirs
self._cache = {}
def __getitem__(self, key):
ret = self.get(key)
if ret is not None:
return ret
raise KeyError(key)
def _read(self, name):
prefix, id = name.split('.', 1)
for suffix in ['.in', '']:
for dir in self.dirs:
filename = "%s/%s%s" % (dir, name, suffix)
if os.path.exists(filename):
f = codecs.open(filename, 'r', 'utf-8')
if prefix == 'control':
return read_control(f)
if prefix == 'tests-control':
return read_tests_control(f)
return f.read()
def get(self, key, default=None):
if key in self._cache:
return self._cache[key]
value = self._cache.setdefault(key, self._read(key))
if value is None:
return default
return value
def read_control(f):
from .debian import Package
return _read_rfc822(f, Package)
def read_tests_control(f):
from .debian import TestsControl
return _read_rfc822(f, TestsControl)
def _read_rfc822(f, cls):
entries = []
eof = False
while not eof:
e = cls()
last = None
lines = []
while True:
line = f.readline()
if not line:
eof = True
break
# Strip comments rather than trying to preserve them
if line[0] == '#':
continue
line = line.strip('\n')
if not line:
break
if line[0] in ' \t':
if not last:
raise ValueError(
'Continuation line seen before first header')
lines.append(line.lstrip())
continue
if last:
e[last] = '\n'.join(lines)
i = line.find(':')
if i < 0:
raise ValueError(u"Not a header, not a continuation: ``%s''" %
line)
last = line[:i]
lines = [line[i + 1:].lstrip()]
if last:
e[last] = '\n'.join(lines)
if e:
entries.append(e)
return entries
class TextWrapper(textwrap.TextWrapper):
wordsep_re = re.compile(
r'(\s+|' # any whitespace
r'(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w))') # em-dash