Skip to content

Commit

Permalink
Merge pull request #43 from Bernardo-MG/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Bernardo-MG committed May 9, 2015
2 parents a3e0d8b + d6e7757 commit 8db15bf
Show file tree
Hide file tree
Showing 14 changed files with 228 additions and 166 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.14'
__version__ = '0.0.15'
__license__ = 'MIT'
25 changes: 2 additions & 23 deletions cwr/grammar/factory/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,31 +163,10 @@ class DefaultFieldTerminalRuleFactory(OptionFieldTerminalRuleFactory):
Factory for acquiring fields rules using the default configuration.
"""

def __init__(self, field_configs, field_values=None, field_rules=None, actions=None):
def __init__(self, field_configs, adapters, field_values=None, field_rules=None, actions=None):
super(DefaultFieldTerminalRuleFactory, self).__init__(field_configs)

# TODO: Don't do this manually
self._adapters['alphanum'] = AlphanumAdapter()
self._adapters['alphanum_ext'] = ExtendedAlphanumAdapter()
self._adapters['numeric'] = NumericAdapter()
self._adapters['boolean'] = BooleanAdapter()
self._adapters['flag'] = FlagAdapter()
self._adapters['date'] = DateAdapter()
self._adapters['time'] = TimeAdapter()
self._adapters['date_time'] = DateTimeAdapter()
self._adapters['blank'] = BlankAdapter()
self._adapters['lookup'] = LookupAdapter()
self._adapters['iswc'] = ISWCAdapter()
self._adapters['ipi_name_n'] = IPINameNumberAdapter()
self._adapters['ipi_base_n'] = IPIBaseNumberAdapter()
self._adapters['percentage'] = PercentageAdapter()
self._adapters['ean13'] = EAN13Adapter()
self._adapters['isrc'] = ISRCAdapter()
self._adapters['visan'] = VISANAdapter()
self._adapters['avi'] = AudioVisualKeydapter()
self._adapters['charset'] = CharSetAdapter()
self._adapters['alphanum_variable'] = VariableAlphanumAdapter()
self._adapters['numeric_float'] = NumericFloatAdapter()
self._adapters = adapters

# Field values are optional
self._field_values = field_values
Expand Down
29 changes: 0 additions & 29 deletions cwr/grammar/factory/record.py

This file was deleted.

94 changes: 89 additions & 5 deletions cwr/grammar/field/special.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from cwr.other import ISWCCode, IPIBaseNumber, VISAN, AVIKey
from cwr.grammar.field import basic
from data_commonworks.accessor import CWRConfiguration
from data_commonworks.accessor import CWRConfiguration, CWRTables


"""
Expand Down Expand Up @@ -260,22 +260,106 @@ def isrc(name=None):
:return: grammar for an ISRC field
"""

config = CWRTables()

if name is None:
name = 'ISRC Field'

separator = pp.Literal('-')
country = basic.lookup(config.get_data('isrc_country_code'))
registrant = basic.alphanum(3)
year = pp.Regex('[0-9]{2}')
work_id = pp.Regex('[0-9]{2}')

field = _isrc_short(name) | _isrc_long(name)

field.setName(name)

return field.setResultsName('isrc')


def _isrc_short(name=None):
"""
Creates the grammar for a short ISRC code.
ISRC stands for International Standard Recording Code, which is the standard ISO 3901. This stores information
identifying a particular recording.
This variant contains separator for the parts, and follows the pattern:
CC-XXX-YY-NN
Where each code means:
- CC: country code
- XXX: registrant
- YY: year
- NN: work id
:param name: name for the field
:return: grammar for an ISRC field
"""

config = CWRTables()

if name is None:
name = 'ISRC Field'

separator = pp.Literal('-')
country = basic.alphanum(2)
country = basic.lookup(config.get_data('isrc_country_code'))
registrant = basic.alphanum(3)
year = pp.Regex('[0-9]{2}')
work_id = pp.Regex('[0-9]{2}')

field = pp.Combine(country + separator + registrant + separator + year + separator + work_id)

country.setName('ISO-2 Country Code')
registrant.setName('Registrant')
year.setName('Year')
work_id.setName('Work ID')

field.setName(name)

# TODO: Fix this field
field = basic.alphanum(12)
field = field.setName(name)
return field.setResultsName('isrc')


def _isrc_long(name=None):
"""
Creates the grammar for a short ISRC code.
ISRC stands for International Standard Recording Code, which is the standard ISO 3901. This stores information
identifying a particular recording.
This variant contain no separator for the parts, and follows the pattern:
CCXXXYYNNNNN
Where each code means:
- CC: country code
- XXX: registrant
- YY: year
- NNNNN: work id
:param name: name for the field
:return: grammar for an ISRC field
"""

config = CWRTables()

if name is None:
name = 'ISRC Field'

separator = pp.Literal('-')
country = basic.lookup(config.get_data('isrc_country_code'))
registrant = basic.alphanum(3)
year = pp.Regex('[0-9]{2}')
work_id = pp.Regex('[0-9]{5}')

field = pp.Combine(country + registrant + year + work_id)

country.setName('ISO-2 Country Code')
registrant.setName('Registrant')
year.setName('Year')
work_id.setName('Work ID')

field.setName(name)

return field.setResultsName('isrc')

Expand Down
2 changes: 1 addition & 1 deletion cwr/parser/decoder/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def decode(self, text):
For this a Pyparsing grammar rule will be applied to the string.
:param data: the data to decode
:param text: the data to decode
:return: a class representing the data
"""
return self._grammar.parseString(text)
40 changes: 38 additions & 2 deletions cwr/parser/decoder/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from cwr.grammar.factory.rule import DefaultRuleFactory
from cwr.grammar.factory.decorator import RecordRuleDecorator, GroupRuleDecorator
from cwr.parser.decoder.dictionary import *
from cwr.grammar.factory.adapter import *


"""
Expand Down Expand Up @@ -84,6 +85,34 @@ def _default_record_decoders():
return decoders


def _default_adapters():
adapters = {}

adapters['alphanum'] = AlphanumAdapter()
adapters['alphanum_ext'] = ExtendedAlphanumAdapter()
adapters['numeric'] = NumericAdapter()
adapters['boolean'] = BooleanAdapter()
adapters['flag'] = FlagAdapter()
adapters['date'] = DateAdapter()
adapters['time'] = TimeAdapter()
adapters['date_time'] = DateTimeAdapter()
adapters['blank'] = BlankAdapter()
adapters['lookup'] = LookupAdapter()
adapters['iswc'] = ISWCAdapter()
adapters['ipi_name_n'] = IPINameNumberAdapter()
adapters['ipi_base_n'] = IPIBaseNumberAdapter()
adapters['percentage'] = PercentageAdapter()
adapters['ean13'] = EAN13Adapter()
adapters['isrc'] = ISRCAdapter()
adapters['visan'] = VISANAdapter()
adapters['avi'] = AudioVisualKeydapter()
adapters['charset'] = CharSetAdapter()
adapters['alphanum_variable'] = VariableAlphanumAdapter()
adapters['numeric_float'] = NumericFloatAdapter()

return adapters


def default_file_decoder():
"""
Creates a decoder which parses a CWR file, creating a CWRFile class instance from it.
Expand All @@ -95,7 +124,7 @@ def default_file_decoder():
_data = _config.load_field_config('table')
_data.update(_config.load_field_config('common'))

_factory_field = DefaultFieldTerminalRuleFactory(_data, CWRTables())
_factory_field = DefaultFieldTerminalRuleFactory(_data, _default_adapters(), field_values=CWRTables())

_rules = _config.load_transaction_config('common')
_rules.update(_config.load_record_config('common'))
Expand All @@ -121,7 +150,7 @@ def default_filename_decoder():
_data.update(_config.load_field_config('common'))
_data.update(_config.load_field_config('filename'))

_factory_field = DefaultFieldTerminalRuleFactory(_data, CWRTables())
_factory_field = DefaultFieldTerminalRuleFactory(_data, _default_adapters(), field_values=CWRTables())

_group_rule_factory = DefaultRuleFactory(_config.load_record_config('filename'), _factory_field)

Expand Down Expand Up @@ -162,6 +191,13 @@ def decode(self, data):
"""
filename = self._filename_decoder.decode(data['filename'])

file_data = data['contents']
i = 0
while file_data[i:i + 1] != 'H':
i += 1
if i > 0:
data['contents'] = file_data[i:]

transmission = self._file_decoder.decode(data['contents'])[0]

return CWRFile(filename, transmission)
Expand Down
74 changes: 0 additions & 74 deletions cwr/utils/reader.py

This file was deleted.

1 change: 1 addition & 0 deletions data_commonworks/cwr_isrc_country_code.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
AC,AD,AE,AF,AG,AI,AL,AM,AN,AO,AR,AS,AT,AU,AW,AX,AZ,BA,BB,BD,BE,BF,BG,BH,BI,BJ,BL,BM,BN,BO,BR,BS,BT,BW,BY,BZ,CA,CD,CF,CG,CH,CI,CK,CL,CM,CN,CO,CR,CU,CV,CY,CZ,DE,DJ,DK,DM,DO,DZ,EC,EE,EG,EH,ER,ES,ET,FI,FJ,FK,FM,FO,FR,GA,GB,GD,GE,GF,GG,GH,GI,GL,GM,GN,GP,GQ,GR,GT,GU,GW,GY,HK,HN,HR,HT,HU,ID,IE,IL,IM,IN,IQ,IR,IS,IT,JE,JM,JO,JP,KE,KG,KH,KI,KM,KN,Kosovo,KP,KR,KW,KY,KZ,LA,LB,LC,LI,LK,LR,LS,LT,LU,LV,LY,MA,MC,MD,ME,MF,MG,MH,MK,ML,MM,MN,MO,MP,MQ,MR,MS,MT,MU,MV,MW,MX,MY,MZ,NA,NC,NE,NF,NG,NI,NL,NO,NP,NR,NU,NZ,OECS,OM,PA,PE,PF,PG,PH,PK,PL,PM,PN,PR,PS,PT,PW,PY,QA,RE,RO,RS,RU,RW,SA,SB,SC,SD,SE,SG,SH,SI,SJ,SK,SL,SM,SN,SO,SR,ST,SV,SY,SZ,TC,TD,TG,TH,TJ,TK,TL,TM,TN,TO,TR,TT,TV,TW,TZ,UA,UG,US,UY,UZ,VA,VC,VE,VG,VI,VN,VU,WF,WS,YE,YT,Yugoslavia,ZA,ZM,ZW,QR,CP,DG,ZZ
Loading

0 comments on commit 8db15bf

Please sign in to comment.