Statistics
| Branch: | Tag: | Revision:

root / pithos / backends / dummy_debug.py @ 4adb68b8

History | View | Annotate | Download (5 kB)

1
"""
2
Dummy backend with debugging output
3

4
A backend with no functionality other than producing debugging output.
5
"""
6

    
7
import logging
8

    
9
def binary_search_name(a, x, lo = 0, hi = None):
10
    """
11
    Search a sorted array of dicts for the value of the key 'name'.
12
    Raises ValueError if the value is not found.
13
    
14
    a -- the array
15
    x -- the value to search for
16
    """
17
    if hi is None:
18
        hi = len(a)
19
    while lo < hi:
20
        mid = (lo + hi) // 2
21
        midval = a[mid]['name']
22
        if midval < x:
23
            lo = mid + 1
24
        elif midval > x: 
25
            hi = mid
26
        else:
27
            return mid
28
    raise ValueError()
29

    
30
def get_account_meta(account):
31
    logging.debug("get_account_meta: %s %s", account, name)
32
    return {'count': 13, 'bytes': 3148237468}
33

    
34
def create_container(account, name):
35
    """
36
    Returns True if the container was created, False if it already exists.
37
    """
38
    logging.debug("create_container: %s %s", account, name)
39
    return True
40

    
41
def delete_container(account, name):
42
    logging.debug("delete_container: %s %s", account, name)
43
    return
44

    
45
def get_container_meta(account, name):
46
    logging.debug("get_container_meta: %s %s", account, name)
47
    return {'count': 22, 'bytes': 245}
48

    
49
def list_containers(account, marker = None, limit = 10000):
50
    logging.debug("list_containers: %s %s %s", account, marker, limit)
51
    
52
    containers = [
53
            {'name': '1', 'count': 2, 'bytes': 123},
54
            {'name': '2', 'count': 22, 'bytes': 245},
55
            {'name': '3', 'count': 222, 'bytes': 83745},
56
            {'name': 'four', 'count': 2222, 'bytes': 274365}
57
        ]
58
    
59
    start = 0
60
    if marker:
61
        try:
62
            start = binary_search_name(containers, marker) + 1
63
        except ValueError:
64
            pass
65
    if not limit or limit > 10000:
66
        limit = 10000
67
    
68
    return containers[start:start + limit]
69

    
70
def list_objects(account, container, prefix = None, delimiter = None, marker = None, limit = 10000):
71
    logging.debug("list_objects: %s %s %s %s %s %s", account, container, prefix, delimiter, marker, limit)
72

    
73
    objects = [
74
            {'name': 'other', 'hash': 'dfgs', 'bytes': 0, 'content_type': 'application/directory', 'last_modified': 23453454},
75
            {'name': 'other/something', 'hash': 'vkajf', 'bytes': 234, 'content_type': 'application/octet-stream', 'last_modified': 878434562},
76
            {'name': 'photos', 'hash': 'kajdsn', 'bytes': 0, 'content_type': 'application/directory', 'last_modified': 1983274},
77
            {'name': 'photos/asdf', 'hash': 'jadsfkj', 'bytes': 0, 'content_type': 'application/directory', 'last_modified': 378465873},
78
            {'name': 'photos/asdf/test', 'hash': 'sudfhius', 'bytes': 37284, 'content_type': 'text/plain', 'last_modified': 93674212},
79
            {'name': 'photos/me.jpg', 'hash': 'sdgsdfgsf', 'bytes': 534, 'content_type': 'image/jpeg', 'last_modified': 262345345},
80
            {'name': 'photos/text.txt', 'hash': 'asdfasd', 'bytes': 34243, 'content_type': 'text/plain', 'last_modified': 45345345}
81
        ]
82
    
83
    if prefix or delimiter:
84
        if prefix:
85
            objects = [x for x in objects if x['name'].startswith(prefix)]
86
        if delimiter:
87
            pseudo_objects = {}
88
            for x in objects:
89
                pseudo_name = x['name'][len(prefix):]
90
                i = pseudo_name.find(delimiter)
91
                if i != -1:
92
                    pseudo_name = pseudo_name[:i]
93
                # TODO: Virtual directories.
94
                if pseudo_name not in pseudo_objects:
95
                    pseudo_objects[pseudo_name] = x
96
            objects = sorted(pseudo_objects.values(), key=lambda o: o['name'])
97
        
98
    start = 0
99
    if marker:
100
        try:
101
            start = binary_search_name(objects, marker) + 1
102
        except ValueError:
103
            pass
104
    if not limit or limit > 10000:
105
        limit = 10000
106
    
107
    return objects[start:start + limit]
108

    
109
def get_object_meta(account, container, name):
110
    logging.debug("get_object_meta: %s %s %s", account, container, name)
111
    meta = {'meat': 'bacon', 'fruit': 'apple'}
112
    return {'hash': 'asdfasd', 'bytes': 34243, 'content_type': 'text/plain', 'last_modified': 45345345, 'meta': meta}
113

    
114
def update_object_meta(account, container, name, meta):
115
    logging.debug("update_object_meta: %s %s %s %s", account, container, name, meta)
116
    for k, v in meta.iteritems():
117
        pass
118
    return
119

    
120
def get_object_data(account, container, name, offset=0, length=0):
121
    logging.debug("get_object_data: %s %s %s %s %s", account, container, name, offset, length)
122
    return ''
123

    
124
def update_object_data(account, container, name, meta, data):
125
    logging.debug("update_object_data: %s %s %s %s %s", account, container, name, meta, data)
126
    return
127

    
128
def copy_object(account, container, name, new_container, new_name):
129
    logging.debug("copy_object: %s %s %s %s %s", account, container, name, new_container, new_name)
130
    return
131

    
132
def delete_object(account, container, name):
133
    logging.debug("delete_object: %s %s %s", account, container, name)
134
    return