Skip to content

Commit

Permalink
Merge pull request #2152 from midichef/with_opens
Browse files Browse the repository at this point in the history
[open-] silence ResourceWarnings for unclosed files
  • Loading branch information
anjakefala authored Dec 4, 2023
2 parents 215b9ef + f9cc12d commit 9fd728b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
20 changes: 11 additions & 9 deletions visidata/loaders/lsv.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,19 @@ def iterload(self):
self._knownCols = set()
row = collections.defaultdict(str)
k = ''
for line in self.open_text_source():
line = line.strip()
if not line:
yield row
row = collections.defaultdict(str)

if ':' in line:
k, line = line.split(':', maxsplit=1)
# else append to previous k
with self.open_text_source() as fp:
for line in fp:
line = line.strip()
if not line:
yield row
row = collections.defaultdict(str)

row[k.strip()] += line.strip()
if ':' in line:
k, line = line.split(':', maxsplit=1)
# else append to previous k

row[k.strip()] += line.strip()

if row:
yield row
25 changes: 13 additions & 12 deletions visidata/loaders/vcf.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@ def reload(self):

addedCols = set()
lines = []
for line in self.open_text_source():
lines.append(line)
if line.startswith('END:'):
row = vobject.readOne('\n'.join(lines))
for k, v in row.contents.items():
if v and str(v[0].value).startswith('(None)'):
continue
if not k in addedCols:
addedCols.add(k)
self.addColumn(Column(k, expr=k, getter=unbox))
self.addRow(row.contents)
lines = []
with self.open_text_source() as fp:
for line in fp:
lines.append(line)
if line.startswith('END:'):
row = vobject.readOne('\n'.join(lines))
for k, v in row.contents.items():
if v and str(v[0].value).startswith('(None)'):
continue
if not k in addedCols:
addedCols.add(k)
self.addColumn(Column(k, expr=k, getter=unbox))
self.addRow(row.contents)
lines = []
3 changes: 2 additions & 1 deletion visidata/loaders/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ def iterload(self):
vd.importExternal('lxml')
from lxml import etree, objectify
p = etree.XMLParser(**self.options.getall('xml_parser_'))
self.root = etree.parse(self.open_text_source(), parser=p)
with self.open_text_source() as fp:
self.root = etree.parse(fp, parser=p)
objectify.deannotate(self.root, cleanup_namespaces=True)
else: # elif isinstance(self.source, XmlElement):
self.root = self.source
Expand Down
3 changes: 2 additions & 1 deletion visidata/stored_prop.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def _decorator(*args, **kwargs):

p = Path(vd.options.visidata_dir)/(f.__name__ + '.json')
if p.exists():
value = json.loads(p.open(encoding='utf-8-sig').read())
with p.open(encoding='utf-8-sig') as fp:
value = json.loads(fp.read())

if value is None:
value = f(*args, **kwargs)
Expand Down

0 comments on commit 9fd728b

Please sign in to comment.