[no subject]
(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?
> 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
> ---
> =C2=A0kupfer/plugin/chromium.py =C2=A0 =C2=A0 =C2=A0 =C2=A0 | =C2=A0 50 +=
++++++++++++++++++++++++
> =C2=A0kupfer/plugin/chromium_support.py | =C2=A0 72 +++++++++++++++++++++=
++++++++++++++++
> =C2=A02 files changed, 122 insertions(+), 0 deletions(-)
> =C2=A0create mode 100644 kupfer/plugin/chromium.py
> =C2=A0create 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__ =3D _("Chromium Bookmarks")
> +__kupfer_sources__ =3D ("BookmarksSource", )
> +__kupfer_contents__ =3D ("BookmarksSource", )
> +__description__ =3D _("Index of Chromium bookmarks")
> +__version__ =3D ""
> +__author__ =3D "Francesco Marella <francesco marella gmail com>"
> +
> +__kupfer_settings__ =3D plugin_support.PluginSettings(
> + =C2=A0 =C2=A0 plugin_support.SETTING_PREFER_CATALOG,
> +)
> +
> +class BookmarksSource (AppLeafContentMixin, Source):
> + =C2=A0 =C2=A0 appleaf_content_id =3D ("chromium-browser")
> + =C2=A0 =C2=A0 def __init__(self):
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 super(BookmarksSource, self).=
__init__(_("Chromium Bookmarks"))
> +
> + =C2=A0 =C2=A0 def _get_chromium_items(self, fpath):
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 """Parse Chromium' bookmarks =
backups"""
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 from chromium_support import =
get_bookmarks
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 self.output_debug("Parsing", =
fpath)
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 bookmarks =3D get_bookmarks(f=
path)
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 for book in bookmarks:
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 y=
ield UrlLeaf(book["href"], book["title"])
> +
> + =C2=A0 =C2=A0 def get_items(self):
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 from chromium_support import =
get_chromium_home_file
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 fpath =3D get_chromium_home_f=
ile("Bookmarks")
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if fpath:
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 t=
ry:
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 return self._get_chromium_items(fpath)
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 e=
xcept Exception, exc:
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 self.output_error(exc)
> +
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 self.output_error("No Chromiu=
m bookmarks file found")
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return []
> +
> + =C2=A0 =C2=A0 def get_description(self):
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return _("Index of Chromium b=
ookmarks")
> + =C2=A0 =C2=A0 def get_gicon(self):
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return self.get_leaf_repr() a=
nd self.get_leaf_repr().get_gicon()
> + =C2=A0 =C2=A0 def get_icon_name(self):
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 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.
> + =C2=A0 =C2=A0 def provides(self):
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 yield UrlLeaf
> diff --git a/kupfer/plugin/chromium_support.py b/kupfer/plugin/chromium_s=
upport.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):
> + =C2=A0 =C2=A0chromium_dir =3D expanduser("~/.config/chromium/Default/")
> + =C2=A0 =C2=A0if not exists(chromium_dir):
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0# no break
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0return None
> +
> + =C2=A0 =C2=A0return join(chromium_dir, needed_file)
> +
> +def get_bookmarks(bookmarks_file):
> + =C2=A0 =C2=A0 """
> + =C2=A0 =C2=A0 Return a list of bookmarks (dictionaries)
> +
> + =C2=A0 =C2=A0 each bookmark has the keys:
> + =C2=A0 =C2=A0 url: URL
> + =C2=A0 =C2=A0 name: description
> + =C2=A0 =C2=A0 type: type
> + =C2=A0 =C2=A0 """
> + =C2=A0 =C2=A0 if not bookmarks_file:
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return []
> +
> + =C2=A0 =C2=A0 m_name =C2=A0=3D ""
> + =C2=A0 =C2=A0 m_type =C2=A0=3D ""
> + =C2=A0 =C2=A0 m_url =C2=A0 =3D ""
> +
> + =C2=A0 =C2=A0 m_all_items =3D []
> +
> + =C2=A0 =C2=A0 with open(bookmarks_file) as f:
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 content =3D f.read().decode("=
UTF-8")
> +
> + =C2=A0 =C2=A0 prog =3D re.compile("(\"([^\"]*)\" *: *\"([^\"]*)\")|[{}]=
", re.M)
> + =C2=A0 =C2=A0 for m in prog.finditer(content):
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if m.group(0) =3D=3D "{":
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 m=
_name =3D ""
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 m=
_type =3D ""
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 m=
_url =3D ""
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 elif m.group(0) =3D=3D "}" an=
d m_type =3D=3D "url" \
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 and m_name !=3D "" and m_url !=3D "":
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 b=
ookmark =3D {
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 "href" : m_url,
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 "title": m_name
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 }
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 m=
_all_items.append(bookmark)
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 elif m.group(0)[0] =3D=3D "\"=
":
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 i=
f m.group(2) =3D=3D "url":
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 m_url =3D m.group(3)
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 i=
f m.group(2) =3D=3D "name":
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 m_name =3D m.group(3)
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 i=
f m.group(2) =3D=3D "type":
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 m_type =3D m.group(3)
> +
> + =C2=A0 =C2=A0 return m_all_items
> +
> +def main():
> + =C2=A0 =C2=A0 fileloc =3D get_chromium_home_file("Bookmarks")
> + =C2=A0 =C2=A0 print fileloc
> + =C2=A0 =C2=A0 print get_bookmarks(fileloc)
> +
> +if __name__ =3D=3D "__main__":
> + =C2=A0 =C2=A0 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
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]