|
1 | | -# Copyright (c) 2017 Sam Kumar <samkumar@berkeley.edu> |
2 | | -# Copyright (c) 2017 Michael P Andersen <m.andersen@cs.berkeley.edu> |
3 | | -# Copyright (c) 2017 University of California, Berkeley |
4 | | -# All rights reserved. |
| 1 | +# btrdb.conn |
| 2 | +# Connection related objects for the BTrDB library |
5 | 3 | # |
6 | | -# Redistribution and use in source and binary forms, with or without |
7 | | -# modification, are permitted provided that the following conditions are met: |
8 | | -# * Redistributions of source code must retain the above copyright |
9 | | -# notice, this list of conditions and the following disclaimer. |
10 | | -# * Redistributions in binary form must reproduce the above copyright |
11 | | -# notice, this list of conditions and the following disclaimer in the |
12 | | -# documentation and/or other materials provided with the distribution. |
13 | | -# * Neither the name of the University of California, Berkeley nor the |
14 | | -# names of its contributors may be used to endorse or promote products |
15 | | -# derived from this software without specific prior written permission. |
| 4 | +# Author: PingThings |
| 5 | +# Created: Fri Dec 21 14:57:30 2018 -0500 |
16 | 6 | # |
17 | | -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
18 | | -# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
19 | | -# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
20 | | -# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNERS OR CONTRIBUTORS BE LIABLE FOR |
21 | | -# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
22 | | -# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
23 | | -# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
24 | | -# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | | -# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
26 | | -# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 7 | +# For license information, see LICENSE.txt |
| 8 | +# ID: conn.py [] allen@pingthings.io $ |
27 | 9 |
|
28 | 10 | """ |
29 | | -The 'btrdb' module provides Python bindings to interact with BTrDB. |
| 11 | +Connection related objects for the BTrDB library |
30 | 12 | """ |
31 | 13 |
|
32 | | -import grpc |
33 | | -import uuid as uuidlib |
| 14 | +########################################################################## |
| 15 | +## Imports |
| 16 | +########################################################################## |
| 17 | + |
34 | 18 | import os |
| 19 | +import re |
| 20 | +import uuid as uuidlib |
35 | 21 |
|
| 22 | +import grpc |
36 | 23 | from grpc._cython.cygrpc import CompressionAlgorithm |
37 | 24 |
|
38 | 25 | from btrdb.stream import Stream, StreamSet |
39 | 26 | from btrdb.utils.general import unpack_stream_descriptor |
40 | 27 | from btrdb.utils.conversion import to_uuid |
| 28 | +from btrdb.exceptions import NotFound |
| 29 | + |
| 30 | +########################################################################## |
| 31 | +## Module Variables |
| 32 | +########################################################################## |
41 | 33 |
|
42 | 34 | MIN_TIME = -(16 << 56) |
43 | 35 | MAX_TIME = 48 << 56 |
44 | 36 | MAX_POINTWIDTH = 63 |
45 | 37 |
|
46 | 38 |
|
| 39 | +########################################################################## |
| 40 | +## Classes |
| 41 | +########################################################################## |
| 42 | + |
47 | 43 | class Connection(object): |
48 | 44 |
|
49 | 45 | def __init__(self, addrportstr, apikey=None): |
@@ -129,7 +125,31 @@ def streams(self, *identifiers, versions=None): |
129 | 125 | if versions and len(versions) != len(identifiers): |
130 | 126 | raise ValueError("number of versions does not match identifiers") |
131 | 127 |
|
132 | | - streams = [self.stream_from_uuid(ident) for ident in identifiers] |
| 128 | + streams = [] |
| 129 | + for ident in identifiers: |
| 130 | + if isinstance(ident, uuidlib.UUID): |
| 131 | + streams.append(self.stream_from_uuid(ident)) |
| 132 | + continue |
| 133 | + |
| 134 | + if isinstance(ident, str): |
| 135 | + # attempt UUID lookup |
| 136 | + pattern = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}" |
| 137 | + if re.match(pattern, ident): |
| 138 | + streams.append(self.stream_from_uuid(ident)) |
| 139 | + continue |
| 140 | + |
| 141 | + # attempt collection/name lookup |
| 142 | + if "/" in ident: |
| 143 | + parts = ident.split("/") |
| 144 | + found = self.streams_in_collection("/".join(parts[:-1]), tags={"name": parts[-1]}) |
| 145 | + if len(found) == 1: |
| 146 | + streams.append(found[0]) |
| 147 | + continue |
| 148 | + raise NotFound(f"Could not identify stream `{ident}`") |
| 149 | + |
| 150 | + raise ValueError(f"Could not identify stream based on `{ident}`. Identifier must be UUID or collection/name.") |
| 151 | + |
| 152 | + |
133 | 153 | obj = StreamSet(streams) |
134 | 154 |
|
135 | 155 | if versions: |
|
0 commit comments