Hi Ulrick, Il giorno mer, 14/10/2009 alle 17.54 +0200, Ulrik Sverdrup ha scritto: > On Wed, Oct 14, 2009 at 04:30:34PM +0200, Francesco Marella wrote: > > + chromium bookmarks plugin > > -- > > Francesco Marella <francesco marella gmail com> > > Hi Francesco, > > Thank you for this contribution. I'm going to comment the patch inline > below on some very minor things, but first -- chromium_support.py > > It looks like chromium_support.py parses JSON by regex. This is > seriously Eww, doesn't matter if Gnome-Do tested it for us, it will have > parsing bugs and it is wrong, when we have better tools for this. > > From Python 2.6, python's batteries include the 'json' module to parse > this! For Python 2.5, kupfer supports 'cjson' as well. Please look > inside firefox3_support.py how this is done -- I promise it is not very > hard. > > (Firefox' bookmark JSON file's structure is very complicated, I think > that Google Chrome is more sane there, so it shouldn't be as complicated > as the Firefox JSON code.) > > If there is something wrong with my shallow research, and that file is > not JSON, what is it? Chrome/Chromium bookmark file is a JSON file. I've put this plugin together in a couple of hours, porting it from do-plugins and practicing with kupfer's internals. I'll change chromium_support.py to make use of [c]json module, instead of re. > > > > From f1f8ad23965e2ff4e96f6528c6f8faba64b4d2df Mon Sep 17 00:00:00 2001 > > From: Francesco Marella <francesco deimos (none)> > > Date: Wed, 14 Oct 2009 15:44:27 +0200 > > Subject: [PATCH] + chromium bookmarks plugin > > > > Index of Chromium Bookmarks > > --- > > kupfer/plugin/chromium.py | 50 +++++++++++++++++++++++++ > > kupfer/plugin/chromium_support.py | 72 +++++++++++++++++++++++++++++++++++++ > > 2 files changed, 122 insertions(+), 0 deletions(-) > > create mode 100644 kupfer/plugin/chromium.py > > create mode 100644 kupfer/plugin/chromium_support.py > > > > diff --git a/kupfer/plugin/chromium.py b/kupfer/plugin/chromium.py > > new file mode 100644 > > index 0000000..8a9ec22 > > --- /dev/null > > +++ b/kupfer/plugin/chromium.py > > @@ -0,0 +1,50 @@ > > +import os > > + > > +from kupfer.objects import Leaf, Action, Source, AppLeafContentMixin > > +from kupfer.objects import UrlLeaf > > +from kupfer import plugin_support > > + > > +__kupfer_name__ = _("Chromium Bookmarks") > > +__kupfer_sources__ = ("BookmarksSource", ) > > +__kupfer_contents__ = ("BookmarksSource", ) > > +__description__ = _("Index of Chromium bookmarks") > > +__version__ = "" > > +__author__ = "Francesco Marella <francesco marella gmail com>" > > + > > +__kupfer_settings__ = plugin_support.PluginSettings( > > + plugin_support.SETTING_PREFER_CATALOG, > > +) > > + > > +class BookmarksSource (AppLeafContentMixin, Source): > > + appleaf_content_id = ("chromium-browser") > > + def __init__(self): > > + super(BookmarksSource, self).__init__(_("Chromium Bookmarks")) > > + > > + def _get_chromium_items(self, fpath): > > + """Parse Chromium' bookmarks backups""" > > + from chromium_support import get_bookmarks > > + self.output_debug("Parsing", fpath) > > + bookmarks = get_bookmarks(fpath) > > + for book in bookmarks: > > + yield UrlLeaf(book["href"], book["title"]) > > + > > + def get_items(self): > > + from chromium_support import get_chromium_home_file > > + fpath = get_chromium_home_file("Bookmarks") > > + if fpath: > > + try: > > + return self._get_chromium_items(fpath) > > + except Exception, exc: > > + self.output_error(exc) > > > + > > + self.output_error("No Chromium bookmarks file found") > > + return [] > > + > > + def get_description(self): > > + return _("Index of Chromium bookmarks") > > + def get_gicon(self): > > + return self.get_leaf_repr() and self.get_leaf_repr().get_gicon() > > + def get_icon_name(self): > > + return "chromium-browser" > > Do you need both get_gicon and get_icon_name? That firefox bookmarks > uses that is just to match either iceweasel or firefox, whichever > application is installed. If get_icon_name always produces the right > icon, only that is ok. todo += Remove get_gicon > > > + def provides(self): > > + yield UrlLeaf > > diff --git a/kupfer/plugin/chromium_support.py b/kupfer/plugin/chromium_support.py > > new file mode 100644 > > index 0000000..968df39 > > --- /dev/null > > +++ b/kupfer/plugin/chromium_support.py > > @@ -0,0 +1,72 @@ > > +#!/usr/bin/env python > > +# -*- coding: UTF-8 -*- > > + > > +""" > > +Port from do-plugins/Chromium plugin which is under GPL v3. > > + > > +Modifications released under GPL v3 (or any later) > > +Francesco Marella <francesco marella gmail com> > > +""" > > +from __future__ import with_statement > > +import re > > +from os.path import join, expanduser, exists, basename > > + > > +def get_chromium_home_file(needed_file): > > + chromium_dir = expanduser("~/.config/chromium/Default/") > > + if not exists(chromium_dir): > > + # no break > > + return None > > + > > + return join(chromium_dir, needed_file) > > + > > +def get_bookmarks(bookmarks_file): > > + """ > > + Return a list of bookmarks (dictionaries) > > + > > + each bookmark has the keys: > > + url: URL > > + name: description > > + type: type > > + """ > > + if not bookmarks_file: > > + return [] > > + > > + m_name = "" > > + m_type = "" > > + m_url = "" > > + > > + m_all_items = [] > > + > > + with open(bookmarks_file) as f: > > + content = f.read().decode("UTF-8") > > + > > + prog = re.compile("(\"([^\"]*)\" *: *\"([^\"]*)\")|[{}]", re.M) > > + for m in prog.finditer(content): > > + if m.group(0) == "{": > > + m_name = "" > > + m_type = "" > > + m_url = "" > > + elif m.group(0) == "}" and m_type == "url" \ > > + and m_name != "" and m_url != "": > > + bookmark = { > > + "href" : m_url, > > + "title": m_name > > + } > > + m_all_items.append(bookmark) > > + elif m.group(0)[0] == "\"": > > + if m.group(2) == "url": > > + m_url = m.group(3) > > + if m.group(2) == "name": > > + m_name = m.group(3) > > + if m.group(2) == "type": > > + m_type = m.group(3) > > + > > + return m_all_items > > + > > +def main(): > > + fileloc = get_chromium_home_file("Bookmarks") > > + print fileloc > > + print get_bookmarks(fileloc) > > + > > +if __name__ == "__main__": > > + main() > > This is very good practice, to make the file testable. This way we can > test that individual modules work across Python 2.5 and Python 2.6, even > though Kupfer as a whole can't run under both versions because of gtk or > so. > > -- > > 1.6.3.3 > > > > Best Regards, > Ulrik > > > > > > _______________________________________________ > > kupfer-list mailing list > > kupfer-list gnome org > > http://mail.gnome.org/mailman/listinfo/kupfer-list > _______________________________________________ > kupfer-list mailing list > kupfer-list gnome org > http://mail.gnome.org/mailman/listinfo/kupfer-list Thanks for your review, always very useful! -- Francesco Marella <francesco marella gmail com>
Attachment:
signature.asc
Description: Questa =?ISO-8859-1?Q?=E8?= una parte del messaggio firmata digitalmente