1
2
3
4
5
6
7
8
9
10
11
12
13 """
14 Some libraries for python programmers.
15
16 Common description.
17 ===================
18
19 XPyLIb library commonly configured throw configuration file. It's sitax see below.
20
21 Root package XPyLIB do main initialization and provide some useful things.
22 The B{dir_...} variables contanes path to place that they named. It more better use
23 this variables then calculate path youself.
24 Also you can obtain needed path by using function L{dir_get}().
25
26 User configurations, logs and other things are plased in user home "~/.xpylib/" directory.
27
28 X{XPyLIB compact distributing}.
29 ===============================
30
31 You can use/distribute XPyLIB package from XPyLIB library in compact form.
32 For this you need compress and move all needed files (".py" or ".pyc" or ".pyo"),
33 exclude "__init__.py[co]",
34 in "XPyLIB/XPyLIB" folder into "xpylib.zip" zip archive in this folder.
35 In initialization XPyLIB package try find "xlogging.py[co]" and if not find
36 then add to C{__path__} "xpylib.zip" path.
37
38 X{XPyLIB configuration file}.
39 =============================
40
41 XPyLIB package auto configures from "XPyLIB/config/xpylib.cfg" file.
42 This file may have next structure:
43
44 - B{[Loggers]} - Loggers section:
45
46 - As options may be used any names and it's values must be
47 path to loggers configuration files (see L{xlogging}). There
48 are four preconfigured files for XPyLIB logging:
49
50 - B{loggers.cfg} - used for release logging - errors, warnings.
51 - B{loggers_no.cfg} - no logging output will be doing.
52 - B{loggers_trace.cfg} - logging only trace output, useful
53 for prerelease version, beta, for example.
54 - B{loggers_dbg.cfg} - logging debug and debugtrace output,
55 useful for debuging and development.
56
57 - B{[Attributes]} - Attributes for XPyLIB package:
58
59 - This attributes will be readed and assign to XPyLIB package.
60
61 All logging output from XPyLIB library saved into "XPyLIB/logs" folder
62 if not set other. If in library occurs error then you may look logs and
63 if they contains info may send it to author in zipped format.
64
65
66 @var _mode: Mode for folder creation.
67 @type _mode: int {0755}
68
69 @var __version__: XPyLIB library version in format "main.minor.svn".
70 @type __version__: str
71
72 @var dir_root: Absolute path to XPyLIB library.
73 @type dir_root: str
74
75 @var dir_rootuser: Absolute path to ".xpylib" folder in user directory. Used for
76 user configuration, logs and etc.
77 @type dir_rootuser: str
78
79 @var dir_logs: Absolute path to user ".xpylib/logs" folder.
80 @type dir_logs: str
81
82 @var dir_config: Absolute path to XPyLIB library "config" folder.
83 @type dir_config: str
84
85 @var dir_configuser: Absolute path to user configuration ".xpylib/config" folder.
86 @type dir_configuser: str
87
88 @var dir_xpylib: Absolute path to "XPyLIB" folder (default same as L{dir_root}).
89 @type dir_xpylib: str
90
91 @var log_levelbodychar: Character that fill indentation spaces.
92 @type log_levelbodychar: str
93 @var log_levelendchar: Character that insert before string output but after
94 level indentation.
95 @type log_levelendchar: str
96 @var log_levelwidth: Number of indentation characters in one indentation level.
97 @type log_levelwidth: int
98 @var log_levelerr: Error message then level indentation is invalid thrn return from
99 wrapped function.
100 @type log_levelerr: int
101 @var log_descfuncall: Function call description.
102 @type log_descfuncall: str
103 @var log_descfunret: Function return description.
104 @type log_descfunret: str
105
106 """
107
108 import os
109 import sys
110 import ConfigParser
111
112 _mode = 755
113
114
115
116
117 _v = _o = None
118
119
120
121
122
123 __version__ = '0.1.1'
124
125 dir_root = os.path.dirname(os.path.abspath(__file__))
126
127 dir_rootuser = os.path.join(os.path.expanduser("~"), ".xpylib")
128 if not os.path.exists(dir_rootuser):
129 try:
130 os.makedirs(dir_rootuser, _mode)
131 except:
132 dir_rootuser = ''
133 sys.stderr.write('XPyLIB: error - can not obtane user home directory.')
134
135 -def dir_get(dirpath='', user=True, create=False):
136 """
137 Return quering path.
138
139 @param dirpath: Relative quering directory path.
140 @type dirpath: str = '' or list of str ['']
141 @param user: If true then return path to dir in user home dir "~/.xpylib/"
142 otherwise in XPyLIB directory "/XPyLIB/".
143 @type user: bool = True
144 @param create: If True and path not exists then create directory recursively.
145 If false then do nothing. Create new directory available only for user home dir
146 (parameter L{user}=True ).
147 @type create: bool = False
148 @return: Absolute path to queried folder or empty string '' if no such folder or
149 can't create or error occurs.
150 """
151 if user:
152 p = dir_rootuser
153 else:
154 p = dir_root
155 create = False
156 if type(dirpath) == str:
157 dirpath = [dirpath]
158 p = os.path.join(p, *dirpath)
159 if not os.path.exists(p):
160 if create:
161 try:
162 os.makedirs(p, _mode)
163 except:
164 p = ''
165 else:
166 p = ''
167
168 return p
169
170 dir_logs = dir_get('logs', True, True)
171
172 dir_config = dir_get('config', False, False)
173
174 dir_configuser = dir_get('config', True, True)
175
176 dir_xpylib = dir_root
177
178
179 _v = os.path.join(dir_xpylib, 'xlogging.py')
180 if not any([ os.path.exists(_v + _o) for _o in ('', 'c', 'o') ]):
181 __path__.append( os.path.join(dir_xpylib, 'xpylib.zip') )
182
183
184
185 import xlogging
186
187
188
189
190 _cp = ConfigParser.SafeConfigParser()
191 _cp.read([os.path.join(dir_config, 'xpylib.cfg'), os.path.join(dir_configuser, 'xpylib.cfg')])
192
193
194
195 log_levelbodychar = '.'
196 log_levelendchar = '>'
197 log_levelwidth = 2
198 log_levelerr = '*** LEVEL ERROR = '
199 log_descfuncall = 'CALL '
200 log_descfunret = 'RET from '
201
202
203
204
205
206
207 if _cp.has_section('Loggers'):
208 for _o in _cp.options('Loggers'):
209
210 xlogging.applyConfigFile(os.path.join(dir_config, _cp.get('Loggers',_o)))
211 xlogging.applyConfigFile(os.path.join(dir_configuser, _cp.get('Loggers',_o)))
212
213
214
215
216
217
218 if _cp.has_section('Attributes'):
219 for _o in _cp.options('Attributes'):
220 _v=_cp.get('Attributes',_o).strip()
221 if len(_v)>0:
222 if _v[0] == '\'':
223
224 _v=_v[1:len(_v)-1]
225 else:
226
227 try:
228 _v=int(_v)
229 except:
230 _v=0
231 try:
232 setattr(sys.modules[__name__], _o, _v)
233 except:
234 pass
235
236
237
238
239 del _v
240 del _cp
241 del _o
242 del ConfigParser
243 del sys
244