| 1 |
import logging |
| 2 |
|
| 3 |
|
| 4 |
logger = logging.getLogger(__name__) |
| 5 |
|
| 6 |
|
| 7 |
def unrepr(value): |
| 8 |
raise NotImplementedError |
| 9 |
|
| 10 |
|
| 11 |
def parse_entries(entries, *, ignoresep=None): |
| 12 |
for entry in entries: |
| 13 |
if ignoresep and ignoresep in entry: |
| 14 |
subentries = [entry] |
| 15 |
else: |
| 16 |
subentries = entry.strip().replace(',', ' ').split() |
| 17 |
for item in subentries: |
| 18 |
if item.startswith('+'): |
| 19 |
filename = item[1:] |
| 20 |
try: |
| 21 |
infile = open(filename) |
| 22 |
except FileNotFoundError: |
| 23 |
logger.debug(f'ignored in parse_entries(): +{filename}') |
| 24 |
return |
| 25 |
with infile: |
| 26 |
# We read the entire file here to ensure the file |
| 27 |
# gets closed sooner rather than later. Note that |
| 28 |
# the file would stay open if this iterator is never |
| 29 |
# exhausted. |
| 30 |
lines = infile.read().splitlines() |
| 31 |
for line in _iter_significant_lines(lines): |
| 32 |
yield line, filename |
| 33 |
else: |
| 34 |
yield item, None |
| 35 |
|
| 36 |
|
| 37 |
def _iter_significant_lines(lines): |
| 38 |
for line in lines: |
| 39 |
line = line.partition('#')[0] |
| 40 |
if not line.strip(): |
| 41 |
continue |
| 42 |
yield line |