Skip to content
Snippets Groups Projects
Commit 058b7678 authored by Florian Spreckelsen's avatar Florian Spreckelsen
Browse files

Merge branch 'f-table-export-find-func' into 'dev'

ENH: more useful table export find func

See merge request !78
parents 93be597d 17c44505
No related branches found
No related tags found
2 merge requests!89ENH: JsonSchemaExporter accepts do_not_create parameter.,!78ENH: more useful table export find func
Pipeline #42712 failed
...@@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ...@@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added ### ### Added ###
* Added support for passing callables as `find_func` to the `BaseTableExporter`.
* Added member `BaseTableExporter.all_keys`
* Parsing from YAML now allows to give an existing model to which the YAML data model shall be * Parsing from YAML now allows to give an existing model to which the YAML data model shall be
added. added.
......
...@@ -27,6 +27,7 @@ them for an export as a table, e.g., for the export to metadata ...@@ -27,6 +27,7 @@ them for an export as a table, e.g., for the export to metadata
repositories. repositories.
""" """
from inspect import signature
import json import json
import logging import logging
...@@ -83,7 +84,7 @@ class BaseTableExporter(object): ...@@ -83,7 +84,7 @@ class BaseTableExporter(object):
``` ```
{"entry_to_be_exported: { {"entry_to_be_exported: {
"optional": True/False "optional": True/False
"find_func": name of member function "find_func": callable or name of member function
"query": query string "query": query string
"selector": selector for the query "selector": selector for the query
"error": error explanation "error": error explanation
...@@ -97,8 +98,8 @@ class BaseTableExporter(object): ...@@ -97,8 +98,8 @@ class BaseTableExporter(object):
- optional: True or False, if not present, the entry is - optional: True or False, if not present, the entry is
assumed to be mandatory. assumed to be mandatory.
- find_func: name of the member function that returns the - find_func: name of the member function that returns the
value for this entry. Must not exist together with value for this entry or callable object. Must not exist
`query` together with `query`
- query: Query string for finding the value for this - query: Query string for finding the value for this
entry. If this is given, a record must be given to the entry. If this is given, a record must be given to the
constructor of this class. The query is then executed as constructor of this class. The query is then executed as
...@@ -132,6 +133,7 @@ class BaseTableExporter(object): ...@@ -132,6 +133,7 @@ class BaseTableExporter(object):
self._check_sanity_of_export_dict() self._check_sanity_of_export_dict()
self.raise_error_if_missing = raise_error_if_missing self.raise_error_if_missing = raise_error_if_missing
self.info = {} self.info = {}
self.all_keys = [key for key in self.export_dict]
def collect_information(self): def collect_information(self):
"""Use the items of `export_dict` to collect the information for the """Use the items of `export_dict` to collect the information for the
...@@ -139,7 +141,8 @@ class BaseTableExporter(object): ...@@ -139,7 +141,8 @@ class BaseTableExporter(object):
""" """
for e, d in self.export_dict.items(): for e in self.all_keys:
d = self.export_dict[e]
if QUERY in d: if QUERY in d:
# TODO: How do we make this more general? There might # TODO: How do we make this more general? There might
# be queries that don't need the record or work with # be queries that don't need the record or work with
...@@ -163,12 +166,15 @@ class BaseTableExporter(object): ...@@ -163,12 +166,15 @@ class BaseTableExporter(object):
else: else:
self._append_missing(e, d) self._append_missing(e, d)
elif FIND_FUNCTION in d: elif FIND_FUNCTION in d:
find_fun = getattr(self, d[FIND_FUNCTION])
try: try:
self.info[e] = find_fun() val = self._call_find_function(d[FIND_FUNCTION], e)
if val is not None:
self.info[e] = val
else:
self._append_missing(e, d)
except Exception as exc: except Exception as exc:
self._append_missing(e, d) self._append_missing(e, d)
logger.debug(exc) logger.error(exc)
# last resort: check if record has e as property: # last resort: check if record has e as property:
else: else:
try: try:
...@@ -200,6 +206,22 @@ class BaseTableExporter(object): ...@@ -200,6 +206,22 @@ class BaseTableExporter(object):
else: else:
logger.error(errmssg) logger.error(errmssg)
def _call_find_function(self, find_function, e):
account_for_self = 0
if callable(find_function):
find_fun = find_function
else:
find_fun = getattr(self, find_function)
account_for_self = 1
sig = signature(find_fun)
params = sig.parameters
if len(params) > (account_for_self + 1):
return find_fun(self.record, e)
elif len(params) > account_for_self:
return find_fun(self.record)
return find_fun()
def prepare_csv_export(self, delimiter=',', print_header=False, def prepare_csv_export(self, delimiter=',', print_header=False,
skip_empty_optionals=False): skip_empty_optionals=False):
"""Return the values in self.info as a single-line string, separated """Return the values in self.info as a single-line string, separated
...@@ -238,7 +260,8 @@ class BaseTableExporter(object): ...@@ -238,7 +260,8 @@ class BaseTableExporter(object):
if print_header: if print_header:
header = "" header = ""
for e, d in self.export_dict.items(): for e in self.all_keys:
d = self.export_dict[e]
if e in self.info: if e in self.info:
body += str(self.info[e]) + delimiter body += str(self.info[e]) + delimiter
...@@ -287,7 +310,9 @@ class BaseTableExporter(object): ...@@ -287,7 +310,9 @@ class BaseTableExporter(object):
# check find function if present # check find function if present
if FIND_FUNCTION in d: if FIND_FUNCTION in d:
if not hasattr(self, d[FIND_FUNCTION]): if callable(d[FIND_FUNCTION]):
pass
elif not hasattr(self, d[FIND_FUNCTION]):
raise TableExportError( raise TableExportError(
"Find function " + d[FIND_FUNCTION] + "Find function " + d[FIND_FUNCTION] +
" was specified for entry " + e + " was specified for entry " + e +
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment