Package eggbasket :: Module validators
[hide private]

Source Code for Module eggbasket.validators

 1  # -*- coding: UTF-8 -*- 
 2   
 3  import os 
 4   
 5  from os.path import exists, join 
 6   
 7  import turbogears as tg 
 8   
 9  from eggbasket.util import is_package_dir, is_package_file 
10   
11 -class ValidPackage(tg.validators.FancyValidator):
12 """Validator checking if a package name refers to a valid package directory. 13 """ 14 15 messages = { 16 'notFound': u'Package not found: %(package)s' 17 } 18
19 - def _to_python(self, value, state):
20 pkg_root = tg.config.get('eggbasket.package_root', os.getcwd()) 21 if not is_package_dir(join(pkg_root, value)): 22 # WART: to handle broken package names, 23 # we also try the given package name in lowercase 24 if is_package_dir(join(pkg_root, value.lower())): 25 value = value.lower() 26 else: 27 raise tg.validators.Invalid( 28 self.message('notFound', state, package=value), value, state) 29 return value
30
31 -class ValidPackageFile(tg.validators.FancyValidator):
32 """Validator checking if a file name refers to a valid package file. 33 34 Must be used as a chained validator. 35 """ 36 37 messages = { 38 'notFound': u'Package file not found: %(filename)s' 39 } 40
41 - def validate_python(self, value, state):
42 filename = value['filename'] 43 pkg_root = tg.config.get('eggbasket.package_root', os.getcwd()) 44 pkg_dir = join(pkg_root, value['package']) 45 pkg_file = join(pkg_dir, filename) 46 if not exists(pkg_file) or not is_package_file(pkg_file): 47 message = self.message('notFound', state, filename=filename) 48 errors = dict(filename=message) 49 raise tg.validators.Invalid(message, value, state, 50 error_dict=errors)
51
52 -class PackageFileSchema(tg.validators.Schema):
53 """Schema for package file spec consisting of package and file name.""" 54 55 package = ValidPackage 56 filename = tg.validators.UnicodeString 57 58 chained_validators = [ValidPackageFile]
59 60 __all__ = [ 61 'PackageFileSchema' 62 'ValidPackage', 63 'ValidPackageFile' 64 ] 65