OSDN Git Service

Multithreads are abandoned. Alternatly, The asyncore substitutes.(#16776)
[fukui-no-namari/fukui-no-namari.git] / src / FukuiNoNamari / bookmark_core.py
1 # Copyright (C) 2006 by Aiwota Programmer
2 # aiwotaprog@tetteke.tk
3 #
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.
8 #
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.
13 #
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
17
18
19 class BookmarkFormatError(Exception):
20
21     def __init__(self, value):
22         self.value = value
23
24     def __str__(self):
25         return self.value
26
27
28 class BaseBookmark(object):
29
30     def __init__(self, uri, name, *categories):
31         self.uri = uri
32         self.name = name
33         self.categories = [cat for cat in categories]
34
35
36 class Bookmark(BaseBookmark):
37
38     def __init__(self, uri=None, name=None, categories=None, formatted=None):
39         if uri and name:
40             pass
41         elif formatted:
42             uri, name, categories = self._parse_text(formatted)
43         else:
44             assert(False), "uri and name, or formatted must be set"
45         if not uri:
46             raise BookmarkFormatError, "uri must not be empty"
47         if not name:
48             raise BookmarkFormatError, "name must not be empty"
49         super(Bookmark, self).__init__(uri, name, *categories)
50
51     def _parse_text(self, formatted):
52         formatted = formatted.rstrip("\n")
53         datalist = formatted.split("\t")
54
55         name = None
56         uri = None
57         categories = []
58
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)
68
69         return uri, name, categories
70
71     def tabbed_text(self):
72         def append_tab(text):
73             if text:
74                 text += "\t"
75             return text
76
77         text = ""
78         if self.name:
79             text = append_tab(text)
80             text += "name=%s" % self.name
81         if self.uri:
82             text = append_tab(text)
83             text += "uri=%s" % self.uri
84         for category in self.categories:
85             if category:
86                 text = append_tab(text)
87                 text += "category=%s" % category
88         return text