0.23 Python bindings/Transition Guide

From MythTV Official Wiki
Revision as of 13:15, 20 July 2010 by Wagnerrp (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

As the new Python bindings in 0.23 will not be backwards compatible, and will break all but the most basic uses, this guide aims to explain the updates needed to use the new bindings.

MythDB

  • MythDB has been split into three different sections. The base class, MythDBConn, provides the basic connection to the MySQL database. MythDBBase provides automatic caching of connections, table descriptions, and settings, as well as access to storage group information. The remainder of the functions remain in the MythDB class.
  • MythDBBase will only open a single connection for each database/server pair. There will be no undue load on the MySQL server for opening thousands of new connections to it. As a side effect, only one set of credentials can be used at a time for a given address and port.
  • MySQL has a default connection timeout of 8 hours, after which the client is dropped. The cursor() function now checks for this, and will reconnect to the database if needed when a new cursor is opened. So that means close your cursors when you're done using them, and open a new one later.

get/setSetting

  • The getSetting() and setSetting() functions have been discarded in favor of the 'settings' psuedo-variable. Settings are can now be accessed as dictionary entry, or class attributes (any value with a space or period will need to be accessed as a dictionary entry). Note that any retrieved setting will be cached locally and not pulled again from the database.
>>> from MythTV import MythDBBase
>>> db = MythDBBase()
>>> db.settings.NULL.DBSchemaVer
u'1247'
>>> db.settings.mybackend.BackendServerIP = '192.168.2.50'
>>> db.settings['mybackend'].BackendServerIP
u'192.168.2.50'
>>> db.settings.NULL.mythvideo.DBSchemaVer
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'DBSchemaVer'
>>> db.settings.NULL['mythvideo.DBSchemaVer']
u'1031'
  • getAllSettings has been removed entirely.

MythTV

  • This has been split into MythBEConn, MythBEBase, MythBE, and FileOps. MythBEConn provides basic socket access to the backend. MythBEBase provides connection caching for MythBEConn. FileOps contains getRecording(), deleteRecording(), forgetRecording(), deleteFile(), and getHash() for dependency requirements. MythBE has access to all available functions and for nearly all purposes should be the only one used.
  • The getFrontend() and getFrontends() functions have been moved to MythDB.

MythVideo

  • This class has been completely gutted. It now contains two functions used for searching videos, and one to prune non-existent files from the database.

Video

  • The majority of the functionality has been moved to this class. These are returned by the two search functions in MythVideo, and each instance manages one entry in videometadata. This class defines each database field as editable through dictionary entries or class attributes. New entries can be added to the database with create(), and new changes can be pushed to an existing entry with update().
  • Cast, genres, and countries are now accessible through pseudo-lists of the same name. Existing values will be presented as a list, and can be managed with add(), delete(), and clean() functions.
>>> from MythTV import Video, MythVideo
>>> mvid = MythVideo()
>>> vids = mvid.searchVideos(season=2,episode=18,title='Chuck')
>>> vids
(Chuck - 2x18 - Chuck Versus the Broken Heart,)
>>> vid = mvid.getVideo(title='The Enemy Below')
>>> vid.cast
Biff Elliot, Frank Albertson, Kurt Kreuger, Russell Collins, Al Hedison, Theodore Bikel, Curd Jürgens, Robert Mitchum
>>> vid.inetref
u'15876'
>>> vid.open()
<open file u'/mnt/mythtv/store/media/video/Movies/The Enemy Below.mpg', mode 'r' at 0x80326fad0>
>>> vid.openFanart()
<open file u'/mnt/mythtv/store/MythVideo/Fanart/0050356.jpg', mode 'r' at 0x80326fad0>

FileTransfer

  • This class behaves as before, emulating a standard python file pointer, however should no longer be called directly. File objects should be opened through the ftopen() helper function, which takes a standard myth URI, and will attempt to access the file locally, before falling back to a file transfer socket.
  • The Program, Recorded, and Video classes have been given open() functions to directly open the file.
>>> from MythTV import ftopen
>>> ftopen('myth://localbackend:6543/2063_20091231033000.mpg','r')
<open file '/mnt/mythtv/fserve_1/video/2063_20091231033000.mpg', mode 'r' at 0x8028cdc68>
>>> ftopen('myth://remotebackend:6543/2063_20091231033000.mpg','r')
<MythTV.MythData.FileTransfer object at 0x8032322d0>

MythLog

  • This has been rewritten to match the logging methods used by the standard myth binaries. Log levels and filters are now a bitwise value, using all available values as see in 'mythfrontend -h help'. Any log matching the global filter will be printed.
  • Both the individual logs, and the global filter can be include several levels, and if any is matched, the log will be printed. The default global filter level is 'important,general'.
  • The global filter can be set in the constructor or with the staticmethod _setlevel(), and allows bitwise (lbit) or string (lstr). The string is the standard logging format used by all myth binaries.
  • The Msg() function is replaced by the log() function for individual logging.
# basic filtered logging
>>> from MythTV import MythLog
>>> log = MythLog('testlog 1')
>>> log.log(log.SOCKET,'test message','more information')
>>> log.log(log.GENERAL,'test message')
2009-12-31 17:25:35.326 testlog 1: test message


# changing the default filter
>>> MythLog._setlevel(lstr='all,noupnp')
>>> log.log(log.UPNP,'upnp test message')
>>> log.log(log.SOCKET,'test message','more information')
2009-12-31 17:27:49.146 testlog 1: test message -- more information

# complex filter/log levels
>>> MythLog._setlevel(lbit=MythLog.GENERAL|MythLog.CHANNEL|MythLog.AUDIO|MythLog.IDLE)
>>> log.log(log.IMPORTANT|log.MEDIA,'multi-level message')
>>> log.log(log.GENERAL|log.IMPORTANT|log.MEDIA,'multi-level message')
2009-12-31 17:32:34.091 testlog 1: multi-level message