rl_config.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. '''module that aggregates config information'''
  2. __all__=('_reset','register_reset')
  3. def _defaults_init():
  4. '''
  5. create & return defaults for all reportlab settings from
  6. reportlab.rl_settings.py
  7. reportlab.local_rl_settings.py
  8. reportlab_settings.py or ~/.reportlab_settings
  9. latter values override earlier
  10. '''
  11. from reportlab.lib.utils import rl_exec
  12. import os
  13. _DEFAULTS={}
  14. rl_exec('from reportlab.rl_settings import *',_DEFAULTS)
  15. _overrides=_DEFAULTS.copy()
  16. try:
  17. rl_exec('from reportlab.local_rl_settings import *',_overrides)
  18. _DEFAULTS.update(_overrides)
  19. except ImportError:
  20. pass
  21. _overrides=_DEFAULTS.copy()
  22. try:
  23. rl_exec('from reportlab_settings import *',_overrides)
  24. _DEFAULTS.update(_overrides)
  25. except ImportError:
  26. _overrides=_DEFAULTS.copy()
  27. try:
  28. try:
  29. fn = os.path.expanduser(os.path.join('~','.reportlab_settings')) #appengine fails with KeyError/ImportError (dev/live)
  30. except (KeyError, ImportError):
  31. fn = None
  32. if fn:
  33. with open(fn,'rb') as f:
  34. rl_exec(f.read(),_overrides)
  35. _DEFAULTS.update(_overrides)
  36. except:
  37. pass
  38. return _DEFAULTS
  39. _DEFAULTS=_defaults_init()
  40. _SAVED = {}
  41. sys_version=None
  42. #this is used to set the options from
  43. def _setOpt(name, value, conv=None):
  44. '''set a module level value from environ/default'''
  45. from os import environ
  46. ename = 'RL_'+name
  47. if ename in environ:
  48. value = environ[ename]
  49. if conv: value = conv(value)
  50. globals()[name] = value
  51. def _startUp():
  52. '''This function allows easy resetting to the global defaults
  53. If the environment contains 'RL_xxx' then we use the value
  54. else we use the given default'''
  55. import os, sys
  56. global sys_version, _unset_
  57. sys_version = sys.version.split()[0] #strip off the other garbage
  58. from reportlab.lib import pagesizes
  59. from reportlab.lib.utils import rl_isdir
  60. if _SAVED=={}:
  61. _unset_ = getattr(sys,'_rl_config__unset_',None)
  62. if _unset_ is None:
  63. class _unset_: pass
  64. sys._rl_config__unset_ = _unset_ = _unset_()
  65. global __all__
  66. A = list(__all__)
  67. for k,v in _DEFAULTS.items():
  68. _SAVED[k] = globals()[k] = v
  69. if k not in __all__:
  70. A.append(k)
  71. __all__ = tuple(A)
  72. #places to search for Type 1 Font files
  73. import reportlab
  74. D = {'REPORTLAB_DIR': os.path.abspath(os.path.dirname(reportlab.__file__)),
  75. 'CWD': os.getcwd(),
  76. 'disk': os.getcwd().split(':')[0],
  77. 'sys_version': sys_version,
  78. 'XDG_DATA_HOME': os.environ.get('XDG_DATA_HOME','~/.local/share'),
  79. }
  80. for k in _SAVED:
  81. if k.endswith('SearchPath'):
  82. P=[]
  83. for p in _SAVED[k]:
  84. d = (p % D).replace('/',os.sep)
  85. if '~' in d:
  86. try:
  87. d = os.path.expanduser(d) #appengine fails with KeyError/ImportError (dev/live)
  88. except (KeyError, ImportError):
  89. continue
  90. if rl_isdir(d): P.append(d)
  91. _setOpt(k,os.pathsep.join(P),lambda x:x.split(os.pathsep))
  92. globals()[k] = list(filter(rl_isdir,globals()[k]))
  93. else:
  94. v = _SAVED[k]
  95. if isinstance(v,(int,float)):
  96. conv = type(v)
  97. elif k=='defaultPageSize':
  98. conv = lambda v,M=pagesizes: getattr(M,v)
  99. elif k in ('trustedHosts','trustedSchemes'):
  100. conv = lambda v: None if v is None else [y for y in [x.strip() for x in v.split(',')] if y] if isinstance(v,str) else v
  101. else: conv = None
  102. _setOpt(k,v,conv)
  103. _registered_resets=[]
  104. def register_reset(func):
  105. '''register a function to be called by rl_config._reset'''
  106. _registered_resets[:] = [x for x in _registered_resets if x()]
  107. L = [x for x in _registered_resets if x() is func]
  108. if L: return
  109. from weakref import ref
  110. _registered_resets.append(ref(func))
  111. def _reset():
  112. '''attempt to reset reportlab and friends'''
  113. _startUp() #our reset
  114. for f in _registered_resets[:]:
  115. c = f()
  116. if c:
  117. c()
  118. else:
  119. _registered_resets.remove(f)
  120. _startUp()