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

⌈⌋ ⎇ branch:  streamtuner2


Check-in [475f736d5d]

Overview
Comment:Split up post-proccessing filters (run after load/update_streams), use filter callback list now. Separate prepare display filters as well (so to hook dedicated favicon callback into). Move conf.show_favicons option into uikit.columns() signature.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 475f736d5d371199e5f1d130cea864e0ea42644e
User & Date: mario on 2015-05-12 20:01:58
Other Links: manifest | tags
Context
2015-05-12
20:03
Use GenericChannel.prepare_filters hook to update "favicon" filenames in rows. Precompile row_to_fn regexps. Fix google_find_homepage params (just a function, not a method). check-in: 8c0b288e66 user: mario tags: trunk
20:01
Split up post-proccessing filters (run after load/update_streams), use filter callback list now. Separate prepare display filters as well (so to hook dedicated favicon callback into). Move conf.show_favicons option into uikit.columns() signature. check-in: 475f736d5d user: mario tags: trunk
2015-05-11
20:29
Break out _on_reload thread callback. check-in: 306eba6f98 user: mario tags: trunk
Changes

Modified channels/__init__.py from [f638d3bd60] to [8b2d122cbc].

130
131
132
133
134
135
136









137
138
139
140
141
142
143
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152







+
+
+
+
+
+
+
+
+







        self.title = self.meta.get("title", self.module)

        # add default options values to config.conf.* dict
        conf.add_plugin_defaults(self.meta, self.module)
        
        # Only if streamtuner2 is run in graphical mode        
        if (parent):
            # Update/display stream processors
            self.prepare_filters += [
                self.prepare_filter_icons,
            ]
            self.postprocess_filters += [
                self.postprocess_filter_required_fields,
                self.postprocess_filter_homepage,
            ]
            # Load cache, instantiate Gtk widgets
            self.cache()
            self.gui(parent)

        # Stub for ST2 main window / dispatcher
        else:
            self.parent = stub_parent(None)

162
163
164
165
166
167
168
169

170
171
172
173
174
175
176
171
172
173
174
175
176
177

178
179
180
181
182
183
184
185







-
+







        self.columns([])
        
        # add to main menu
        uikit.add_menu([parent.channelmenuitems], self.meta["title"], lambda w: parent.channel_switch_by_name(self.module) or 1)

    # Just wraps uikit.columns() to retain liststore, rowmap and pix_entry
    def columns(self, entries=None):
        self.ls, self.rowmap, self.pix_entry = uikit.columns(self.gtk_list, self.datamap, entries)
        self.ls, self.rowmap, self.pix_entry = uikit.columns(self.gtk_list, self.datamap, entries, show_favicons=conf.show_favicons)

    # Statusbar stub (defers to parent/main window, if in GUI mode)
    def status(self, *args, **kw):
        if self.parent: self.parent.status(*args, **kw)
        else: log.INFO("status():", *v)


288
289
290
291
292
293
294

295
296
297
298
299
300
301
302
303
304
305




306
307
308
309
310
311
312
313
297
298
299
300
301
302
303
304
305










306
307
308
309

310
311
312
313
314
315
316







+

-
-
-
-
-
-
-
-
-
-
+
+
+
+
-







            self.status("Updating streams...")
            self.status(-0.1)
            if category == "empty":
                new_streams = self.empty_stub
            else:
                new_streams = self.update_streams(category)
  
            # Postprocess new list of streams (e.g. assert existing title and url)
            if new_streams:
                # check and modify entry;
                # assert that title and url are present
                modified = []
                for row in new_streams:
                    if len(set(["", None]) & set([row.get("title"), row.get("url")])):
                        continue
                    try:
                        modified.append( self.postprocess(row) )
                    except Exception as e:
                        log.DATA(e, "Missing title or url. Postprocessing failed:", row)
                try:
                    new_streams = self.postprocess(new_streams)
                except Exception as e:
                    log.ERR(e, "Updating new streams, postprocessing failed:", row)
                new_streams = modified
  
                # don't lose forgotten streams
                if conf.retain_deleted:
                   self.streams[category] = new_streams + self.deleted_streams(new_streams, self.streams.get(category,[]))
                else:
                   self.streams[category] = new_streams
  
352
353
354
355
356
357
358
359

360
361

362
363
364

365
366
367





368
369

370
371
372
373
374
375
376






377
378
379
380
381
382
383
384






385
386

387
388
389
390
391
392
393
394
395





396
397
398
399
400
401

402

403
404
405
406
407
408
409
355
356
357
358
359
360
361

362


363


364
365



366
367
368
369
370


371







372
373
374
375
376
377







378
379
380
381
382
383
384
385

386









387
388
389
390
391
392
393
394
395
396
397
398

399
400
401
402
403
404
405
406







-
+
-
-
+
-
-

+
-
-
-
+
+
+
+
+
-
-
+
-
-
-
-
-
-
-
+
+
+
+
+
+
-
-
-
-
-
-
-

+
+
+
+
+
+

-
+
-
-
-
-
-
-
-
-
-
+
+
+
+
+






+
-
+







        for row in old:
            if ("url" in row and (row.get("url") not in new)):
                row["deleted"] = 1
                diff.append(row)
        return diff

    
    # prepare data for display
    # Prepare stream list for display
    #
    #  - favourite icon
    prepare_filters = []
    #  - or deleted icon
    #
    def prepare(self, streams):
        for f in self.prepare_filters:
        for i,row in enumerate(streams):
            # state icon: bookmark star
            if (conf.show_bookmarks and "bookmarks" in self.parent.channels and self.parent.bookmarks.is_in(streams[i].get("url", "file:///tmp/none"))):
            map(f, streams)
        return streams
    # state icon: bookmark star, or deleted mark
    def prepare_filter_icons(self, row):
        if conf.show_bookmarks:# and "bookmarks" in self.parent.channels:
                streams[i]["favourite"] = 1
            
            row["favourite"] = self.parent.bookmarks.is_in(row.get("url", "file:///tmp/none"))
            # state icon: INFO or DELETE
            if (not row.get("state")):
                if row.get("favourite"):
                    streams[i]["state"] = gtk.STOCK_ABOUT
                if conf.retain_deleted and row.get("deleted"):
                    streams[i]["state"] = gtk.STOCK_DELETE
            
        if not row.get("state"):
            if row.get("favourite"):
                row["state"] = gtk.STOCK_ABOUT
            if row.get("deleted"):
                row["state"] = gtk.STOCK_DELETE

            # Favicons? construct local cache filename, basically reimplements favicon.row_to_fn()
            if conf.show_favicons and "favicon" in self.parent.features:
                url = row.get("img") or row.get("homepage")
                if url:
                    # Normalize by stripping proto:// and non-alphanumeric chars
                    url = re.sub("[^\w._-]", "_", re.sub("^\w+://|/$", "", url.lower()))
                    streams[i]["favicon"] = "{}/icons/{}.png".format(conf.dir, url)

    # Stream list prepareations directly after reload,
    # can remove entries, or just update fields.
    postprocess_filters = []
    def postprocess(self, streams):
        for f in self.postprocess_filters:
            streams = filter(f, streams)
        return streams

    # Filter entries without title or url

    # data preparations directly after reload
    #
    # - drop shoutcast homepage links
    # - or find homepage name in title
    #
    def postprocess(self, row):
        # deduce homepage URLs from title
        # by looking for www.xyz.com domain names
    def postprocess_filter_required_fields(self, row):
        return not len(set(["", None]) & set([row.get("title"), row.get("url")]))
    # Deduce homepage URLs from title
    # by looking for www.xyz.com domain names
    def postprocess_filter_homepage(self, row):
        if not row.get("homepage"):
            url = self.rx_www_url.search(row.get("title", ""))
            if url:
                url = url.group(0).lower().replace(" ", "")
                url = (url if url.find("www.") == 0 else "www."+url)
                row["homepage"] = ahttp.fix_url(url)
                print row
        return row
        return True

        

    # reload current stream from web directory
    def reload(self):
        self.load(self.current, force=1)
    def switch(self):

Modified uikit.py from [9362b926ba] to [fe56c2beaf].

82
83
84
85
86
87
88
89

90
91
92
93
94
95
96
82
83
84
85
86
87
88

89
90
91
92
93
94
95
96







-
+







    #
    # An according entries list then would contain a dictionary for each row:
    #   entries = [ {"titlerow":"first", "interndat":123}, {"titlerow":"..."}, ]
    # Keys not mentioned in the datamap get ignored, and defaults are applied
    # for missing cols. All values must already be in the correct type however.
    #
    @staticmethod
    def columns(widget, datamap=[], entries=None, pix_entry=False):
    def columns(widget, datamap=[], entries=None, show_favicons=True, pix_entry=False):

        # create treeviewcolumns?
        if (not widget.get_column(0)):
            # loop through titles
            datapos = 0
            for n_col,desc in enumerate(datamap):
                                
182
183
184
185
186
187
188
189

190
191
192
193
194
195
196
182
183
184
185
186
187
188

189
190
191
192
193
194
195
196







-
+







                # map Python2 unicode to str
                row = [ str(value) if type(value) is unicode else value  for value in row ]

                # autotransform string -> gtk image object
                if (pix_entry and type(row[pix_entry]) == str):
                    pix = None
                    try:
                        if os.path.exists(row[pix_entry]):
                        if show_favicons and os.path.exists(row[pix_entry]):
                            pix = gtk.gdk.pixbuf_new_from_file(row[pix_entry])
                    except Exception as e:
                        log.ERR("uikik.columns: Pixbuf fail,", e)
                    row[pix_entry] = pix or defaults[gtk.gdk.Pixbuf]

                try:
                    # add