input validation in python using Schema
schema = Schema(
{
'rovi_data_dir':
And(
And(
os.path.exists,
os.path.isdir,
lambda s: os.access(s, os.R_OK),
error="The path to rovi data '%s' should be an existing directory and be readable. Please check" % arguments['<rovi_data_dir>']
),
And(
lambda s: os.path.exists(os.path.join(os.path.realpath(s), 'bound')),
error="Expected the directory 'bound' to exist under the rovi data directory: '%s'. Please check!" % arguments['<rovi_data_dir>'],
),
And(
lambda s: os.path.exists(os.path.join(os.path.realpath(s), 'unbound')),
error="Expected the directory 'unbound' to exist under the rovi data directory : '%s'. Please check!" % arguments['<rovi_data_dir>'],
)
),
'--schema-dir':
And(
And(
os.path.exists,
os.path.isdir,
lambda s: os.access(s, os.R_OK),
error="The path containing the rovi db schema sql '%s' should be an existing directory and be readable. Please check" % arguments['--schema-dir']
),
And(
lambda s: os.path.exists(os.path.join(os.path.realpath(s), 'Rovi 2.0 Bound US')),
error="Expected the directory 'Rovi 2.0 Bound US' to exist under the schema directory: '%s'. Please check!" % arguments['--schema-dir'],
),
And(
lambda s: os.path.exists(os.path.join(os.path.realpath(s), 'Rovi 2.0 Unbound')),
error="Expected the directory 'Rovi 2.0 Unbound' to exist under the schema directory : '%s'. Please check!" % arguments['--schema-dir'],
)
),
'--table-load-order':
And(
os.path.exists,
os.path.isfile,
error="The path to the file, '%s', doesn't exist, or doesn't point to a readable file. Please check! " % arguments['--table-load-order']
)
})
try:
args = schema.validate({
'--table-load-order': arguments['--table-load-order'],
'--schema-dir': arguments['--schema-dir'],
'rovi_data_dir': arguments['<rovi_data_dir>']
})
# The '--output' directory applies only when generating the sqlite db, so
# if sqlite != True, then don't check anything else
if arguments['sqlite']:
Schema(
{
'--output':
And(
And(os.path.exists,
os.path.isdir,
lambda s: os.access(s, os.W_OK),
error="The path to the database output, '%s', doesn't exist, or doesn't point to a writeable directory. Please check! " % arguments['--output']
),
#Or the directory should be creatable...(the existing root of the specified directory tree should be writeable)
And(lambda s: os.access(env.find_existing_root_dir(s), os.W_OK),
error="The path to the database output, '%s', has to be created, but the current process doesn't have permissions to create the directory tree. Please check!" % arguments['--output']
)
)
}
).validate({'--output': arguments['--output']})
except SchemaError as schema_error:
exit(schema_error)