Skip to content

Commit

Permalink
fix(SIM115): Use context manager on openlibrary/ (#10306)
Browse files Browse the repository at this point in the history
* fix(SIM115): Use context manager on openlibrary/
* feat: use @contextmanager to keep file open until caller is done
---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
drona-gyawali and pre-commit-ci[bot] authored Jan 19, 2025
1 parent 3e18ea8 commit b69f768
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 43 deletions.
3 changes: 2 additions & 1 deletion openlibrary/admin/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ def setup_ol_config(openlibrary_config_file):
if config.get("infobase_config_file"):
dir = os.path.dirname(openlibrary_config_file)
path = os.path.join(dir, config.infobase_config_file)
config.infobase = yaml.safe_load(open(path).read())
with open(path) as file:
config.infobase = yaml.safe_load(file)

infogami._setup()

Expand Down
18 changes: 9 additions & 9 deletions openlibrary/catalog/marc/mnemonics.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,15 +698,15 @@

def load_table(filename):
mapping = {}
for line in (i.split(',') for i in open(filename) if i.startswith('{')):
key = line[0]
value = ''
for d in line[2].strip().split(" "):
assert len(d) == 4
assert d[3] == 'd'
value += chr(int(d[0:3]))

mapping[key] = value
with open(filename) as file:
for line in (i.split(',') for i in file if i.startswith('{')):
key = line[0]
value = ''
for d in line[2].strip().split(" "):
assert len(d) == 4
assert d[3] == 'd'
value += chr(int(d[0:3]))
mapping[key] = value
return mapping


Expand Down
3 changes: 2 additions & 1 deletion openlibrary/core/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ def new(name: str, submitter: str | None = None) -> "Batch":

def load_items(self, filename):
"""Adds all the items specified in the filename to this batch."""
items = [line.strip() for line in open(filename) if line.strip()]
with open(filename) as file:
items = [line.strip() for line in file if line.strip()]
self.add_items(items)

def dedupe_items(self, items):
Expand Down
9 changes: 5 additions & 4 deletions openlibrary/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ def parse_data_table(filename):
"""Parses the dump of data table and returns an iterator with
<key, type, revision, json> for all entries.
"""
for line in open(filename):
thing_id, revision, json_data = pgdecode(line).strip().split("\t")
d = json.loads(json_data)
yield d['key'], d['type']['key'], str(d['revision']), json_data
with open(filename) as file:
for line in file:
thing_id, revision, json_data = pgdecode(line).strip().split("\t")
d = json.loads(json_data)
yield d['key'], d['type']['key'], str(d['revision']), json_data
5 changes: 4 additions & 1 deletion openlibrary/data/dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ def read_data_file(filename: str, max_lines: int = 0):

def xopen(path: str, mode: str):
if path.endswith(".gz"):
return gzip.open(path, mode)
with gzip.open(
path, mode
) as file: # need to add no QA when the rule is enabled
return file
else:
return open(path, mode)

Expand Down
15 changes: 7 additions & 8 deletions openlibrary/data/sitemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,8 @@


def gzwrite(path, data):
f = gzopen(path, 'w')
f.write(data)
f.close()
with gzopen(path, 'w') as file:
file.write(data)


def write_sitemaps(data, outdir, prefix):
Expand Down Expand Up @@ -114,8 +113,9 @@ def write_siteindex(data, outdir, prefix):


def parse_index_file(index_file):
data = (line.strip().split("\t") for line in open(index_file))
data = ([t[0], " ".join(t[1:-2]), t[-2], t[-1]] for t in data)
with open(index_file) as file:
data = (line.strip().split("\t") for line in file)
data = ([t[0], " ".join(t[1:-2]), t[-2], t[-1]] for t in data)
return data


Expand All @@ -133,9 +133,8 @@ def write(path, data):
print("writing", path)
mkdir_p(os.path.dirname(path))

f = open(path, "w")
f.write(data)
f.close()
with open(path, "w") as file:
file.write(data)


def dirindex(dir, back=".."):
Expand Down
3 changes: 2 additions & 1 deletion openlibrary/mocks/mock_infobase.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,8 @@ def mock_site(request):

def read_types():
for path in glob.glob("openlibrary/plugins/openlibrary/types/*.type"):
text = open(path).read()
with open(path) as file:
text = file.read()
doc = eval(text, {'true': True, 'false': False})
if isinstance(doc, list):
yield from doc
Expand Down
5 changes: 2 additions & 3 deletions openlibrary/plugins/ol_infobase.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,8 @@ def write(path, data):
dir = os.path.dirname(path)
if not os.path.exists(dir):
os.makedirs(dir)
f = open(path, 'w')
f.write(data)
f.close()
with open(path, 'w') as file:
file.write(data)


def save_error(dir, prefix):
Expand Down
19 changes: 7 additions & 12 deletions openlibrary/plugins/openlibrary/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,11 @@ def visit(key):

@infogami.action
def sampleload(filename='sampledump.txt.gz'):
if filename.endswith('.gz'):
import gzip
import gzip

f = gzip.open(filename)
else:
f = open(filename)
with gzip.open(filename) if filename.endswith('.gz') else open(filename) as file:
queries = [json.loads(line) for line in file]

queries = [json.loads(line) for line in f]
print(web.ctx.site.save_many(queries))


Expand Down Expand Up @@ -400,9 +397,8 @@ def save(filename, text):
dir = os.path.dirname(path)
if not os.path.exists(dir):
os.makedirs(dir)
f = open(path, 'w')
f.write(text)
f.close()
with open(path, 'w') as file:
file.write(text)


def change_ext(filename, ext):
Expand Down Expand Up @@ -1119,9 +1115,8 @@ def save_error():
os.makedirs(dir)

error = web.safestr(web.djangoerror())
f = open(path, 'w')
f.write(error)
f.close()
with open(path, 'w') as file:
file.write(error)

print('error saved to', path, file=web.debug)
return name
Expand Down
7 changes: 4 additions & 3 deletions openlibrary/tests/solr/test_types_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

def test_up_to_date():
types_path = os.path.join(root, '..', '..', 'solr', 'solr_types.py')
assert (
generate().strip() == open(types_path).read().strip()
), """
with open(types_path) as file:
assert (
generate().strip() == file.read().strip()
), """
This auto-generated file is out-of-date. Run:
./openlibrary/solr/types_generator.py > ./openlibrary/solr/solr_types.py
"""

0 comments on commit b69f768

Please sign in to comment.