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

« back to all changes in this revision

Viewing changes to ubuntuone/storageprotocol/dircontent.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:
4
4
#
5
5
# Copyright 2009 Canonical Ltd.
6
6
#
7
 
# This program is free software: you can redistribute it and/or modify it 
 
7
# This program is free software: you can redistribute it and/or modify it
8
8
# under the terms of the GNU Affero General Public License version 3,
9
9
# as published by the Free Software Foundation.
10
10
#
11
 
# This program is distributed in the hope that it will be useful, but 
12
 
# WITHOUT ANY WARRANTY; without even the implied warranties of 
13
 
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
 
11
# This program is distributed in the hope that it will be useful, but
 
12
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
13
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
14
14
# PURPOSE.  See the GNU Affero General Public License for more details.
15
15
#
16
16
# You should have received a copy of the GNU Affero General Public License
21
21
"""
22
22
 
23
23
import re
24
 
# DIRECTORY, FILE, and SYMLINK are for re-export
25
 
# pylint: disable-msg=W0611
26
 
# pylint: disable=W0611
27
 
from ubuntuone.storageprotocol.dircontent_pb2 import \
28
 
    DirectoryContent, DIRECTORY, FILE, SYMLINK
 
24
from ubuntuone.storageprotocol.dircontent_pb2 import DirectoryContent
29
25
 
30
26
ILLEGAL_FILENAMES = [u".", u".."]
31
27
ILLEGAL_FILENAME_CHARS_RE_SOURCE = r'[\000/]'
32
28
ILLEGAL_FILENAME_CHARS_RE = re.compile(ILLEGAL_FILENAME_CHARS_RE_SOURCE)
33
29
 
 
30
 
34
31
class InvalidFilename(Exception):
35
32
    """Raised when a filename is invalid."""
36
33
 
 
34
 
37
35
def validate_filename(filename):
38
36
    """Validates a filename for use with the storage service; raises
39
37
    InvalidFilename if the filename is invalid."""
44
42
    if ILLEGAL_FILENAME_CHARS_RE.search(filename) is not None:
45
43
        raise InvalidFilename(u"%s contains illegal characters" % (filename,))
46
44
 
 
45
 
47
46
def normalize_filename(filename):
48
47
    """Takes a unicode filename and returns the normalized form,
49
48
    raising InvalidFilename if the filename is invalid for use with
51
50
    validate_filename(filename)
52
51
    return filename
53
52
 
 
53
 
54
54
def parse_dir_content(stream):
55
55
    """Unserializes directory content from a stream.
56
56
 
67
67
        yield DirEntry(name=entry.name, node_type=entry.node_type,
68
68
                       uuid=entry.node)
69
69
 
 
70
 
70
71
def by_utf8_name(entry_a, entry_b):
71
72
    """Compares two entries by the UTF-8 form of their names."""
72
73
    return cmp(entry_a.utf8_name, entry_b.utf8_name)
73
74
 
 
75
 
74
76
def write_dir_content(entries, stream):
75
77
    """Takes a sequence of DirEntry objects, sorts them, and writes
76
78
    the corresponding serialized directory content to the given stream.
83
85
    for chunk in yield_presorted_dir_content(sorted_entries):
84
86
        stream.write(chunk)
85
87
 
 
88
 
86
89
def yield_presorted_dir_content(sorted_entries):
87
90
    """Takes a presorted sequence of DirEntry objects and yields each
88
91
    chunks of serialized content.