public
Description: My collection of potentially useful Python functions
Clone URL: git://github.com/cosmin/ob2lib.git
Search Repo:
adding helper functions for creating tables in sqlalchemy and moved 
optparse code to specific module
cosmin (author)
Tue Jul 01 21:33:58 -0700 2008
commit  e7e5514062978cdcad5d4212868b72de3a3e432f
tree    97671c710b6266c0254e8fe50cf510e10d3c8b97
parent  5f83439dc73d54ca2820e8df582411a649ac4279
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
 
...
 
 
1
2
3
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
0
@@ -1,55 +1,5 @@
0
-import os
0
-
0
 ## The purpose of this module is to create functions that allows for better syntax
0
 ## when performing some common and repetitive tasks. For example adding options
0
 ## to optparse
0
 
0
-def make_option_maker(parser, cmdline_prefix="", env_prefix="", dest_prefix=""):
0
- """Return a function that can be used to add_option with saner syntax.
0
-
0
- Arguments:
0
- parser: the parser object that will be used by the returned function
0
- cmdline_prefix: will be used together with the name to build the command
0
- line option. a suffix of -- and a prefix of - will be added so you
0
- do not need to include those (--cmdline_prefix-<name>)
0
- env_prefix: will be used together with the name to specify the environment
0
- variable from which the option will default (ENV_PREFIX_<NAME>)
0
- dest_prefix: will be used together with name to specify the variable name
0
- in which the value of the option will be stored
0
-
0
- Example:
0
- parser = OptionParser()
0
- omaker = make_option_maker(parser, dest_prefix = "someprefix",
0
- env_prefix = "SOMEPREFIX",
0
- cmdline_prefix = "someprefix")
0
-
0
- omaker('foo', action="append", help="collect a list of foo")
0
- omaker('bar', help="store bar", default="Blah")
0
- """
0
-
0
- env_prefix = env_prefix.upper()
0
-
0
- if cmdline_prefix and not cmdline_prefix.endswith("-"):
0
- cmdline_prefix += "-"
0
-
0
- if env_prefix and not env_prefix.endswith("_"):
0
- env_prefix += "_"
0
-
0
- if dest_prefix and not dest_prefix.endswith("_"):
0
- dest_prefix += "_"
0
-
0
- def option_maker(name, action = "store", help = "", default = None):
0
- """Use the given name, action and help string to add an option"""
0
-
0
- if default is None:
0
- _default = os.environ.get(env_prefix + name.upper())
0
- else:
0
- _default = default
0
-
0
- parser.add_option("--" + cmdline_prefix + name.lower(),
0
- action = action, dest = dest_prefix + name.lower(),
0
- default= _default,
0
- help = help + " [" + env_prefix + name.upper() + "]")
0
-
0
- return option_maker
0
-
0
+modules = ['optparse', 'sqlalchemy']

Comments

    No one has commented yet.