Commit fa8b1400 authored by Nic's avatar Nic
Browse files

fix io.to_configobj

parent 114bddf5
......@@ -93,8 +93,11 @@ def read_excel(fpath, sheet_name=None, engine=DEFAULT_EXCEL_ENGINE):
return ret
def read_configobj(fpath, sheet_name=None, row_prefix="sniff"):
def read_configobj(fpath, as_content=False, sheet_name=None, row_prefix="sniff"):
ret = {}
if as_content:
# fpath **is** the content of the file.
fpath = StringIO(fpath)
config = ConfigObj(fpath, indent_type=" ", unrepr=True, write_empty_values=True)
# transform sections to tabs and data to columns...
if row_prefix == "sniff":
......@@ -115,12 +118,16 @@ def read_configobj(fpath, sheet_name=None, row_prefix="sniff"):
raise NotImplementedError("not imp")
def to_configobj(data, fpath):
data.to_configobj(fpath)
def to_configobj(data, fpath=None):
ret = data.to_configobj(fpath)
if not fpath:
ret = "\n".join([s.decode() for s in ret])
return ret
def to_excel(data, fpath):
data.to_excel(fpath)
return fpath
# =============================================================================
......@@ -231,7 +238,7 @@ class IOMixin:
self.reset()
self._data = read_configobj(fpath, sheet_name=sheet_name)
def to_configobj(self, fpath):
def to_configobj(self, fpath=None):
"""write data as a config obj two-levels nested file"""
if not IS_CONFIGOBJ:
raise ValueError(
......@@ -240,7 +247,8 @@ class IOMixin:
config = ConfigObj(
indent_type=" ", unrepr=True, write_empty_values=True, encoding="utf-8"
)
config.filename = fpath
if not fpath:
config.filename = None
for tabname, df in self._data.items():
df = df.fillna("")
df2dict = df.T.to_dict()
......@@ -249,8 +257,14 @@ class IOMixin:
# multiline headers are not allowed
id_data = {k.replace("\n", ":::"): v for k, v in id_data.items()}
config[tabname][f"{ROW_PREFIX}{id_}"] = id_data
config.write()
return fpath
if not fpath:
config.filename = None
txt = config.write()
return txt
else:
config.filename = fpath
config.write()
return fpath
# # ========================================================================
# # INI files to
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment