summaryrefslogtreecommitdiff
path: root/src
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
parentb1576c3c691015463eeaac73fca29dd5367a08ba (diff)
downloadsexparse-186634b8f9039f09b30b24b3714329e17e40f68b.tar.gz
Improved type-casting, parsing, etcHEADmaster
Diffstat (limited to 'src')
-rw-r--r--src/sexparse/__init__.py1
-rw-r--r--src/sexparse/config.py27
-rw-r--r--src/sexparse/functions.py36
-rw-r--r--src/sexparse/param.py32
4 files changed, 80 insertions, 16 deletions
diff --git a/src/sexparse/__init__.py b/src/sexparse/__init__.py
index b9c3acd..98d2596 100644
--- a/src/sexparse/__init__.py
+++ b/src/sexparse/__init__.py
@@ -23,3 +23,4 @@
from .base import *
from .config import *
from .param import *
+from .functions import *
diff --git a/src/sexparse/config.py b/src/sexparse/config.py
index c4fc2ab..200af21 100644
--- a/src/sexparse/config.py
+++ b/src/sexparse/config.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
+from sexparse.base import SexConfigBase
+from sexparse.functions import *
__all__ = ['SexConfig']
-class SexConfig(sexparse.SexConfigBase):
+
+class SexConfig(SexConfigBase):
def __init__(self, filename):
- sexparse.SexConfigBase.__init__(self, filename)
+ SexConfigBase.__init__(self, filename)
self.read()
def read(self):
@@ -36,12 +38,23 @@ class SexConfig(sexparse.SexConfigBase):
continue
comment_position = line.find(self.comment)
line = line[0:comment_position]
- keypairs = line.split()
- if not keypairs:
+ key = line[0:line.find(' ')]
+
+ if not key:
continue
- self.pairs[keypairs[0]] = keypairs[1:]
- if not keypairs:
+
+ item = line[line.find(' '):].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]
+
+ self.pairs[key] = item
def write(self, filename=None):
if filename is None:
diff --git a/src/sexparse/functions.py b/src/sexparse/functions.py
index 08ba5d9..22aae50 100644
--- a/src/sexparse/functions.py
+++ b/src/sexparse/functions.py
@@ -20,13 +20,47 @@
#ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#(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 sexparse
+def sexdump(obj, col_width=16, truncate=True):
+ width = col_width
+ trunc = "<trunc>"
+ if not isinstance(obj, sexparse.SexConfigBase):
+ return None
+
+ for k, v in obj:
+ retainer = v
+ if truncate:
+ if len(repr(v)) > width:
+ v = repr(v)[0:width - len(trunc)] + trunc
+
+ show_type = type(retainer)
+ sformat = "{:>{width}} | {:<{width}} | {:<{width}}"
+ sformat_t = (k, v, show_type)
+
+ try:
+ if obj.status:
+ status = "Enabled" if convert_type(obj.is_enabled(k)) else "Disabled"
+ sformat += " | {:>{width}}"
+ sformat_t = (k, v, status, show_type)
+ except AttributeError:
+ pass
+
+ if isinstance(v, list) or isinstance(v, tuple):
+ show_type = [ type(x) for x in retainer ]
+
+ if isinstance(v, dict):
+ show_type = [ type(x) for x in retainer.iteritems() ]
+
+ print(sformat.format(*sformat_t, width=col_width))
+
def convert_type(s):
+
for cast in (int, float):
try:
return cast(s)
except ValueError:
pass
- return s \ No newline at end of file
+ return s
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()
-
+