summaryrefslogtreecommitdiff
path: root/src/sexparse
diff options
context:
space:
mode:
Diffstat (limited to 'src/sexparse')
-rw-r--r--src/sexparse/__init__.py25
-rw-r--r--src/sexparse/base.py52
-rw-r--r--src/sexparse/config.py52
-rw-r--r--src/sexparse/param.py99
4 files changed, 228 insertions, 0 deletions
diff --git a/src/sexparse/__init__.py b/src/sexparse/__init__.py
new file mode 100644
index 0000000..b9c3acd
--- /dev/null
+++ b/src/sexparse/__init__.py
@@ -0,0 +1,25 @@
+#Copyright (c) 2014, Joseph Hunkeler <jhunkeler at gmail.com>
+#All rights reserved.
+#
+#Redistribution and use in source and binary forms, with or without
+#modification, are permitted provided that the following conditions are met:
+#
+#1. Redistributions of source code must retain the above copyright notice, this
+# list of conditions and the following disclaimer.
+#2. Redistributions in binary form must reproduce the above copyright notice,
+# this list of conditions and the following disclaimer in the documentation
+# and/or other materials provided with the distribution.
+#
+#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+#ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+#WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+#DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+#ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+#(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+#LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+#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.
+from .base import *
+from .config import *
+from .param import *
diff --git a/src/sexparse/base.py b/src/sexparse/base.py
new file mode 100644
index 0000000..2ee5ce5
--- /dev/null
+++ b/src/sexparse/base.py
@@ -0,0 +1,52 @@
+#Copyright (c) 2014, Joseph Hunkeler <jhunkeler at gmail.com>
+#All rights reserved.
+#
+#Redistribution and use in source and binary forms, with or without
+#modification, are permitted provided that the following conditions are met:
+#
+#1. Redistributions of source code must retain the above copyright notice, this
+# list of conditions and the following disclaimer.
+#2. Redistributions in binary form must reproduce the above copyright notice,
+# this list of conditions and the following disclaimer in the documentation
+# and/or other materials provided with the distribution.
+#
+#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+#ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+#WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+#DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+#ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+#(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+#LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+#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 os
+from collections import OrderedDict
+
+__all__ = ['SexConfigBase']
+
+class SexConfigBase(object):
+ def __init__(self, filename):
+ self.filename = os.path.abspath(filename)
+ self.pairs = OrderedDict()
+ self.comment = '#'
+
+ def diff(self, obj):
+ if self.pairs != obj.pairs:
+ return True
+ return False
+
+ def read(self):
+ pass
+
+ def write(self, filename=None):
+ pass
+
+ def __getitem__(self, key):
+ return self.pairs[key]
+
+ def __setitem__(self, key, item):
+ self.pairs[key] = item
+
+ def __iter__(self):
+ return self.pairs.iteritems()
diff --git a/src/sexparse/config.py b/src/sexparse/config.py
new file mode 100644
index 0000000..c4fc2ab
--- /dev/null
+++ b/src/sexparse/config.py
@@ -0,0 +1,52 @@
+#Copyright (c) 2014, Joseph Hunkeler <jhunkeler at gmail.com>
+#All rights reserved.
+#
+#Redistribution and use in source and binary forms, with or without
+#modification, are permitted provided that the following conditions are met:
+#
+#1. Redistributions of source code must retain the above copyright notice, this
+# list of conditions and the following disclaimer.
+#2. Redistributions in binary form must reproduce the above copyright notice,
+# this list of conditions and the following disclaimer in the documentation
+# and/or other materials provided with the distribution.
+#
+#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+#ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+#WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+#DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+#ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+#(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+#LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+#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 os
+import sexparse
+
+__all__ = ['SexConfig']
+
+class SexConfig(sexparse.SexConfigBase):
+ def __init__(self, filename):
+ sexparse.SexConfigBase.__init__(self, filename)
+ self.read()
+
+ def read(self):
+ for line in open(self.filename):
+ if line.startswith(self.comment):
+ continue
+ comment_position = line.find(self.comment)
+ line = line[0:comment_position]
+ keypairs = line.split()
+ if not keypairs:
+ continue
+ self.pairs[keypairs[0]] = keypairs[1:]
+ if not keypairs:
+ continue
+
+ def write(self, filename=None):
+ if filename is None:
+ filename = self.filename
+ outfile = open(filename, 'w+')
+ for key, value in self.pairs.iteritems():
+ outfile.write("{}\t{}{}".format(key, value, os.linesep))
+ outfile.close()
diff --git a/src/sexparse/param.py b/src/sexparse/param.py
new file mode 100644
index 0000000..8963c05
--- /dev/null
+++ b/src/sexparse/param.py
@@ -0,0 +1,99 @@
+#Copyright (c) 2014, Joseph Hunkeler <jhunkeler at gmail.com>
+#All rights reserved.
+#
+#Redistribution and use in source and binary forms, with or without
+#modification, are permitted provided that the following conditions are met:
+#
+#1. Redistributions of source code must retain the above copyright notice, this
+# list of conditions and the following disclaimer.
+#2. Redistributions in binary form must reproduce the above copyright notice,
+# this list of conditions and the following disclaimer in the documentation
+# and/or other materials provided with the distribution.
+#
+#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+#ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+#WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+#DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+#ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+#(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+#LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+#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 os
+import sexparse
+
+__all__ = ['SexParam']
+
+class SexParam(sexparse.SexConfigBase):
+ def __init__(self, filename):
+ super(SexParam, self).__init__(filename)
+ self.status = []
+ self.lparenthesis = '('
+ self.rparenthesis = ')'
+ self.read()
+
+ def is_enabled(self, key):
+ for status, keyword in zip(self.status, self.pairs.iterkeys()):
+ if key == keyword:
+ return status
+
+ def enable(self, key, state):
+ status_new = []
+ mapping = zip(self.status, self.pairs.iterkeys())
+
+ for status, keyword in mapping:
+ if key == keyword:
+ status = state
+ status_new.append(status)
+
+ self.status = status_new
+
+ def align_values(self):
+ pass
+
+ def read(self):
+ for index, line in enumerate(open(self.filename)):
+ 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
+
+ # how about that syntax? did anyone screw up the file?
+ if " " in key or "," in key:
+ raise SyntaxError('{}:{}:malformed key\n{}'.format(self.filename, index+1, line))
+
+ if open_position != -1 or close_position != -1:
+ # we found a so-called valid item
+ item = line[open_position+1:close_position]
+ # 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))
+ else:
+ # legitimately found nothing
+ item = ''
+
+ # strip comments for dictionary key values but keep track of them
+ if key.startswith(self.comment):
+ key = key.lstrip(self.comment)
+ self.status.append(False)
+ else:
+ self.status.append(True)
+
+ self.pairs[key] = item
+
+ def write(self, filename=None):
+ if filename is None:
+ filename = self.filename
+ outfile = open(filename, 'w+')
+ for key, item in self.pairs.iteritems():
+ if not self.is_enabled(key):
+ key = self.comment + key
+ if item:
+ item = self.lparenthesis + str(item) + self.rparenthesis
+ outfile.write("{}{}{}".format(key, item, os.linesep))
+ outfile.close()
+