Rewrite function _sanitise() with calls to other new check functions().

* Remove function _type_check_and_remove_escapes().
feature/documentation-builds-dirhtml
Isis Lovecruft 2013-03-06 15:35:52 +00:00
parent 5f28127b6f
commit abab2c53f2
No known key found for this signature in database
GPG Key ID: A3ADB67A2CDB8B35
1 changed files with 31 additions and 38 deletions

View File

@ -779,46 +779,39 @@ def _sanitise(*args, **kwargs):
GnuPG process. GnuPG process.
@param kwargs: (optional) The arguments and their inputs, which will be passed @param kwargs: (optional) The arguments and their inputs, which will be passed
to the GnuPG process. to the GnuPG process.
@ivar sanitised: A dictionary contained the sanitised allowed options.
@return: :ivar:`sanitised`.
""" """
def _type_check_and_remove_escapes(*args, **kwargs): sanitised = {}
"""
Take an arg or the key portion of a kwarg and check that it has the if args:
correct type. Each new option that we support that is not a boolean, for arg in args:
but instead has some extra inputs, i.e. "--encrypt-file foo.txt", try:
will need some basic safety checks added here. allowed = _is_allowed(arg)
""" except ProtectedOption as po:
_sanitised = {} logger.warn("Dropping option '%s'..." % _fix_unsafe(arg))
_unsanitised = [] else:
if args: safe = _fix_unsafe(allowed)
for arg in args: logger.msg("Got allowed option '%s'." % safe)
underscored = _underscore(arg) _sanitised[safe] = True
try: if kwargs:
assert underscored in _allowed for key, value in kwargs:
except AssertionError as ae: try:
logger.warn("Dropping option '%s'..." % underscored) allowed = _is_allowed(key)
raise ProtectedOption("Option '%s' not supported." % underscored) assert isinstance(value, str), "_sanitise(): value not a string"
else: except AssertionError as ae:
logger.msg("Got allowed option '%s'." % underscored) logger.warn(ae)
_sanitised[underscored] = True except ProtectedOption as po:
if kwargs: logger.warn("Dropping option '%s'..." % _fix_unsafe(value))
for key, value in kwargs: else:
underscored = _underscore(key) if key == 'encrypt' or 'encrypt_file' or 'decrypt' or 'decrypt_file' \
try: or 'import' or 'verify':
assert underscored in _allowed, \ ## Place checks here:
"Option '%s' not supported" % underscored ##
assert isinstance(value, str), \ ## xxx what other things should we check for?
"Odd, value is not a string...it should always be." _is_file(value)
except AssertionError as ae: _sanitised[allowed] = _fix_unsafe(value)
raise ProtectedOption(ae.message)
else:
if key == 'encrypt' or 'encrypt_file' or 'decrypt' \
or 'decrypt_file' or 'import' or 'verify':
## xxx what other things should we check for?
_is_file(value)
_sanitised[underscored] = _fix_unsafe(value)
return _sanitised
sanitised = _type_check_and_remove_escapes(*args, **kwargs)
return sanitised return sanitised
class GPG(object): class GPG(object):