Skip to content

Commit

Permalink
load_config and write_config now accept file-like objects as well as …
Browse files Browse the repository at this point in the history
…filepaths
  • Loading branch information
h-croser committed Dec 6, 2024
1 parent 6fd2ac0 commit 7de1101
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions src/rocrate_tabular/tabulator.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from os import PathLike

from rocrate_tabular.tinycrate import TinyCrate, TinyCrateException
from argparse import ArgumentParser
from pathlib import Path
Expand Down Expand Up @@ -50,8 +52,19 @@ def __init__(self):

def load_config(self, config_file):
"""Load config from file"""
with open(config_file, "r") as jfh:
self.cf = json.load(jfh)
close_file = False
if isinstance(config_file, (str, PathLike)):
config_file = open(config_file, "r")
close_file = True
else:
config_file.seek(0)

self.cf = json.load(config_file)

if close_file:
config_file.close()
else:
config_file.seek(0)

def infer_config(self):
"""Create a default config based on the properties table"""
Expand All @@ -76,8 +89,19 @@ def infer_config(self):

def write_config(self, config_file):
"""Write the config file with any changes made"""
with open(config_file, "w") as f:
json.dump(self.cf, f, indent=4)
close_file = False
if isinstance(config_file, (str, PathLike)):
config_file = open(config_file, "w")
close_file = True
else:
config_file.seek(0)

json.dump(self.cf, config_file, indent=4)

if close_file:
config_file.close()
else:
config_file.seek(0)

def crate_to_db(self, crate_uri, db_file):
"""Load the crate and build the properties table"""
Expand Down

0 comments on commit 7de1101

Please sign in to comment.