Utility functions

enigma2_http_api.utils.LISTING_ITEM_KEY_PAIRS = [('eventname', 'descriptionExtended'), ('title', 'longdesc'), ('name', 'descriptionextended'), ('eventname', 'description'), ('title', 'longinfo')]

list of tuples which may contain title and description of an event

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

Label:Namespace map

enigma2_http_api.utils.NS_DVB_C = 4294901760

Namespace - DVB-C services

enigma2_http_api.utils.NS_DVB_S = 12582912

Namespace - DVB-S services

enigma2_http_api.utils.NS_DVB_T = 4008574976

Namespace - DVB-T services

enigma2_http_api.utils.NS_LOOKUP = {0: 'File', 130: 'DVB-S', 192: 'DVB-S', 61166: 'DVB-T', 65535: 'DVB-C', 8519680: 'DVB-S', 12582912: 'DVB-S', 4008574976: 'DVB-T', 4294901760: 'DVB-C'}

Namespace:Label lookup map

enigma2_http_api.utils.create_picon(*args, **kwargs)[source]

Generate a (Enigma2 style) program icon 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
enigma2_http_api.utils.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
enigma2_http_api.utils.enigma_trunkname(path)[source]

Determine the trunk of enigma2 specific files.

Parameters:path – filename
Returns:trunk
>>> enigma_trunkname(None)
Traceback (most recent call last):
    ...
ValueError: None
>>> enigma_trunkname('')
Traceback (most recent call last):
    ...
ValueError: ''
>>> enigma_trunkname(2)
Traceback (most recent call last):
    ...
ValueError: 2
>>> enigma_trunkname('somefile')
Traceback (most recent call last):
    ...
ValueError: 'somefile' has no extension
>>> enigma_trunkname('somefile.bla')
Traceback (most recent call last):
    ...
ValueError: 'somefile.bla' has bad extension 'bla'
>>> enigma_trunkname('somefile.bla.bla')
Traceback (most recent call last):
    ...
ValueError: 'somefile.bla.bla' has bad extension 'bla'
>>> enigma_trunkname('somefile.ts')
'somefile'
>>> enigma_trunkname('/tmp/somefile.ts')
'somefile'
>>> enigma_trunkname('somefile.ts.sc')
'somefile'
enigma2_http_api.utils.filter_simple_events(data)[source]
[
    u'begin',
    u'sname',
    u'end',
    u'title',
    u'id',
    u'now_timestamp',
    u'picon',
    u'longdesc',
    u'duration',
    u'duration_sec',
    u'sref',
    u'date',
    u'shortdesc',
    u'progress',
    u'tleft',
    u'begin_timestamp'],
Parameters:data
Returns:
enigma2_http_api.utils.guess_namespace_label(value, fallback='UNKNOWN')[source]

Try to guess a textual representation for given namespace value.

Parameters:
  • value – namespace
  • fallback – value to be returned if no matching label is found
Returns:

label

Return type:

basestring

>>> guess_namespace_label(1234)
'UNKNOWN'
>>> guess_namespace_label(99, 'DVB-S')
'DVB-S'
>>> guess_namespace_label(0x0)
'File'
>>> guess_namespace_label(0x00c00000)
'DVB-S'
>>> guess_namespace_label(0x0c0)
'DVB-S'
>>> guess_namespace_label(0x00c0)
'DVB-S'
>>> guess_namespace_label(0x00c01234)
'DVB-S'
>>> guess_namespace_label(0x00820000)
'DVB-S'
>>> guess_namespace_label(0x0082)
'DVB-S'
>>> guess_namespace_label(0x00820082)
'DVB-S'
>>> guess_namespace_label(0x00000001)
'UNKNOWN'
>>> guess_namespace_label(0xffef1234)
'UNKNOWN'
>>> guess_namespace_label(0xeeeeffff)
'DVB-T'
enigma2_http_api.utils.normalise_servicereference(serviceref)[source]

Create a normalised representation of serviceref to be used e.g. as sorting hint

Parameters:serviceref – service reference
Returns:
>>> sref = '1:0:1:300:7:85:00c00000:0:0:0:'
>>> normalise_servicereference(sref)
'0001:0300:0007:0085:00C00000'
>>> sref2 = '1:64:A:0:0:0:0:0:0:0::SKY Sport'
>>> normalise_servicereference(sref2)
'000A:0000:0000:0000:00000000'
enigma2_http_api.utils.parse_servicereference(serviceref)[source]

Parse a Enigma2 style service reference string representation.

Parameters:serviceref (string) – Enigma2 style service reference
>>> 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_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}
>>> sref3 = '1:0:0:0:0:0:0:0:0:0:/media/hdd/movie/20170921 2055 - DASDING - DASDING Sprechstunde - .ts'
>>> result3 = parse_servicereference(sref3)
>>> result3
{'service_type': 0, 'oid': 0, 'tsid': 0, 'ns': 0, 'sid': 0}
enigma2_http_api.utils.pseudo_unique_id(item)[source]

Generate a pseudo unique ID for an event item, movie item or timer item. The ID is based on the item’s title and description attribute. Neither title nor description may be empty.

Parameters:item – event, movie or timer
Returns:generated pseudo ID
Return type:str
>>> pseudo_unique_id({'event': 1})
Traceback (most recent call last):
    ...
AssertionError: name or desc may not be None
>>> pseudo_unique_id({'eventname': 1, 'descriptionExtended': 'bla'})
Traceback (most recent call last):
    ...
AttributeError: 'int' object has no attribute 'strip'
>>> pseudo_unique_id({'eventname': "x", 'descriptionExtended': 'bla'})
'7a6615ef8ca6b06ac6a837741293759d3083a49c'
>>> pseudo_unique_id({'eventname': "x", 'description': 'bla'})
'7a6615ef8ca6b06ac6a837741293759d3083a49c'
>>> pseudo_unique_id({'eventname': " ", 'descriptionExtended': '  '})
Traceback (most recent call last):
    ...
AssertionError: name or desc may not be empty
>>> pseudo_unique_id({'title': "x", 'longdesc': 'bla'})
'7a6615ef8ca6b06ac6a837741293759d3083a49c'
>>> pseudo_unique_id({'name': "x", 'descriptionextended': 'bla'})
'7a6615ef8ca6b06ac6a837741293759d3083a49c'
>>> pseudo_unique_id({'name': "x", 'descriptionextended': ' 17 Min.'})
Traceback (most recent call last):
    ...
AssertionError: desc_mangled may not be empty
>>> pseudo_unique_id({'name': "x", 'descriptionextended': 'Bla Bla 17 Min.'})
'4db3822ad252366e57d9515b1e37d3449a45a0cb'
>>> pseudo_unique_id({'name': "x", 'descriptionextended': 'Bla Bla 18 Min.'})
'4db3822ad252366e57d9515b1e37d3449a45a0cb'
>>> pseudo_unique_id({'eventname': "x", 'descriptionExtended': 'bla', 'description': ''})
'7a6615ef8ca6b06ac6a837741293759d3083a49c'
enigma2_http_api.utils.set_output_encoding(encoding='utf-8')[source]
Stolen from https://stackoverflow.com/
questions/19696652/piping-output-causes-python-program-to-fail

When piping to the terminal, python knows the encoding needed, and sets it automatically. But when piping to another program (for example, | less), python can not check the output encoding. In that case, it is None. What I am doing here is to catch this situation for both stdout and stderr and force the encoding