diff --git a/cwr/__init__.py b/cwr/__init__.py index 52bf2c1..ed88061 100644 --- a/cwr/__init__.py +++ b/cwr/__init__.py @@ -7,5 +7,5 @@ :license: MIT, see LICENSE for more details. """ -__version__ = '0.0.30' +__version__ = '0.0.33' __license__ = 'MIT' diff --git a/cwr/grammar/factory/rule.py b/cwr/grammar/factory/rule.py index 3eed134..ba6a7ea 100644 --- a/cwr/grammar/factory/rule.py +++ b/cwr/grammar/factory/rule.py @@ -191,9 +191,9 @@ def _build_terminal_rule(self, rule): rule_type = rule.rule_type # TODO: This is a patch for an error which should not be happening - if isinstance(modifiers, pp.ParseResults): + try: modifiers = modifiers.asList() - elif isinstance(modifiers, str): + except AttributeError: modifiers = [] if rule_type == 'field': @@ -212,7 +212,8 @@ def _build_terminal_rule(self, rule): return rule - def _apply_modifiers(self, rule, modifiers): + @staticmethod + def _apply_modifiers(rule, modifiers): if 'grouped' in modifiers: rule = pp.Group(rule) diff --git a/cwr/grammar/field/basic.py b/cwr/grammar/field/basic.py index 60cf09f..ee10e43 100644 --- a/cwr/grammar/field/basic.py +++ b/cwr/grammar/field/basic.py @@ -465,8 +465,11 @@ def lookup(values, name=None): raise ValueError('The values can no be None') # TODO: This should not be needed, it is just a patch. Fix this. - if isinstance(values, ParseResults): - values = values.asList() + try: + v = values.asList() + values = v + except AttributeError: + values = values # Only the specified values are allowed lookup_field = pp.oneOf(values) diff --git a/cwr/parser/decoder/dictionary.py b/cwr/parser/decoder/dictionary.py index 23c3ce6..e86ae12 100644 --- a/cwr/parser/decoder/dictionary.py +++ b/cwr/parser/decoder/dictionary.py @@ -942,7 +942,4 @@ def __init__(self): super(VISANDictionaryDecoder, self).__init__() def decode(self, data): - return VISAN(data['version'], - data['isan'], - data['episode'], - data['check_digit']) + return data diff --git a/cwr/parser/encoder/cwrjson.py b/cwr/parser/encoder/cwrjson.py index 7959e1a..5926810 100644 --- a/cwr/parser/encoder/cwrjson.py +++ b/cwr/parser/encoder/cwrjson.py @@ -61,9 +61,9 @@ def _unicode_handler(obj): :param obj: object to transform into it's UTF-8 equivalent :return: the UTF-8 equivalent of the string """ - if isinstance(obj, str): + try: result = obj.isoformat() - else: + except AttributeError: raise TypeError("Unserializable object {} of type {}".format(obj, type(obj))) diff --git a/cwr/parser/encoder/dictionary.py b/cwr/parser/encoder/dictionary.py index 288839d..d015616 100644 --- a/cwr/parser/encoder/dictionary.py +++ b/cwr/parser/encoder/dictionary.py @@ -259,12 +259,7 @@ def __init__(self): def encode(self, visan): encoded = {} - encoded['version'] = visan.version - encoded['isan'] = visan.isan - encoded['episode'] = visan.episode - encoded['check_digit'] = visan.check_digit - - return encoded + return visan class TransactionHeaderDictionaryEncoder(Encoder): diff --git a/tests/grammar/factory/test_default_rule_factory_stress.py b/tests/grammar/factory/test_default_rule_factory_stress.py index ac82ab8..5949475 100644 --- a/tests/grammar/factory/test_default_rule_factory_stress.py +++ b/tests/grammar/factory/test_default_rule_factory_stress.py @@ -18,18 +18,18 @@ class TestDefaultRuleFactory(unittest.TestCase): def setUp(self): self._factory = default_grammar_factory() - def test_10000(self): + def test_10(self): grammar = self._factory.get_rule('transactions') record = '' if sys.version_info[0] == 2: - for x in xrange(10): + for x in xrange(35): if len(record) == 0: record = _agreement_full() elif len(record) > 0: record = record + '\n' + _agreement_full() else: - for x in range(10): + for x in range(35): if len(record) == 0: record = _agreement_full() elif len(record) > 0: @@ -41,7 +41,7 @@ def test_10000(self): time_parse = (end - start) - self.assertTrue(time_parse < 1) + self.assertTrue(time_parse < 1.5) def _agreement_full(): diff --git a/tests/parser/dictionary/decoder/other/test_visan.py b/tests/parser/dictionary/decoder/other/test_visan.py index ebc4330..3b1ecb0 100644 --- a/tests/parser/dictionary/decoder/other/test_visan.py +++ b/tests/parser/dictionary/decoder/other/test_visan.py @@ -27,9 +27,6 @@ def test_encoded(self): data['episode'] = 3 data['check_digit'] = 4 - record = self._decoder.decode(data) + record = self._decoder.decode(1234) - self.assertEqual(1, record.version) - self.assertEqual(2, record.isan) - self.assertEqual(3, record.episode) - self.assertEqual(4, record.check_digit) + self.assertEqual(1234, record) diff --git a/tests/parser/dictionary/decoder/record/test_work_origin.py b/tests/parser/dictionary/decoder/record/test_work_origin.py index 9dcfbac..2dce7ca 100644 --- a/tests/parser/dictionary/decoder/record/test_work_origin.py +++ b/tests/parser/dictionary/decoder/record/test_work_origin.py @@ -32,7 +32,7 @@ def test_encoded(self): data['cut_number'] = 5 data['library'] = 'LIB467' data['bltvr'] = 'BLTVR' - data['visan'] = VISAN(1234567, 12345678912, 123, 1) + data['visan'] = 1234567123456789121231 data['production_n'] = 'PROD145' data['episode_title'] = 'EPISODE' data['episode_n'] = 'EP145' @@ -50,10 +50,7 @@ def test_encoded(self): self.assertEqual(5, record.cut_number) self.assertEqual('LIB467', record.library) self.assertEqual('BLTVR', record.bltvr) - self.assertEqual(1, record.visan.check_digit) - self.assertEqual(123, record.visan.episode) - self.assertEqual(12345678912, record.visan.isan) - self.assertEqual(1234567, record.visan.version) + self.assertEqual(1234567123456789121231, record.visan) self.assertEqual('PROD145', record.production_n) self.assertEqual('EPISODE', record.episode_title) self.assertEqual('EP145', record.episode_n) diff --git a/tests/parser/dictionary/encoder/other/test_visan.py b/tests/parser/dictionary/encoder/other/test_visan.py index 778662d..0f98cce 100644 --- a/tests/parser/dictionary/encoder/other/test_visan.py +++ b/tests/parser/dictionary/encoder/other/test_visan.py @@ -21,11 +21,6 @@ def setUp(self): self._encoder = VISANDictionaryEncoder() def test_encoded(self): - data = VISAN(1, 2, 3, 4) + encoded = self._encoder.encode(1234) - encoded = self._encoder.encode(data) - - self.assertEqual(1, encoded['version']) - self.assertEqual(2, encoded['isan']) - self.assertEqual(3, encoded['episode']) - self.assertEqual(4, encoded['check_digit']) + self.assertEqual(1234, encoded) diff --git a/tests/parser/dictionary/encoder/record/test_work_origin.py b/tests/parser/dictionary/encoder/record/test_work_origin.py index acef568..1165990 100644 --- a/tests/parser/dictionary/encoder/record/test_work_origin.py +++ b/tests/parser/dictionary/encoder/record/test_work_origin.py @@ -22,7 +22,6 @@ def setUp(self): self._encoder = WorkOriginDictionaryEncoder() def test_encoded(self): - visan = VISAN(1234567, 12345678912, 123, 1) avi = AVIKey(123, 'ABC') data = WorkOriginRecord(record_type='ORN', @@ -34,7 +33,7 @@ def test_encoded(self): cut_number=5, library='LIB467', bltvr='BLTVR', - visan=visan, + visan=1234567123456789121231, production_n='PROD145', episode_title='EPISODE', episode_n='EP145', @@ -57,10 +56,7 @@ def test_encoded(self): self.assertEqual('EP145', encoded['episode_n']) self.assertEqual(1994, encoded['year_production']) - self.assertEqual(1, encoded['visan']['check_digit']) - self.assertEqual(123, encoded['visan']['episode']) - self.assertEqual(12345678912, encoded['visan']['isan']) - self.assertEqual(1234567, encoded['visan']['version']) + self.assertEqual(1234567123456789121231, encoded['visan']) self.assertEqual(123, encoded['audio_visual_key']['society_code']) self.assertEqual('ABC', encoded['audio_visual_key']['av_number']) diff --git a/tests/visual/file_json.py b/tests/visual/file_json.py index 1490115..584731c 100644 --- a/tests/visual/file_json.py +++ b/tests/visual/file_json.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- import codecs import os +import time from cwr.parser.decoder.file import default_file_decoder from cwr.parser.encoder.cwrjson import JSONEncoder @@ -34,11 +35,31 @@ data['filename'] = os.path.basename(path) data['contents'] = codecs.open(path, 'r', 'latin-1').read() + print('Begins parsing CWR at %s' % time.ctime()) + start = time.clock() data = decoder.decode(data) + end = time.clock() + time_parse = (end - start) + + print('Parsed the file in %s seconds' % time_parse) + print('\n') encoder = JSONEncoder() + + print('Begins creating JSON at %s' % time.ctime()) + start = time.clock() result = encoder.encode(data) + end = time.clock() + time_parse = (end - start) + print('Created the JSON in %s seconds' % time_parse) + print('\n') + + start = time.clock() output = codecs.open(output, 'w', 'latin-1') + end = time.clock() + time_parse = (end - start) + + print('Saved the JSON in %s seconds' % time_parse) output.write(result)