beeseek-devs team mailing list archive
-
beeseek-devs team
-
Mailing list archive
-
Message #00035
BeeSeek Classroom - config.py
hivelib/config.py is the most commented and documented file. I've
recently updated the DocStrings so it should be an easy start point.
Here I will describe the Python language using the config.py file to
explain some Hive basics too. If you don't understand something, please
tell me what. ;)
Note: I won't describe some code that I consider simple, but if you have
some doubts, ask me the questions!
> #coding=UTF-8
UTF-8 is a very good encoding (my favorite) and is the Linux standard,
so I use it for all the project. To tell the Python interpreter you have
to add #coding=UTF-8 at the first or at the second line.
> import os
This module is for specific OS operation, but I use it only to get
absolute paths.
> import re
Regular expression following the Perl regex rules. Check out the Python
documentation for help with syntax.
> from optparse import OptionParser
A command line option parser. Note that I import only the class
OptionParser and not all the optparse module to keep the instructions
short and save a bit of RAM memory. I don't use this method with os and
sys because they are completely preloaded by the interpreter and I don't
use this method with re because I will use this module a lot in other
files so it is too uncomfortable :)
> class NullFile(file):
This class inherits file but doesn't write hard disks and implements all
file methods. Useful for dynamic file assignment.
For example, if the user doesn't want to have a log file I have to
choose one of three ways:
1. if logfile is not None: logfile.write('something')
2. logfile.write('something')
In the first case I have to use an 'if' instruction that is
uncomfortable. The second class is possible only if logfile is an
instance of NullFile. It is the same of file('/dev/null'), but this is
not compatible for non unix systems (perhaps I will use /dev/null
however).
> def __init__(*args): pass
> def write(*args): pass
> def read(*args): pass
> ...
pass indicates that the object is empty. I can't leave it because Python
use indentations instead of start/end instructions. With this method I
can overwrite all file's attributes and leave the code.
> def __getitem__(self, items):
This is a builtin.
object.__getitem__(123)
is the same of
object[123]
> def __setitem__(self, cat, value):
This is a builtin too.
object.__getitem__(123, 'abc')
is the same of
object[123] = 'abc'
> def init_types(self):
This function initialize types needed to casting. For example, if
"LogFile" option needs a file opened in append mode, it will use file-a.
If the log file is specified, the function for casting will open it, if
possible, else it will use NullFile.
> def parse_args(self, category=None, args=None):
Parses command line arguments and puts the parsed options in the given
category.
> def load(self, load_category=None):
Load the configuration from CONFIG_FILE. load_category is needed because
some Hive plugins may request some options. Plugins are loaded after
getting the plugins directory from the config file so if I don't specify
this parameter, the function will parse all the configuration file and
will throw an error for any unknown option.
Here's an example:
# To use the settings manager, first you have to initialize the class:
config = SettingsManager()
# Then you may create a category:
config['MyCategory'] = {'OptionName': ('DefaultValue', 'Type (see
init_types)')}
# An another way is:
config['MyCategory', 'OptionName'] = 'DefaultValue', 'Type'
# Then you can parse the command line arguments, if you want
config.parse_args()
# And finally all the configuration file:
config.load('Category_for_plugins')
load_plugins()
config.load()
--
Andrea Corbellini <andrea.corbellini@xxxxxxxxxxx>
Blog: http://andreabs.wordpress.com/
Launchpad: https://launchpad.net/~andrea-bs
Attachment:
signature.asc
Description: This is a digitally signed message part