Permalink
Browse files

Deal with a problem where we could update fields with None, causing e…

…xceptions and weirdness
  • Loading branch information...
1 parent 6312720 commit 5b7f12d387f17007c286148512e793f7d3523919 @batterseapower batterseapower committed Feb 14, 2010
Showing with 22 additions and 3 deletions.
  1. +14 −1 pinyin/tests/updatergraph.py
  2. +5 −2 pinyin/updater.py
  3. +3 −0 pinyin/updatergraph.py
@@ -86,7 +86,20 @@ def testEverythingGerman(self):
"audio" : u"[sound:" + os.path.join("Test", "shu1.ogg") + "]",
"color" : u'<span style="color:#ff0000">书</span>',
"trad" : u"", "simp" : u""
- })
+ })
+
+ def testEverythingFrench(self):
+ config = dict(forceexpressiontobesimptrad = False, tonedisplay = "tonified",
+ dictlanguage = "fr", detectmeasurewords = True,
+ audioextensions = [".ogg"], tonecolors = [u"#ff0000", u"#ffaa00", u"#00aa00", u"#0000ff", u"#545454"])
+ self.assertProduces({ "expression" : u"", "mwfieldinfact" : True }, config, {
+ "reading" : u'<span style="color:#ff0000">shū</span>',
+ "meaning" : None,
+ "mw" : u'<span style="color:#00aa00">本</span> - <span style="color:#00aa00">běn</span>, <span style="color:#0000ff">册</span> - <span style="color:#0000ff">cè</span>, <span style="color:#0000ff">部</span> - <span style="color:#0000ff">bù</span>',
+ "audio" : u"[sound:" + os.path.join("Test", "shu1.ogg") + "]",
+ "color" : u'<span style="color:#ff0000">书</span>',
+ "trad" : u"", "simp" : u""
+ })
def testPreservesWhitespace(self):
config = dict(forceexpressiontobesimptrad = False, tonedisplay = "tonified", colorizedpinyingeneration = False)
View
@@ -23,7 +23,6 @@
# TODO:
# * Only update fields that could have been changed by the update (quite important for audio!)
# * (Perhaps) record hash in generated tag, so we can avoid recomputing more generally
-# * Make updating itself faster: e.g. prefer audio that already exists in the Anki deck
class FieldUpdater(object):
def __init__(self, field, *args):
@@ -62,7 +61,11 @@ def updatefact(self, fact, value, alwaysreformat=False):
# Fields can be missing from the graph if they are generated fields in the input fact that
# the graph failed to find a way to update. This really does happen!
if shouldupdatefield(self.graphbasedupdater.config)(field) and field in graph:
- fact[field] = (graph[field][0] and factproxy.markgeneratedfield or (lambda x: x))(graph[field][1]())
+ value = graph[field][1]()
+ # The value can be None if we tried to update the field but just couldn't find any information.
+ # This can happen e.g. with the "meaning" field if there is no definition in the dictionary.
+ if value is not None:
+ fact[field] = (graph[field][0] and factproxy.markgeneratedfield or (lambda x: x))(value)
class FieldUpdaterFromExpression(FieldUpdater):
def __init__(self, *args):
View
@@ -370,6 +370,9 @@ def fillme(field=field, possiblefillers=possiblefillers):
assert (field in fact and not anyinputsdirty)
dirty[field] = False
return unmarkgeneratedfield(fact[field])
+
+ # What if all of the possible updaters failed? Ideally we would not be in the graph at all, but it's too late for that.
+ # All we can do is return None, and deal with this possibility later on.
graph[field] = (True, Thunk(fillme))

0 comments on commit 5b7f12d

Please sign in to comment.