-
Notifications
You must be signed in to change notification settings - Fork 30
/
channel.py
123 lines (86 loc) · 3.12 KB
/
channel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
"""
Meta and Base Classes for basic channel support
"""
STATUS_BAD, STATUS_UGLY, STATUS_GOOD = 0, 1, 2
class ChannelRegistry(object):
def __init__(self):
self.channels = {}
def register(self, channel_class):
"""
Registers a channel subclass for use by the plugin.
"""
sn = channel_class.short_name
if sn in self.channels:
raise Exception("a channel with the short_name '%s' is already registered."%sn)
self.channels[channel_class.short_name] = channel_class
def unregister(self, channel_class_or_short_name):
"""
Un-register a channel subclass.
"""
if isinstance(channel_class_or_short_name, Channel):
sn = channel_class_or_short_name.short_name
else:
sn = channel_class_or_short_name
if sn not in self.channels:
raise Exception("channel %s is not registered" % (sn,))
del self.channels[sn]
class ChannelMetaClass(type):
registry = ChannelRegistry()
def __init__(cls, name, bases, d):
is_abstract = d.get('is_abstract', False)
super(ChannelMetaClass, cls).__init__(name, bases, d)
if not is_abstract:
cls.registry.register(cls)
class ChannelException(Exception): pass
class BaseChannel(object):
"""
The Base of all Channel classes.
"""
in_development = False
cache_timeout = 60*60*4
short_name = None
long_name = None
icon_path = None
description = None
root_url = ''
swf_url = None
is_abstract = True
status = STATUS_GOOD
default_action = 'browse'
__metaclass__ = ChannelMetaClass
def __init__(self, plugin, **kwargs):
self.plugin = plugin
self.args = kwargs
self.cache_timeout = int(self.plugin.get_setting('default_cache_timeout'))
@classmethod
def get_channel_entry_info(self):
"""
This method is responsible for returning the info
used to generate the Channel listitem at the plugin's
root level.
"""
info = {
'Title': self.long_name,
'Thumb': self.icon_path,
'action': self.default_action,
'remote_url': None,
'channel': self.short_name,
'use_rtmp': 0,
}
return info
def action_browse(self):
rurl = self.get_url(self.args['remote_url'])
self.plugin.add_list_item({'Title': 'Hi!'})
self.plugin.end_list()
def get_url(self, url=None):
if url is None:
url = self.get_root_url()
return "%s%s" % (self.base_url, url)
def get_root_url(self):
return self.root_url
def __call__(self):
action = self.args.get('action', 'browse')
if not hasattr(self, 'action_%s' % (action,)):
raise ChannelException("No Such Action: %s" % (action,))
action_method = getattr(self, 'action_%s' % (action, ))
return action_method()