Giter Site home page Giter Site logo

python-wvalib's Introduction

Python WVA Library

Build Status Coverage Status Code Climate Latest Version License

This library contains a set of classes and functions for performing common operations using the WVA Web Services API. It contains both general web services helpers (using the Python requests library) as well as more specific helpers.

In addition, packaged with the library is a command-line utility using the API that serves as both an example of how the library may be used as well as a convenient tool for developer use.

Installation

The library may be installed using pip by doing the following:

pip install wva

This will install both the library and command-line application.

CLI Usage

Help for the CLI application can be obtained by running the application with the --help option. This will show the available commands:

$ wva --help
Usage: wva [OPTIONS] COMMAND [ARGS]...

  Command-line interface for interacting with a WVA device

Options:
  --https / --no-https  Use HTTPS instead of HTTP
  --hostname TEXT       Force use of the specified hostname
  --username TEXT       Force use of the specified username
  --password TEXT       Force use of the specified password
  --config-dir TEXT     Directory containing wva configuration files
  --help                Show this message and exit.

Commands:
  cliconfig      View and clear CLI config
  delete         DELETE the specified URI Example: $ wva get...
  get            Perform an HTTP GET of the provided URI The...
  post           POST file data to a specific URI Note that...
  put            PUT file data to a specific URI Example: $...
  subscriptions  View and Edit subscriptions
  vehicle        Vehicle Data Commands

You can get help for a specific command by specifying --help after the command. For instance,

$ wva subscriptions --help
Usage: wva subscriptions [OPTIONS] COMMAND [ARGS]...

  View and Edit subscriptions

Options:
  --help  Show this message and exit.

Commands:
  add     Add a subscription with a given short_name...
  clear   Remove all registered subscriptions Example:...
  delete  Delete a specific subscription by short name
  graph   Present a live graph of the incoming...
  list    List short name of all current subscriptions
  listen  Output the contents of the WVA event stream...
  show    Show metadata for a specific subscription...

The first time you attempt to talk to a device, you will need to provide credentials for talking to the device. The CLI application will then store those credentials in the file ~/.wva/config.json and attempt to use those credentials in the future. If the credentials are ever invalid, you will be prompted to enter the credentials again.

You can clear the credentials that are stored by deleting the wva configuration or by running the following command:

$ wva cliconfig clear

Library Usage and Examples

See the Full API documentation for full details on the API and its usage. Here's some examples:

Subscribe to event streams

from wva import WVA
wva = WVA("<ip>", "user", "password")

# clear any existing subscriptions
for sub in wva.get_subscriptions():
    sub.delete()

# add subscriptions for some pieces of vehicle data
wva.get_subscription("speed").create("vehicle/data/VehicleSpeed", interval=3)
wva.get_subscription("rpm").create("vehicle/data/EngineSpeed", interval=5)

# receive vehicle data and print it
def data_received(data):
    print("<- {}".format(data))

es = wva.get_event_stream()
es.add_event_listner(data_received)
es.enable()

Sample vehicle data

from wva import WVA
from wva.exceptions import WVAHttpServiceUnavailableError

wva = WVA("<ip>", "user", "password")

# print out all available data elements and whether
# they currently have data or not
for name, element in wva.get_vehicle_data_elements().items():
    try:
        curval = element.sample()
    except WVAHttpServiceUnavailableError:
        print("{} (Unavailable)".format(name))
    else:
        print("{} = {} at {}".format(name, curval.value, curval.timestamp.ctime()))

Make direct web services calls

from wva import WVA

wva = WVA("<ip>", "user", "password")

client = wva.get_http_client()

# write a hello.py file to the python filesystem
client.put("/files/userfs/WEB/python/hello.py".format(relpath), "print 'Hello, World!'\n")

# print the contents of hello.py on the target to the screen
print(client.get("/files/userfs/WEB/python/hello.py"))

# delete hello.py
client.delete("/files/userfs/WEB/python/hello.py")

Contributing and Developer Information

Contributions to the project are very welcome. Please submit any issues you find on the github issue tracker. If you have a change you would like to have included in the library, please submit a pull request.

Information for developers on coding style, how to run the tests, etc. may be found in the Developer's README.

Support

This library is in "Alpha" currently and is not tested beyond the unit tests included in the code and basic developer testing. Prior to a 1.0 release, the APIs may change in backwards incompatible ways at each minor revision.

If you run into issues, please create an issue on the project's Github Page.

License

This software is open-source. Copyright (c), Digi International Inc., 2015.

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, you can obtain one at http://mozilla.org/MPL/2.0/.

Digi, Digi International, the Digi logo, the Digi website, and Digi Device Cloud are trademarks or registered trademarks of Digi International, Inc. in the United States and other countries worldwide. All other trademarks are the property of their respective owners.

THE SOFTWARE AND RELATED TECHNICAL INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL DIGI OR ITS SUBSIDIARIES BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE AND TECHNICAL INFORMATION HEREIN, INCLUDING ALL SOURCE AND OBJECT CODES, IRRESPECTIVE OF HOW IT IS USED. YOU AGREE THAT YOU ARE NOT PROHIBITED FROM RECEIVING THIS SOFTWARE AND TECHNICAL INFORMATION UNDER UNITED STATES AND OTHER APPLICABLE COUNTRY EXPORT CONTROL LAWS AND REGULATIONS AND THAT YOU WILL COMPLY WITH ALL APPLICABLE UNITED STATES AND OTHER COUNTRY EXPORT LAWS AND REGULATIONS WITH REGARD TO USE AND EXPORT OR RE-EXPORT OF THE SOFTWARE AND TECHNICAL INFORMATION.

python-wvalib's People

Contributors

posborne avatar tpmanley avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

python-wvalib's Issues

ssh: cli: Add support for uploading multiple keys to target

Currently, the algorithm for wva ssh authorize is the following:

  1. Grab public key from current development machine
  2. Write a authorized keys file to the WVA with the public key for the development machine

If there are multiple developers using a single device, this can be annoying as each developer needs to keep re-authorizing. The proposed change would be to do the following:

  1. Grab the authorized keys file from the target if it exists
  2. Add the development machine public key to the list of authorized keys (maybe do a set operation to avoid duplicates)
  3. Write the modified authorized keys file back to the device

Handle bad auth/ip case better

In the case that the stored IP address or credentials are invalid, the CLI command currently produces somewhat unsavory output:

For an invalid IP address:

$ wva get /
Traceback (most recent call last):
  File "/home/posborne/.local/bin/wva", line 9, in <module>
    load_entry_point('wva==0.1.0', 'console_scripts', 'wva')()
  File "/home/posborne/.local/venvs/wva/local/lib/python2.7/site-packages/wva/cli.py", line 552, in main
    cli(auto_envvar_prefix="WVA")
  File "/home/posborne/.local/venvs/wva/local/lib/python2.7/site-packages/click/core.py", line 610, in __call__
    return self.main(*args, **kwargs)
  File "/home/posborne/.local/venvs/wva/local/lib/python2.7/site-packages/click/core.py", line 590, in main
    rv = self.invoke(ctx)
  File "/home/posborne/.local/venvs/wva/local/lib/python2.7/site-packages/click/core.py", line 936, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/posborne/.local/venvs/wva/local/lib/python2.7/site-packages/click/core.py", line 782, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/posborne/.local/venvs/wva/local/lib/python2.7/site-packages/click/core.py", line 416, in invoke
    return callback(*args, **kwargs)
  File "/home/posborne/.local/venvs/wva/local/lib/python2.7/site-packages/wva/cli.py", line 196, in get
    cli_pprint(http_client.get(uri))
  File "/home/posborne/.local/venvs/wva/local/lib/python2.7/site-packages/wva/http_client.py", line 135, in get
    return self.request("GET", uri, **kwargs)
  File "/home/posborne/.local/venvs/wva/local/lib/python2.7/site-packages/wva/http_client.py", line 113, in request
    response = self.raw_request(method, uri, **kwargs)
  File "/home/posborne/.local/venvs/wva/local/lib/python2.7/site-packages/wva/http_client.py", line 95, in raw_request
    six.raise_from(WVAHttpRequestError(e), e)
  File "/home/posborne/.local/venvs/wva/local/lib/python2.7/site-packages/six.py", line 692, in raise_from
    raise value
wva.exceptions.WVAHttpRequestError: ('Connection aborted.', error(113, 'No route to host'))

And for a credentials problem:

Traceback (most recent call last):
  File "/home/posborne/.local/bin/wva", line 9, in <module>
    load_entry_point('wva==0.1.0', 'console_scripts', 'wva')()
  File "/home/posborne/.local/venvs/wva/local/lib/python2.7/site-packages/wva/cli.py", line 552, in main
    cli(auto_envvar_prefix="WVA")
  File "/home/posborne/.local/venvs/wva/local/lib/python2.7/site-packages/click/core.py", line 610, in __call__
    return self.main(*args, **kwargs)
  File "/home/posborne/.local/venvs/wva/local/lib/python2.7/site-packages/click/core.py", line 590, in main
    rv = self.invoke(ctx)
  File "/home/posborne/.local/venvs/wva/local/lib/python2.7/site-packages/click/core.py", line 936, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/posborne/.local/venvs/wva/local/lib/python2.7/site-packages/click/core.py", line 936, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/posborne/.local/venvs/wva/local/lib/python2.7/site-packages/click/core.py", line 782, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/posborne/.local/venvs/wva/local/lib/python2.7/site-packages/click/core.py", line 416, in invoke
    return callback(*args, **kwargs)
  File "/home/posborne/.local/venvs/wva/local/lib/python2.7/site-packages/wva/cli.py", line 541, in authorize
    http_client.put(authorized_keys_uri, authorized_key_contents)
  File "/home/posborne/.local/venvs/wva/local/lib/python2.7/site-packages/wva/http_client.py", line 162, in put
    return self.request("PUT", uri, data=data, **kwargs)
  File "/home/posborne/.local/venvs/wva/local/lib/python2.7/site-packages/wva/http_client.py", line 116, in request
    raise exception_class(response)
wva.exceptions.WVAHttpUnauthorizedError: Unexpected HTTP status 401 'WVAHttpUnauthorizedError'

In the case of an HTTP exception, a stack trace seems suboptimal. In addition, it would be nice to give hints to the user as to what may have gone wrong and what can be done to fix it. For both of these cases, telling the user that runngin wva cliconfig clear would be very helpful information.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.