Merge pull request #41 from blackducksw/ubuntu_14
[ohcount] / python / __init__.py
1 import os, collections
2 from abc import abstractmethod
3 import ohcount
4
5 class _OhcountBase(object):
6
7     def __init__(self, base):
8         self._base = base
9
10     def __getattr__(self, name):
11         if name == '_base':
12             return object.__getattr__(self, name)
13         raise AttributeError
14
15     def __setattr__(self, name, value):
16         if name == '_base':
17             return object.__setattr__(self, name, value)
18         raise AttributeError
19
20 class _OhcountDict(_OhcountBase, collections.Mapping, collections.KeysView):
21
22     def __init__(self, base, mapping):
23         _OhcountBase.__init__(self, base)
24         collections.KeysView.__init__(self, mapping)
25
26     def __getattr__(self, name):
27         if name == '_mapping':
28             return collections.KeysView.__getattr__(self, name)
29         try:
30             return _OhcountBase.__getattr__(self, name)
31         except AttributeError:
32             try:
33                 return self.__getitem__(name)
34             except KeyError:
35                 raise AttributeError
36         except:
37             raise
38
39     def __setattr__(self, name, value):
40         if name == '_mapping':
41             return collections.KeysView.__setattr__(self, name, value)
42         try:
43             return _OhcountBase.__setattr__(self, name, value)
44         except AttributeError:
45             try:
46                 return self.__setitem__(name, value)
47             except KeyError:
48                 raise AttributeError
49         except:
50             raise
51
52     def keys(self):
53         return self._mapping
54
55     def __setitem__(self, key, item):
56         raise KeyError
57
58     def __delitem__(self, key):
59         raise KeyError
60
61     def __str__(self):
62         return dict([(key, self[key]) for key in self.keys()]).__str__()
63
64     def iterkeys(self):
65         return iter(self.keys())
66
67     def itervalues(self):
68         for key in self.keys():
69             yield self[key]
70
71     def iteritems(self):
72         for key in self.keys():
73             yield (key, self[key])
74
75     def items(self):
76         return [(key, self[key]) for key in self.keys()]
77
78     def values(self):
79         return [self[key] for key in self.keys()]
80
81 class _OhcountList(_OhcountBase):
82
83     @abstractmethod
84     def _get_value(self, inner):
85         raise NotImplementedError
86
87     def __len__(self):
88         count = 0
89         for e in self:
90             count += 1
91         return count
92
93     def __iter__(self):
94         return self.next()
95
96     def next(self):
97         iter = self._base.head
98         while iter is not None:
99             yield self._get_value(iter)
100             iter = iter.next
101
102     def __str__(self):
103         return [v for v in self].__str__()
104
105 class License(_OhcountDict):
106
107     def __init__(self, base):
108         _OhcountDict.__init__(self, base,
109             ['name','url','nice_name'])
110
111     def __getitem__(self, key):
112         if key == 'name':
113             return self._base.name
114         if key == 'url':
115             return self._base.url
116         if key == 'nice_name':
117             return self._base.nice_name
118         raise KeyError
119
120 class Loc(_OhcountDict):
121
122     def __init__(self, base):
123         _OhcountDict.__init__(self, base,
124             ['lang','code','comments','blanks','filecount','total'])
125
126     def __getitem__(self, key):
127         if key == 'lang' or key == 'language':
128             return self._base.language
129         if key == 'code':
130             return self._base.code
131         if key == 'comments':
132             return self._base.comments
133         if key == 'blanks':
134             return self._base.blanks
135         if key == 'filecount':
136             return self._base.filecount
137         if key == 'total':
138             return self._base.total()
139         raise KeyError
140
141 class LocList(_OhcountDict, _OhcountList):
142
143     def __init__(self, base):
144         _OhcountDict.__init__(self, base,
145             ['code','comments','blanks','filecount','total'])
146
147     def _get_value(self, inner):
148         return Loc(inner.loc)
149
150     def __getitem__(self, key):
151         if key == 'code':
152             return self._base.code()
153         if key == 'comments':
154             return self._base.comments()
155         if key == 'blanks':
156             return self._base.blanks()
157         if key == 'filecount':
158             return self._base.filecount()
159         if key == 'total':
160             return self._base.total()
161         raise KeyError
162
163     def __str__(self):
164         return _OhcountDict.__str__(self)
165
166     def compact(self):
167         return LocList(self._base.compact())
168
169 class SourceFile(_OhcountDict):
170
171     def __init__(self, base):
172         _OhcountDict.__init__(self, base,
173             ['filepath','filename','ext','contents','size','language',
174                 'licenses','locs'])
175
176     def _get_licenses(self):
177         result = []
178         list = self._base.get_license_list()
179         if list is not None:
180             iter = list.head
181             while iter is not None:
182                 result.append(License(iter.lic))
183                 iter = iter.next
184         return result
185
186     def _get_locs(self):
187         return LocList(self._base.get_loc_list())
188
189     def __getitem__(self, key):
190         if key == 'filepath':
191            return self._base.filepath
192         if key == 'filename':
193            return self._base.filename
194         if key == 'ext':
195            return self._base.ext
196         if key == 'contents':
197            return self._base.get_contents()
198         if key == 'size':
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()
204         if key == 'locs':
205             return self._get_locs()
206         raise AttributeError
207
208     def annotate(self):
209         return self._base.annotate()
210
211     def raw_entities(self):
212         return self._base.raw_entities()
213
214 class SourceFileList(_OhcountList):
215
216     def __init__(self, **kwargs):
217         _OhcountList.__init__(self, ohcount.SourceFileList(kwargs))
218
219     def _get_value(self, inner):
220         return SourceFile(inner.sf)
221
222     def analyze_languages(self):
223         return LocList( self._base.analyze_languages() )
224
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)
229
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)
234