diff martini/config.py @ 9:77c7556fa8e0

well, dammit
author Jeff Hammel <k0scist@gmail.com>
date Tue, 23 Aug 2016 09:11:49 -0700
parents 81aed4352851
children 66d11212175d
line wrap: on
line diff
--- a/martini/config.py	Wed Nov 24 11:05:40 2010 -0800
+++ b/martini/config.py	Tue Aug 23 09:11:49 2016 -0700
@@ -4,16 +4,21 @@
 import sys
 import urllib2
 
-from odict import OrderedDict
 from ConfigParser import ConfigParser
 from ConfigParser import InterpolationMissingOptionError
 from ConfigParser import MissingSectionHeaderError
 from ConfigParser import NoOptionError
 from StringIO import StringIO
 
+try:
+    from collections import OrderedDict
+except ImportError:
+    from odict import OrderedDict
+
+
 def file_pointer(resource):
     """returns a file-like object given a string"""
-    # XXX could go in utils.py 
+    # XXX could go in utils.py
 
     if not isinstance(resource, basestring):
         # assume resource is already a file-like object
@@ -61,29 +66,29 @@
         else:
             _section = {}
         self.read({newname: _section})
-    
+
     def dict(self):
-        """return a dictionary of dictionaries; 
+        """return a dictionary of dictionaries;
         the outer with keys of section names;
         the inner with keys, values of the section"""
-        return dict([(section, self[section])
-                     for section in self.sections()])
+        return OrderedDict([(section, self[section])
+                            for section in self.sections()])
 
     def read(self, *ini):
         for _ini in ini:
-            if isinstance(_ini, dict):
+            if isinstance(_ini, (dict, OrderedDict)):
                 for section, contents in _ini.items():
                     for option, value in contents.items():
                         self.set(section, option, value)
-            elif isinstance(_ini, list) or isinstance(_ini, tuple):
+            elif isinstance(_ini, (list, tuple)):
 
                 # ensure list or tuple of 3-tuples
                 assert len([option for option in _ini
-                            if isinstance(option, tuple) 
+                            if isinstance(option, tuple)
                             and len(option) == 3])
 
                 for section, option, value in _ini:
-                    self.set(section, option, value)                
+                    self.set(section, option, value)
             else:
                 fp = file_pointer(_ini)
                 try:
@@ -92,11 +97,11 @@
                     fp.seek(0)
                     fp = StringIO("[DEFAULTS]\n" + fp.read())
                     self.readfp(fp)
-            
+
     def missing(self):
         """returns missing variable names"""
-        missing = set()        
 
+        missing = set()
         for section in self.sections():
             for key, val in self.items(section, raw=True):
                 try:
@@ -112,7 +117,7 @@
         """
         options = []
         for section in self.sections():
-            options.extend([(section,) + item 
+            options.extend([(section,) + item
                             for item in self.items(section)])
         return options