Skip to content

Commit

Permalink
make the parser base more abstract
Browse files Browse the repository at this point in the history
  • Loading branch information
luojing1211 committed Feb 20, 2019
1 parent ac6bbc0 commit 20f6adc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 19 deletions.
4 changes: 2 additions & 2 deletions adx/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ def types(self):
def set_up_table(self):
self.tables_cols = {}
for prs in self.parsers:
self.tables_cols[prs] = [pf[0] for pf in prs.parse_funcs]
self.tables_cols[prs] = [pi for pi in prs.info_list]

def get_info_entry(self, item, parser):
""" Parse the item information following the table column.
"""
info = parser(item)
entry = ()
for tc in self.tables_cols[parser._type]:
for tc in self.tables_cols[parser.item_type]:
entry += (info[tc],)
return entry

Expand Down
22 changes: 5 additions & 17 deletions adx/parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,9 @@ class ParserBase:
Atributes
---------
parse_funs: list of tuple
The request information name and the method/callable functions to get
the information. The first element is the request information name (in
ADX this attribute will be used as the database column name.), and the
second value is the callable fucntions that parse the information.
One should organize the parse functions accordingly.
The default is an empty list.
info_list: list
The list of information that parser returns. This will be used in the
logger.
reader: instance
An item instance provides the methods to interpret the data.
"""
Expand All @@ -52,7 +48,7 @@ def __init__(self, item_type, extensions, reader_cls=None, reader_args={}):
self.reader_cls = reader_cls
self.reader_args = reader_args
self.reader = None
self.parse_funcs = []
self.info_list = []

def __call__(self, itemname, **kwargs):
""" High-level parse_info method.
Expand All @@ -69,15 +65,7 @@ def __call__(self, itemname, **kwargs):
This if for the general purpose, however, it can be redefined in the
subclass.
"""
if not self.check_type(itemname):
return None
else:
if parse_funcs == []:
raise ValueError("Parser needs parse functions.")
result = {}
for k, f in self.parse_funs:
result[k] = f(itemname, **kwargs)
return result
raise NotImplementedError

def check_type(self, itemname):
""" User defined item type checker.
Expand Down

0 comments on commit 20f6adc

Please sign in to comment.