Skip to content

Commit

Permalink
Merge pull request #44 from Bernardo-MG/dict_parsers
Browse files Browse the repository at this point in the history
Dict parsers
  • Loading branch information
Bernardo-MG committed May 12, 2015
2 parents 8fdbdce + 7f7b242 commit f8467bb
Show file tree
Hide file tree
Showing 11 changed files with 443 additions and 9 deletions.
9 changes: 5 additions & 4 deletions cwr/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,11 @@ def __str__(self):
self._version)

def __repr__(self):
return '<class %s>(year=%s, sequence_n=%r, sender=%r, receiver=%r, version=%r)' % (self.__class__.__name__, self._year,
self._sequence_n,
self._sender, self._receiver,
self._version)
return '<class %s>(year=%s, sequence_n=%r, sender=%r, receiver=%r, version=%r)' % (
self.__class__.__name__, self._year,
self._sequence_n,
self._sender, self._receiver,
self._version)

@property
def year(self):
Expand Down
7 changes: 6 additions & 1 deletion cwr/parser/decoder/cwrjson.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-

from cwr.parser.decoder.common import Decoder
from cwr.parser.decoder.dictionary import FileDictionaryDecoder
import json

"""
Classes for decoding CWR classes from JSON dictionaries.
Expand All @@ -14,6 +16,9 @@
class JSONDecoder(Decoder):
def __init__(self):
super(JSONDecoder, self).__init__()
self._dict_decoder = FileDictionaryDecoder()

def decode(self, data):
pass
decoded = json.loads(data)

return self._dict_decoder.decode(decoded)
112 changes: 110 additions & 2 deletions cwr/parser/decoder/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,52 @@
__status__ = 'Development'


class TransactionRecordDictionaryDecoder(Decoder):
def __init__(self):
super(TransactionRecordDictionaryDecoder, self).__init__()
self._decoders = {}

self._decoders['ACK'] = AcknowledgementDictionaryDecoder()
self._decoders['AGR'] = AgreementDictionaryDecoder()
self._decoders['TER'] = AgreementTerritoryDictionaryDecoder()
self._decoders['ARI'] = AdditionalRelatedInformationDictionaryDecoder()
self._decoders['ALT'] = AlternateTitleDictionaryDecoder()
self._decoders['EWT'] = AuthoredWorkDictionaryDecoder()
self._decoders['VER'] = AuthoredWorkDictionaryDecoder()
self._decoders['COM'] = ComponentDictionaryDecoder()
self._decoders['IPA'] = InterestedPartyForAgreementDictionaryDecoder()
self._decoders['SPT'] = IPTerritoryOfControlDictionaryDecoder()
self._decoders['SWT'] = IPTerritoryOfControlDictionaryDecoder()
self._decoders['IND'] = InstrumentationDetailDictionaryDecoder()
self._decoders['INS'] = InstrumentationSummaryDictionaryDecoder()
self._decoders['MSG'] = MessageDictionaryDecoder()
self._decoders['PER'] = PerformingArtistDictionaryDecoder()
self._decoders['PWR'] = PublisherForWriterDictionaryDecoder()
self._decoders['REC'] = RecordingDetailDictionaryDecoder()
self._decoders['EXC'] = WorkDictionaryDecoder()
self._decoders['ISW'] = WorkDictionaryDecoder()
self._decoders['NWR'] = WorkDictionaryDecoder()
self._decoders['REV'] = WorkDictionaryDecoder()
self._decoders['ORN'] = WorkOriginDictionaryDecoder()
self._decoders['SWR'] = WriterRecordDictionaryDecoder()
self._decoders['OWR'] = WriterRecordDictionaryDecoder()
self._decoders['OWR'] = WriterRecordDictionaryDecoder()
self._decoders['NPA'] = NonRomanAlphabetAgreementPartyDictionaryDecoder()
self._decoders['NOW'] = NonRomanAlphabetOtherWriterDictionaryDecoder()
self._decoders['NPR'] = NonRomanAlphabetPerformanceDataDictionaryDecoder()
self._decoders['NPN'] = NonRomanAlphabetPublisherNameDictionaryDecoder()
self._decoders['NAT'] = NonRomanAlphabetTitleDictionaryDecoder()
self._decoders['NET'] = NonRomanAlphabetWorkDictionaryDecoder()
self._decoders['NCT'] = NonRomanAlphabetWorkDictionaryDecoder()
self._decoders['NVT'] = NonRomanAlphabetWorkDictionaryDecoder()
self._decoders['NWN'] = NonRomanAlphabetWriterNameDictionaryDecoder()
self._decoders['SPU'] = PublisherRecordDictionaryDecoder()
self._decoders['OPU'] = PublisherRecordDictionaryDecoder()

def decode(self, data):
return self._decoders[data['record_type']].decode(data)


class AcknowledgementDictionaryDecoder(Decoder):
def __init__(self):
super(AcknowledgementDictionaryDecoder, self).__init__()
Expand Down Expand Up @@ -308,20 +354,82 @@ def decode(self, data):
media_type=data['media_type'])


class FileDictionaryDecoder(Decoder):
def __init__(self):
super(FileDictionaryDecoder, self).__init__()

self._tag_decoder = FileTagDictionaryDecoder()
self._transmission_decoder = TransmissionDictionaryDecoder()

def decode(self, data):
tag = data['tag']
if isinstance(tag, dict):
tag = self._tag_decoder.decode(tag)

transmission = data['transmission']
if isinstance(transmission, dict):
transmission = self._transmission_decoder.decode(transmission)

return CWRFile(tag, transmission)


class TransmissionDictionaryDecoder(Decoder):
def __init__(self):
super(TransmissionDictionaryDecoder, self).__init__()

self._header_decoder = TransmissionHeaderDictionaryDecoder()
self._trailer_decoder = TransmissionTrailerDictionaryDecoder()
self._group_decoder = GroupDictionaryDecoder()

def decode(self, data):
return Transmission(data['transmission_header'], data['transmission_trailer'], data['groups'])
header = data['header']
if isinstance(header, dict):
header = self._header_decoder.decode(header)

trailer = data['trailer']
if isinstance(trailer, dict):
trailer = self._trailer_decoder.decode(trailer)

groups = []
if len(data['groups']) > 0:
if isinstance(data['groups'][0], dict):
for group in data['groups']:
groups.append(self._group_decoder.decode(group))
else:
groups = data['groups']

return Transmission(header, trailer, groups)


class GroupDictionaryDecoder(Decoder):
def __init__(self):
super(GroupDictionaryDecoder, self).__init__()

self._header_decoder = GroupHeaderDictionaryDecoder()
self._trailer_decoder = GroupTrailerDictionaryDecoder()
self._transaction_decoder = TransactionRecordDictionaryDecoder()

def decode(self, data):
return Group(data['group_header'], data['group_trailer'], data['transactions'])
header = data['group_header']
if isinstance(header, dict):
header = self._header_decoder.decode(header)

trailer = data['group_trailer']
if isinstance(trailer, dict):
trailer = self._trailer_decoder.decode(trailer)

transactions = []
if len(data['transactions']) > 0:
if isinstance(data['transactions'][0][0], dict):
for transaction in data['transactions']:
transaction_records = []
for record in transaction:
transaction_records.append(self._transaction_decoder.decode(record))
transactions.append(transaction_records)
else:
transactions = data['transactions']

return Group(header, trailer, transactions)


class TransmissionHeaderDictionaryDecoder(Decoder):
Expand Down
2 changes: 2 additions & 0 deletions data_commonworks/record_config_common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@ transmission_header:
id: transmission_date
- rule_type: field
id: character_set
results_name: header

transmission_trailer:
id: transmission_trailer
Expand All @@ -644,6 +645,7 @@ transmission_trailer:
id: transaction_count
- rule_type: field
id: record_count
results_name: trailer

work:
id: work
Expand Down
67 changes: 67 additions & 0 deletions tests/parser/dictionary/decoder/control/test_group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# -*- coding: utf-8 -*-

import unittest
import datetime

from cwr.parser.decoder.dictionary import GroupDictionaryDecoder


"""
Group from dictionary encoding tests.
The following cases are tested:
"""

__author__ = 'Bernardo Martínez Garrido'
__license__ = 'MIT'
__version__ = '0.0.0'
__status__ = 'Development'


class TestGroupDictionaryEncoding(unittest.TestCase):
def setUp(self):
self._decoder = GroupDictionaryDecoder()

def test_encoded(self):
trailer = {}
trailer['record_count'] = 20
trailer['record_type'] = 'GRT'
trailer['group_id'] = 3
trailer['transaction_count'] = 15

header = {}
header['record_type'] = 'GRH'
header['version_number'] = '02.10'
header['group_id'] = 3
header['batch_request_id'] = 15
header['transaction_type'] = 'AGR'

transactions = [[{'original_group_id': 4, 'creation_date_time': datetime.date(2003, 2, 15),
'original_transaction_type': 'AGR', 'record_sequence_n': 15, 'record_type': 'ACK',
'creation_title': 'TITLE', 'original_transaction_sequence_n': 5, 'transaction_status': 'AS',
'recipient_creation_n': 'B124', 'submitter_creation_n': 'A123', 'transaction_sequence_n': 3,
'processing_date': datetime.date(2003, 2, 16)},
{'original_record_sequence_n': 124, 'validation_n': 'AB3', 'message_record_type': 'AGR',
'message_text': 'THE MESSAGE', 'record_sequence_n': 15, 'record_type': 'MSG',
'message_level': 'F', 'message_type': 'G', 'transaction_sequence_n': 3},
{'original_record_sequence_n': 124, 'validation_n': 'AB3', 'message_record_type': 'AGR',
'message_text': 'THE MESSAGE', 'record_sequence_n': 15, 'record_type': 'MSG',
'message_level': 'F', 'message_type': 'G', 'transaction_sequence_n': 3},
{'sales_manufacture_clause': 'M', 'date_of_signature': datetime.date(2003, 2, 17),
'prior_royalty_start_date': datetime.date(2003, 2, 19), 'advance_given': True,
'retention_end_date': datetime.date(2003, 2, 18), 'international_standard_code': 'DFG135',
'prior_royalty_status': 'D', 'agreement_end_date': datetime.date(2003, 2, 16),
'record_type': 'AGR', 'shares_change': True, 'post_term_collection_status': 'D',
'agreement_type': 'OS', 'submitter_agreement_n': 'AB12',
'society_assigned_agreement_n': 'DF35', 'record_sequence_n': 15,
'agreement_start_date': datetime.date(2003, 2, 15), 'transaction_sequence_n': 3,
'post_term_collection_end_date': datetime.date(2003, 2, 20), 'number_of_works': 12}]]

dict = {'group_trailer': trailer, 'transactions': transactions, 'group_header': header}

record = self._decoder.decode(dict)

self.assertEqual('GRH', record.group_header.record_type)
self.assertEqual('GRT', record.group_trailer.record_type)

self.assertEqual('ACK', record.transactions[0][0].record_type)
92 changes: 92 additions & 0 deletions tests/parser/dictionary/decoder/control/test_transmission.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# -*- coding: utf-8 -*-

import unittest
import datetime

from cwr.parser.decoder.dictionary import TransmissionDictionaryDecoder


"""
Dictionary to Message decoding tests.
The following cases are tested:
"""

__author__ = 'Bernardo Martínez Garrido'
__license__ = 'MIT'
__version__ = '0.0.0'
__status__ = 'Development'


class TestTransmissionDecoder(unittest.TestCase):
def setUp(self):
self._decoder = TransmissionDictionaryDecoder()

def test_encoded(self):
header = {}
header['record_type'] = 'HDR'
header['sender_id'] = 'SND123'
header['sender_name'] = 'THE SENDER'
header['sender_type'] = 'SO'
header['creation_date_time'] = datetime.datetime.strptime('20030216', '%Y%m%d').date()
header['transmission_date'] = datetime.datetime.strptime('20030217', '%Y%m%d').date()
header['edi_standard'] = '01.10'
header['character_set'] = 'ASCII'

trailer = {}
trailer['record_type'] = 'TRL'
trailer['group_count'] = 11
trailer['transaction_count'] = 22
trailer['record_count'] = 33

trailer_group = {}
trailer_group['record_count'] = 20
trailer_group['record_type'] = 'GRT'
trailer_group['group_id'] = 3
trailer_group['transaction_count'] = 15

header_group = {}
header_group['record_type'] = 'GRH'
header_group['version_number'] = '02.10'
header_group['group_id'] = 3
header_group['batch_request_id'] = 15
header_group['transaction_type'] = 'AGR'

transactions = [[{'original_group_id': 4, 'creation_date_time': datetime.date(2003, 2, 15),
'original_transaction_type': 'AGR', 'record_sequence_n': 15, 'record_type': 'ACK',
'creation_title': 'TITLE', 'original_transaction_sequence_n': 5, 'transaction_status': 'AS',
'recipient_creation_n': 'B124', 'submitter_creation_n': 'A123', 'transaction_sequence_n': 3,
'processing_date': datetime.date(2003, 2, 16)},
{'original_record_sequence_n': 124, 'validation_n': 'AB3', 'message_record_type': 'AGR',
'message_text': 'THE MESSAGE', 'record_sequence_n': 15, 'record_type': 'MSG',
'message_level': 'F', 'message_type': 'G', 'transaction_sequence_n': 3},
{'original_record_sequence_n': 124, 'validation_n': 'AB3', 'message_record_type': 'AGR',
'message_text': 'THE MESSAGE', 'record_sequence_n': 15, 'record_type': 'MSG',
'message_level': 'F', 'message_type': 'G', 'transaction_sequence_n': 3},
{'sales_manufacture_clause': 'M', 'date_of_signature': datetime.date(2003, 2, 17),
'prior_royalty_start_date': datetime.date(2003, 2, 19), 'advance_given': True,
'retention_end_date': datetime.date(2003, 2, 18), 'international_standard_code': 'DFG135',
'prior_royalty_status': 'D', 'agreement_end_date': datetime.date(2003, 2, 16),
'record_type': 'AGR', 'shares_change': True, 'post_term_collection_status': 'D',
'agreement_type': 'OS', 'submitter_agreement_n': 'AB12',
'society_assigned_agreement_n': 'DF35', 'record_sequence_n': 15,
'agreement_start_date': datetime.date(2003, 2, 15), 'transaction_sequence_n': 3,
'post_term_collection_end_date': datetime.date(2003, 2, 20), 'number_of_works': 12}]]

groups = [{'group_header': header_group, 'transactions': transactions, 'group_trailer': trailer_group}]

transmission = {'header': header, 'groups': groups, 'trailer': trailer}

record = self._decoder.decode(transmission)

self.assertEqual('HDR', record.header.record_type)
self.assertEqual('TRL', record.trailer.record_type)

self.assertEqual(1, len(record.groups))

group = record.groups[0]

self.assertEqual('GRH', group.group_header.record_type)
self.assertEqual('GRT', group.group_trailer.record_type)

self.assertEqual('ACK', group.transactions[0][0].record_type)
1 change: 1 addition & 0 deletions tests/parser/dictionary/decoder/file/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__author__ = 'Bernardo'
Loading

0 comments on commit f8467bb

Please sign in to comment.