summaryrefslogtreecommitdiff
path: root/src/sexparse/param.py
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunk@stsci.edu>2014-05-08 23:43:58 -0400
committerJoseph Hunkeler <jhunk@stsci.edu>2014-05-08 23:43:58 -0400
commit186634b8f9039f09b30b24b3714329e17e40f68b (patch)
treec4dbf144a7e139f54a7a0cd0382166d339216470 /src/sexparse/param.py
parentb1576c3c691015463eeaac73fca29dd5367a08ba (diff)
downloadsexparse-186634b8f9039f09b30b24b3714329e17e40f68b.tar.gz
Improved type-casting, parsing, etcHEADmaster
Diffstat (limited to 'src/sexparse/param.py')
-rw-r--r--src/sexparse/param.py32
1 files changed, 24 insertions, 8 deletions
diff --git a/src/sexparse/param.py b/src/sexparse/param.py
index 8963c05..e0e6623 100644
--- a/src/sexparse/param.py
+++ b/src/sexparse/param.py
@@ -21,13 +21,15 @@
#(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
#SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import os
-import sexparse
+import collections
+from sexparse.base import SexConfigBase
+from sexparse.functions import *
__all__ = ['SexParam']
-class SexParam(sexparse.SexConfigBase):
+class SexParam(SexConfigBase):
def __init__(self, filename):
- super(SexParam, self).__init__(filename)
+ SexConfigBase.__init__(self, filename)
self.status = []
self.lparenthesis = '('
self.rparenthesis = ')'
@@ -57,7 +59,7 @@ class SexParam(sexparse.SexConfigBase):
open_position = line.find(self.lparenthesis)
close_position = line.find(self.rparenthesis)
key = line[0:open_position]
-
+
# empty lines don't matter to us
if not key:
continue
@@ -68,7 +70,17 @@ class SexParam(sexparse.SexConfigBase):
if open_position != -1 or close_position != -1:
# we found a so-called valid item
- item = line[open_position+1:close_position]
+ item = line[open_position+1:close_position].split(',')
+
+ if not item:
+ continue
+
+ item = [ x.strip(' ') for x in item ]
+ item = [ convert_type(x) for x in item ]
+
+ if len(item) == 1:
+ item = item[0]
+
# we need to take a close look.
if open_position < 0 or close_position < 0:
raise SyntaxError('{}:{}:malformed input item\n{}'.format(self.filename, index+1, line))
@@ -78,7 +90,7 @@ class SexParam(sexparse.SexConfigBase):
# strip comments for dictionary key values but keep track of them
if key.startswith(self.comment):
- key = key.lstrip(self.comment)
+ key = key.strip(self.comment)
self.status.append(False)
else:
self.status.append(True)
@@ -93,7 +105,11 @@ class SexParam(sexparse.SexConfigBase):
if not self.is_enabled(key):
key = self.comment + key
if item:
- item = self.lparenthesis + str(item) + self.rparenthesis
+ if isinstance(item, list):
+ item = self.lparenthesis + ",".join(map(str, item)) + self.rparenthesis
+ else:
+ item = self.lparenthesis + str(item) + self.rparenthesis
+
outfile.write("{}{}{}".format(key, item, os.linesep))
outfile.close()
-
+