Skip to content

Commit

Permalink
Merge pull request #167 from weso/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Bernardo-MG committed Jul 31, 2015
2 parents 93f776d + 443cdbf commit f759e88
Show file tree
Hide file tree
Showing 54 changed files with 188 additions and 550 deletions.
2 changes: 0 additions & 2 deletions config_cwr/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@


1 change: 1 addition & 0 deletions config_cwr/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os

import yaml

from cwr.grammar.factory.config import rule_config_file

"""
Expand Down
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.29'
__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
34 changes: 7 additions & 27 deletions cwr/grammar/field/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def boolean(name=None):
name = 'Boolean Field'

# Basic field
field = pp.Combine(pp.Literal('Y') | pp.Literal('N'))
field = pp.Regex('[YN]')

# Parse action
field.setParseAction(lambda b: _to_boolean(b[0]))
Expand Down Expand Up @@ -346,10 +346,7 @@ def flag(name=None):
name = 'Flag Field'

# Basic field
field = pp.Combine(pp.Word('YNU', exact=1))

# Parse action
field.setParseAction(lambda f: _to_flag(f[0]))
field = pp.Regex('[YNU]')

# Name
field.setName(name)
Expand All @@ -359,26 +356,6 @@ def flag(name=None):
return field


def _to_flag(string):
"""
Transforms a string into a flag value.
If a value which is not 'Y', 'N' or 'U' is received, a ParseException is
thrown.
The received string is untouched, so if no exception is thrown the same
value that is received will be the one returned.
:param: string: the string to transform
:return: the received string
"""

if string not in ('Y', 'N', 'U'):
raise pp.ParseException(string, msg='Is not a valid flag value')

return string


"""
Date field (D).
Expand Down Expand Up @@ -488,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
1 change: 1 addition & 0 deletions cwr/grammar/field/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# Acquires data sources
_config = CWRConfiguration()


# RECORD FIELDS

# Prefix fields
Expand Down
157 changes: 38 additions & 119 deletions cwr/grammar/field/special.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pyparsing as pp

from cwr.other import ISWCCode, IPIBaseNumber, VISAN, AVIKey
from cwr.other import VISAN, AVIKey
from cwr.grammar.field import basic
from config_cwr.accessor import CWRConfiguration
from data_cwr.accessor import CWRTables
Expand All @@ -31,6 +31,7 @@
lineEnd = pp.lineEnd.suppress()
lineEnd.setName("End of line")


# CONCRETE CASES FIELDS


Expand All @@ -53,31 +54,7 @@ def ipi_base_number(name=None):
if name is None:
name = 'IPI Base Number Field'

# Separators are '-'
separator = pp.Literal('-').suppress()
separator = separator.setName('IPI Base Number Separator') \
.setResultsName('separator')

# Header is a digit in uppercase
header = pp.Literal('I')
header = header.setName('IPI Base Number Header').setResultsName('header')

# ID code is composed of 9 numbers
id_code = basic.numeric(9)
id_code = id_code.setName('ID Code').setResultsName('id_code')
id_code = id_code.setParseAction(lambda c: int(c[0]))

# Check digit is a single number
check_digit = pp.Regex('[0-9]')
check_digit = check_digit.setName('Check Digit') \
.setResultsName('check_digit')
check_digit = check_digit.setParseAction(lambda c: int(c[0]))

# Digit followed separator, 9 numbers, separator and 1 number
field = pp.Group(header + separator + id_code + separator + check_digit)

# Parse action
field.setParseAction(lambda c: _to_ipibasecode(c[0]))
field = pp.Regex('I-[0-9]{9}-[0-9]')

# Name
field.setName(name)
Expand All @@ -93,21 +70,6 @@ def ipi_base_number(name=None):
return field.setResultsName('ipi_base_n')


def _to_ipibasecode(code):
"""
Transforms the result of parsing an IPI Base Number code string into a
IPIBaseNumber instance.
:param code: the parsed code
:return: a IPIBaseNumber instance
"""

if code:
return IPIBaseNumber(code.header, code.id_code, code.check_digit)
else:
return code


def ipi_name_number(name=None):
"""
IPI Name Number field.
Expand Down Expand Up @@ -149,24 +111,8 @@ def iswc(name=None):
if name is None:
name = 'ISWC Field'

# Header is always T
header = pp.Literal('T').suppress()
header = header.setName('ISWC Header').setResultsName('header')

# ID code is composed of 9 numbers
id_code = basic.numeric(9)
id_code = id_code.setName('ID Code').setResultsName('id_code')

# Check digit is a single number
check_digit = basic.numeric(1)
check_digit = check_digit.setName('Check Digit') \
.setResultsName('check_digit')

# T followed by 10 numbers
field = pp.Group(header + id_code + check_digit)

# Parse action
field.setParseAction(lambda c: _to_iswccode(c[0]))
field = pp.Regex('T[0-9]{10}')

# Name
field.setName(name)
Expand All @@ -177,20 +123,6 @@ def iswc(name=None):
return field.setResultsName('iswc')


def _to_iswccode(code):
"""
Transforms the result of parsing a ISWC code string into a ISWCCode
instance.
:param code: the parsed code
:return: a ISWCCode instance
"""
if code:
return ISWCCode(code.id_code, code.check_digit)
else:
return code


def percentage(columns, maximum=100, name=None):
"""
Creates the grammar for a Numeric (N) field storing a percentage and
Expand Down Expand Up @@ -307,19 +239,25 @@ def _isrc_short(name=None):
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}')
# separator = pp.Literal('-')
country = 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_regex = ''
for c in country:
if len(country_regex) > 0:
country_regex += '|'
country_regex += c
country_regex = '(' + country_regex + ')'

country.setName('ISO-2 Country Code')
registrant.setName('Registrant')
year.setName('Year')
work_id.setName('Work ID')
field = pp.Regex(country_regex + '-.{3}-[0-9]{2}-[0-9]{2}')

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

field.setName(name)

Expand Down Expand Up @@ -352,17 +290,24 @@ def _isrc_long(name=None):
if name is None:
name = 'ISRC Field'

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}')
country = config.get_data('isrc_country_code')
# registrant = basic.alphanum(3)
# year = pp.Regex('[0-9]{2}')
# work_id = pp.Regex('[0-9]{5}')

country_regex = ''
for c in country:
if len(country_regex) > 0:
country_regex += '|'
country_regex += c
country_regex = '(' + country_regex + ')'

field = pp.Combine(country + registrant + year + work_id)
field = pp.Regex(country_regex + '.{3}[0-9]{2}[0-9]{5}')

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

field.setName(name)

Expand All @@ -382,39 +327,13 @@ def visan(name=None):
if name is None:
name = 'V-ISAN Field'

version = basic.numeric(8)
version = version.setName('Version').setResultsName('version')

isan = basic.numeric(12)
isan = isan.setName('ISAN').setResultsName('isan')

episode = basic.numeric(4)
episode = episode.setName('Episode').setResultsName('episode')

check_digit = basic.numeric(1)
check_digit = check_digit.setName('Check Digit') \
.setResultsName('check_digit')

field = pp.Group(version + isan + episode + check_digit)

field.setParseAction(lambda v: _to_visan(v[0]))
field = pp.Regex('[0-9]{25}')

field.setName(name)

return field.setResultsName('visan')


def _to_visan(parsed):
"""
Transforms the data from a V-ISAN field into a VISAN instance.
:param parsed: the data parsed from a V-ISAN field
:return: a VISAN instance created from the data
"""
return VISAN(parsed.version, parsed.isan, parsed.episode,
parsed.check_digit)


def audio_visual_key(name=None):
"""
Creates the grammar for an Audio Visual Key code.
Expand Down
24 changes: 4 additions & 20 deletions cwr/parser/decoder/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
InstrumentationSummaryRecord, PerformingArtistRecord, WorkOriginRecord, \
WorkRecord
from cwr.file import CWRFile, FileTag
from cwr.other import ISWCCode, IPIBaseNumber, AVIKey, VISAN
from cwr.other import AVIKey, VISAN
from cwr.table_value import MediaTypeValue, TableValue, InstrumentValue

"""
Expand Down Expand Up @@ -917,16 +917,7 @@ def __init__(self):

def decode(self, data):
if data:
if isinstance(data, IPIBaseNumber):
result = data
elif isinstance(data, int) or data.__class__.__name__ == 'long':
result = IPIBaseNumber(None,
data,
None)
else:
result = IPIBaseNumber(data['header'],
data['id_code'],
data['check_digit'])
result = data
else:
result = None

Expand All @@ -939,11 +930,7 @@ def __init__(self):

def decode(self, data):
if data:
if isinstance(data, ISWCCode):
result = data
else:
result = ISWCCode(data['id_code'],
data['check_digit'])
result = data
else:
result = None

Expand All @@ -955,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
1 change: 0 additions & 1 deletion cwr/parser/decoder/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ def default_file_decoder():
:return: a CWR file decoder for the default standard
"""
transmission_rule = default_grammar_factory().get_rule('transmission')
transmission_rule.enablePackrat()

return FileDecoder(
transmission_rule,
Expand Down
Loading

0 comments on commit f759e88

Please sign in to comment.