Code Monkey home page Code Monkey logo

python-xcall's Introduction

python-xcall

A Python x-callback-url client for communicating with x-callback-url enabled macOS applications. python-xcall uses the handy xcall command line tool.

It is used by:

Software compatability

Requires:

  • macOS
  • python 2.7
  • Uses xcall (included).
  • Needs pytest and mock for testing

Installation

Check it out:

$ git clone https://github.com/robwalton/python-xcall.git
Cloning into 'python-xcall'...

Basic use

Call a scheme (ulysses) with an action (get-version):

>>> import xcall
>>> xcall.xcall('ulysses', 'get-version')
{u'apiVersion': u'2', u'buildNumber': u'33542'}

An x-success reply will be utf-8 un-encoded, then url unquoted, and then un-marshaled using json into Python objects and returned.

A dictionary of action parameters can also be provided (each value is utf-8 encoded and then url quoted before sending):

>>> xcall.xcall('ulysses', 'new-sheet', {'text':'My new sheet', 'index':'2'})

If the application calls back with an x-error, an XCallbackError will be raised:

>>> xcall.xcall('ulysses', 'an-invalid-action')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
...
XCallbackError: x-error callback: '{
  "errorMessage" : "Invalid Action",
  "errorCode" : "100"
}
' (in response to url: 'ulysses://x-callback-url/an-invalid-action')

More control

For more control create an instance of xcall.XCallClient, specifying the scheme to use, whether responses should be un-marshaled using json, and an x-error handler. For example:

class UlyssesError(XCallbackError):
    pass

def ulysses_xerror_handler(xerror, requested_url):
    error_message = eval(xerror)['errorMessage']
    error_code = eval(xerror)['errorCode']
    raise UlyssesError(
        ("%(error_message)s. Code=%(error_code)s. "
         "In response to sending the url '%(requested_url)s'") % locals())

ulysses_client = XCallClient(
    'ulysses', on_xerror_handler=ulysses_xerror_handler,  json_decode_success=True)

Make calls using:

>>> ulysses_client.xcall('get-version')

or just:

>>> ulysses_client('get-version')

On thread/process safety

Call to this module are probably not thread/process safe. An attempt is made to ensure that xcall is not already running, but there is 20-30ms window in which multiple calls to this module will result in multiple xcall processes running and the chance of replies being mixed up.

Testing

Running the tests requires the pytest and mock packages. Some optional integration tests currently require Ulysses. Code your access-token into the top of test_calls.py. Obtain the access token string by removing the @skip marker from test_authorise() in test_calls.py and running the tests.

From the root package folder call:

MacBook:python-xcall walton$ pytest
...

Licensing & Thanks

The code and the documentation are released under the MIT and Creative Commons Attribution-NonCommercial licences respectively.

Thanks to:

Todo

  • Upload PyPi after working out how distrubute the lib folder containing xcall.app.
  • Logs could go somewhere more sensible that stdout.

python-xcall's People

Contributors

robwalton avatar

Watchers

James Cloos avatar Allison Wampler avatar

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.