feeds: make function output more consistent

e.g. setters return nothing, getters return sth relevant or None (i.e. no empty strings)
master
pictuga 2020-03-20 12:23:15 +01:00
parent 5705a0be17
commit 912c323c40
1 changed files with 13 additions and 8 deletions

View File

@ -206,11 +206,11 @@ class ParserBase(object):
pass pass
def rule_remove(self, rule): def rule_remove(self, rule):
# remove node from its parent # remove node from its parent. Returns nothing
pass pass
def rule_set(self, rule, value): def rule_set(self, rule, value):
# value is always a str? # set the value. Returns nothing
pass pass
def rule_str(self, rule): def rule_str(self, rule):
@ -247,23 +247,28 @@ class ParserBase(object):
def get(self, rule_name): def get(self, rule_name):
# simple function to get nice text from the rule name # simple function to get nice text from the rule name
# for use in @property, ie. self.get_str('title') # for use in @property, ie. self.get('title')
if rule_name not in self.rules: if rule_name not in self.rules:
return None return None
return self.rule_str(self.rules[rule_name]) return self.rule_str(self.rules[rule_name]) or None
def set(self, rule_name, value): def set(self, rule_name, value):
# simple function to set nice text from the rule name. Returns nothing
if rule_name not in self.rules: if rule_name not in self.rules:
return None return
if value is None:
self.rmv(rule_name)
return
try: try:
return self.rule_set(self.rules[rule_name], value) self.rule_set(self.rules[rule_name], value)
except AttributeError: except AttributeError:
# does not exist, have to create it # does not exist, have to create it
self.rule_create(self.rules[rule_name]) self.rule_create(self.rules[rule_name])
return self.rule_set(self.rules[rule_name], value) self.rule_set(self.rules[rule_name], value)
def rmv(self, rule_name): def rmv(self, rule_name):
# easy deleter # easy deleter
@ -430,7 +435,7 @@ class ParserXML(ParserBase):
return etree.tostring(match, method='text', encoding='unicode').strip() return etree.tostring(match, method='text', encoding='unicode').strip()
else: else:
return match or "" return match # might be None is no match
class ParserHTML(ParserXML): class ParserHTML(ParserXML):