src/reportlab/rl_config.py
author robin <robin@reportlab.com>
Fri, 23 Mar 2018 16:02:08 +0000
changeset 4389 61a7f0840d00
parent 4185 8c932a166cd2
child 4616 7f2231703dc7
permissions -rw-r--r--
more controllable under and strike lines; fix for issue 137 contributed by Tom Alexander @ bitbucket; version-->3.4.30
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3916
647b4ba8b885 rl_config.py & rl_settings.py: values in rl_settings.py, allows better local_rl_settings.py
robin
parents: 3860
diff changeset
     1
'''module that aggregates config information'''
647b4ba8b885 rl_config.py & rl_settings.py: values in rl_settings.py, allows better local_rl_settings.py
robin
parents: 3860
diff changeset
     2
__all__=('_reset','register_reset')
4050
a899d3baa5f4 __init__.py, rl_config.py & rl_settings.py: revamp initialization yet again
robin
parents: 3933
diff changeset
     3
a899d3baa5f4 __init__.py, rl_config.py & rl_settings.py: revamp initialization yet again
robin
parents: 3933
diff changeset
     4
def _defaults_init():
a899d3baa5f4 __init__.py, rl_config.py & rl_settings.py: revamp initialization yet again
robin
parents: 3933
diff changeset
     5
    '''
a899d3baa5f4 __init__.py, rl_config.py & rl_settings.py: revamp initialization yet again
robin
parents: 3933
diff changeset
     6
    create & return defaults for all reportlab settings from
a899d3baa5f4 __init__.py, rl_config.py & rl_settings.py: revamp initialization yet again
robin
parents: 3933
diff changeset
     7
    reportlab.rl_settings.py
a899d3baa5f4 __init__.py, rl_config.py & rl_settings.py: revamp initialization yet again
robin
parents: 3933
diff changeset
     8
    reportlab.local_rl_settings.py
4054
d717a23e8380 rl_config.py: allow overriders to see existing settings
robin
parents: 4052
diff changeset
     9
    reportlab_settings.py or ~/.reportlab_settings
4050
a899d3baa5f4 __init__.py, rl_config.py & rl_settings.py: revamp initialization yet again
robin
parents: 3933
diff changeset
    10
a899d3baa5f4 __init__.py, rl_config.py & rl_settings.py: revamp initialization yet again
robin
parents: 3933
diff changeset
    11
    latter values override earlier
a899d3baa5f4 __init__.py, rl_config.py & rl_settings.py: revamp initialization yet again
robin
parents: 3933
diff changeset
    12
    '''
a899d3baa5f4 __init__.py, rl_config.py & rl_settings.py: revamp initialization yet again
robin
parents: 3933
diff changeset
    13
    from reportlab.lib.utils import rl_exec
a899d3baa5f4 __init__.py, rl_config.py & rl_settings.py: revamp initialization yet again
robin
parents: 3933
diff changeset
    14
    import os
a899d3baa5f4 __init__.py, rl_config.py & rl_settings.py: revamp initialization yet again
robin
parents: 3933
diff changeset
    15
a899d3baa5f4 __init__.py, rl_config.py & rl_settings.py: revamp initialization yet again
robin
parents: 3933
diff changeset
    16
    _DEFAULTS={}
a899d3baa5f4 __init__.py, rl_config.py & rl_settings.py: revamp initialization yet again
robin
parents: 3933
diff changeset
    17
    rl_exec('from reportlab.rl_settings import *',_DEFAULTS)
a899d3baa5f4 __init__.py, rl_config.py & rl_settings.py: revamp initialization yet again
robin
parents: 3933
diff changeset
    18
4054
d717a23e8380 rl_config.py: allow overriders to see existing settings
robin
parents: 4052
diff changeset
    19
    _overrides=_DEFAULTS.copy()
4050
a899d3baa5f4 __init__.py, rl_config.py & rl_settings.py: revamp initialization yet again
robin
parents: 3933
diff changeset
    20
    try:
a899d3baa5f4 __init__.py, rl_config.py & rl_settings.py: revamp initialization yet again
robin
parents: 3933
diff changeset
    21
        rl_exec('from reportlab.local_rl_settings import *',_overrides)
a899d3baa5f4 __init__.py, rl_config.py & rl_settings.py: revamp initialization yet again
robin
parents: 3933
diff changeset
    22
        _DEFAULTS.update(_overrides)
a899d3baa5f4 __init__.py, rl_config.py & rl_settings.py: revamp initialization yet again
robin
parents: 3933
diff changeset
    23
    except ImportError:
a899d3baa5f4 __init__.py, rl_config.py & rl_settings.py: revamp initialization yet again
robin
parents: 3933
diff changeset
    24
        pass
a899d3baa5f4 __init__.py, rl_config.py & rl_settings.py: revamp initialization yet again
robin
parents: 3933
diff changeset
    25
4054
d717a23e8380 rl_config.py: allow overriders to see existing settings
robin
parents: 4052
diff changeset
    26
    _overrides=_DEFAULTS.copy()
4050
a899d3baa5f4 __init__.py, rl_config.py & rl_settings.py: revamp initialization yet again
robin
parents: 3933
diff changeset
    27
    try:
a899d3baa5f4 __init__.py, rl_config.py & rl_settings.py: revamp initialization yet again
robin
parents: 3933
diff changeset
    28
        rl_exec('from reportlab_settings import *',_overrides)
a899d3baa5f4 __init__.py, rl_config.py & rl_settings.py: revamp initialization yet again
robin
parents: 3933
diff changeset
    29
        _DEFAULTS.update(_overrides)
a899d3baa5f4 __init__.py, rl_config.py & rl_settings.py: revamp initialization yet again
robin
parents: 3933
diff changeset
    30
    except ImportError:
4054
d717a23e8380 rl_config.py: allow overriders to see existing settings
robin
parents: 4052
diff changeset
    31
        _overrides=_DEFAULTS.copy()
4050
a899d3baa5f4 __init__.py, rl_config.py & rl_settings.py: revamp initialization yet again
robin
parents: 3933
diff changeset
    32
        try:
4115
f1913fcae595 rl_config.py: appengine fix contributed by anonymous @ bitbucket (issue 39)
robin
parents: 4067
diff changeset
    33
            try:
4185
8c932a166cd2 catch ImportErrors for live GAE
jamesbynd
parents: 4176
diff changeset
    34
                fn = os.path.expanduser(os.path.join('~','.reportlab_settings'))    #appengine fails with KeyError/ImportError (dev/live)
8c932a166cd2 catch ImportErrors for live GAE
jamesbynd
parents: 4176
diff changeset
    35
            except (KeyError, ImportError):
4115
f1913fcae595 rl_config.py: appengine fix contributed by anonymous @ bitbucket (issue 39)
robin
parents: 4067
diff changeset
    36
                fn = None
f1913fcae595 rl_config.py: appengine fix contributed by anonymous @ bitbucket (issue 39)
robin
parents: 4067
diff changeset
    37
            if fn:
f1913fcae595 rl_config.py: appengine fix contributed by anonymous @ bitbucket (issue 39)
robin
parents: 4067
diff changeset
    38
                with open(fn,'rb') as f:
f1913fcae595 rl_config.py: appengine fix contributed by anonymous @ bitbucket (issue 39)
robin
parents: 4067
diff changeset
    39
                    rl_exec(f.read(),_overrides)
f1913fcae595 rl_config.py: appengine fix contributed by anonymous @ bitbucket (issue 39)
robin
parents: 4067
diff changeset
    40
                _DEFAULTS.update(_overrides)
4050
a899d3baa5f4 __init__.py, rl_config.py & rl_settings.py: revamp initialization yet again
robin
parents: 3933
diff changeset
    41
        except:
a899d3baa5f4 __init__.py, rl_config.py & rl_settings.py: revamp initialization yet again
robin
parents: 3933
diff changeset
    42
            pass
a899d3baa5f4 __init__.py, rl_config.py & rl_settings.py: revamp initialization yet again
robin
parents: 3933
diff changeset
    43
    return _DEFAULTS
a899d3baa5f4 __init__.py, rl_config.py & rl_settings.py: revamp initialization yet again
robin
parents: 3933
diff changeset
    44
a899d3baa5f4 __init__.py, rl_config.py & rl_settings.py: revamp initialization yet again
robin
parents: 3933
diff changeset
    45
_DEFAULTS=_defaults_init()
2224
e7f97f92f59a Added rlhome/../fonts
rgbecker
parents: 2213
diff changeset
    46
e7f97f92f59a Added rlhome/../fonts
rgbecker
parents: 2213
diff changeset
    47
_SAVED = {}
2230
bf741cc35992 Sys_version was being used in older python
rgbecker
parents: 2224
diff changeset
    48
sys_version=None
1093
cf7ac33f559d Added environment mechanism for most/many config options
rgbecker
parents: 1070
diff changeset
    49
3359
171fa15695a8 reportlab: optional A85 patch contributed by Yoann Roman
rgbecker
parents: 3338
diff changeset
    50
#this is used to set the options from
1093
cf7ac33f559d Added environment mechanism for most/many config options
rgbecker
parents: 1070
diff changeset
    51
def _setOpt(name, value, conv=None):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1635
diff changeset
    52
    '''set a module level value from environ/default'''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1635
diff changeset
    53
    from os import environ
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1635
diff changeset
    54
    ename = 'RL_'+name
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3254
diff changeset
    55
    if ename in environ:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1635
diff changeset
    56
        value = environ[ename]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1635
diff changeset
    57
    if conv: value = conv(value)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1635
diff changeset
    58
    globals()[name] = value
1093
cf7ac33f559d Added environment mechanism for most/many config options
rgbecker
parents: 1070
diff changeset
    59
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1635
diff changeset
    60
def _startUp():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1635
diff changeset
    61
    '''This function allows easy resetting to the global defaults
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1635
diff changeset
    62
    If the environment contains 'RL_xxx' then we use the value
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1635
diff changeset
    63
    else we use the given default'''
2767
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2627
diff changeset
    64
    import os, sys
2360
0fbaee224de1 rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents: 2351
diff changeset
    65
    global sys_version, _unset_
2767
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2627
diff changeset
    66
    sys_version = sys.version.split()[0]        #strip off the other garbage
2224
e7f97f92f59a Added rlhome/../fonts
rgbecker
parents: 2213
diff changeset
    67
    from reportlab.lib import pagesizes
2255
bca9c1a120be Use rl_isdir
rgbecker
parents: 2230
diff changeset
    68
    from reportlab.lib.utils import rl_isdir
1172
f6ef588e5f01 Allowed for resets
rgbecker
parents: 1171
diff changeset
    69
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1635
diff changeset
    70
    if _SAVED=={}:
2360
0fbaee224de1 rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents: 2351
diff changeset
    71
        _unset_ = getattr(sys,'_rl_config__unset_',None)
0fbaee224de1 rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents: 2351
diff changeset
    72
        if _unset_ is None:
0fbaee224de1 rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents: 2351
diff changeset
    73
            class _unset_: pass
0fbaee224de1 rl_config add _unset_, graphics.renderxxx refactoring
rgbecker
parents: 2351
diff changeset
    74
            sys._rl_config__unset_ = _unset_ = _unset_()
3916
647b4ba8b885 rl_config.py & rl_settings.py: values in rl_settings.py, allows better local_rl_settings.py
robin
parents: 3860
diff changeset
    75
        global __all__
647b4ba8b885 rl_config.py & rl_settings.py: values in rl_settings.py, allows better local_rl_settings.py
robin
parents: 3860
diff changeset
    76
        A = list(__all__)
647b4ba8b885 rl_config.py & rl_settings.py: values in rl_settings.py, allows better local_rl_settings.py
robin
parents: 3860
diff changeset
    77
        for k,v in _DEFAULTS.items():
647b4ba8b885 rl_config.py & rl_settings.py: values in rl_settings.py, allows better local_rl_settings.py
robin
parents: 3860
diff changeset
    78
            _SAVED[k] = globals()[k] = v
647b4ba8b885 rl_config.py & rl_settings.py: values in rl_settings.py, allows better local_rl_settings.py
robin
parents: 3860
diff changeset
    79
            if k not in __all__:
647b4ba8b885 rl_config.py & rl_settings.py: values in rl_settings.py, allows better local_rl_settings.py
robin
parents: 3860
diff changeset
    80
                A.append(k)
647b4ba8b885 rl_config.py & rl_settings.py: values in rl_settings.py, allows better local_rl_settings.py
robin
parents: 3860
diff changeset
    81
        __all__ = tuple(A)
735
5dffc9c425d6 Made imageCaching a positive quantity and proeprty of the canvas
rgbecker
parents: 720
diff changeset
    82
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1635
diff changeset
    83
    #places to search for Type 1 Font files
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1635
diff changeset
    84
    import reportlab
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1635
diff changeset
    85
    D = {'REPORTLAB_DIR': os.path.abspath(os.path.dirname(reportlab.__file__)),
4050
a899d3baa5f4 __init__.py, rl_config.py & rl_settings.py: revamp initialization yet again
robin
parents: 3933
diff changeset
    86
        'CWD': os.getcwd(),
2767
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2627
diff changeset
    87
        'disk': os.getcwd().split(':')[0],
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1635
diff changeset
    88
        'sys_version': sys_version,
4052
7a869ff0ee64 rl_config.py & rl_settings.py: fix wrongly named envirnment variable
robin
parents: 4050
diff changeset
    89
        'XDG_DATA_HOME': os.environ.get('XDG_DATA_HOME','~/.local/share'),
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1635
diff changeset
    90
        }
1170
5a1829bc0f31 Fixed up a better T1SearchPath
rgbecker
parents: 1166
diff changeset
    91
3916
647b4ba8b885 rl_config.py & rl_settings.py: values in rl_settings.py, allows better local_rl_settings.py
robin
parents: 3860
diff changeset
    92
    for k in _SAVED:
647b4ba8b885 rl_config.py & rl_settings.py: values in rl_settings.py, allows better local_rl_settings.py
robin
parents: 3860
diff changeset
    93
        if k.endswith('SearchPath'):
647b4ba8b885 rl_config.py & rl_settings.py: values in rl_settings.py, allows better local_rl_settings.py
robin
parents: 3860
diff changeset
    94
            P=[]
647b4ba8b885 rl_config.py & rl_settings.py: values in rl_settings.py, allows better local_rl_settings.py
robin
parents: 3860
diff changeset
    95
            for p in _SAVED[k]:
647b4ba8b885 rl_config.py & rl_settings.py: values in rl_settings.py, allows better local_rl_settings.py
robin
parents: 3860
diff changeset
    96
                d = (p % D).replace('/',os.sep)
4115
f1913fcae595 rl_config.py: appengine fix contributed by anonymous @ bitbucket (issue 39)
robin
parents: 4067
diff changeset
    97
                if '~' in d:
f1913fcae595 rl_config.py: appengine fix contributed by anonymous @ bitbucket (issue 39)
robin
parents: 4067
diff changeset
    98
                    try:
4185
8c932a166cd2 catch ImportErrors for live GAE
jamesbynd
parents: 4176
diff changeset
    99
                        d = os.path.expanduser(d)   #appengine fails with KeyError/ImportError (dev/live)
8c932a166cd2 catch ImportErrors for live GAE
jamesbynd
parents: 4176
diff changeset
   100
                    except (KeyError, ImportError):
4115
f1913fcae595 rl_config.py: appengine fix contributed by anonymous @ bitbucket (issue 39)
robin
parents: 4067
diff changeset
   101
                        continue
3916
647b4ba8b885 rl_config.py & rl_settings.py: values in rl_settings.py, allows better local_rl_settings.py
robin
parents: 3860
diff changeset
   102
                if rl_isdir(d): P.append(d)
3932
a26ea864c24a allow environment setting of multiple search paths contributed by bitbucket user nikis (transplanted from 3784 with changes)
robin
parents: 3916
diff changeset
   103
            _setOpt(k,os.pathsep.join(P),lambda x:x.split(os.pathsep))
3933
72eb6482f2e3 rl_config.py: fix the setting of search paths for python3
robin
parents: 3932
diff changeset
   104
            globals()[k] = list(filter(rl_isdir,globals()[k]))
3916
647b4ba8b885 rl_config.py & rl_settings.py: values in rl_settings.py, allows better local_rl_settings.py
robin
parents: 3860
diff changeset
   105
        else:
647b4ba8b885 rl_config.py & rl_settings.py: values in rl_settings.py, allows better local_rl_settings.py
robin
parents: 3860
diff changeset
   106
            v = _SAVED[k]
647b4ba8b885 rl_config.py & rl_settings.py: values in rl_settings.py, allows better local_rl_settings.py
robin
parents: 3860
diff changeset
   107
            if isinstance(v,(int,float)): conv = type(v)
647b4ba8b885 rl_config.py & rl_settings.py: values in rl_settings.py, allows better local_rl_settings.py
robin
parents: 3860
diff changeset
   108
            elif k=='defaultPageSize': conv = lambda v,M=pagesizes: getattr(M,v)
647b4ba8b885 rl_config.py & rl_settings.py: values in rl_settings.py, allows better local_rl_settings.py
robin
parents: 3860
diff changeset
   109
            else: conv = None
647b4ba8b885 rl_config.py & rl_settings.py: values in rl_settings.py, allows better local_rl_settings.py
robin
parents: 3860
diff changeset
   110
            _setOpt(k,v,conv)
1166
5f5c1226be05 naive users rl_config.
rgbecker
parents: 1150
diff changeset
   111
2767
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2627
diff changeset
   112
_registered_resets=[]
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2627
diff changeset
   113
def register_reset(func):
3916
647b4ba8b885 rl_config.py & rl_settings.py: values in rl_settings.py, allows better local_rl_settings.py
robin
parents: 3860
diff changeset
   114
    '''register a function to be called by rl_config._reset'''
2767
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2627
diff changeset
   115
    _registered_resets[:] = [x for x in _registered_resets if x()]
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2627
diff changeset
   116
    L = [x for x in _registered_resets if x() is func]
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2627
diff changeset
   117
    if L: return
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2627
diff changeset
   118
    from weakref import ref
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2627
diff changeset
   119
    _registered_resets.append(ref(func))
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2627
diff changeset
   120
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2627
diff changeset
   121
def _reset():
3916
647b4ba8b885 rl_config.py & rl_settings.py: values in rl_settings.py, allows better local_rl_settings.py
robin
parents: 3860
diff changeset
   122
    '''attempt to reset reportlab and friends'''
2767
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2627
diff changeset
   123
    _startUp()  #our reset
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2627
diff changeset
   124
    for f in _registered_resets[:]:
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2627
diff changeset
   125
        c = f()
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2627
diff changeset
   126
        if c:
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2627
diff changeset
   127
            c()
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2627
diff changeset
   128
        else:
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2627
diff changeset
   129
            _registered_resets.remove(f)
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2627
diff changeset
   130
1975
1e7973738de4 Fix up setting of invariant
rgbecker
parents: 1838
diff changeset
   131
_startUp()