Skip to content
Snippets Groups Projects
Commit d8aaa48b authored by Finn's avatar Finn
Browse files

Add Device class.

parent b2565c8a
No related branches found
No related tags found
Loading
# Copyright (C) 2018 Bloomberg LP
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# <http://www.apache.org/licenses/LICENSE-2.0>
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Device
======
A device.
"""
import uuid
from buildgrid._protos.google.devtools.remoteworkers.v1test2 import worker_pb2
class Device:
def __init__(self, properties=None):
""" Creates devices available to the worker
The first device is know as the Primary Device - the revice which
is running a bit and responsible to actually executing commands.
All other devices are known as Attatched Devices and must be controlled
by the Primary Device.
properties (list(dict(string : string))) : Properties of device. Keys may
repeated.
"""
self._properties = {}
self.__property_keys = ['os', 'has-docker']
self.__name = str(uuid.uuid4())
if properties:
for prop in properties:
self._add_property(prop)
@property
def name(self):
return self.__name
@property
def properties(self):
return self._properties
def get_pb2(self):
device = worker_pb2.Device(handle=self.__name)
for k, v in self._properties.items():
for prop in v:
property_message = worker_pb2.Device.Property()
property_message.key = k
property_message.value = prop
device.properties.extend([property_message])
return device
def _add_property(self, key, value):
if key in self.__property_keys:
prop = self._properties.get(key)
if not prop:
self._properties[key] = [value]
else:
prop[key].append(value)
else:
raise KeyError('Key not supported: [{}]'.format(key))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment