refactor: move json writing into utility functions
The same options where reused in multiple places
This commit is contained in:
@@ -151,13 +151,8 @@ class bam_config:
|
|||||||
descr="bam repository",
|
descr="bam repository",
|
||||||
)
|
)
|
||||||
|
|
||||||
with open(filepath, 'w') as f:
|
from bam_utils.system import write_json_to_file
|
||||||
json.dump(
|
write_json_to_file(filepath, data)
|
||||||
data, f, ensure_ascii=False,
|
|
||||||
check_circular=False,
|
|
||||||
# optional (pretty)
|
|
||||||
sort_keys=True, indent=4, separators=(',', ': '),
|
|
||||||
)
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def write_bamignore(cwd=None):
|
def write_bamignore(cwd=None):
|
||||||
@@ -503,6 +498,7 @@ class bam_commands:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def commit(paths, message):
|
def commit(paths, message):
|
||||||
|
from bam_utils.system import write_json_to_file, write_json_to_zip
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
# Load project configuration
|
# Load project configuration
|
||||||
@@ -632,14 +628,6 @@ class bam_commands:
|
|||||||
|
|
||||||
# make a paths remap that only includes modified files
|
# make a paths remap that only includes modified files
|
||||||
# TODO(cam), from 'packer.py'
|
# TODO(cam), from 'packer.py'
|
||||||
def write_dict_as_json(f, dct):
|
|
||||||
zip_handle.writestr(
|
|
||||||
f,
|
|
||||||
json.dumps(dct,
|
|
||||||
check_circular=False,
|
|
||||||
# optional (pretty)
|
|
||||||
sort_keys=True, indent=4, separators=(',', ': '),
|
|
||||||
).encode('utf-8'))
|
|
||||||
|
|
||||||
paths_remap_subset = {
|
paths_remap_subset = {
|
||||||
f_rel: f_rel_in_proj
|
f_rel: f_rel_in_proj
|
||||||
@@ -649,7 +637,7 @@ class bam_commands:
|
|||||||
for f_rel in paths_add})
|
for f_rel in paths_add})
|
||||||
|
|
||||||
# paths_remap_subset.update(paths_remap_subset_add)
|
# paths_remap_subset.update(paths_remap_subset_add)
|
||||||
write_dict_as_json(".bam_paths_remap.json", paths_remap_subset)
|
write_json_to_zip(zip_handle, ".bam_paths_remap.json", paths_remap_subset)
|
||||||
|
|
||||||
# build a list of path manipulation operations
|
# build a list of path manipulation operations
|
||||||
paths_ops = {}
|
paths_ops = {}
|
||||||
@@ -659,7 +647,7 @@ class bam_commands:
|
|||||||
f_abs_remote = paths_remap[f_rel]
|
f_abs_remote = paths_remap[f_rel]
|
||||||
paths_ops[f_abs_remote] = 'D'
|
paths_ops[f_abs_remote] = 'D'
|
||||||
|
|
||||||
write_dict_as_json(".bam_paths_ops.json", paths_ops)
|
write_json_to_zip(zip_handle, ".bam_paths_ops.json", paths_ops)
|
||||||
log.debug(paths_ops)
|
log.debug(paths_ops)
|
||||||
|
|
||||||
if os.path.exists(basedir_temp):
|
if os.path.exists(basedir_temp):
|
||||||
@@ -698,26 +686,19 @@ class bam_commands:
|
|||||||
# TODO, handle error cases
|
# TODO, handle error cases
|
||||||
ok = True
|
ok = True
|
||||||
if ok:
|
if ok:
|
||||||
# NOTE, we may want to generalize the 'update uuid' code & share it.
|
|
||||||
def write_dict_as_json(filepath, dct):
|
|
||||||
with open(os.path.join(session_rootdir, filepath), 'w') as f:
|
|
||||||
json.dump(
|
|
||||||
dct, f, ensure_ascii=False,
|
|
||||||
check_circular=False,
|
|
||||||
# optional (pretty)
|
|
||||||
sort_keys=True, indent=4, separators=(',', ': '),
|
|
||||||
)
|
|
||||||
# ----------
|
# ----------
|
||||||
# paths_uuid
|
# paths_uuid
|
||||||
paths_uuid.update(paths_uuid_update)
|
paths_uuid.update(paths_uuid_update)
|
||||||
write_dict_as_json(".bam_paths_uuid.json", paths_uuid_update)
|
write_json_to_file(os.path.join(session_rootdir, ".bam_paths_uuid.json"), paths_uuid_update)
|
||||||
|
|
||||||
# -----------
|
# -----------
|
||||||
# paths_remap
|
# paths_remap
|
||||||
paths_remap.update(paths_remap_subset)
|
paths_remap.update(paths_remap_subset)
|
||||||
for k in paths_remove:
|
for k in paths_remove:
|
||||||
del paths_remap[k]
|
del paths_remap[k]
|
||||||
write_dict_as_json(".bam_paths_remap.json", paths_remap)
|
write_json_to_file(os.path.join(session_rootdir, ".bam_paths_remap.json"), paths_remap)
|
||||||
|
del write_json_to_file
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def status(paths, use_json=False):
|
def status(paths, use_json=False):
|
||||||
|
@@ -69,3 +69,26 @@ def uuid_from_file(fn, block_size=1 << 20):
|
|||||||
# skip the '0x'
|
# skip the '0x'
|
||||||
return hex(size)[2:] + sha1.hexdigest()
|
return hex(size)[2:] + sha1.hexdigest()
|
||||||
|
|
||||||
|
|
||||||
|
def write_json_to_zip(zip_handle, path, data=None):
|
||||||
|
import json
|
||||||
|
zip_handle.writestr(
|
||||||
|
path,
|
||||||
|
json.dumps(
|
||||||
|
data,
|
||||||
|
check_circular=False,
|
||||||
|
# optional (pretty)
|
||||||
|
sort_keys=True, indent=4, separators=(',', ': '),
|
||||||
|
).encode('utf-8'))
|
||||||
|
|
||||||
|
|
||||||
|
def write_json_to_file(path, data):
|
||||||
|
import json
|
||||||
|
with open(path, 'w') as file_handle:
|
||||||
|
json.dump(
|
||||||
|
data, file_handle, ensure_ascii=False,
|
||||||
|
check_circular=False,
|
||||||
|
# optional (pretty)
|
||||||
|
sort_keys=True, indent=4, separators=(',', ': '),
|
||||||
|
)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user