-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix config tests, add tests for (and relocate) fact proxy and have it…
… support case-insensitivity
- Loading branch information
1 parent
ebb09a5
commit 25f3dd8
Showing
6 changed files
with
91 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from pinyin.logger import log | ||
|
||
""" | ||
The fact proxy is responsible for making an Anki fact look like a dictionary with known keys. | ||
It is responsible for choosing which of the available fields on a fact we map to each purpose. | ||
""" | ||
class FactProxy(object): | ||
def __init__(self, candidateFieldNamesByKey, fact): | ||
self.fact = fact | ||
|
||
# NB: the fieldnames dictionary IS part of the interface of this class | ||
self.fieldnames = {} | ||
for key, candidateFieldNames in candidateFieldNamesByKey.items(): | ||
# Don't add a key into the dictionary if we can't find a field, or we end | ||
# up reporting that we the contain the field but die during access | ||
fieldname = chooseField(candidateFieldNames, fact) | ||
if fieldname is not None: | ||
self.fieldnames[key] = fieldname | ||
|
||
def __contains__(self, key): | ||
return key in self.fieldnames | ||
|
||
def __getitem__(self, key): | ||
return self.fact[self.fieldnames[key]] | ||
|
||
def __setitem__(self, key, value): | ||
self.fact[self.fieldnames[key]] = value | ||
|
||
def chooseField(candidateFieldNames, fact): | ||
# Find the first field that is present in the fact | ||
for candidateField in candidateFieldNames: | ||
for factfieldname in [factfieldname for factfieldname in fact.keys() if factfieldname.lower() == candidateField.lower()]: | ||
log.info("Choose %s as a field name from the fact for %s", factfieldname, candidateField) | ||
return factfieldname | ||
|
||
# No suitable field found! | ||
log.warn("No field matching %s in the fact", candidateFieldNames) | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import unittest | ||
|
||
from pinyin.factproxy import * | ||
|
||
|
||
class FactProxyTest(unittest.TestCase): | ||
def testDontContainMissingFields(self): | ||
self.assertFalse("key" in FactProxy({"key" : ["Foo", "Bar"]}, { "Baz" : "Hi" })) | ||
|
||
def testContainsPresentFields(self): | ||
self.assertTrue("key" in FactProxy({"key" : ["Foo", "Bar"]}, { "Bar" : "Hi" })) | ||
|
||
def testSet(self): | ||
fact = { "Baz" : "Hi" } | ||
FactProxy({"key" : ["Foo", "Baz"]}, fact)["key"] = "Bye" | ||
self.assertEquals(fact["Baz"], "Bye") | ||
|
||
def testGet(self): | ||
fact = { "Baz" : "Hi" } | ||
self.assertEquals(FactProxy({"key" : ["Foo", "Baz"]}, fact)["key"], "Hi") | ||
|
||
def testPriority(self): | ||
fact = { "Baz" : "Hi", "Foo" : "Meh" } | ||
FactProxy({"key" : ["Foo", "Baz"]}, fact)["key"] = "Bye" | ||
self.assertEquals(fact, { "Baz" : "Hi", "Foo" : "Bye" }) | ||
|
||
fact = { "Baz" : "Hi", "Foo" : "Meh" } | ||
FactProxy({"key" : ["Baz", "Foo"]}, fact)["key"] = "Bye" | ||
self.assertEquals(fact, { "Baz" : "Bye", "Foo" : "Meh" }) | ||
|
||
def testCase(self): | ||
fact = { "Foo" : "Meh" } | ||
FactProxy({"key" : ["foo"]}, fact)["key"] = "Bye" | ||
self.assertEquals(fact, { "Foo" : "Bye" }) |