Python Bindings

From MythTV Official Wiki
(Redirected from 0.25 Python Bindings)
Jump to: navigation, search


The MythTV Python bindings are a set of bindings designed to provide access to the MythTV database and interfaces in an object oriented, 'pythonic' manner. If upgrading from 0.23, please see the upgrade guide.


These are the external runtime dependencies required by the python bindings. They are checked at configure time prior to installation, and the bindings will not install if these are not met.

Also required is one of the following two modules.

Python 2.7 is only supported in combination with the oursql module

Data Model

The data model used in the bindings is based around the DictData class, an ordered dictionary class intended to abstract lists of data provided by the backend and database, and provide them to users as dictionary values or object attributes.

Connection Classes

Data Classes

Backend Data

Database Read-Only

Database Read-Write

External Classes

These classes provide access to external functions.

Other Classes


The MythLog class intends to provide a logging faculty emulating that used by the MythTV libraries. The class accepts three keyword inputs:

  • module - (optional) string, to be used for identification of log entries (def: 'pythonbindings')
  • lstr - (optional) string, comma separated list of log filters
  • lbit - (optional) bitwise, operator defining which log filters are active

The log filters are a number of keywords defining what sections of logs should be displayed. The stored filter is a bitwise operator, so multiple sections can be active simultaneously. If any bit in the filter matches the section specified in an individual log entry, the log will be passed. If neither 'lstr' nor 'lbit' is defined, the class will default to 'important,general'. The log filter is a global value, and will be shared among all instances of the MythLog class.

The MythLog.log method takes four inputs:

  • mask - bitwise operator to test against log filter
  • level - logging level
  • message - log message to output
  • detail - (optional) additional detail to output

The mask accepts multiple values, and if any bit matches with the filter, the message will be formatted and outputted by the log, but only if the given level is less than or equal to the current logging level. The formatting exists as:

YYYY-MM-DD HH:mm:ss.ttt <module>: <message>
YYYY-MM-DD HH:mm:ss.ttt <module>: <message> - <detail>

The logger accepts multi-line messages, and will format them as:

YYYY-MM-DD HH:mm:ss.ttt <module>:
    <message line 1>
    <message line 2>
    <message line ...>
        <detail line 1>
        <detail line ...>

Objects of the MythLog class are callable directly, and will run this 'log' method.

The MythLog.logTB method takes level as an input and will print a copy of the current traceback to the log.


The MythLog class contains several classmethods for altering the function of all open instances.

  • _setlevel - accepts 'lstr' and 'lbit' inputs, can be used to directly alter the log filter
  • _setfile - accepts a filename, opens a new file path to log to, closing the previous (WARNING: file will be truncated)
  • _setfileobject - accepts a file object to log to, closing the previous


Five exception classes are available for error handling. All errors will have at minimum 'ecode', 'ename', and 'args' defined as attributes. 'args' will be a list with a single string at index 0. The exceptions may provide additional attributes depending on the error code.


Base exception class, can be used to except all internally thrown errors.

  • SYSTEM - retcode, command, stderr
  • SOCKET - sockcode, sockerr


  • DB_RAW - sqlerr
  • DB_CONNECTION - dbconn
  • DB_SETTING - setting, hostname
  • DB_SCHEMAMISMATCH - setting, remote, local
  • DB_SCHEMAUPDATE - sqlerr


  • PROTO_CONNECTION - backend, port
  • PROTO_ANNOUNCE - backend, port, response
  • PROTO_MISMATCH - remote, local


  • FE_CONNECTION - frontend, port
  • FR_ANNOUNCE - frontend, port


  • FILE_FAILED_WRITE - file, reason
  • FILE_FAILED_SEEK - file, offset, whence

For 'try' statements, MythError is a catch-all class. The exception will provide three attributes:

  • args - tuple containing a single error string
  • ename - string with the name of the error code
  • ecode - numeric error code



The OrdDict class provides an ordered dictionary implementation. It behaves exactly as a normal dictionary, except that order of values are maintained in the order that they are added. Python 2.7 now includes an ordered dictionary implementation, so this class should be considered obsolete, and due for removal at such time as Python 2.7 or 3.x is make a minimum requirement.


The DictInvert class provides a pair of entangled dictionaries, which have their values and keys swapped. Any new entries added to one is automatically mapped to the other.

UPnP Search

The MSearch class provides a basic UPnP search utility. The search method accepts the following inputs:

  • timeout - float, number of seconds to run the search before being automatically terminated
  • filter - a single string or list/tuple of strings, if given, only results whose URN is listed in the filter will be passed

The search method is a generator, only processing results as requested by the user. As with all iterables, the result is only available once, and must be stored manually if multiple uses are desired. The method returns a dictionary with the following fields:

  • content-length
  • request
  • date
  • usn
  • location
  • cache-control
  • server
  • ext
  • st

The search method can be terminated prematurely by the terminate method.

Canned searches of searchMythBE and searchMythFE will return one instance per host found.


This class connects to a backend and will remain locked until it has received an event matching a given compiled regular expression. The wait method will block until the event is received and the object unlocked.


The function takes a Myth URI and returns a file object, either from the local file system if found locally, or from a FileTransfer socket connected to the backend.


Advanced Use