Skip to content

Instantly share code, notes, and snippets.

@nskeip
Created September 21, 2012 10:51
Show Gist options
  • Save nskeip/3760880 to your computer and use it in GitHub Desktop.
Save nskeip/3760880 to your computer and use it in GitHub Desktop.
Another way of parsing: what if we have some 'types' of entrieS? We look for type_field_EVENT (or for field_EVENT).
class ParseTarget(object):
TYPES = frozenset(('tire', 'disc', 'oil', 'acc', 'additional',))
def __init__(self):
self.current_tag = None
self.current_type = None
self._depth = 0
def execute_method(self, event, *args, **kwargs):
if self.current_tag is None or self.current_type is None:
return
big_method_name = '_'.join((self.current_type, self.current_tag, event,)) # tire_vendor_start
short_method_name = '_'.join((self.current_tag, event,)) # vendor_start
method = getattr(self, big_method_name, None) or getattr(self, short_method_name, None)
if method is not None:
return method(*args, **kwargs)
def start(self, tag, attrib):
if self._depth == 1 and tag in self.TYPES:
self.current_type = tag
self._depth += 1
self.current_tag = tag
self.execute_method('start', attrib)
def end(self, tag):
self.execute_method('stop')
self.current_tag = None
self._depth -= 1
def close(self):
self.current_tag = None
def data(self, data):
if self.current_tag is None:
return
self.execute_method('data', data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment