utility modules

Modules in the miro package namespace located in portable/ in the source tarball that contain utility functions and classes.

commandline - commandline handling

miro.commandline – This modules handles the parsing of files/URLs passed to Miro on the command line.

Frontends should call set_ommand_line_args() passing it a list of arguments that the users gives. This should just be suspected torrents/videos, not things like --help, --version, etc.

Frontends should trap when a user opens a torrent/video with Miro while Miro is already running. They should arrange for add_video or add_torrent to be called in the existing Miro process.

miro.commandline.parse_command_line_args(args=None)

This goes through a list of files which could be arguments passed in on the command line or a list of files from other source.

If the args list is None, then this pulls from the internal _command_line_args list which is populated by set_command_line_args.

displaytext - text display functions

miro.displaytext – Format strings to send to the user.

miro.displaytext.strftime_to_unicode(bytes)

Convert the value return by strftime() to a unicode string.

By default, it’s in whatever the default codeset is.

filetypes - filetype handling

miro.filetypes - functions for determining things from the filename, enclosure, content-type, and other things.

miro.filetypes.guess_extension(mimetype)
Pass a mime type to this method and it will return a corresponding file extension, or None if it doesn’t know about the type.
miro.filetypes.guess_mime_type(filename)
Pass a filename to this method and it will return a corresponding mime type, or ‘video/unknown’ if the filename has a known video extension but no corresponding mime type, or None if it doesn’t know about the file extension.
miro.filetypes.is_allowed_filename(filename)
Pass a filename to this method and it will return a boolean saying if the filename represents video, audio or torrent.
miro.filetypes.is_audio_filename(filename)
Pass a filename to this method and it will return a boolean saying if the filename represents an audio file.
miro.filetypes.is_feed_content_type(contentType)
Is a content-type for a RSS feed?
miro.filetypes.is_feed_filename(filename)
Pass a filename to this method and it will return a boolean saying if the filename possibly represents an Atom or RSS feed URL.
miro.filetypes.is_maybe_feed_content_type(contentType)
Could the content type contain a feed?
miro.filetypes.is_maybe_rss(body)

Sniffs the body to determine whether it’s a feed or not.

this is very loosely taken from Firefox nsFeedSniffer.cpp and ideas in http://blogs.msdn.com/rssteam/articles/PublishersGuide.aspx

miro.filetypes.is_media_filename(filename)
Check if a filename is a video or audio filename
miro.filetypes.is_playable_filename(filename)
Pass a filename to this method and it will return a boolean saying if the filename represents video or audio.
miro.filetypes.is_torrent_filename(filename)
Pass a filename to this method and it will return a boolean saying if the filename represents a torrent file.
miro.filetypes.is_video_enclosure(enclosure)
Pass an enclosure dictionary to this method and it will return a boolean saying if the enclosure is a video or not.
miro.filetypes.is_video_filename(filename)
Pass a filename to this method and it will return a boolean saying if the filename represents a video file.

flashscraper - url converters for flash movie sites

miro.flashscraper – functions for converting a web-page url to a media url.

miro.flashscraper.is_maybe_flashscrapable(url)

Returns whether or not the given url is possibly handled by one of the flash url converters we have.

Example:

>>> is_maybe_flashscrapable(u"http://www.youtube.com/watch?v=qRuNxHqwazs")
True

gtcache - string localization functions

miro.gtcache – Caching gettext functions.

miro.gtcache.declarify(text)

Takes the return from a gettext call, and “declarifies” it. If the item has | symbols in it, then this splits the string on | and returns the last string. If the item has no | symbols in it, then it returns the string.

>>> declarify("foo")
'foo'
>>> declarify("View|All")
'All'

Returns a unicode string.

miro.gtcache.gettext(text, values=None)

Returns the translated form of the given text. If values are provided, expands the string with the given values.

In the case where the translated string is improperly formed and throws a ValueError when expanded, this caches and returns the original text. This reduces the likelihood that Miro will throw an error and stop functioning with bad translated strings.

For example, if the string is:

"%(countfiles) fichiers analyses"
             ^^^

the d is missing.

Note

This converts unicode strings to strings in utf-8 encoding before translating. This definitely slows things down, so if you don’t need unicode characters, use a string and not a unicode.

Returns a unicode string.

miro.gtcache.ngettext(text1, text2, count, values=None)

Given two strings and a count.

text1
the singular form of the string to be translated
text2
the plural form of the string to be translated
count
the number of things involved
values
the dict of values to expand the string with

See Python gettext.ngettext documentation and the GNU gettext documentation for more details.

http://www.gnu.org/software/gettext/manual/gettext.html#Plural-forms

Returns a unicode string.

subscription - subscribe url handlers

miro.subscription – Functions for handling subscriptions.

This file handles checking URLs that the user clicks on to see if they are subscribe links. Subscribe links are specially formatted URLs that signal that we should subscribe the user to a feed, add a new channel guide, start a new video download, or something similar.

Our basic strategy is to have have links with the host subscribe.getmiro.com. That way we can parse them in miro and have an actual page on subscribe.getmiro.com that the user will see if they click it in an actual web browser.

class miro.subscription.Subscriber

This class represents the common functionality of the subscription handlers (OPML import, one-click links in the Guide, and command-line additions).

add_subscriptions(subscriptions_list, parent_folder=None)

We loop through the list of subscriptions, creating things as we go (if needed). We also keep track of what we’ve added.

Each type (folder, feed, site, download) gets dispatched to one of our methods. Each dispatcher returns True if it’s added the subscription, anything else if it’s been ignored for some reason (generally because it’s already present in the DB).

The only exception to this is the ‘folder’ type, which has the same return signature as this method.

Returns a tuple of dictionaries (added, ignored). Each dictionary maps a subscription type (feed, site, download) to the number of added/ignored items in this subscription.

handle_download(download_dict, parent_folder)

Download subscriptions look like:

{
    'type': 'download',
    'url': URL of the file to download
    'title': name of the download (optional),
    'link': page representing this download (optional),
    'feed': RSS feed containing this item (optional),
    'mime_type': the MIME type of the item (optional),
    'description': a description of the item (optional),
    'thumbnail': a thumbnail image for the item (optional),
    'length': the length in seconds of the item (optional)
}
handle_feed(feed_dict, parent_folder)

Feed subscriptions look like:

{
    'type': 'feed',
    'url': URL of the RSS/Atom feed
    'title': name of the feed (optional),
    'section': one of ['audio', 'video'] (ignored if it's in a folder),
    'search_term': terms for which this feed is a search (optional),
    'auto_download': one of 'all', 'new', 'off' (optional),
    'expiry_time': one of 'system', 'never', an integer of hours (optional),
}
handle_folder(folder_dict, parent_folder)

Folder subscriptions look like:

{
    'type': 'folder',
    'title': name of the folder,
    'section': one of ['audio', 'video'],
    'children': a list of sub-feeds
}
handle_site(site_dict, parent_folder)

Site subscriptions look like:

{
    'type': 'site',
    'url': URL of the site
    'title': name of the site (optional),
}
Given a URL, test if it’s trying to subscribe the user using subscribe.getdemocracy.com. Returns the list of parsed URLs.

Returns whether this is a subscribe url or not.

It’s pretty hearty and shouldn’t throw exceptions.

util - utility functions

miro.util – Utility functions.

This module contains self-contained utility functions. It shouldn’t import any other Miro modules.

class miro.util.AutoFlushingStream(stream)
Converts a stream to an auto-flushing one. It behaves in exactly the same way, except all write() calls are automatically followed by a flush().
class miro.util.AutoLoggingStream(logging_callback, prefix)
Create a stream that intercepts write calls and sends them to the log.
class miro.util.ExponentialBackoffTracker(baseDelay)
Utility class to track exponential backoffs.
class miro.util.HTMLStripper

Strips html from text while maintaining links and newline-like HTML bits.

This class resets itself after every strip call, so you can re-use the class if you want. However, this class is not threadsafe.

strip(s)
Takes a string s and returns the stripped version.
class miro.util.Matrix(columns, rows, initial_value=None)

2 Dimensional matrix.

Matrix objects are accessed like a list, except tuples are used as indices, for example:

>>> m = Matrix(5, 5)
>>> m[3, 4] = "foo"
>>> m
None, None, None, None, None
None, None, None, None, None
None, None, None, None, None
None, None, None, None, None
None, None, None, 'foo', None
column(column)
Iterator that yields all the objects in a column.
remove(value)
This sets the value to None–it does NOT remove the cell from the Matrix because that doesn’t make any sense.
row(row)
Iterator that yields all the objects in a row.
exception miro.util.MiroUnicodeError
Returned when input to a template function isn’t unicode
class miro.util.ThreadSafeCounter(initialValue=0)

Implements a counter that can be access by multiple threads.

dec()
Decrements the value by 1.
getvalue()
Returns the current value.
inc()
Increments the value by 1.
miro.util.ascii_lower(str)

Converts a string to lower case, using a simple translations of ASCII characters.

This method is not locale-dependant, which is useful in some cases. Normally str.lower() should be used though.

miro.util.call_command(*args, **kwargs)
Call an external command. If the command doesn’t exit with status 0, or if it outputs to stderr, an exception will be raised. Returns stdout.
miro.util.checkB(text)
Raise an exception if input isn’t a binary string
miro.util.checkF(text)
Returns exception if input isn’t a filename type
miro.util.checkU(text)
Raise an exception if input isn’t unicode
miro.util.checkURL(text)
Raise an exception if input isn’t a URL type
miro.util.cmp_enclosures(enclosure1, enclosure2)
Returns:
-1 if enclosure1 is preferred, 1 if enclosure2 is preferred, and zero if there is no preference between the two of them
miro.util.formatSizeForUser(bytes, zeroString='', withDecimals=True, kbOnly=False)
Format an int containing the number of bytes into a string suitable for printing out to the user. zeroString is the string to use if bytes == 0.
miro.util.gather_media_files(path)

Gather media files on the disk in a directory tree. This is used by the first time startup dialog.

path – absolute file path to search

miro.util.getFirstVideoEnclosure(entry)
Find the first “best” video enclosure in a feedparser entry. Returns the enclosure, or None if no video enclosure is found.
miro.util.get_nice_stack()
Get a stack trace that’s a easier to read that the full one.
miro.util.make_dummy_socket_pair()

Create a pair of sockets connected to each other on the local interface. Used to implement SocketHandler.wakeup().

On Unixish systems, port 0 will pick the next available port. But that appears to have problems on Windows possibly with firewall software. So if we hit a socketerror with port 0, we try ports between 50000 and 65500.

miro.util.no_console_startupinfo()
Returns the startupinfo argument for subprocess.Popen so that we don’t open a console window. On platforms other than windows, this is just None. On windows, it’s some win32 silliness.
miro.util.query_revision(fn)

Called at build-time to ask Subversion for the revision number of this checkout. Going to fail without Cygwin. Yeah, oh well. Pass the file or directory you want to use as a reference point.

Returns the (url, revision) on success and None on failure.

miro.util.quoteUnicodeURL(url)
Quote international characters contained in a URL according to w3c, see: <http://www.w3.org/International/O-URL-code.html>
miro.util.read_simple_config_file(path)

Parse a configuration file in a very simple format and return contents as a dict.

Each line is either whitespace or “Key = Value”. Whitespace is ignored at the beginning of Value, but the remainder of the line is taken literally, including any whitespace.

Note: There is no way to put a newline in a value.

miro.util.returnsBinary(func)
Decorator that raised an exception if the function doesn’t return unicode
miro.util.returnsFilename(func)
Decorator that raised an exception if the function doesn’t return a filename
miro.util.returnsURL(func)
Decorator that raised an exception if the function doesn’t return a filename
miro.util.returnsUnicode(func)
Decorator that raised an exception if the function doesn’t return unicode
miro.util.setup_logging()
Adds TIMING and JSALERT logging levels.
miro.util.stringify(u, handleerror='xmlcharrefreplace')

Takes a possibly unicode string and converts it to a string string. This is required for some logging especially where the things being logged are filenames which can be Unicode in the Windows platform.

You can pass in a handleerror argument which defaults to "xmlcharrefreplace". This will increase the string size as it converts unicode characters that don’t have ascii equivalents into escape sequences. If you don’t want to increase the string length, use "replace" which will use ? for unicode characters that don’t have ascii equivalents.

Note

This is not the inverse of unicodify!

miro.util.unicodify(d)
Turns all strings in data structure to unicode.
miro.util.write_simple_config_file(path, data)
Given a dict, write a configuration file in the format that read_simple_config_file reads.

xhtmltools - xml related utility functions

miro.xhtmltools – XML related utility functions.

class miro.xhtmltools.XHTMLifier

Very simple parser to convert HTML to XHTML

convert(data, addTopTags=False, filterFontTags=False)
Converts an HTML data unicode string to an XHTML data unicode string.
miro.xhtmltools.fix_html_header(data, charset)

Adds a <meta http-equiv=”Content-Type” content=”text/html; charset=blah”> tag to an HTML document

Since we’re only feeding this to our own HTML Parser anyway, we don’t care that it might bung up XHTML.

miro.xhtmltools.fix_xml_header(data, charset)
Adds a <?xml ?> header to the given xml data or replaces an existing one without a charset with one that has a charset
miro.xhtmltools.unescape(data)
Parses HTML entities in data
miro.xhtmltools.url_encode_dict(orig)
Converts a Python dictionary to data suitable for a POST or GET submission
miro.xhtmltools.urldecode(data)
Gets a string from a URL
miro.xhtmltools.urlencode(data)
Encodes string for use in a URL
miro.xhtmltools.xhtmlify(data, addTopTags=False, filterFontTags=False)
Returns XHTMLified version of HTML document