[34] | 1 | #============================================================================ |
---|
| 2 | # This library is free software; you can redistribute it and/or |
---|
| 3 | # modify it under the terms of version 2.1 of the GNU Lesser General Public |
---|
| 4 | # License as published by the Free Software Foundation. |
---|
| 5 | # |
---|
| 6 | # This library is distributed in the hope that it will be useful, |
---|
| 7 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 8 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 9 | # Lesser General Public License for more details. |
---|
| 10 | # |
---|
| 11 | # You should have received a copy of the GNU Lesser General Public |
---|
| 12 | # License along with this library; if not, write to the Free Software |
---|
| 13 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 14 | #============================================================================ |
---|
| 15 | # Copyright (C) 2004, 2005 Mike Wray <mike.wray@hp.com> |
---|
| 16 | #============================================================================ |
---|
| 17 | |
---|
| 18 | """Variable definition and help support for Python defconfig files. |
---|
| 19 | """ |
---|
| 20 | |
---|
| 21 | import sys |
---|
| 22 | |
---|
| 23 | class Vars: |
---|
| 24 | """A set of configuration variables. |
---|
| 25 | """ |
---|
| 26 | |
---|
| 27 | def __init__(self, name, help, env): |
---|
| 28 | """Create a variable set. |
---|
| 29 | |
---|
| 30 | name name of the defconfig file |
---|
| 31 | help help flag |
---|
| 32 | env local environment |
---|
| 33 | """ |
---|
| 34 | self.name = name |
---|
| 35 | self.help = help |
---|
| 36 | self.env = env |
---|
| 37 | self.vars = [] |
---|
| 38 | |
---|
| 39 | def var(self, name, use=None, check=None): |
---|
| 40 | """Define a configuration variable. |
---|
| 41 | If provided, the check function will be called as check(var, val) |
---|
| 42 | where var is the variable name and val is its value (string). |
---|
| 43 | It should return a new value for the variable, or raise ValueError if |
---|
| 44 | the value is not acceptable. |
---|
| 45 | |
---|
| 46 | name variable name |
---|
| 47 | use variable usage string |
---|
| 48 | check variable check function |
---|
| 49 | """ |
---|
| 50 | self.vars.append(Var(name, use, check)) |
---|
| 51 | |
---|
| 52 | def check(self): |
---|
| 53 | """Execute the variable checks or print help, depending on the value |
---|
| 54 | of the help flag passed to the constructor. |
---|
| 55 | """ |
---|
| 56 | if self.help: |
---|
| 57 | self.doHelp() |
---|
| 58 | else: |
---|
| 59 | for v in self.vars: |
---|
| 60 | v.doCheck(self.env) |
---|
| 61 | |
---|
| 62 | def doHelp(self, out=sys.stderr): |
---|
| 63 | """Print help for the variables. |
---|
| 64 | """ |
---|
| 65 | if self.vars: |
---|
| 66 | print >>out, "\nConfiguration variables for %s:\n" % self.name |
---|
| 67 | for v in self.vars: |
---|
| 68 | v.doHelp(out) |
---|
| 69 | print >>out |
---|
| 70 | |
---|
| 71 | class Var: |
---|
| 72 | """A single variable. |
---|
| 73 | """ |
---|
| 74 | |
---|
| 75 | def __init__(self, name, use, check): |
---|
| 76 | """Create a variable. |
---|
| 77 | |
---|
| 78 | name variable name |
---|
| 79 | use variable use string |
---|
| 80 | check variable value check function |
---|
| 81 | """ |
---|
| 82 | self.name = name |
---|
| 83 | self.use = use or '' |
---|
| 84 | self.check = check |
---|
| 85 | |
---|
| 86 | def doCheck(self, env): |
---|
| 87 | """Execute the check and set the variable to the new value. |
---|
| 88 | """ |
---|
| 89 | if not self.check: return |
---|
| 90 | try: |
---|
| 91 | env[self.name] = self.check(self.name, env.get(self.name)) |
---|
| 92 | except StandardError, ex: |
---|
| 93 | raise sys.exc_type, self.name + " - " + str(ex) |
---|
| 94 | |
---|
| 95 | def doHelp(self, out): |
---|
| 96 | """Print help for the variable. |
---|
| 97 | """ |
---|
| 98 | print >>out, "%-12s" % self.name, self.use |
---|
| 99 | |
---|
| 100 | |
---|