Bring some more sanity into quickstart validators.

This commit is contained in:
Georg Brandl 2009-04-29 12:26:38 +02:00
parent ab07eba895
commit ba0039cc4d
2 changed files with 37 additions and 29 deletions

View File

@ -464,30 +464,39 @@ def mkdir_p(dir):
os.makedirs(dir)
class ValidationError(Exception):
"""Raised for validation errors."""
def is_path(x):
"""Please enter a valid path name."""
return path.isdir(x) or not path.exists(x)
if path.exists(x) and not path.isdir(x):
raise ValidationError("Please enter a valid path name.")
return x
def nonempty(x):
"""Please enter some text."""
return len(x)
if not x:
raise ValidationError("Please enter some text.")
return x
def choice(*l):
def val(x):
return x in l
val.__doc__ = 'Please enter one of %s.' % ', '.join(l)
if x not in l:
raise ValidationError('Please enter one of %s.' % ', '.join(l))
return x
return val
def boolean(x):
"""Please enter either 'y' or 'n'."""
return x.upper() in ('Y', 'YES', 'N', 'NO')
if x.upper() not in ('Y', 'YES', 'N', 'NO'):
raise ValidationError("Please enter either 'y' or 'n'.")
return x.upper() in ('Y', 'YES')
def suffix(x):
"""Please enter a file suffix, e.g. '.rst' or '.txt'."""
return x[0:1] == '.' and len(x) > 1
if not (x[0:1] == '.' and len(x) > 1):
raise ValidationError("Please enter a file suffix, "
"e.g. '.rst' or '.txt'.")
return x
def ok(x):
return True
return x
def do_prompt(d, key, text, default=None, validator=nonempty):
@ -510,8 +519,10 @@ def do_prompt(d, key, text, default=None, validator=nonempty):
x = x.decode('utf-8')
except UnicodeDecodeError:
x = x.decode('latin1')
if validator and not validator(x):
print red('* ' + validator.__doc__)
try:
x = validator(x)
except ValidationError, err:
print red('* ' + str(err))
continue
break
d[key] = x
@ -617,7 +628,7 @@ directly.'''
repr('sphinx.ext.' + name)
for name in ('autodoc', 'doctest', 'intersphinx', 'todo', 'coverage',
'pngmath', 'jsmath', 'ifconfig')
if d['ext_' + name].upper() in ('Y', 'YES'))
if d['ext_' + name])
d['copyright'] = time.strftime('%Y') + ', ' + d['author']
d['author_texescaped'] = unicode(d['author']).\
translate(texescape.tex_escape_map)
@ -634,11 +645,10 @@ directly.'''
if not path.isdir(d['path']):
mkdir_p(d['path'])
separate = d['sep'].upper() in ('Y', 'YES')
srcdir = separate and path.join(d['path'], 'source') or d['path']
srcdir = d['sep'] and path.join(d['path'], 'source') or d['path']
mkdir_p(srcdir)
if separate:
if d['sep']:
builddir = path.join(d['path'], 'build')
d['exclude_trees'] = ''
else:
@ -649,7 +659,7 @@ directly.'''
mkdir_p(path.join(srcdir, d['dot'] + 'static'))
conf_text = QUICKSTART_CONF % d
if d['ext_intersphinx'].upper() in ('Y', 'YES'):
if d['ext_intersphinx']:
conf_text += INTERSPHINX_CONFIG
f = open(path.join(srcdir, 'conf.py'), 'w')
@ -661,18 +671,16 @@ directly.'''
f.write((MASTER_FILE % d).encode('utf-8'))
f.close()
create_makefile = d['makefile'].upper() in ('Y', 'YES')
if create_makefile:
d['rsrcdir'] = separate and 'source' or '.'
d['rbuilddir'] = separate and 'build' or d['dot'] + 'build'
if d['makefile']:
d['rsrcdir'] = d['sep'] and 'source' or '.'
d['rbuilddir'] = d['sep'] and 'build' or d['dot'] + 'build'
f = open(path.join(d['path'], 'Makefile'), 'w')
f.write((MAKEFILE % d).encode('utf-8'))
f.close()
create_batch = d['batchfile'].upper() in ('Y', 'YES')
if create_batch:
d['rsrcdir'] = separate and 'source' or '.'
d['rbuilddir'] = separate and 'build' or d['dot'] + 'build'
if d['batchfile']:
d['rsrcdir'] = d['sep'] and 'source' or '.'
d['rbuilddir'] = d['sep'] and 'build' or d['dot'] + 'build'
f = open(path.join(d['path'], 'make.bat'), 'w')
f.write((BATCHFILE % d).encode('utf-8'))
f.close()
@ -681,7 +689,7 @@ directly.'''
print bold('Finished: An initial directory structure has been created.')
print '''
You should now populate your master file %s and create other documentation
source files. ''' % masterfile + ((create_makefile or create_batch) and '''\
source files. ''' % masterfile + ((d['makefile'] or d['batchfile']) and '''\
Use the Makefile to build the docs, like so:
make builder
''' or '''\

View File

@ -65,9 +65,9 @@ def test_do_prompt():
qs.do_prompt(d, 'k2', 'Q2')
assert d['k2'] == 'v2'
qs.do_prompt(d, 'k4', 'Q4', validator=qs.boolean)
assert d['k4'] == 'yes'
assert d['k4'] is True
qs.do_prompt(d, 'k5', 'Q5', validator=qs.boolean)
assert d['k5'] == 'no'
assert d['k5'] is False
raises(AssertionError, qs.do_prompt, d, 'k6', 'Q6', validator=qs.boolean)