Skip to content

Commit

Permalink
Merge pull request #166 from Bernardo-MG/merge_main
Browse files Browse the repository at this point in the history
Merge main
  • Loading branch information
Bernardo-MG committed Jul 31, 2015
2 parents ef67ea0 + 60d7d6f commit 443cdbf
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 45 deletions.
2 changes: 1 addition & 1 deletion cwr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
:license: MIT, see LICENSE for more details.
"""

__version__ = '0.0.30'
__version__ = '0.0.33'
__license__ = 'MIT'
7 changes: 4 additions & 3 deletions cwr/grammar/factory/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand All @@ -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)

Expand Down
7 changes: 5 additions & 2 deletions cwr/grammar/field/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 1 addition & 4 deletions cwr/parser/decoder/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions cwr/parser/encoder/cwrjson.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

Expand Down
7 changes: 1 addition & 6 deletions cwr/parser/encoder/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 4 additions & 4 deletions tests/grammar/factory/test_default_rule_factory_stress.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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():
Expand Down
7 changes: 2 additions & 5 deletions tests/parser/dictionary/decoder/other/test_visan.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
7 changes: 2 additions & 5 deletions tests/parser/dictionary/decoder/record/test_work_origin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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)
Expand Down
9 changes: 2 additions & 7 deletions tests/parser/dictionary/encoder/other/test_visan.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
8 changes: 2 additions & 6 deletions tests/parser/dictionary/encoder/record/test_work_origin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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'])
21 changes: 21 additions & 0 deletions tests/visual/file_json.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)

0 comments on commit 443cdbf

Please sign in to comment.