[yt-svn] commit/yt: samskillman: Adding pyparselibconfig
commits-noreply at bitbucket.org
commits-noreply at bitbucket.org
Fri May 16 13:45:41 PDT 2014
1 new commit in yt:
https://bitbucket.org/yt_analysis/yt/commits/54e87aca90a6/
Changeset: 54e87aca90a6
Branch: yt-3.0
User: samskillman
Date: 2014-05-16 22:45:25
Summary: Adding pyparselibconfig
Affected #: 3 files
diff -r 89e0c26a2f0b1815b54447323f8b5b711def0d1e -r 54e87aca90a647aa2a7dceb8d74056884b936de3 yt/utilities/pyparselibconfig/__init__.py
--- /dev/null
+++ b/yt/utilities/pyparselibconfig/__init__.py
@@ -0,0 +1,2 @@
+from api import libconfig
+
diff -r 89e0c26a2f0b1815b54447323f8b5b711def0d1e -r 54e87aca90a647aa2a7dceb8d74056884b936de3 yt/utilities/pyparselibconfig/api.py
--- /dev/null
+++ b/yt/utilities/pyparselibconfig/api.py
@@ -0,0 +1,1 @@
+from libconfig import libconfig
diff -r 89e0c26a2f0b1815b54447323f8b5b711def0d1e -r 54e87aca90a647aa2a7dceb8d74056884b936de3 yt/utilities/pyparselibconfig/libconfig.py
--- /dev/null
+++ b/yt/utilities/pyparselibconfig/libconfig.py
@@ -0,0 +1,95 @@
+
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, Samuel Skillman
+#
+# Distributed under the terms of the MIT License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
+
+class libconfig(dict):
+ def __init__(self, config=None):
+ if config is not None:
+ self.read(config)
+
+ def read(self, config):
+ cfile = open(config, 'r')
+ lines = cfile.readlines()
+
+ # Strip out spaces and blanks
+ lines = [line.strip() for line in lines if len(line) > 0]
+
+ # Strip out comments
+ lines = [line for line in lines if not (line.startswith('#') or
+ line.startswith('/'))]
+
+ # Concatenate
+ oneline = ''
+ for line in lines:
+ oneline += line
+
+ statements = oneline.split(';')
+
+ self.parse_statements(self, statements)
+
+ def parse_statements(self, this_dict, statements):
+ while len(statements) > 0:
+ statement = statements.pop(0)
+ if len(statement) == 0:
+ continue
+ if ':' in statement:
+ # DICTIONARY
+ new_level_lines = []
+ k, v = statement.split(':', 1)
+ k = k.strip(' :')
+ v = v.strip(' {')
+ level = 1 + v.count(':')
+ this_dict[k] = {}
+ new_level_lines.append(v)
+
+ while(level > 0 and len(statements) > 0):
+ nextline = statements.pop(0).strip()
+ level += nextline.count('{')
+ if nextline == '}' and level == 1:
+ level = 0
+ break
+ new_level_lines.append(nextline)
+ level -= nextline.count('}')
+ self.parse_statements(this_dict[k], new_level_lines)
+ else:
+ k, v = statement.split('=')
+ k = k.strip()
+ v = v.strip()
+ this_dict[k] = self.correct_type(v)
+
+ def correct_type(self, v):
+ return eval(v)
+
+ def write(self, filename):
+ f = file(filename, 'w')
+
+ self.write_dict(f, self, 0)
+
+ def write_dict(self, f, this_dict, level):
+ tab = ' '*4
+
+ dict_dict = {}
+ for k, v in this_dict.iteritems():
+ if type(v) == dict:
+ dict_dict[k] = v
+ else:
+ if type(v) == str:
+ v = '"%s"' % v
+ f.writelines(tab*level + '%s = %s;\n' % (k, v))
+
+ for k, v in dict_dict.iteritems():
+ f.writelines('\n')
+ f.writelines(tab*level + '%s :\n' % k)
+ f.writelines(tab*level+'{\n')
+ self.write_dict(f, v, level+1)
+ f.writelines(tab*level+'};\n')
+
+if __name__ == '__main__':
+ cfg = libconfig()
+ cfg.read('test_config.cfg')
+ print cfg
Repository URL: https://bitbucket.org/yt_analysis/yt/
--
This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.
More information about the yt-svn
mailing list