Skip to content

Commit

Permalink
cleaned up strings, refs #3
Browse files Browse the repository at this point in the history
  • Loading branch information
godber committed May 3, 2015
1 parent 4c1c323 commit 7c3c133
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
22 changes: 20 additions & 2 deletions pds3label/pds3_label_visitor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import print_function
import re

try:
from collections import OrderedDict
Expand Down Expand Up @@ -40,8 +41,25 @@ def visitScalarIdentifier(self, ctx):

def visitScalarString(self, ctx):
ODLv21Visitor.visitScalarString(self, ctx)
return ctx.STRING().getText()
return Pds3LabelVisitor._clean_string(ctx.STRING().getText())

def visitScalarSymbol(self, ctx):
ODLv21Visitor.visitScalarSymbol(self, ctx)
return ctx.SYMBOL_STRING().getText()
return Pds3LabelVisitor._clean_symbol(ctx.SYMBOL_STRING().getText())

@classmethod
def _clean_symbol(cls, instring):
"""Strips the single quotes off of the symbol"""
instring = re.sub(r"'", '', instring)
return instring

@classmethod
def _clean_string(cls, instring):
"""Cleans up the provided string, including the following things:
* Strips off double quotes
* Replaces all whitespace (including newlines) with a single space.
"""
instring = re.sub(r'"', '', instring) # strip "
instring = re.sub(r"-\s*\n\s+", '', instring) # remove hyphen and its trailing whitespace
instring = re.sub(r"\s+", ' ', instring) # replace whitespace with single space
return instring
12 changes: 12 additions & 0 deletions test/test_pds3label.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,15 @@ def test_string2(self):
assert label.label_dict['INTEGER1'] == 1
assert label.label_dict['FLOAT'] == 2.3
assert label.label_dict['COMMENT1'] == "THING TEST"
assert label.label_dict['COMMENT2'] == "Alive."
assert label.label_dict['COMMENT_1'] == "THING TEST"
assert label.label_dict['COMMENT_2'] == "Alive."
assert label.label_dict['COMMENT_2_A'] == "Alive Again."
assert label.label_dict['SYMBOL_STR'] == "JBD-123"

def test_string3(self):
label = Pds3Label('test/data/string3.lbl')
assert label.infile == 'test/data/string3.lbl'
assert label.label_dict['MULTILINE'] == 'This is a test of the emergency broadcasting system.'
assert label.label_dict['HYPHENATED'] == 'The planet Jupiter is very big'
assert label.label_dict['EMPTY_STRING'] == ''

0 comments on commit 7c3c133

Please sign in to comment.