Skip to content

Commit

Permalink
merged HostPythonGrammarTools into PythonGrammarTools
Browse files Browse the repository at this point in the history
  • Loading branch information
nesaro committed Jul 8, 2012
1 parent 7f0b97b commit 387b0a1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 25 deletions.
29 changes: 9 additions & 20 deletions pydsl/Grammar/Tool/Python.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,25 @@
LOG = logging.getLogger(__name__)

class PythonGrammarTools(GrammarTools, Indexable):
def __init__(self, matchFun, propFun = None, enumFun = None, alphabetFun = None):
def __init__(self, matchFun, auxdic = {}, propFun = None, enumFun = None, alphabetFun = None):
GrammarTools.__init__(self)
self._matchFun = matchFun
self._askprop = propFun
self._enumFun = enumFun
self._alphabetFun = alphabetFun
self.auxgrammar = {}
from pydsl.Memory.Storage.Loader import load_checker
for key, value in auxdic.items():
self.auxgrammar[key] = load_checker(value)

def check(self, word):
if not self._matchFun:
return False #Match function isn't defined
try:
return self._matchFun(word)
if self.auxgrammar:
return self._matchFun(word, self.auxgrammar)
else:
return self._matchFun(word)
except UnicodeDecodeError:
return False

Expand Down Expand Up @@ -67,21 +74,3 @@ def alphabet(self) -> set:
def summary(self):
return {"iclass":"PythonGrammar", "ancestors":self.ancestors() }

class HostPythonGrammarTools(PythonGrammarTools):
def __init__(self, matchFun, auxdic, propFun = None):
PythonGrammarTools.__init__(self, matchFun, propFun)
self.auxgrammar = {}
from pydsl.Memory.Storage.Loader import load_checker
for key, value in auxdic.items():
self.auxgrammar[key] = load_checker(value)

def check(self, word):
if not self._matchFun:
return False #Match function isn't defined
try:
return self._matchFun(word, self.auxgrammar)
except UnicodeDecodeError:
LOG.exception("Unicode Error while calling matchfun")
return False


3 changes: 0 additions & 3 deletions pydsl/Memory/Storage/Directory/DirStorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ def load_python_file(moduleobject, **kwargs):
elif iclass == "PythonGrammar":
from pydsl.Grammar.Tool.Python import PythonGrammarTools
return PythonGrammarTools(**resultdic)
elif iclass == "HostPythonGrammar":
from pydsl.Grammar.Tool.Python import HostPythonGrammarTools
return HostPythonGrammarTools(**resultdic)
elif iclass == "SymbolGrammar":
from pydsl.Grammar.Tool.Symbol import SymbolGrammarTools
return SymbolGrammarTools(**resultdic)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_Grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ def testIterate(self):
class TestHostPythonGrammars(unittest.TestCase):
"""HostPythonGrammar test"""
def setUp(self):
from pydsl.Grammar.Tool.Python import HostPythonGrammarTools
self.g1 = HostPythonGrammarTools(lambda x,aux: aux["ext"].check(x), {"ext":"email"})
from pydsl.Grammar.Tool.Python import PythonGrammarTools
self.g1 = PythonGrammarTools(lambda x,aux: aux["ext"].check(x), {"ext":"email"})

def testCheck(self):
self.assertTrue(self.g1.check("[email protected]"))
Expand Down

0 comments on commit 387b0a1

Please sign in to comment.