Commit 56f501b2 authored by Nic's avatar Nic
Browse files

add io.to_configobj, io.to_excel functions

parent e77c2207
......@@ -115,6 +115,14 @@ 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_excel(data, fpath):
data.to_excel(fpath)
# =============================================================================
# Object wrapper
# =============================================================================
......@@ -212,6 +220,38 @@ class IOMixin:
df.to_excel(writer, sheet_name=tabname, index=False)
return fpath
# ========================================================================
# CONFIGOBJ io
# ========================================================================
def read_configobj(self, fpath, sheet_name=None, row_prefix="sniff"):
if not IS_CONFIGOBJ:
raise ValueError(
"Cannot export to configobj. Please install configobj before"
)
self.reset()
self._data = read_configobj(fpath, sheet_name=sheet_name)
def to_configobj(self, fpath):
"""write data as a config obj two-levels nested file"""
if not IS_CONFIGOBJ:
raise ValueError(
"Cannot export to configobj. Please install configobj before"
)
config = ConfigObj(
indent_type=" ", unrepr=True, write_empty_values=True, encoding="utf-8"
)
config.filename = fpath
for tabname, df in self._data.items():
df = df.fillna("")
df2dict = df.T.to_dict()
config[tabname] = {} # create a section
for id_, id_data in df2dict.items():
# 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
# # ========================================================================
# # INI files to
# # ========================================================================
......@@ -277,39 +317,6 @@ class IOMixin:
# df = _cfg_section_to_df(tabname, data, len(row_prefix))
# self._data[tabname] = df
# ========================================================================
# CONFIGOBJ io
# ========================================================================
def to_configobj(self, fpath):
"""write data as a config obj two-levels nested file"""
if not IS_CONFIGOBJ:
raise ValueError(
"Cannot export to configobj. Please install configobj before"
)
config = ConfigObj(
indent_type=" ", unrepr=True, write_empty_values=True, encoding="utf-8"
)
config.filename = fpath
for tabname, df in self._data.items():
df = df.fillna("")
df2dict = df.T.to_dict()
config[tabname] = {} # create a section
for id_, id_data in df2dict.items():
# 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
def read_configobj(self, fpath, sheet_name=None, row_prefix="sniff"):
if not IS_CONFIGOBJ:
raise ValueError(
"Cannot export to configobj. Please install configobj before"
)
self.reset()
self._data = read_configobj(fpath, sheet_name=sheet_name)
def _sniff_row_prefix(sec_title):
return re.match(r"^(.*)\d+$", sec_title).group(1)
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