Mercurial > libervia-web
comparison browser/collections.py @ 1124:28e3eb3bb217
files reorganisation and installation rework:
- files have been reorganised to follow other SàT projects and usual Python organisation (no more "/src" directory)
- VERSION file is now used, as for other SàT projects
- replace the overcomplicated setup.py be a more sane one. Pyjamas part is not compiled anymore by setup.py, it must be done separatly
- removed check for data_dir if it's empty
- installation tested working in virtual env
- libervia launching script is now in bin/libervia
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 25 Aug 2018 17:59:48 +0200 |
parents | src/browser/collections.py@44df078187b9 |
children |
comparison
equal
deleted
inserted
replaced
1123:63a4b8fe9782 | 1124:28e3eb3bb217 |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # Libervia: a Salut à Toi frontend | |
5 # Copyright (C) 2014 Jérôme Poisson <goffi@goffi.org> | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 class OrderedDict(object): | |
21 """Naive implementation of OrderedDict which is compatible with pyjamas""" | |
22 | |
23 def __init__(self, *args, **kwargs): | |
24 self.__internal_dict = {} | |
25 self.__keys = [] # this list keep the keys in order | |
26 if args: | |
27 if len(args)>1: | |
28 raise TypeError("OrderedDict expected at most 1 arguments, got {}".format(len(args))) | |
29 if isinstance(args[0], (dict, OrderedDict)): | |
30 for key, value in args[0].iteritems(): | |
31 self[key] = value | |
32 for key, value in args[0]: | |
33 self[key] = value | |
34 | |
35 def __len__(self): | |
36 return len(self.__keys) | |
37 | |
38 def __setitem__(self, key, value): | |
39 if key not in self.__keys: | |
40 self.__keys.append(key) | |
41 self.__internal_dict[key] = value | |
42 | |
43 def __getitem__(self, key): | |
44 return self.__internal_dict[key] | |
45 | |
46 def __delitem__(self, key): | |
47 del self.__internal_dict[key] | |
48 self.__keys.remove(key) | |
49 | |
50 def __contains__(self, key): | |
51 return key in self.__keys | |
52 | |
53 def clear(self): | |
54 self.__internal_dict.clear() | |
55 del self.__keys[:] | |
56 | |
57 def copy(self): | |
58 return OrderedDict(self) | |
59 | |
60 @classmethod | |
61 def fromkeys(cls, seq, value=None): | |
62 ret = OrderedDict() | |
63 for key in seq: | |
64 ret[key] = value | |
65 return ret | |
66 | |
67 def get(self, key, default=None): | |
68 try: | |
69 return self.__internal_dict[key] | |
70 except KeyError: | |
71 return default | |
72 | |
73 def has_key(self, key): | |
74 return key in self.__keys | |
75 | |
76 def keys(self): | |
77 return self.__keys[:] | |
78 | |
79 def iterkeys(self): | |
80 for key in self.__keys: | |
81 yield key | |
82 | |
83 def items(self): | |
84 ret = [] | |
85 for key in self.__keys: | |
86 ret.append((key, self.__internal_dict[key])) | |
87 return ret | |
88 | |
89 def iteritems(self): | |
90 for key in self.__keys: | |
91 yield (key, self.__internal_dict[key]) | |
92 | |
93 def values(self): | |
94 ret = [] | |
95 for key in self.__keys: | |
96 ret.append(self.__internal_dict[key]) | |
97 return ret | |
98 | |
99 def itervalues(self): | |
100 for key in self.__keys: | |
101 yield (self.__internal_dict[key]) | |
102 | |
103 def popitem(self, last=True): | |
104 try: | |
105 key = self.__keys.pop(-1 if last else 0) | |
106 except IndexError: | |
107 raise KeyError('dictionnary is empty') | |
108 value = self.__internal_dict.pop(key) | |
109 return((key, value)) | |
110 | |
111 def setdefault(self, key, default=None): | |
112 try: | |
113 return self.__internal_dict[key] | |
114 except KeyError: | |
115 self[key] = default | |
116 return default | |
117 | |
118 def update(self, *args, **kwargs): | |
119 if len(args) > 1: | |
120 raise TypeError('udpate expected at most 1 argument, got {}'.format(len(args))) | |
121 if args: | |
122 if hasattr(args[0], 'keys'): | |
123 for k in args[0]: | |
124 self[k] = args[0][k] | |
125 else: | |
126 for (k, v) in args[0]: | |
127 self[k] = v | |
128 for k, v in kwargs.items(): | |
129 self[k] = v | |
130 | |
131 def pop(self, *args): | |
132 if not args: | |
133 raise TypeError('pop expected at least 1 argument, got 0') | |
134 try: | |
135 self.__internal_dict.pop(args[0]) | |
136 except KeyError: | |
137 if len(args) == 2: | |
138 return args[1] | |
139 raise KeyError(args[0]) | |
140 self.__keys.remove(args[0]) | |
141 | |
142 def viewitems(self): | |
143 raise NotImplementedError | |
144 | |
145 def viewkeys(self): | |
146 raise NotImplementedError | |
147 | |
148 def viewvalues(self): | |
149 raise NotImplementedError |