Utilities

controllers.utilities.NS = {'DVB-C': 4294901760, 'DVB-S': 12582912, 'DVB-T': 4008574976}

Namespace map

Type:Label
controllers.utilities.NS_DVB_C = 4294901760

Namespace - DVB-C services

controllers.utilities.NS_DVB_S = 12582912

Namespace - DVB-S services

controllers.utilities.NS_DVB_T = 4008574976

Namespace - DVB-T services

controllers.utilities.NS_LOOKUP = {12582912: 'DVB-S', 4008574976: 'DVB-T', 4294901760: 'DVB-C'}

Label lookup map

Type:Namespace
controllers.utilities.PATTERN_HOST_HEADER = '(\\[(?P<ipv6_addr>.+?)\\]|(?P<hostname>.+?))(\\:(?P<port>\\d+))?$'

regular expression pattern for strings containing something resembling a hostname/port combination (<hostname>:<port>)

controllers.utilities.add_expires_header(request, expires=False)[source]
Parameters:
  • request (twisted.web.server.Request) – HTTP request object
  • expires – expiration in seconds or False for imediately / no caching
controllers.utilities.build_url(hostname, path=None, args=None, scheme='http', port=None)[source]

Create an URL based on parameters.

Parameters:
  • hostname – hostname portion
  • path – path portion
  • args – query parameters
  • scheme – scheme portion
  • port – port portion
Returns:

Generated URL

Return type:

basestring

>>> build_url("some.host", "/", {})
'http://some.host/'
>>> build_url("some.host", "/")
'http://some.host/'
>>> build_url("some.host")
'http://some.host'
>>> build_url("some.host", port=27080)
'http://some.host:27080'
>>> build_url("", port=27080)
Traceback (most recent call last):
    ...
ValueError: empty hostname!
>>> build_url("some.host", "x")
'http://some.host/x'
>>> build_url("some.host", "/x")
'http://some.host/x'
>>> build_url("some.host", "/x/")
'http://some.host/x/'
>>> build_url("some.host", "x/")
'http://some.host/x/'
>>> build_url("some.host", "x/../")
'http://some.host/x/../'
>>> build_url("some.host", "/:x/äöü-blabla/")
'http://some.host/%3Ax/%C3%A4%C3%B6%C3%BC-blabla/'
>>> build_url("some.host", u'/:x/äöü-blabla/'.encode('utf-8'))
'http://some.host/%3Ax/%C3%A4%C3%B6%C3%BC-blabla/'
controllers.utilities.create_servicereference(*args, **kwargs)[source]

Generate a (Enigma2 style) service reference string representation.

Parameters:
  • args[0] (dict) – Service Reference Parameter as dict
  • service_type (int) – Service Type
  • sid (int) – SID
  • tsid (int) – TSID
  • oid (int) – OID
  • ns (int) – Enigma2 Namespace
controllers.utilities.get_config_attribute(path, root_obj, head=None)[source]

Determine attribute of root_obj to be accessed by path in a (somewhat) safe manner. This implementation will allow key and index based accessing too (e.g. config.some_list[0] or config.some_dict['some_key']) The path value needs to start with head (default=’config’).

Parameters:
  • path – character string specifying which attribute is to be accessed
  • root_obj – An object whose attributes are to be accessed.
  • head – Value of the first portion of path
Returns:

Attribute of root_obj

Raises:
  • ValueError – If path is invalid.
  • AttributeError – If attribute cannot be accessed
controllers.utilities.get_servicereference_portions(value, raise_on_empty=False)[source]

Try to match possible portions of a servicereference in value.

Parameters:
  • value (basestring) – a servicereference-like string
  • raise_on_empty (boolean) – If a ValueError should be raised
Returns:

matched portions

Return type:

list

Raises:

ValueError – If result is empty list and raise_on_empty is True

>>> deadbeef = ['de', 'ad', 'be', 'ef']
>>> get_servicereference_portions(None)
[]
>>> get_servicereference_portions(True)
[]
>>> get_servicereference_portions(False)
[]
>>> get_servicereference_portions('de:ad:be:ef') == deadbeef
True
>>> get_servicereference_portions('de,ad$be_ef??') == deadbeef
True
>>> get_servicereference_portions('-1:ad:be:ef:')
['1', 'ad', 'be', 'ef']
>>> get_servicereference_portions('-^ghi', raise_on_empty=True)
Traceback (most recent call last):
    ...
ValueError: -^ghi
>>> get_servicereference_portions('1:0:19:7C:6:85:FFFF0000:0:0:0:')
['1', '0', '19', '7C', '6', '85', 'FFFF0000', '0', '0', '0']
controllers.utilities.lenient_decode(value, encoding=None)[source]

Decode an encoded string and convert it to an unicode string.

Parameters:
  • value – input value
  • encoding – string encoding, defaults to utf-8
Returns:

decoded value

Return type:

unicode

>>> lenient_decode("Hallo")
u'Hallo'
>>> lenient_decode(u"Hallo")
u'Hallo'
>>> lenient_decode("HällöÜ")
u'H\xe4ll\xf6\xdc'
controllers.utilities.lenient_force_utf_8(value)[source]
Parameters:value – input value
Returns:basestring utf-8 encoded value
>>> isinstance(lenient_force_utf_8(''), basestring)
True
>>> lenient_force_utf_8(u"Hallo")
'Hallo'
>>> lenient_force_utf_8("HällöÜ")
'H\xc3\xa4ll\xc3\xb6\xc3\x9c'
controllers.utilities.mangle_host_header_port(value=None, fallback_port='80', fallback_hostname='localhost', want_url=False)[source]
Parameters:
  • value – header data
  • fallback_port – fallback value for port (default 80)
  • fallback_hostname – fallback value for hostname (default localhost)
  • want_url – return an URL string
Returns:

Mangled port, proto values

Return type:

dict

>>> resi1 = mangle_host_header_port()
>>> resi1['netloc']
'localhost'
>>> resi2 = mangle_host_header_port("localhost:80")
>>> resi2['netloc']
'localhost'
>>> resi3 = mangle_host_header_port("x:123")
>>> resi3['netloc']
'x:123'
>>> resi4 = mangle_host_header_port("haha:342111")
>>> resi4['netloc']
'haha'
>>> resi5 = mangle_host_header_port("haha:342111", want_url=True)
>>> resi5
'http://haha'
>>> resi6 = mangle_host_header_port("localhost:12345", want_url=True)
>>> resi6
'http://localhost:12345'
>>> mangle_host_header_port("[2001:0db8:85a3:08d3::0370:7344]", want_url=True)  # NOQA
'http://[2001:0db8:85a3:08d3::0370:7344]'
>>> mangle_host_header_port("[2001:0db8:85a3:08d3::0370:7344]")['netloc']
'[2001:0db8:85a3:08d3::0370:7344]'
>>> mangle_host_header_port("[2001:0db8:85a3:08d3::0370:7344]:8080/", want_url=True)  # NOQA
'http://[2001:0db8:85a3:08d3::0370:7344]:8080/'
>>> mangle_host_header_port("[2001:0db8:85a3:08d3::0370:7344]:8080")['netloc']  # NOQA
'[2001:0db8:85a3:08d3::0370:7344]:8080'
controllers.utilities.mangle_service_type_arg(item)[source]

Translate ‘tv’ or ‘radio’ to a set containing the needed service_type IDs. Other values of item are expected to be an integer value.

Parameters:item (str or int) – service_type value
Returns:service_type IDs
Return type:set
Raises:ValueError – If a non-integer value was provided
>>> mangle_service_type_arg("tv") == set([1, 195, 134, 17, 22, 25, 31])
True
>>> mangle_service_type_arg("radio") == set([2, 10])
True
>>> mangle_service_type_arg(0x10) == { 16 }
True
>>> mangle_service_type_arg(1) == {1}
True
>>> mangle_service_type_arg("1") == {1}
True
>>> mangle_service_type_arg("") == {1}
Traceback (most recent call last):
    ...
ValueError: invalid literal for int() with base 10: ''
controllers.utilities.mangle_snp(value)[source]

Mangle service_name as suggested by SNP (Service Name Picons)

Parameters:value (basestring) – service name
Returns:normalised service name
Return type:str
>>> mangle_snp('?ANTENNE? BAYERN')
'antennebayern'
>>> mangle_snp('?Sky? ?Cine?ma +?24?')
'skycinemaplus24'
controllers.utilities.parse_servicereference(serviceref, separators=None, extended=False)[source]

Parse a Enigma2 style service reference string representation.

Parameters:
  • serviceref – Enigma2 style service reference
  • separators – Allowed separators
Returns:

dict containing parsed values

Raises:

ValueError – If serviceref is invalid.

>>> sref = '1:0:1:300:7:85:00c00000:0:0:0:'
>>> result = parse_servicereference(sref)
>>> result
{'service_type': 1, 'oid': 133, 'tsid': 7, 'ns': 12582912, 'sid': 768}
>>> sref_dashes = '1-0-1-300-7-85-00c00000-0-0-0-'
>>> result_dashes = parse_servicereference(sref_dashes, ':-')
>>> result == result_dashes
True
>>> sref_g = create_servicereference(**result)
>>> sref_g
'1:0:1:300:7:85:00c00000:0:0:0:'
>>> sref_g2 = create_servicereference(result)
>>> sref_g2
'1:0:1:300:7:85:00c00000:0:0:0:'
>>> sref == sref_g
True
>>> sref2 = '1:64:A:0:0:0:0:0:0:0::SKY Sport'
>>> result2 = parse_servicereference(sref2)
>>> result2
{'service_type': 10, 'oid': 0, 'tsid': 0, 'ns': 0, 'sid': 0}
>>> result2e = parse_servicereference(sref2, extended=True)
>>> result2e['kind']
100
>>> sref3 = '1:0:0:0:0:0:0:0:0:0:/media/hdd/movie/20170921 2055 - DASDING - DASDING Sprechstunde - .ts'  # NOQA
>>> result3 = parse_servicereference(sref3)
>>> result3
{'service_type': 0, 'oid': 0, 'tsid': 0, 'ns': 0, 'sid': 0}
controllers.utilities.parse_simple_index(source)[source]
>>> snp_index = os.path.join(CONTRIB, 'picon-source/snp.index')
>>> snp = parse_simple_index(snp_index)
>>> len(snp.keys()) > 1
True
>>> snp['wdrduesseldorf']
'wdr'
>>> snp['wdrdusseldorf']
'wdr'
controllers.utilities.require_valid_file_parameter(request, parameter_key)[source]
Parameters:
Returns:

existing filename

Return type:

basestring

Raises:
  • ValueError – If parameter_key is missing.
  • IOError – If filename does not point to an existing file path
controllers.utilities.sanitise_filename_slashes(value)[source]
Parameters:value (basestring) – input value
Returns:value w/o multiple slashes
>>> in_value = "///tmp/x/y/z"
>>> expected = re.sub("^/+", "/", "///tmp/x/y/z")
>>> sanitise_filename_slashes(in_value) == expected
True
controllers.models.model_utilities.mangle_epg_text(value)[source]

Remove EPG data-specific utf-8 characters. Replace EPG specific newline character 0xc28a with newline.

Parameters:request (basestring) – EPG text
Returns:mangled EPG text
Return type:(basestring)
controllers.models.stream.create_file_m3u(request)[source]

Create M3U contents for file streaming.

Parameters:request (twisted.web.server.Request) – HTTP request object
Returns:M3U contents
controllers.models.stream.create_stream_m3u(session, request, m3ufile)[source]

Create M3U contents for service streaming.

Parameters:
Returns:

M3U contents

controllers.models.stream.create_transcoding_args(machinebuild, for_phone)[source]

Create transcoding query parameters dict based on machine build and is mobile device option.

Parameters:
  • machinebuild – machine build indicator
  • for_phone – for mobile device indicator
Returns:

query parameters

Return type:

dict

controllers.models.movies.MOVIE_LIST_ROOT_FALLBACK = '/media'

fallback value for ‘movie list root’

controllers.models.movies.MOVIE_LIST_SREF_ROOT = '2:0:1:0:0:0:0:0:0:0:'

servicereference prefix for movie itmes

controllers.models.movies.getMovieList(rargs=None, locations=None)[source]

Generate a dict containing movie items information.

Deprecated since version 0.86.

Parameters:
  • rargs (dict) – request object’s args
  • locations (list) – paths where recordings might be stored
Returns:

movie items

Return type:

dict

controllers.models.movies.getPosition(cutfile, movie_len)[source]

Retrieve ‘last watched’ position.

Deprecated since version 0.86.

Parameters:
  • cutfile (basestring) – movie’s cutfile path
  • movie_len (int) – movie length in minutes
Returns:

movie items

Return type:

dict