|
| 1 | +from __future__ import absolute_import |
| 2 | +import collections |
| 3 | + |
| 4 | +from six import string_types |
| 5 | + |
| 6 | +from ..hubstorage.collectionsrt import Collection as _Collection |
| 7 | + |
| 8 | +from .utils import _Proxy |
| 9 | +from .utils import format_iter_filters |
| 10 | +from .utils import proxy_methods |
| 11 | +from .utils import wrap_kwargs |
| 12 | + |
| 13 | + |
| 14 | +class Collections(_Proxy): |
| 15 | + """Access to project collections. |
| 16 | +
|
| 17 | + Not a public constructor: use :class:`Project` instance to get a |
| 18 | + :class:`Collections` instance. See :attr:`Project.collections` attribute. |
| 19 | +
|
| 20 | + Usage:: |
| 21 | +
|
| 22 | + >>> collections = project.collections |
| 23 | + >>> collections.list() |
| 24 | + [{'name': 'Pages', 'type': 's'}] |
| 25 | + >>> foo_store = collections.get_store('foo_store') |
| 26 | + """ |
| 27 | + |
| 28 | + def get(self, coltype, colname): |
| 29 | + """Base method to get a collection with a given type and name.""" |
| 30 | + self._origin._validate_collection(coltype, colname) |
| 31 | + return Collection(self._client, self, coltype, colname) |
| 32 | + |
| 33 | + def get_store(self, colname): |
| 34 | + return self.get('s', colname) |
| 35 | + |
| 36 | + def get_cached_store(self, colname): |
| 37 | + return self.get('cs', colname) |
| 38 | + |
| 39 | + def get_versioned_store(self, colname): |
| 40 | + return self.get('vs', colname) |
| 41 | + |
| 42 | + def get_versioned_cached_store(self, colname): |
| 43 | + return self.get('vcs', colname) |
| 44 | + |
| 45 | + def iter(self): |
| 46 | + """Iterate through collections of a project.""" |
| 47 | + return self._origin.apiget('list') |
| 48 | + |
| 49 | + def list(self): |
| 50 | + """List collections of a project.""" |
| 51 | + return list(self.iter()) |
| 52 | + |
| 53 | + |
| 54 | +class Collection(object): |
| 55 | + """Representation of a project collection object. |
| 56 | +
|
| 57 | + Not a public constructor: use :class:`Collections` instance to get a |
| 58 | + :class:`Collection` instance. See :meth:`Collections.get_store` and |
| 59 | + similar methods. # noqa |
| 60 | +
|
| 61 | + Usage: |
| 62 | +
|
| 63 | + - add a new item to collection:: |
| 64 | +
|
| 65 | + >>> foo_store.set({'_key': '002d050ee3ff6192dcbecc4e4b4457d7', |
| 66 | + 'value': '1447221694537'}) |
| 67 | +
|
| 68 | + - count items in collection:: |
| 69 | +
|
| 70 | + >>> foo_store.count() |
| 71 | + 1 |
| 72 | +
|
| 73 | + - get an item from collection:: |
| 74 | +
|
| 75 | + >>> foo_store.get('002d050ee3ff6192dcbecc4e4b4457d7') |
| 76 | + {'value': '1447221694537'} |
| 77 | +
|
| 78 | + - get all items from collection:: |
| 79 | +
|
| 80 | + >>> foo_store.iter() |
| 81 | + <generator object jldecode at 0x1049eef10> |
| 82 | +
|
| 83 | + - iterate iterate over _key & value pair:: |
| 84 | +
|
| 85 | + >>> for elem in foo_store.iter(count=1)): |
| 86 | + >>> ... print(elem) |
| 87 | + [{'_key': '002d050ee3ff6192dcbecc4e4b4457d7', |
| 88 | + 'value': '1447221694537'}] |
| 89 | +
|
| 90 | + - filter by multiple keys, only values for keys that exist will be returned:: |
| 91 | +
|
| 92 | + >>> foo_store.list(key=['002d050ee3ff6192dcbecc4e4b4457d7', 'blah']) |
| 93 | + [{'_key': '002d050ee3ff6192dcbecc4e4b4457d7', 'value': '1447221694537'}] |
| 94 | +
|
| 95 | + - delete an item by key:: |
| 96 | +
|
| 97 | + >>> foo_store.delete('002d050ee3ff6192dcbecc4e4b4457d7') |
| 98 | + """ |
| 99 | + |
| 100 | + def __init__(self, client, collections, coltype, colname): |
| 101 | + self._client = client |
| 102 | + self._origin = _Collection(coltype, colname, collections._origin) |
| 103 | + proxy_methods(self._origin, self, [ |
| 104 | + 'create_writer', 'count', |
| 105 | + ('iter', 'iter_values'), |
| 106 | + ('iter_raw_json', 'iter_json'), |
| 107 | + ]) |
| 108 | + # simplified version of _Proxy._wrap_iter_methods logic |
| 109 | + # to provide better support for filter param in iter methods |
| 110 | + for method in ['iter', 'iter_raw_json']: |
| 111 | + wrapped = wrap_kwargs(getattr(self, method), format_iter_filters) |
| 112 | + setattr(self, method, wrapped) |
| 113 | + |
| 114 | + def list(self, *args, **kwargs): |
| 115 | + """Convenient shortcut to list iter results. |
| 116 | +
|
| 117 | + Please note that list() method can use a lot of memory and for a large |
| 118 | + amount of elements it's recommended to iterate through it via iter() |
| 119 | + method (all params and available filters are same for both methods). |
| 120 | + """ |
| 121 | + return list(self.iter(*args, **kwargs)) |
| 122 | + |
| 123 | + def get(self, key, *args, **kwargs): |
| 124 | + """Get item from collection by key. |
| 125 | +
|
| 126 | + :param key: string item key |
| 127 | + :return: an item dictionary if exists |
| 128 | + """ |
| 129 | + if key is None: |
| 130 | + raise ValueError("key cannot be None") |
| 131 | + return self._origin.get(key, *args, **kwargs) |
| 132 | + |
| 133 | + def set(self, *args, **kwargs): |
| 134 | + """Set item to collection by key. |
| 135 | +
|
| 136 | + The method returns None (original method returns an empty generator). |
| 137 | + """ |
| 138 | + self._origin.set(*args, **kwargs) |
| 139 | + |
| 140 | + def delete(self, keys): |
| 141 | + """Delete item(s) from collection by key(s). |
| 142 | +
|
| 143 | + The method returns None (original method returns an empty generator). |
| 144 | + """ |
| 145 | + if (not isinstance(keys, string_types) and |
| 146 | + not isinstance(keys, collections.Iterable)): |
| 147 | + raise ValueError("You should provide string key or iterable " |
| 148 | + "object providing string keys") |
| 149 | + self._origin.delete(keys) |
| 150 | + |
| 151 | + def iter_raw_msgpack(self, requests_params=None, **apiparams): |
| 152 | + return self._origin._collections.iter_msgpack( |
| 153 | + self._origin.coltype, self._origin.colname, |
| 154 | + requests_params=requests_params, **apiparams) |
0 commit comments