Internet radio browser GUI for music/video streams from various directory services.

⌈⌋ branch:  streamtuner2


Check-in [c83b236a5a]

Overview
Comment:Perform basic version dependency checks before displaying downloadable plugins.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: c83b236a5ad18baa43c99d11bd5d7d59ea95a454
User & Date: mario on 2015-05-03 20:24:47
Other Links: manifest | tags
Context
2015-05-03
20:26
Add some version: bumps and add a few versions to depends: references. check-in: beebec9c76 user: mario tags: trunk
20:24
Perform basic version dependency checks before displaying downloadable plugins. check-in: c83b236a5a user: mario tags: trunk
20:23
Disable iCast plugin for now. check-in: 9d1b6cc7f5 user: mario tags: trunk
Changes

Modified channels/pluginmanager2.py from [d1c55d3594] to [978f889bbd].

1
2
3
4
5
6
7
8

9
10
11
12
13
14
15
1
2
3
4
5
6
7

8
9
10
11
12
13
14
15







-
+







# encoding: UTF-8
# api: streamtuner2
# title: User Plugin Manager Ⅱ
# description: Downloads new plugins, or updates them.
# version: 0.2
# type: hook
# category: config
# depends: uikit, config, pluginconf
# depends: uikit >= 1.9, config >= 2.7, streamtuner2 >= 2.1.8, pluginconf < 1.0
# config:
#   { name: plugin_repos, type: text, value: "http://fossil.include-once.org/repo.json/streamtuner2/contrib/*.py, http://fossil.include-once.org/repo.json/streamtuner2/channels/*.py", description: "Plugin repository JSON source references." }
#   { name: plugin_auto, type: boolean, value: 1, description: Apply plugin activation/disabling without restart. }
# priority: extra
# support: experimental
#
# Scans for new plugins from the repository server, using
32
33
34
35
36
37
38

39
40
41
42
43
44
45
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46







+







from channels import __path__ as channels__path__
import os
from config import *
from uikit import *
import ahttp
import json
import compat2and3
from xml.sax.saxutils import escape as html_escape


# Plugin manager
class pluginmanager2(object):

    module = "pluginmanager2"
    meta = plugin_meta()
121
122
123
124
125
126
127
128
129

130
131
132
133

134
135
136

137

138
139
140
141







142
143
144
145
146

147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163

164

165
166
167
168
169
170
171
122
123
124
125
126
127
128


129

130
131

132
133
134
135
136

137
138



139
140
141
142
143
144
145
146
147
148


149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167

168
169
170
171
172
173
174
175







-
-
+
-


-
+



+
-
+

-
-
-
+
+
+
+
+
+
+



-
-
+

















+
-
+







        for url in re.split("[\s,]+", conf.plugin_repos.strip()):
            if re.match("https?://", url):
                d = ahttp.get(url, encoding='utf-8') or []
                meta += json.loads(d)
                self.parent.status()
        
        # Clean up placeholders in vbox
        vbox = self.vbox
        for c in vbox.get_children()[3:]:
        _ = [self.vbox.remove(c) for c in self.vbox.get_children()[3:]]
            vbox.remove(c)
        
        # Query existing plugins
        have = {name: plugin_meta(module=name) for name in module_list()}
        dep = dependency()
        # Attach available downloads
        for newpl in meta:
            id = newpl.get("$name")
            # skip __init__.py
            if id.find("__") == 0:   # skip __init__.py
            if id.find("__") == 0:
                continue
            if have.get(id):
                if have[id]["version"] >= newpl.get("version"):
                    continue;
            # exclude if newer/current version already installed
            if dep.have.get(id) and dep.have[id]["version"] >= newpl.get("version"):
                continue
            # check dependencies
            #newpl["depends"] = "streamtuner2 < 2.2.0, config >= 2.5"
            if not dep.depends(newpl):
                continue
            self.add_plugin(newpl)

        # Readd some filler labels
        for i in range(1,3):
            self.add_(uikit.label(""))
        _ = [self.add_(uikit.label("")) for i in range(1,3)]


    # Entry for plugin list
    def add_plugin(self, p):
        b = self.button("Install", stock="gtk-save", cb=lambda *w:self.install(p))
        p = self.update_p(p)
        text = "<b>$title</b>, "\
               "<small>version:</small> <span weight='bold' color='orange'>$version</span>, "\
               "<small>type: <i><span color='#559'>$type</span></i> "\
               "category: <i><span color='blue'>$category</span></i></small>\n"\
               "<span variant='smallcaps' color='#333'>$description</span>\n"\
               "<span size='small' color='#532' weight='ultralight'>$extras, <a href='view-source:$file'>view src</a></span>"
        self.add_(b, safe_format(text, **p), markup=1)

        
    # Add placeholder fields
    def update_p(self, p):
        fields = ("status", "priority", "support", "author", "depends")
        extras = ["{}: <b>{}</b>".format(n, p[n]) for n in ("status", "support", "author", "depends") if p.get(n)]
        extras = ["{}: <b>{}</b>".format(n, html_escape(p[n])) for n in fields if p.get(n)]
        p["extras"] = " ".join(["💁"] + extras)
        p["file"] = p["$file"]
        for field in ("version", "title", "description", "type", "category"):
            p.setdefault(field, "-")
        return p
    

202
203
204
205
206
207
208
209
























































210
211
212
213
214
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274








+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+





                log.WARN("Cannot disable feature plugin '{}'.".format(name))
                p.status("Disabling feature plugins requires a restart.")

        # just let main load any new plugins
        p.load_plugin_channels()



# Do minimal depends: probing
class dependency(object):

    # prepare list of known plugins and versions
    def __init__(self):
        self.have = {name: plugin_meta(module=name) for name in module_list()}
        # dependencies on core modules are somewhat more interesting:
        self.have.update({
            "streamtuner2": plugin_meta(module="st2", plugin_base=["config"]),
            "uikit": plugin_meta(module="uikit", plugin_base=["config"]),
            "config": plugin_meta(module="config", plugin_base=["config"]),
            "action": plugin_meta(module="action", plugin_base=["config"]),
        })
    have = {}

    # depends:    
    def depends(self, plugin):
        if plugin.get("depends"):
            d = self.deps(plugin["depends"])
            if not self.cmp(d, self.have):
                return False
        return True

    # Split trivial "pkg, mod >= 1, uikit < 4.0" list
    def deps(self, dep_str):
        d = []
        for dep in re.split(r"\s*[,;]+\s*", dep_str):
            # skip deb:pkg-name, rpm:name, bin:name etc.
            if not len(dep) or dep.find(":") >= 0:
                continue
            # find comparison and version num
            m = re.search(r"([\w.-]+)\s*([>=<!~]+)\s*([\d.]+([-~.]\w+)*)", dep + " >= 0")
            if m and m.group(2):
                d.append([m.group(i) for i in (1,2,3)])
        return d
    
    # Do actual comparison
    def cmp(self, d, have):
        r = True
        for name, op, ver in d:
            # skip unknown plugins, might be python module references ("depends: re, json")
            if not have.get(name, {}).get("version"):
                continue
            curr = have[name]["version"]
            tbl = {
               ">=": curr >= ver,
               "<=": curr <= ver,
               "==": curr == ver,
               ">":  curr > ver,
               "<":  curr < ver,
               "!=": curr != ver,
            }
            r &= tbl.get(op, True)
        return r


# Alternative to .format(), with keys possibly being absent
from string import Template
def safe_format(str, **kwargs):
    return Template(str).safe_substitute(**kwargs)