__init__.py, rl_config.py & rl_settings.py: revamp initialization yet again py33
authorrobin
Thu, 27 Feb 2014 12:51:04 +0000
branchpy33
changeset 4050 a899d3baa5f4
parent 4049 926e36d3bb2c
child 4051 b6d01982a5fb
__init__.py, rl_config.py & rl_settings.py: revamp initialization yet again
src/reportlab/__init__.py
src/reportlab/rl_config.py
src/reportlab/rl_settings.py
--- a/src/reportlab/__init__.py	Thu Feb 27 10:32:27 2014 +0000
+++ b/src/reportlab/__init__.py	Thu Feb 27 12:51:04 2014 +0000
@@ -3,9 +3,9 @@
 #history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/__init__.py
 __version__=''' $Id$ '''
 __doc__="""The Reportlab PDF generation library."""
-Version = "3.0-20140220"
+Version = "3.0-20140226"
 
-import sys
+import sys, os, imp
 
 if sys.version_info[0:2]!=(2, 7) and sys.version_info<(3, 3):
     raise ImportError("""reportlab requires Python 2.7+ or 3.3+; 3.0-3.2 are not supported.""")
@@ -26,8 +26,23 @@
     __builtin__.ascii = ascii
     del ascii, __builtin__
 
-#the module reportlab.local_rl_mods can be used to customize just about anything
+#try to use dynamic modifications from
+#reportlab.local_rl_mods.py
+#reportlab_mods or ~/.reportlab_mods
 try:
     import reportlab.local_rl_mods
 except ImportError:
     pass
+
+def _fake_import(fn,name):
+    if os.path.isfile(fn):
+        with open(fn,'rb') as f:
+            imp.load_source('reportlab_mods',fn,f)
+
+try:
+    import reportlab_mods   #application specific modifications can be anywhere on python path
+except ImportError:
+    try:
+        _fake_import(os.path.expanduser(os.path.join('~','.reportlab_mods')),'reportlab_mods')
+    except ImportError:
+        pass
--- a/src/reportlab/rl_config.py	Thu Feb 27 10:32:27 2014 +0000
+++ b/src/reportlab/rl_config.py	Thu Feb 27 12:51:04 2014 +0000
@@ -1,19 +1,43 @@
 '''module that aggregates config information'''
 __all__=('_reset','register_reset')
-from reportlab.lib.utils import rl_exec
-_overrides = {}
-try:
-    rl_exec('from reportlab.local_rl_settings import *',_overrides)
-except ImportError:
-    pass
-try:
-    rl_exec('from local_rl_settings import *',_overrides)
-except ImportError:
-    pass
-_DEFAULTS={}
-rl_exec('from reportlab.rl_settings import *',_DEFAULTS)
-_DEFAULTS.update(_overrides)
-del _overrides,rl_exec
+
+def _defaults_init():
+    '''
+    create & return defaults for all reportlab settings from
+    reportlab.rl_settings.py
+    reportlab.local_rl_settings.py
+    reportlab_settings or ~/.reportlab_settings
+
+    latter values override earlier
+    '''
+    from reportlab.lib.utils import rl_exec
+    import os
+
+    _DEFAULTS={}
+    rl_exec('from reportlab.rl_settings import *',_DEFAULTS)
+
+    _overrides={}
+    try:
+        rl_exec('from reportlab.local_rl_settings import *',_overrides)
+        _DEFAULTS.update(_overrides)
+    except ImportError:
+        pass
+
+    _overrides={}
+    try:
+        rl_exec('from reportlab_settings import *',_overrides)
+        _DEFAULTS.update(_overrides)
+    except ImportError:
+        _overrides={}
+        try:
+            with open(os.path.expanduser(os.path.join('~','.reportlab_settings')),'rb') as f:
+                rl_exec(f.read(),_overrides)
+            _DEFAULTS.update(_overrides)
+        except:
+            pass
+    return _DEFAULTS
+
+_DEFAULTS=_defaults_init()
 
 _SAVED = {}
 sys_version=None
@@ -54,9 +78,10 @@
     #places to search for Type 1 Font files
     import reportlab
     D = {'REPORTLAB_DIR': os.path.abspath(os.path.dirname(reportlab.__file__)),
-        'HOME': os.environ.get('HOME',os.getcwd()),
+        'CWD': os.getcwd(),
         'disk': os.getcwd().split(':')[0],
         'sys_version': sys_version,
+        'XDG_DATADIR': os.environ.get('XDG_DATADIR','~/.local/share'),
         }
 
     for k in _SAVED:
@@ -64,6 +89,7 @@
             P=[]
             for p in _SAVED[k]:
                 d = (p % D).replace('/',os.sep)
+                if '~' in d: d = os.path.expanduser(d)
                 if rl_isdir(d): P.append(d)
             _setOpt(k,os.pathsep.join(P),lambda x:x.split(os.pathsep))
             globals()[k] = list(filter(rl_isdir,globals()[k]))
--- a/src/reportlab/rl_settings.py	Thu Feb 27 10:32:27 2014 +0000
+++ b/src/reportlab/rl_settings.py	Thu Feb 27 12:51:04 2014 +0000
@@ -117,7 +117,11 @@
                 '%(REPORTLAB_DIR)s/fonts',              #special
                 '%(REPORTLAB_DIR)s/../fonts',           #special
                 '%(REPORTLAB_DIR)s/../../fonts',        #special
-                '%(HOME)s/fonts',                       #special
+                '%(CWD)s/fonts',                        #special
+                '~/fonts',
+                '~/.fonts',
+                '%(XDG_DATADIR)s/fonts',
+                '~/.local/share/fonts',
                  )
 
 # places to look for TT Font information
@@ -129,7 +133,11 @@
                 '%(REPORTLAB_DIR)s/fonts',      #special
                 '%(REPORTLAB_DIR)s/../fonts',   #special
                 '%(REPORTLAB_DIR)s/../../fonts',#special
-                '%(HOME)s/fonts',               #special
+                '%(CWD)s/fonts',                #special
+                '~/fonts',
+                '~/.fonts',
+                '%(XDG_DATADIR)s/fonts',
+                '~/.local/share/fonts',
                 #mac os X - from
                 #http://developer.apple.com/technotes/tn/tn2024.html
                 '~/Library/Fonts',
@@ -162,7 +170,12 @@
                   '%(REPORTLAB_DIR)s/fonts/CMap',       #special
                   '%(REPORTLAB_DIR)s/../fonts/CMap',    #special
                   '%(REPORTLAB_DIR)s/../../fonts/CMap', #special
-                  '%(HOME)s/fonts/CMap',                #special
+                  '%(CWD)s/fonts/CMap',             #special
+                  '%(CWD)s/fonts',              #special
+                  '~/fonts/CMap',
+                  '~/.fonts/CMap',
+                  '%(XDG_DATADIR)s/fonts/CMap',
+                  '~/.local/share/fonts/CMap',
                   )
 
 if sys.platform.startswith('linux'):