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
correct type. Each new option that we support that is not a boolean,
but instead has some extra inputs, i.e. "--encrypt-file foo.txt",
will need some basic safety checks added here.
"""
_sanitised = {}
_unsanitised = []
if args: if args:
for arg in args: for arg in args:
underscored = _underscore(arg)
try: try:
assert underscored in _allowed allowed = _is_allowed(arg)
except AssertionError as ae: except ProtectedOption as po:
logger.warn("Dropping option '%s'..." % underscored) logger.warn("Dropping option '%s'..." % _fix_unsafe(arg))
raise ProtectedOption("Option '%s' not supported." % underscored)
else: else:
logger.msg("Got allowed option '%s'." % underscored) safe = _fix_unsafe(allowed)
_sanitised[underscored] = True logger.msg("Got allowed option '%s'." % safe)
_sanitised[safe] = True
if kwargs: if kwargs:
for key, value in kwargs: for key, value in kwargs:
underscored = _underscore(key)
try: try:
assert underscored in _allowed, \ allowed = _is_allowed(key)
"Option '%s' not supported" % underscored assert isinstance(value, str), "_sanitise(): value not a string"
assert isinstance(value, str), \
"Odd, value is not a string...it should always be."
except AssertionError as ae: except AssertionError as ae:
raise ProtectedOption(ae.message) logger.warn(ae)
except ProtectedOption as po:
logger.warn("Dropping option '%s'..." % _fix_unsafe(value))
else: else:
if key == 'encrypt' or 'encrypt_file' or 'decrypt' \ if key == 'encrypt' or 'encrypt_file' or 'decrypt' or 'decrypt_file' \
or 'decrypt_file' or 'import' or 'verify': or 'import' or 'verify':
## Place checks here:
##
## xxx what other things should we check for? ## xxx what other things should we check for?
_is_file(value) _is_file(value)
_sanitised[underscored] = _fix_unsafe(value) _sanitised[allowed] = _fix_unsafe(value)
return _sanitised
sanitised = _type_check_and_remove_escapes(*args, **kwargs)
return sanitised return sanitised
class GPG(object): class GPG(object):