/usr/share/pyshared/piston_mini_client/validators.py is in python-piston-mini-client 0.7.2-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | # -*- coding: utf-8 -*-
# Copyright 2010-2012 Canonical Ltd. This software is licensed under the
# GNU Lesser General Public License version 3 (see the file LICENSE).
"""These decorators can be applied to your ``PistonAPI`` methods to control
how your method arguments are validated."""
import re
from functools import wraps
from auth import BasicAuthorizer, OAuthAuthorizer
class ValidationException(Exception):
pass
def validate_pattern(varname, pattern, required=True):
"""Validate argument ``varname`` against regex pattern ``pattern``.
The provided argument for ``varname`` will need to inherit from
``basestring``.
If ``required`` is ``False`` then the argument can be omitted entirely.
Your method signature will need to provide a default value in this case.
"""
if not pattern.endswith('$'):
pattern = pattern + '$'
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
if varname in kwargs:
if not isinstance(kwargs[varname], basestring):
raise ValidationException(
"Argument '%s' must be a string" % varname)
if not re.match(pattern, kwargs[varname]):
raise ValidationException(
"Argument '%s' must match pattern '%s'" % (varname,
pattern))
elif required:
raise ValidationException(
"Required named argument '%s' missing" % varname)
return func(*args, **kwargs)
return wrapper
return decorator
def validate(varname, cls, required=True):
"""Check that argument ``varname`` is of class ``cls``.
If ``required`` is ``False`` then the argument can be omitted entirely.
Your method signature will need to provide a default value in this case.
"""
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
if varname in kwargs:
if not isinstance(kwargs[varname], cls):
raise ValidationException(
"Argument '%s' must be a %s" % (varname, cls))
elif required:
raise ValidationException(
"Required named argument '%s' missing" % varname)
return func(*args, **kwargs)
return wrapper
return decorator
def validate_integer(varname, min=None, max=None, required=True):
"""Check that argument ``varname`` is between ``min`` and ``max``.
The provided argument for ``varname`` will need to be of type ``int``.
If ``required`` is ``False`` then the argument can be omitted entirely.
Your method signature will need to provide a default value in this case.
"""
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
if varname in kwargs:
var = kwargs[varname]
if not isinstance(var, int):
raise ValidationException(
"Argument '%s' must be an int" % varname)
elif min is not None and var < min:
raise ValidationException(
"Argument '%s' must be at least %s" % (varname, min))
elif max is not None and var > max:
raise ValidationException(
"Argument '%s' must be at most %s" % (varname, max))
elif required:
raise ValidationException(
"Required named argument '%s' missing" % varname)
return func(*args, **kwargs)
return wrapper
return decorator
def oauth_protected(func):
"""Only allow a method to be called with an ``OAuthAuthorizer`` available.
To be able to call the method you've decorated you'll need to instantiate
the ``PistonAPI`` providing a valid ``OAuthAuthorizer``.
"""
@wraps(func)
def wrapper(self, *args, **kwargs):
if not hasattr(self, '_auth') or self._auth is None:
raise ValidationException("This method is OAuth protected. "
"Pass in an 'auth' argument to the constructor.")
if not isinstance(self._auth, OAuthAuthorizer):
raise ValidationException("self.auth must be an OAuthAuthorizer.")
return func(self, *args, **kwargs)
return wrapper
def basic_protected(func):
"""Only allow a method to be called with an ``BasicAuthorizer`` available.
To be able to call the method you've decorated you'll need to instantiate
the ``PistonAPI`` providing a valid ``BasicAuthorizer``.
"""
@wraps(func)
def wrapper(self, *args, **kwargs):
if not hasattr(self, '_auth') or self._auth is None:
raise ValidationException("This method uses Basic auth. "
"Pass in an 'auth' argument to the constructor.")
if not isinstance(self._auth, BasicAuthorizer):
raise ValidationException("self.auth must be a BasicAuthorizer.")
return func(self, *args, **kwargs)
return wrapper
|