coherence.web (package) — Cohen3 0.8.0 documentation

# coherence.web (package)

## coherence.web.ui (module)

### WebUI

The `WebUI` is used to enable an html interface where the user can browse the devices content via web browser. By default, the WebUI interface is disabled and could be enabled from config file or by config dictionary when initializing `Coherence`

Changed in version 0.8.2.

Note

Be aware that the browser should support Web Sockets and js enabled. All modern browsers should have this features integrated (tested with firefox and google chrome).

Warning

Don’t create your web server into port 9000. This is reserved for the js WebSocket.

#### WebUi Example

A simple server with web-ui enabled:

    from coherence.base import Coherence
    from coherence.upnp.core.uuid import UUID
    from twisted.internet import reactor
    new_uuid = UUID()
    coherence = Coherence(
            {'web-ui': 'yes',
             'serverport': '9001',
             'logmode': 'info',
             'controlpoint': 'yes',
             'plugin': {'backend': 'FSStore',
                        'name': 'WEB UI FSStore',
                        'content': 'path-to-a-directory-with-media-content',
                        'uuid': new_uuid
                        }
             }
    )
    reactor.run()

class `WSBroadcastServerProtocol`

  Bases: `autobahn.twisted.websocket.WebSocketServerProtocol`

  WSBroadcastServerProtocol deals with the async WebSocket client connection.

  New in version 0.8.2.

  Changed in version 0.9.0: Migrated from louie/dispatcher to EventDispatcher

  Note

  We can attach a callback into the variable message_callback, this callback will be triggered whenever onMessage is called.

  `factory` = None
  `message_callback` = None
  `onMessage`(payload, isBinary)

    Implements `autobahn.websocket.interfaces.IWebSocketChannel.onMessage()`

  `onOpen`()

    Implements `autobahn.websocket.interfaces.IWebSocketChannel.onOpen()`

  `connectionLost`(reason)

    Called when the connection is shut down.

    Clear any circular references here, and any external references to this Protocol. The connection has been closed.

    @type reason: L{twisted.python.failure.Failure}

class `WSBroadcastServerFactory`(client_tracker)

  Bases: `autobahn.twisted.websocket.WebSocketServerFactory`

  WSBroadcastServerFactory is the central WebSocket server side component shared between connections.

  New in version 0.8.2.

  `register`(client)
  `unregister`(client)
  `broadcast`(msg)

class `WSClientTracker`

  Bases: `object`

  Helper to keep track of connections, accessed by the sync and async methods.

  New in version 0.8.2.

  `register`(client)
  `unregister`(client)

class `MenuItemElement`(loader, name)

  Bases: `twisted.web._element.Element`

  Helper class to render a menu entry for the main navigation bar, created with `MenuNavigationBar`.

  New in version 0.8.2.

  `name`(request, tag)

class `MenuNavigationBar`(page)

  Bases: `twisted.web._element.Element`

  Convenient class to create a dynamic navigation bar

  New in version 0.8.2.

  Note

  This is strongly related with the file: templates/template_index.html. The content of the each element should be implemented dynamically (here or in any subclass) or statically (into the mentioned file).

  `loader` = <twisted.web.template.XMLString object>
  `menuData` = ['cohen3', 'devices', 'logging', 'about']
  `menu_elements`(request, tag)

class `DevicesWatcher`(page)

  Bases: `coherence.log.LogAble`

  To manage the connected devices. Broadcast messages informing about the connected/disconnected devices via the web socket interface. This messages can be received by the html/js side, which will be responsible to add or to remove the devices.

  Parameters: | page (object) – An instance of `WebUI`.

  New in version 0.8.2.

  `addSlash` = False
  `isLeaf` = True
  `detected` = []
  `add_device`(device)
  `remove_device`(usn)
  `going_live`()

`format_log`(message, *args, **kwargs)

  Simple method to format the captured logs.

  Parameters: | message (str) – Message from the captured log. *args (list) – The args from the captured log. **kwargs (dict) – The kwargs from the captured log.
  Returns: | A formatted string including the args and the kwargs.

  New in version 0.8.2.

class `LogsWatcher`(page, active)

  Bases: `coherence.log.LogAble`

  Object that takes control of all known loggers (at init time) and redirects them into the web-ui interface.

  Parameters: | page (object) – An instance of `WebUI`. active (bool) – Choice to enable disable the web-ui logging system

  New in version 0.8.2.

  `logCategory` = 'webui-logger'
  `addSlash` = False
  `isLeaf` = True
  `_messages` = []
  `_ws_ready` = False
  `going_live`()
  `send_log`(type, message, *args, **kwargs)
  `log`(message, *args, **kwargs)
  `warning`(message, *args, **kwargs)
  `info`(message, *args, **kwargs)
  `critical`(message, *args, **kwargs)
  `debug`(message, *args, **kwargs)
  `error`(message, *args, **kwargs)
  `exception`(message, *args, **kwargs)

class `IndexResource`(web_resource)

  Bases: `twisted.web._element.Element`, `coherence.log.LogAble`

  A sub class of `twisted.web.template.Element` which represents the main page for the web-ui interface. This takes care of rendering the main page as an element template, so we could add some dynamic elements when initializing it, like the navigation bar or the current version of the program.

  New in version 0.8.2.

  `loader` = <XMLFile of FilePath('/home/opacam/Devel/Cohen3/coherence/web/templates/template_index.xml')>
  `version`(request, data)
  `menu`(request, data)

class `Web`(coherence)

  Bases: `object`

  Convenient class describing an adapterFactory that allow us to register `registerAdapter`

  Parameters: | coherence – An instance of ~coherence.base.Coherence

class `WebUI`(coherence, *a, **kw)

  Bases: `twisted.web.resource.Resource`, `coherence.log.LogAble`

  A convenient html interface to browse the connected devices via preferred web browser. This interface could be enabled when initializing `Coherence` by setting “‘web-ui’: ‘yes’” into your config command or via config file using the same key and value.

  Parameters: | coherence (object) – An instance of ~coherence.base.Coherence

  Changed in version 0.8.2.

  Warning

  Be aware that the browser should support Web Sockets and to have js enabled. All modern browsers should have this features integrated (tested with firefox and google chrome).

  `logCategory` = 'webui'
  `addSlash` = True
  `isLeaf` = False
  `ws_recived` = []
  `on_ws_message`(payload, isBinary)
  `render`(request)

    Render a given resource. See L{IResource}’s render method.

    I delegate to methods of self with the form ‘render_METHOD’ where METHOD is the HTTP that was used to make the request. Examples: render_GET, render_HEAD, render_POST, and so on. Generally you should implement those methods instead of overriding this one.

    render_METHOD methods are expected to return a byte string which will be the rendered page, unless the return value is C{server.NOT_DONE_YET}, in which case it is this class’s responsibility to write the results using C{request.write(data)} and then call C{request.finish()}.

    Old code that overrides render() directly is likewise expected to return a byte string or NOT_DONE_YET.

    @see: L{IResource.render}

  `render_GET`(request)
  `getChild`(name, request)

    Retrieve a ‘child’ resource from me.

    Implement this to create dynamic resource generation – resources which are always available may be registered with self.putChild().

    This will not be called if the class-level variable ‘isLeaf’ is set in your subclass; instead, the ‘postpath’ attribute of the request will be left as a list of the remaining path elements.

    For example, the URL /foo/bar/baz will normally be:

        | site.resource.getChild('foo').getChild('bar').getChild('baz').

    However, if the resource returned by ‘bar’ has isLeaf set to true, then the getChild call will never be made on it.

    Parameters and return value have the same meaning and requirements as those defined by L{IResource.getChildWithDefault}.