Skip to content
This repository has been archived by the owner on Feb 15, 2018. It is now read-only.

Commit

Permalink
Adds element support for labels
Browse files Browse the repository at this point in the history
This adds support for managing elements as part of
labels. Adding/removing/replacing sensors and elements is now done in
the `label sensor` and `label element` commands and is no longer
supported in `label update`
  • Loading branch information
madninja committed Jan 24, 2017
1 parent 9144c26 commit 9ead2e5
Show file tree
Hide file tree
Showing 10 changed files with 2,330 additions and 750 deletions.
192 changes: 139 additions & 53 deletions helium_commander/commands/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,38 @@
Client,
Label,
Sensor,
Element,
device_sort_option,
device_mac_option,
metadata_filter_option,
ResourceParamType
)
from helium_commander.commands import metadata, timeseries
from collections import namedtuple


pass_client = click.make_pass_decorator(Client)
label_includes = [Sensor, Element]

LabelActionResources = namedtuple('LabelResourceActions',
['add', 'remove', 'replace'])


def lookup_label_action_resources(client, cls, mac=False, **kwargs):
"""Look up resources for a label."""
def _lookup(action, resources):
id_reps = kwargs.pop(action, None)
if not id_reps:
return None # No change
if 'none' in id_reps:
return [] # Empty out the resources
return [cls.lookup(client, id, resources=resources, mac=mac)
for id in id_reps]

all_resources = cls.all(client)
return LabelActionResources(_lookup('add', all_resources),
_lookup('remove', all_resources),
_lookup('replace', all_resources))


@click.group()
Expand All @@ -31,50 +55,53 @@ def list(client, label, **kwargs):
organization.
"""
include = [Sensor]
if label:
labels = [Label.lookup(client, label, include=include)]
labels = [Label.lookup(client, label, include=label_includes)]
else:
metadata = kwargs.get('metadata') or None
labels = Label.where(client, include=include, metadata=metadata)
Label.display(client, labels, include=include)
labels = Label.where(client, include=label_includes, metadata=metadata)
Label.display(client, labels, include=label_includes)


cli.add_command(timeseries.cli(Label, history=False, writable=False))


@cli.command()
@click.argument('label')
@device_sort_option
@pass_client
def sensor(client, label, **kwargs):
"""Lists sensors for a label.
Lists sensors for a given LABEL.
"""
label = Label.lookup(client, label, include=[Sensor])
sensors = label.sensors(use_included=True)
Sensor.display(client, sensors, **kwargs)


@cli.command()
@click.argument('name')
@click.option('--add',
@click.option('--sensors',
type=ResourceParamType(metavar='SENSOR'),
help="Add sensors to a label")
@click.option('--elements',
type=ResourceParamType(metavar='ELEMENT'),
help="Add elements to a label")
@click.pass_context
def create(ctx, name, **kwargs):
def create(ctx, name, sensors, elements):
"""Create a label.
Creates a label with a given NAME and an (optional) list of sensors
associated with that label.
Creates a label with a given NAME and an (optional) list of
sensors and elements associated with that label.
"""
client = ctx.find_object(Client)

sensors = sensors or []
if sensors:
all_sensors = Sensor.all(client)
sensors = [Sensor.lookup(client, id, resources=all_sensors)
for id in sensors]

elements = elements or []
if elements:
all_elements = Element.all(client)
elements = [Element.lookup(client, id, resources=all_elements)
for id in elements]

label = Label.create(client, attributes={
'name': name
})
}, sensors=sensors, elements=elements)

ctx.invoke(update, client, label=label.id, **kwargs)
label = Label.find(client, label.id, include=label_includes)
Label.display(client, [label], include=label_includes)


@cli.command()
Expand All @@ -92,6 +119,33 @@ def delete(client, label):
click.echo("Deleted {} ".format(entry.id))


@cli.command()
@click.argument('label')
@click.option('--name',
help="the new name for the label")
@pass_client
def update(client, label, name):
"""Update a label.
Changes basic attributes on a label.
To add or remove sensors or elements from a label see the `label
element` and `label sensor` commands.
"""
label = Label.lookup(client, label)
if name:
label.update(attributes={
'name': name
})

label = Label.find(client, label.id, include=label_includes)
Label.display(client, [label], include=label_includes)


cli.add_command(metadata.cli(Label))


@cli.command()
@click.argument('label')
@click.option('--add',
Expand All @@ -103,42 +157,74 @@ def delete(client, label):
@click.option('--replace',
type=ResourceParamType(metavar='SENSOR'),
help="Replace all sensors in a label")
@click.option('--clear', is_flag=True, default=False,
help="Remove all sensors from a label")
@click.option('--name',
help="the new name for the label")
@device_sort_option
@device_mac_option
@pass_client
def update(client, label, name, **kwargs):
"""Update sensors in a label.
def sensor(client, label, mac, **kwargs):
"""List sensors for a label.
List sensors for a given LABEL.
Adds, removes, replaces or clears all sensors in the given LABEL.
Add, remove or replace sensors from the LABEL by using the --add,
--remove or --replace arguments respectively. Note that you can
specify "none" with these to indicate an empty list.
"""
label = Label.lookup(client, label)
if name:
label.update(attributes={
'name': name
})

all_sensors = Sensor.all(client)
add_sensors = kwargs.pop('add', None) or []
remove_sensors = kwargs.pop('remove', None) or []
clear_sensors = kwargs.pop('clear', False)
sensors = [Sensor.lookup(client, s, resources=all_sensors)
for s in add_sensors]
if sensors:
label.add_sensors(sensors)
actions = lookup_label_action_resources(client, Sensor,
mac=mac, **kwargs)

sensors = [Sensor.lookup(client, s, resources=all_sensors)
for s in remove_sensors]
if sensors:
label.remove_sensors(sensors)
if actions.add is not None:
label.add_sensors(actions.add)

if clear_sensors:
label.update_sensors([])
if actions.remove is not None:
label.remove_sensors(actions.remove)

include = [Sensor]
label = Label.find(client, label.id, include=include)
Label.display(client, [label], include=include)
if actions.replace is not None:
label.update_sensors(actions.replace)

cli.add_command(metadata.cli(Label))
sensors = label.sensors()
Sensor.display(client, sensors, **kwargs)


@cli.command()
@click.argument('label')
@click.option('--add',
type=ResourceParamType(metavar='SENSOR'),
help="Add sensors to a label")
@click.option('--remove',
type=ResourceParamType(metavar='SENSOR'),
help="Remove sensors from a label")
@click.option('--replace',
type=ResourceParamType(metavar='SENSOR'),
help="Replace all sensors in a label")
@device_sort_option
@device_mac_option
@pass_client
def element(client, label, mac, **kwargs):
"""List elements for a label.
List elements for a given LABEL.
Add, remove or replace sensors from the LABEL by using the --add,
--remove or --replace arguments respectively. Note that you can
specify "none" with these to indicate an empty list.
"""
label = Label.lookup(client, label)

actions = lookup_label_action_resources(client, Element,
mac=mac, **kwargs)

if actions.add is not None:
label.add_elements(actions.add)

if actions.remove is not None:
label.remove_elements(actions.remove)

if actions.replace is not None:
label.update_elements(actions.replace)

elements = label.elements()
Element.display(client, elements, **kwargs)
7 changes: 6 additions & 1 deletion helium_commander/label.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
from __future__ import unicode_literals
from helium import Label, Sensor
from helium import Label, Sensor, Element
from operator import attrgetter


def display_map(cls, client, include=None):
def _count_sensor(self):
return len(self.sensors(use_included=True))

def _count_element(self):
return len(self.elements(use_included=True))

dict = super(Label, cls).display_map(client)
if include and Sensor in include:
dict['sensors'] = _count_sensor
if include and Element in include:
dict['elements'] = _count_element
dict.update([
('name', attrgetter('name'))
])
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
author = 'Helium'
author_email = 'hello@helium.com'
install_requires = [
'helium-python>=0.6.5',
'helium-python>=0.8.0',
'future>=0.15',
'futures>=3.0',
'terminaltables>=2.1.0',
Expand Down
Loading

0 comments on commit 9ead2e5

Please sign in to comment.