1 """
2 A module for managing session-wide global data.
3
4 This module provides the ``session`` object, storing variables on a per-thread
5 basis: each thread has its own values for the session variables.
6
7 A set of session variables are predefined and have a default value (the dict
8 ``get_session_defaults`` returns them in a dict). User-defined session
9 variables can be used but must be prefixed with "x_". Note that deleting a
10 predefined session variable does not actually deletes it but restores it to its
11 default value.
12
13 All errors (trying to read or delete an non-existant session variable, or
14 trying to set an invalid one) raise AttributeError.
15
16 E.g.::
17 from advene.util.session import session
18 session.package = some_package
19 session.user = "pchampin"
20 session.x_info = "some additional info"
21 # those variables are only set in the scope of the current thread
22 """
23
24 import os
25 import sys
26 from threading import local
27 from shutil import rmtree
28
29 _session_defaults = {"package": None, "user": os.getlogin()}
30
33
38
40 if not hasattr(self, name) and name[:2] != "x_":
41 raise AttributeError("%s is not a session variable "
42 "(use 'x_%s' instead)" % (name, name))
43 self.__dict__[name] = value
44
50
52 return [ n for n in dir(self) if n[0] != "_" ]
53
54 tempdir_list = []
55
57 """Remove the temp. directories used during the session.
58
59 No check is done to see wether it is in use or not. This
60 method is intended to be used at the end of the application,
61 to clean up the mess.
62 """
63 for d in tempdir_list:
64 print "Cleaning up %s" % d
65 if os.path.isdir(d.encode(sys.getfilesystemencoding())):
66 rmtree(d, ignore_errors=True)
67
68 session = _Session()
69