1 # Copyright (C) 2006 by Aiwota Programmer
2 # aiwotaprog@tetteke.tk
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 class BookmarkFormatError(Exception):
21 def __init__(self, value):
28 class BaseBookmark(object):
30 def __init__(self, uri, name, *categories):
33 self.categories = [cat for cat in categories]
36 class Bookmark(BaseBookmark):
38 def __init__(self, uri=None, name=None, categories=None, formatted=None):
42 uri, name, categories = self._parse_text(formatted)
44 assert(False), "uri and name, or formatted must be set"
46 raise BookmarkFormatError, "uri must not be empty"
48 raise BookmarkFormatError, "name must not be empty"
49 super(Bookmark, self).__init__(uri, name, *categories)
51 def _parse_text(self, formatted):
52 formatted = formatted.rstrip("\n")
53 datalist = formatted.split("\t")
59 for keyvalue in datalist:
60 if keyvalue.startswith("name="):
61 name = keyvalue[len("name="):]
62 elif keyvalue.startswith("uri="):
63 uri = keyvalue[len("uri="):]
64 elif keyvalue.startswith("category="):
65 category = keyvalue[len("category="):]
66 if category and category not in categories:
67 categories.append(category)
69 return uri, name, categories
71 def tabbed_text(self):
79 text = append_tab(text)
80 text += "name=%s" % self.name
82 text = append_tab(text)
83 text += "uri=%s" % self.uri
84 for category in self.categories:
86 text = append_tab(text)
87 text += "category=%s" % category