Re: [PATCH] + chromium bookmarks plugin



here is my second attempt for chromium bookmarks plugin.. :) 
I hope it's in time for version c17.

-- 
Francesco Marella <francesco marella gmail com>
From 102785e4741b78c39d8380a940c92711e1681a48 Mon Sep 17 00:00:00 2001
From: Francesco Marella <francesco marella gmail com>
Date: Fri, 23 Oct 2009 12:02:57 +0200
Subject: [PATCH] + chromium bookmarks plugin

Index of Chromium Bookmarks
---
 kupfer/plugin/chromium.py         |   48 ++++++++++++++++++++++++++++
 kupfer/plugin/chromium_support.py |   62 +++++++++++++++++++++++++++++++++++++
 2 files changed, 110 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..62fa17c
--- /dev/null
+++ b/kupfer/plugin/chromium.py
@@ -0,0 +1,48 @@
+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["url"], book["name"])
+
+	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_icon_name(self):
+		return "chromium-browser"
+	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..675a0eb
--- /dev/null
+++ b/kupfer/plugin/chromium_support.py
@@ -0,0 +1,62 @@
+# -*- coding: UTF-8 -*-
+
+from __future__ import with_statement
+try:
+	import cjson
+	json_decoder = cjson.decode
+except ImportError:
+	import json
+	json_decoder = json.loads
+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):
+	# construct and configure the parser
+	if not bookmarks_file:
+		return []
+
+	with open(bookmarks_file) as f:
+		content = f.read().decode("UTF-8")
+		root = json_decoder(content)
+
+	# make a dictionary of unique bookmarks
+	bmap = {}
+
+	def bmap_add(bmark, bmap):
+		if bmark["id"] not in bmap:
+			bmap[bmark["id"]] = bmark
+	
+	CONTAINER = "folder"
+	UNWANTED_SCHEME = ("data", "place", "javascript")
+
+	def is_container(ch):
+		return ch["type"] == CONTAINER
+	def is_bookmark(ch):
+		return ch.get("url")
+	def is_good(ch):
+		return not ch["url"].split(":", 1)[0] in UNWANTED_SCHEME
+
+	folders = []
+	
+	# add some folders
+	folders.extend(root['roots']['bookmark_bar']['children'])
+	folders.extend(root['roots']['other']['children'])
+
+	for item in folders:
+		if is_bookmark(item) and is_good(item):
+			bmap_add(item, bmap)
+		if is_container(item):
+			folders.extend(item["children"])
+	
+	return bmap.values()
+
+if __name__ == "__main__":
+	fpath = get_chromium_home_file("Bookmarks")
+	print "Parsed # bookmarks:", len(list(get_bookmarks(fpath)))
-- 
1.6.3.3

Attachment: signature.asc
Description: Questa =?ISO-8859-1?Q?=E8?= una parte del messaggio firmata digitalmente



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]