Skip to content

Commit

Permalink
Merge pull request #58 from Bernardo-MG/optionals_rework
Browse files Browse the repository at this point in the history
Removed the optional wrapper from adapters.
  • Loading branch information
Bernardo-MG committed Jul 9, 2015
2 parents c049f0f + eb599d7 commit e7315bb
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 74 deletions.
1 change: 0 additions & 1 deletion cwr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@

__version__ = '0.0.28'
__license__ = 'MIT'

74 changes: 4 additions & 70 deletions cwr/grammar/factory/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

from abc import ABCMeta, abstractmethod

import pyparsing as pp

from cwr.grammar.field import basic, special, table, filename

"""
Expand Down Expand Up @@ -53,40 +51,8 @@ def get_field(self, name=None, columns=None, values=None):
"""
raise NotImplementedError("The get_field method is not implemented")

def wrap_as_optional(self, field, name, columns):
"""
Adds a wrapper rule to the field to accept empty strings.
This empty string should be of the same size as the columns parameter.
One smaller or bigger will be rejected.
This wrapper will return None if the field is empty.
:param field: the field to wrap
:param name: name of the field
:param columns: number of columns it takes
:return: the field with an additional rule to allow empty strings
"""
# Regular expression accepting as many whitespaces as columns
field_empty = pp.Regex('[ ]{' + str(columns) + '}')

field_empty.setName(name)

# Whitespaces are not removed
field_empty.leaveWhitespace()

# None is returned by this rule
field_empty.setParseAction(pp.replaceWith(None))

field_empty = field_empty.setResultsName(field.resultsName)

field = field | field_empty

field.setName(name)

field.leaveWhitespace()

return field
def is_numeric(self):
return False


class AlphanumAdapter(FieldAdapter):
Expand Down Expand Up @@ -184,40 +150,8 @@ def __init__(self):
def get_field(self, name=None, columns=None, values=None):
return basic.date(name)

def wrap_as_optional(self, field, name, columns):
"""
Adds a wrapper rule to the field to accept empty strings.
This empty string should be of the same size as the columns parameter.
One smaller or bigger will be rejected.
This wrapper will return None if the field is empty.
:param field: the field to wrap
:param name: name of the field
:param columns: number of columns it takes
:return: the field with an additional rule to allow empty strings
"""
# Regular expression accepting as many whitespaces as columns
field_empty = pp.Regex('[0]{8}|[ ]{' + str(columns) + '}')

field_empty.setName(name)

# Whitespaces are not removed
field_empty.leaveWhitespace()

# None is returned by this rule
field_empty.setParseAction(pp.replaceWith(None))

field_empty = field_empty.setResultsName(field.resultsName)

field = field | field_empty

field.setName(name)

field.leaveWhitespace()

return field
def is_numeric(self):
return True


class TimeAdapter(FieldAdapter):
Expand Down
71 changes: 69 additions & 2 deletions cwr/grammar/factory/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,75 @@ def decorate(self, field_base, field_id):
config = self._field_configs[field_id]

# It is not compulsory, the wrapped is added
field = self._wrap_as_optional(field_base, config['name'],
config['size'])

adapter = self._adapters[config['type']]
field = adapter.wrap_as_optional(field_base, config['name'],
config['size'])
if (adapter.is_numeric()):
field = self._wrap_as_optional_numeric(field, config['name'],
config['size'])

return field

def _wrap_as_optional_numeric(self, field, name, columns):
# Regular expression accepting as many whitespaces as columns
field_empty = pp.Regex('[0]{' + str(columns) + '}')

resultsName = field.resultsName

field_empty.setName(name)

# Whitespaces are not removed
field_empty.leaveWhitespace()

# None is returned by this rule
field_empty.setParseAction(pp.replaceWith(None))

field_empty = field_empty.setResultsName(field.resultsName)

field = field | field_empty

field.setName(name)
field = field.setResultsName(resultsName)

field.leaveWhitespace()

return field

def _wrap_as_optional(self, field, name, columns):
"""
Adds a wrapper rule to the field to accept empty strings.
This empty string should be of the same size as the columns parameter.
One smaller or bigger will be rejected.
This wrapper will return None if the field is empty.
:param field: the field to wrap
:param name: name of the field
:param columns: number of columns it takes
:return: the field with an additional rule to allow empty strings
"""
# Regular expression accepting as many whitespaces as columns
field_empty = pp.Regex('[ ]{' + str(columns) + '}')

resultsName = field.resultsName

field_empty.setName(name)

# Whitespaces are not removed
field_empty.leaveWhitespace()

# None is returned by this rule
field_empty.setParseAction(pp.replaceWith(None))

field_empty = field_empty.setResultsName(resultsName)

field = field | field_empty

field.setName(name)
field = field.setResultsName(resultsName)

field.leaveWhitespace()

return field
5 changes: 4 additions & 1 deletion cwr/parser/decoder/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,11 @@ 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(
default_grammar_factory().get_rule('transmission'),
transmission_rule,
default_filename_decoder()
)

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pyparsing==2.0.3
pyyaml==3.11

# Deployment
setuptools>=17.0
twine>=1.5.0

# Documentation
Expand Down

0 comments on commit e7315bb

Please sign in to comment.