Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(117)

Side by Side Diff: client/libs/logdog/bootstrap.py

Issue 1961603002: Add LogDog Python client library. (Closed) Base URL: https://github.com/luci/luci-py@master
Patch Set: Updated w/ comments. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright 2016 The LUCI Authors. All rights reserved.
2 # Use of this source code is governed by the Apache v2.0 license that can be
3 # found in the LICENSE file.
4
5 import collections
6 import os
7
8 from client.libs.logdog import stream, streamname
9
10
11 class NotBootstrappedError(RuntimeError):
12 """Raised when the current environment is missing Butler bootstrap variables.
13 """
14 pass
nodir 2016/05/12 21:29:57 Unnecessary because docstring
dnj (Google) 2016/05/16 16:11:11 Acknowledged.
15
16
17 _ButlerBootstrapBase = collections.namedtuple('_ButlerBootstrapBase',
18 ('project', 'prefix', 'streamserver_uri'))
19
20
21 class ButlerBootstrap(_ButlerBootstrapBase):
22 """Loads LogDog Butler bootstrap parameters from the environment.
23
24 LogDog Butler adds variables describing the LogDog stream parameters to the
25 environment when it bootstraps an application. This class probes the
26 environment and identifies those parameters.
27 """
28
29 _ENV_PROJECT = 'LOGDOG_STREAM_PROJECT'
30 _ENV_PREFIX = 'LOGDOG_STREAM_PREFIX'
31 _ENV_STREAM_SERVER_PATH = 'LOGDOG_STREAM_SERVER_PATH'
32
33 @classmethod
34 def probe(cls, env=None):
35 """Returns (ButlerBootstrap): The probed bootstrap environment.
36
37 Args:
38 env (dict): The environment to probe. If None, `os.getenv` will be used.
39
40 Raises:
41 NotBootstrappedError if the current environment is not boostrapped.
42 """
43 if env is None:
44 env = os.environ
45 project = env.get(cls._ENV_PROJECT)
46 prefix = env.get(cls._ENV_PREFIX)
47
48 if not project:
49 raise NotBootstrappedError('Missing project [%s]' % (cls._ENV_PROJECT,))
50
51 if not prefix:
52 raise NotBootstrappedError('Missing prefix [%s]' % (cls._ENV_PREFIX,))
53 try:
54 streamname.validate_stream_name(prefix)
55 except ValueError as e:
56 raise NotBootstrappedError('Prefix (%s) is invalid: %s' % (prefix, e))
57
58 return cls(project=project, prefix=prefix,
59 streamserver_uri=env.get(cls._ENV_STREAM_SERVER_PATH))
60
61 def stream_client(self):
62 """Returns: (StreamClient) stream client for the bootstrap streamserver URI.
63
64 If the Butler accepts external stream connections, it will export a
65 streamserver URI in the environment. This will create a StreamClient
66 instance to operate on the streamserver if one is defined.
67
68 Raises:
69 ValueError: If no streamserver URI is present in the environment.
70 """
71 if not self.streamserver_uri:
72 raise ValueError('No streamserver in bootstrap environment.')
73 return stream.StreamClient.create(self.streamserver_uri)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698