~ubuntuone-control-tower/ubuntuone-storage-protocol/stable-1-6

« back to all changes in this revision

Viewing changes to ubuntuone/storageprotocol/validators.py

  • Committer: Tarmac
  • Author(s): Rodney Dawes
  • Date: 2010-11-16 15:10:48 UTC
  • mfrom: (121.1.4 use-packages)
  • Revision ID: tarmac-20101116151048-b0e20j7lorb4yhe1
Switch to using packaged mocker and ubuntuone-dev-tools
Use pyflakes with u1lint and also run pep8
Fix a lot of pylint/pyflakes/pep8 errors

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
from google.protobuf.message import Message as _PBMessage
8
8
from google.protobuf.internal.containers import BaseContainer as _PBContainer
9
9
 
 
10
 
10
11
def is_valid_node(node_id):
11
12
    """
12
13
    A node id is a hex UUID.
15
16
        return str(UUID(node_id)) == node_id
16
17
    except StandardError:
17
18
        return False
18
 
is_valid_parent_node = is_valid_node
19
 
is_valid_new_parent_node = is_valid_node
20
 
is_valid_subtree = is_valid_node
 
19
 
21
20
 
22
21
def is_valid_crc32(crc32):
23
22
    """
25
24
    """
26
25
    return int(crc32) >= 0
27
26
 
 
27
 
28
28
def is_valid_share(share_id):
29
29
    """
30
30
    A share id is either the empty string, or a node id.
31
31
    """
32
32
    return share_id == '' or is_valid_node(share_id)
33
 
is_valid_share_id = is_valid_share
 
33
 
34
34
 
35
35
def is_valid_sha1(sha1):
36
36
    """Validate 'sha1'.
40
40
    """
41
41
    return bool(re.match(r'sha1:[0-9a-z]{40}$', sha1))
42
42
 
 
43
 
43
44
def is_valid_hash(a_hash):
44
45
    """Validate 'a_hash'.
45
46
 
53
54
               is_valid_sha1(a_hash)
54
55
    return is_valid
55
56
 
 
57
 
56
58
def validate_message(message):
57
59
    """
58
60
    Recursively validate a message's fields
59
61
    """
60
62
    # we will import ourselves
61
 
    # pylint: disable-msg=W0406
62
63
    # pylint: disable=W0406
63
64
 
64
65
    is_invalid = []
65
 
    from ubuntuone.storageprotocol import validators # this is us!
 
66
    from ubuntuone.storageprotocol import validators  # this is us!
66
67
    for descriptor, submsg in message.ListFields():
67
68
        if isinstance(submsg, _PBContainer):
68
69
            # containers are iterables that have messages in them
74
75
        else:
75
76
            # we got down to the actual fields! yay
76
77
            validator = getattr(validators,
77
 
                                "is_valid_"+descriptor.name, None)
 
78
                                "is_valid_" + descriptor.name, None)
78
79
            if validator is not None:
79
80
                if not validator(submsg):
80
81
                    is_invalid.append("Invalid %s: %r"
81
82
                                      % (descriptor.name, submsg))
82
83
    return is_invalid
 
84
 
 
85
 
 
86
# these are valid, pylint: disable=C0103
 
87
is_valid_parent_node = is_valid_node
 
88
is_valid_new_parent_node = is_valid_node
 
89
is_valid_subtree = is_valid_node
 
90
is_valid_share_id = is_valid_share
 
91
# pylint: enable=C0103