Skip to content

Commit

Permalink
allow passed ZipWriter objectd to write_jsonld, supports USEPA/LCIAfo…
Browse files Browse the repository at this point in the history
  • Loading branch information
bl-young committed Apr 23, 2024
1 parent 7fbf75f commit 7ed509a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
4 changes: 2 additions & 2 deletions fedelemflowlist/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def get_flowmapping(source=None):
return flowmappings


def write_jsonld(flows, path: Path, mappings=None):
def write_jsonld(flows, path: Path, mappings=None, **kwargs):
""" Writes a standard openLCA JSON-LD zip archive with elementary flows and optionally
flowmappings
Expand All @@ -76,7 +76,7 @@ def write_jsonld(flows, path: Path, mappings=None):
:return: writes out .zip file
"""
writer = jsonld.Writer(flow_list=flows, flow_mapping=mappings)
writer.write_to(path)
writer.write_to(path, zw=kwargs.get('zw'))

def get_alt_conversion():
"""returns a dataframe of all flowables with altunits and alt conversion factors
Expand Down
14 changes: 10 additions & 4 deletions fedelemflowlist/jsonld.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,20 +130,26 @@ def __init__(self, flow_list: pd.DataFrame,
self.flow_mapping = flow_mapping
self._context_uids = {}

def write_to(self, path: Path):
def write_to(self, path: Path, zw: zipio.ZipWriter = None):
"""
Writes json dictionaries to files
:param path: string path to file
:param zw: optional zipio.ZipWriter
:return: None
"""
if path.exists():
if (path and path.exists()):
log.warning(f'File {path} already exists and will be overwritten')
path.unlink()
zw = zipio.ZipWriter(path)
if not zw:
passed_zw = False
zw = zipio.ZipWriter(path)
else:
passed_zw = True
self._write_flows(zw)
if self.flow_mapping is not None:
self._write_mappings(zw)
zw.close()
if not passed_zw:
zw.close()

def _write_flows(self, zw: zipio.ZipWriter):
altflowlist=fedelemflowlist.get_alt_conversion()
Expand Down

0 comments on commit 7ed509a

Please sign in to comment.