2 from abc import abstractmethod
5 class _OhcountBase(object):
7 def __init__(self, base):
10 def __getattr__(self, name):
12 return object.__getattr__(self, name)
15 def __setattr__(self, name, value):
17 return object.__setattr__(self, name, value)
20 class _OhcountDict(_OhcountBase, collections.Mapping, collections.KeysView):
22 def __init__(self, base, mapping):
23 _OhcountBase.__init__(self, base)
24 collections.KeysView.__init__(self, mapping)
26 def __getattr__(self, name):
27 if name == '_mapping':
28 return collections.KeysView.__getattr__(self, name)
30 return _OhcountBase.__getattr__(self, name)
31 except AttributeError:
33 return self.__getitem__(name)
39 def __setattr__(self, name, value):
40 if name == '_mapping':
41 return collections.KeysView.__setattr__(self, name, value)
43 return _OhcountBase.__setattr__(self, name, value)
44 except AttributeError:
46 return self.__setitem__(name, value)
55 def __setitem__(self, key, item):
58 def __delitem__(self, key):
62 return dict([(key, self[key]) for key in self.keys()]).__str__()
65 return iter(self.keys())
68 for key in self.keys():
72 for key in self.keys():
73 yield (key, self[key])
76 return [(key, self[key]) for key in self.keys()]
79 return [self[key] for key in self.keys()]
81 class _OhcountList(_OhcountBase):
84 def _get_value(self, inner):
85 raise NotImplementedError
97 iter = self._base.head
98 while iter is not None:
99 yield self._get_value(iter)
103 return [v for v in self].__str__()
105 class License(_OhcountDict):
107 def __init__(self, base):
108 _OhcountDict.__init__(self, base,
109 ['name','url','nice_name'])
111 def __getitem__(self, key):
113 return self._base.name
115 return self._base.url
116 if key == 'nice_name':
117 return self._base.nice_name
120 class Loc(_OhcountDict):
122 def __init__(self, base):
123 _OhcountDict.__init__(self, base,
124 ['lang','code','comments','blanks','filecount','total'])
126 def __getitem__(self, key):
127 if key == 'lang' or key == 'language':
128 return self._base.language
130 return self._base.code
131 if key == 'comments':
132 return self._base.comments
134 return self._base.blanks
135 if key == 'filecount':
136 return self._base.filecount
138 return self._base.total()
141 class LocList(_OhcountDict, _OhcountList):
143 def __init__(self, base):
144 _OhcountDict.__init__(self, base,
145 ['code','comments','blanks','filecount','total'])
147 def _get_value(self, inner):
148 return Loc(inner.loc)
150 def __getitem__(self, key):
152 return self._base.code()
153 if key == 'comments':
154 return self._base.comments()
156 return self._base.blanks()
157 if key == 'filecount':
158 return self._base.filecount()
160 return self._base.total()
164 return _OhcountDict.__str__(self)
167 return LocList(self._base.compact())
169 class SourceFile(_OhcountDict):
171 def __init__(self, base):
172 _OhcountDict.__init__(self, base,
173 ['filepath','filename','ext','contents','size','language',
176 def _get_licenses(self):
178 list = self._base.get_license_list()
181 while iter is not None:
182 result.append(License(iter.lic))
187 return LocList(self._base.get_loc_list())
189 def __getitem__(self, key):
190 if key == 'filepath':
191 return self._base.filepath
192 if key == 'filename':
193 return self._base.filename
195 return self._base.ext
196 if key == 'contents':
197 return self._base.get_contents()
199 return self._base.contents_size()
200 if key == 'language':
201 return self._base.get_language()
202 if key == 'licenses':
203 return self._get_licenses()
205 return self._get_locs()
209 return self._base.annotate()
211 def raw_entities(self):
212 return self._base.raw_entities()
214 class SourceFileList(_OhcountList):
216 def __init__(self, **kwargs):
217 _OhcountList.__init__(self, ohcount.SourceFileList(kwargs))
219 def _get_value(self, inner):
220 return SourceFile(inner.sf)
222 def analyze_languages(self):
223 return LocList( self._base.analyze_languages() )
225 def add_directory(self, path):
226 if not os.path.isdir(path):
227 raise SyntaxError('Input path is not a directory: %s' % path)
228 self._base.add_directory(path)
230 def add_file(self, filepath):
231 if not os.path.isfile(filepath):
232 raise SyntaxError('Input path is not a file: %s' % filepath)
233 self._base.add_file(filepath)