diff options
author | Steven Knight <knight@baldmt.com> | 2004-09-24 14:23:40 (GMT) |
---|---|---|
committer | Steven Knight <knight@baldmt.com> | 2004-09-24 14:23:40 (GMT) |
commit | b5fb7056061b2a3d8ee97b05a4fc1a92beb0f13d (patch) | |
tree | f139aee5ab2c3a82b3fae7bc57ad63a962a5e0dd /src/engine/SCons/Options/PathOption.py | |
parent | db9601646dfa4e7848e239e7a935a0d36d821027 (diff) | |
download | SCons-b5fb7056061b2a3d8ee97b05a4fc1a92beb0f13d.zip SCons-b5fb7056061b2a3d8ee97b05a4fc1a92beb0f13d.tar.gz SCons-b5fb7056061b2a3d8ee97b05a4fc1a92beb0f13d.tar.bz2 |
Add validator functions for PathOption. (Steve Christensen)
Diffstat (limited to 'src/engine/SCons/Options/PathOption.py')
-rw-r--r-- | src/engine/SCons/Options/PathOption.py | 78 |
1 files changed, 54 insertions, 24 deletions
diff --git a/src/engine/SCons/Options/PathOption.py b/src/engine/SCons/Options/PathOption.py index cea4750..f407864 100644 --- a/src/engine/SCons/Options/PathOption.py +++ b/src/engine/SCons/Options/PathOption.py @@ -55,31 +55,61 @@ Usage example: __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__" -__all__ = ('PathOption',) - import os +import os.path import SCons.Errors -def _validator(key, val, env): - """ - """ - # todo: write validator, check for path - if not os.path.exists(val): - raise SCons.Errors.UserError( - 'Path does not exist for option %s: %s' % (key, val)) - - -def PathOption(key, help, default): - # NB: searchfunc is currenty undocumented and unsupported - """ - The input parameters describe a 'path list' option, thus they - are returned with the correct converter and validator appended. The - result is usable for input to opts.Add() . - - A 'package list' option may either be 'all', 'none' or a list of - package names (seperated by space). - """ - return (key, '%s ( /path/to/%s )' % (help, key), default, - _validator, None) - +class _PathOptionClass: + + def PathIsDir(self, key, val, env): + """Validator to check if Path is a directory.""" + if not os.path.isdir(val): + if os.path.isfile(val): + m = 'Directory path for option %s is a file: %s' + else: + m = 'Directory path for option %s does not exist: %s' + raise SCons.Errors.UserError(m % (key, val)) + + def PathIsDirCreate(self, key, val, env): + """Validator to check if Path is a directory, + creating it if it does not exist.""" + if os.path.isfile(val): + m = 'Path for option %s is a file, not a directory: %s' + raise SCons.Errors.UserError(m % (key, val)) + if not os.path.isdir(val): + os.makedirs(val) + + def PathIsFile(self, key, val, env): + """validator to check if Path is a file""" + if not os.path.isfile(val): + if os.path.isdir(val): + m = 'File path for option %s is a directory: %s' + else: + m = 'File path for option %s does not exist: %s' + raise SCons.Errors.UserError(m % (key, val)) + + def PathExists(self, key, val, env): + """validator to check if Path exists""" + if not os.path.exists(val): + m = 'Path for option %s does not exist: %s' + raise SCons.Errors.UserError(m % (key, val)) + + def __call__(self, key, help, default, validator=None): + # NB: searchfunc is currenty undocumented and unsupported + """ + The input parameters describe a 'path list' option, thus they + are returned with the correct converter and validator appended. The + result is usable for input to opts.Add() . + + A 'package list' option may either be 'all', 'none' or a list of + package names (seperated by space). + + validator is a validator, see this file for examples + """ + if validator is None: + validator = self.PathExists + return (key, '%s ( /path/to/%s )' % (help, key), default, + validator, None) + +PathOption = _PathOptionClass() |