Python: module adesklets.configfile

adesklets.configfile | index configfile.py

`adesklets configfile module.`

Modules
`` |  |
compiler | mmap | re |

Classes
`` |  | __builtin__.dict(__builtin__.object) ConfigFile ConfigImport
class ConfigFile(__builtin__.dict)
`` | `This is a configuration dictionary base class; it takes care of reading and writing the configuration to a file transparently as needed. Thanks to Mike Pirnat <exilejedi@users.sourceforge.net> for the original idea and code this was copied from as well as a number of useful comments and tips. It has to be subclassed, so that cfg_default contains the default option mapping, and __doc__ the comment to be printed out at the beginning of the configuration file. For instance, one could define: class MyConfig(adesklets.ConfigFile): """ This is my configuration file description """ cfg_default = { 'my_one_parameter' : 'some_value' } def __init__(self, id, filename): adesklets. ConfigFile.__init__(self, id, filename) And then call, from somewhere in its code: config = MyConfig(adesklets.get_id(),'/path/to/config_file') From there, all configuration read and write would be taken care of, and the parameter value could be accessed through config['my_one_parameter']. Parameter resolution works this way: - Parameters are first looked up in a textual representation of a dictionnary from the 'config_file' file named id##, ## being the ID number. - All undefined parameters are set to their default values from the cfg_default dictionary - Whenever a parameter is changed using something such as: config['my_one_parameter']='new_value' or anytime a load phase is over, all the parameters initially present in 'config_file' (NOT default ones) are pretty printed back to the file. - In the special case where no id## entry did exist in 'config_file', the dictionary of default values is copied instead. This behavior ensure desklets writers being able to add or remove parameters on upgrade while preserving backward compatibility.`
| Method resolution order: ConfigFile __builtin__.dict __builtin__.object Methods defined here: __init__(self, id, filename) __setitem__(self, key, value) `This was 'overloaded' since we wanted to make sure the configuration file was kept in sync with the live configuration data` Data and other attributes defined here: __dict__ = <dictproxy object> `dictionary for instance variables (if defined)` __weakref__ = <attribute '__weakref__' of 'ConfigFile' objects> `list of weak references to the object (if defined)` cfg_default = {} Methods inherited from __builtin__.dict: __cmp__(...) `x.__cmp__(y) <==> cmp(x,y)` __contains__(...) `D.__contains__(k) -> True if D has a key k, else False` __delitem__(...) `x.__delitem__(y) <==> del x[y]` __eq__(...) `x.__eq__(y) <==> x==y` __ge__(...) `x.__ge__(y) <==> x>=y` __getattribute__(...) `x.__getattribute__('name') <==> x.name` __getitem__(...) `x.__getitem__(y) <==> x[y]` __gt__(...) `x.__gt__(y) <==> x>y` __hash__(...) `x.__hash__() <==> hash(x)` __iter__(...) `x.__iter__() <==> iter(x)` __le__(...) `x.__le__(y) <==> x<=y` __len__(...) `x.__len__() <==> len(x)` __lt__(...) `x.__lt__(y) <==> x<y` __ne__(...) `x.__ne__(y) <==> x!=y` __repr__(...) `x.__repr__() <==> repr(x)` clear(...) `D.clear() -> None. Remove all items from D.` copy(...) `D.copy() -> a shallow copy of D` get(...) `D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.` has_key(...) `D.has_key(k) -> True if D has a key k, else False` items(...) `D.items() -> list of D's (key, value) pairs, as 2-tuples` iteritems(...) `D.iteritems() -> an iterator over the (key, value) items of D` iterkeys(...) `D.iterkeys() -> an iterator over the keys of D` itervalues(...) `D.itervalues() -> an iterator over the values of D` keys(...) `D.keys() -> list of D's keys` pop(...) `D.pop(k[,d]) -> v, remove specified key and return the corresponding value If key is not found, d is returned if given, otherwise KeyError is raised` popitem(...) `D.popitem() -> (k, v), remove and return some (key, value) pair as a 2-tuple; but raise KeyError if D is empty` setdefault(...) `D.setdefault(k[,d]) -> D. get(k,d), also set D[k]=d if k not in D` update(...) `D.update(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k] (if E has keys else: for (k, v) in E: D[k] = v) then: for k in F: D[k] = F[k]` values(...) `D.values() -> list of D's values` Data and other attributes inherited from __builtin__.dict: __new__ = <built-in method __new__ of type object> `T.__new__(S, ...) -> a new object with type S, a subtype of T` fromkeys = <built-in method fromkeys of type object> `dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v. v defaults to None.`
class ConfigImport
`` | `This class makes use of the AST (Abstract Syntax Tree) generated by the compiler package parsing an arbitrary string of python code to 'walk' through it until it finishes retrieving all the global variables completely determined at compile time. For instance, one could call: ConfigImport.load('i=1; j={2:True,3:False}') To get back, as a dictionary: {'i':1, 'j': {2:True,3:False}} This was written as a safe alternative to calls such as: exec file('some_File','r') in some_dict It is indeed much safer, since no arbitrary code ever get executed (only parsed), and variadic symbols (i.e everything that is not a known constant at compile time) are all discarded.`
| Data and other attributes defined here: load = <adesklets.configfile._StaticConfigImport instance>

Functions
`` |  | lockf(...) `lockf (fd, operation, length=0, start=0, whence=0) This is essentially a wrapper around the fcntl() locking calls. fd is the file descriptor of the file to lock or unlock, and operation is one of the following values: LOCK_UN - unlock LOCK_SH - acquire a shared lock LOCK_EX - acquire an exclusive lock When operation is LOCK_SH or LOCK_EX, it can also be bit-wise OR'd with LOCK_NB to avoid blocking on lock acquisition. If LOCK_NB is used and the lock cannot be acquired, an IOError will be raised and the exception will have an errno attribute set to EACCES or EAGAIN (depending on the operating system -- for portability, check for either value). length is the number of bytes to lock, with the default meaning to lock to EOF. start is the byte offset, relative to whence, to that the lock starts. whence is as with fileobj.seek(), specifically: 0 - relative to the start of the file (SEEK_SET) 1 - relative to the current buffer position (SEEK_CUR) 2 - relative to the end of the file (SEEK_END)`

Data
`` |  | LOCK_EX = 2 LOCK_UN = 8