Commits

Nikolaj Sjujskij committed 70a4909

Use global `/etc/lodgeitrc` for CLI-paster on POSIX systems

Apps on POSIX systems traditionally make use of global (for all users) config
file. In order to implement this for `lodgeit.py`, function
`load_default_settings` was split into two separate functions:
* `load_default_settings` for determining rcfile list (tuple, actually) to
parse and merging configurations;
* `parse_rcfile` for actual file parsing

Comments (0)

Files changed (1)

scripts/lodgeit.py

     print >> sys.stderr, 'ERROR: %s' % msg
     sys.exit(code)
 
+def parse_rcfile(rcfile, key_list=None):
+    """Parse lodgeitrc file, return dict of settings."""
+    settings = {}
+    try:
+        f = open(rcfile)
+        for line in f:
+            if line.strip()[:1] in '#;':
+                continue
+            p = line.split('=', 1)
+            if len(p) == 2:
+                key = p[0].strip().lower()
+                if key_list is not None and key not in key_list:
+                    continue
+                if key in ('clipboard', 'open_browser', 'raw'):
+                    settings[key] = p[1].strip().lower() in \
+                                    ('true', '1', 'on', 'yes')
+                else:
+                    settings[key] = p[1].strip()
+        f.close()
+    except IOError:
+        pass
+    return settings
+
 
 def load_default_settings():
-    """Load the defaults from the lodgeitrc file."""
+    """Load the defaults from the lodgeitrc file(s)."""
+
     settings = {
         'language':     None,
         'clipboard':    True,
         'encoding':     'iso-8859-15',
         'server_name':  'http://paste.pocoo.org',
     }
-    rcfile = None
+    key_list = settings.keys()
+    rcfiles = ()
     if os.name == 'posix':
-        rcfile = os.path.expanduser('~/.lodgeitrc')
+        rcfiles = ('/etc/lodgeitrc',
+                   os.path.expanduser('~/.lodgeitrc'))
     elif os.name == 'nt' and 'APPDATA' in os.environ:
-        rcfile = os.path.expandvars(r'$APPDATA\_lodgeitrc')
-    if rcfile:
-        try:
-            f = open(rcfile)
-            for line in f:
-                if line.strip()[:1] in '#;':
-                    continue
-                p = line.split('=', 1)
-                if len(p) == 2:
-                    key = p[0].strip().lower()
-                    if key in settings:
-                        if key in ('clipboard', 'open_browser', 'raw'):
-                            settings[key] = p[1].strip().lower() in \
-                                            ('true', '1', 'on', 'yes')
-                        else:
-                            settings[key] = p[1].strip()
-            f.close()
-        except IOError:
-            pass
+        rcfiles = (os.path.expandvars(r'$APPDATA\_lodgeitrc'), )
+    [settings.update(parse_rcfile(x, key_list)) for x in rcfiles]
     settings['tags'] = []
     settings['title'] = None
     return settings