Merge lp:~soren/surveilr/driver-infrastructure into lp:surveilr

Proposed by Soren Hansen
Status: Merged
Approved by: Soren Hansen
Approved revision: 6
Merged at revision: 6
Proposed branch: lp:~soren/surveilr/driver-infrastructure
Merge into: lp:surveilr
Diff against target: 221 lines (+202/-0)
4 files modified
surveilr/drivers.py (+58/-0)
surveilr/exceptions.py (+33/-0)
surveilr/tests/__init__.py (+19/-0)
surveilr/tests/test_drivers.py (+92/-0)
To merge this branch: bzr merge lp:~soren/surveilr/driver-infrastructure
Reviewer Review Type Date Requested Status
Soren Hansen Pending
Review via email: mp+82981@code.launchpad.net

Commit message

Add basic driver management infrastructure.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'surveilr/drivers.py'
2--- surveilr/drivers.py 1970-01-01 00:00:00 +0000
3+++ surveilr/drivers.py 2011-11-22 09:03:23 +0000
4@@ -0,0 +1,58 @@
5+"""
6+ Surveilr - Log aggregation, analysis and visualisation
7+
8+ Copyright (C) 2011 Linux2Go
9+
10+ This program is free software: you can redistribute it and/or
11+ modify it under the terms of the GNU Affero General Public License
12+ as published by the Free Software Foundation, either version 3 of
13+ the License, or (at your option) any later version.
14+
15+ This program is distributed in the hope that it will be useful,
16+ but WITHOUT ANY WARRANTY; without even the implied warranty of
17+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+ GNU Affero General Public License for more details.
19+
20+ You should have received a copy of the GNU Affero General Public
21+ License along with this program. If not, see
22+ <http://www.gnu.org/licenses/>.
23+
24+ Driver infrastructure
25+"""
26+
27+from surveilr import exceptions
28+
29+registry = {}
30+
31+
32+def register_driver(driver_type, name, driver):
33+ if driver_type not in registry:
34+ registry[driver_type] = {}
35+
36+ if name in registry[driver_type]:
37+ raise exceptions.DuplicateDriverError()
38+
39+ registry[driver_type][name] = driver
40+
41+
42+def unregister_driver(driver_type, name):
43+ if driver_type not in registry:
44+ raise exceptions.UnknownDriverTypeError()
45+
46+ if name not in registry[driver_type]:
47+ raise exceptions.UnknownDriverError()
48+
49+ del registry[driver_type][name]
50+
51+ if not registry[driver_type]:
52+ del registry[driver_type]
53+
54+
55+def get_driver(driver_type, name):
56+ if driver_type not in registry:
57+ raise exceptions.UnknownDriverTypeError()
58+
59+ if name not in registry[driver_type]:
60+ raise exceptions.UnknownDriverError()
61+
62+ return registry[driver_type][name]
63
64=== added file 'surveilr/exceptions.py'
65--- surveilr/exceptions.py 1970-01-01 00:00:00 +0000
66+++ surveilr/exceptions.py 2011-11-22 09:03:23 +0000
67@@ -0,0 +1,33 @@
68+"""
69+ Surveilr - Log aggregation, analysis and visualisation
70+
71+ Copyright (C) 2011 Linux2Go
72+
73+ This program is free software: you can redistribute it and/or
74+ modify it under the terms of the GNU Affero General Public License
75+ as published by the Free Software Foundation, either version 3 of
76+ the License, or (at your option) any later version.
77+
78+ This program is distributed in the hope that it will be useful,
79+ but WITHOUT ANY WARRANTY; without even the implied warranty of
80+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
81+ GNU Affero General Public License for more details.
82+
83+ You should have received a copy of the GNU Affero General Public
84+ License along with this program. If not, see
85+ <http://www.gnu.org/licenses/>.
86+
87+ Exception classes
88+"""
89+
90+
91+class DuplicateDriverError(Exception):
92+ pass
93+
94+
95+class UnknownDriverTypeError(Exception):
96+ pass
97+
98+
99+class UnknownDriverError(Exception):
100+ pass
101
102=== added file 'surveilr/tests/__init__.py'
103--- surveilr/tests/__init__.py 1970-01-01 00:00:00 +0000
104+++ surveilr/tests/__init__.py 2011-11-22 09:03:23 +0000
105@@ -0,0 +1,19 @@
106+"""
107+ Surveilr - Log aggregation, analysis and visualisation
108+
109+ Copyright (C) 2011 Linux2Go
110+
111+ This program is free software: you can redistribute it and/or
112+ modify it under the terms of the GNU Affero General Public License
113+ as published by the Free Software Foundation, either version 3 of
114+ the License, or (at your option) any later version.
115+
116+ This program is distributed in the hope that it will be useful,
117+ but WITHOUT ANY WARRANTY; without even the implied warranty of
118+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
119+ GNU Affero General Public License for more details.
120+
121+ You should have received a copy of the GNU Affero General Public
122+ License along with this program. If not, see
123+ <http://www.gnu.org/licenses/>.
124+"""
125
126=== added file 'surveilr/tests/test_drivers.py'
127--- surveilr/tests/test_drivers.py 1970-01-01 00:00:00 +0000
128+++ surveilr/tests/test_drivers.py 2011-11-22 09:03:23 +0000
129@@ -0,0 +1,92 @@
130+"""
131+ Surveilr - Log aggregation, analysis and visualisation
132+
133+ Copyright (C) 2011 Linux2Go
134+
135+ This program is free software: you can redistribute it and/or
136+ modify it under the terms of the GNU Affero General Public License
137+ as published by the Free Software Foundation, either version 3 of
138+ the License, or (at your option) any later version.
139+
140+ This program is distributed in the hope that it will be useful,
141+ but WITHOUT ANY WARRANTY; without even the implied warranty of
142+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
143+ GNU Affero General Public License for more details.
144+
145+ You should have received a copy of the GNU Affero General Public
146+ License along with this program. If not, see
147+ <http://www.gnu.org/licenses/>.
148+
149+ Tests for driver infrastructure
150+"""
151+
152+import unittest
153+
154+from surveilr import drivers
155+from surveilr import exceptions
156+
157+
158+class DriversTests(unittest.TestCase):
159+ def register_empty_driver(self, driver_type, driver_name,
160+ add_cleanup=True):
161+ class SomeDriver(object):
162+ pass
163+
164+ driver = SomeDriver()
165+ drivers.register_driver(driver_type, driver_name, driver)
166+ if add_cleanup:
167+ self.addCleanup(drivers.unregister_driver,
168+ driver_type, driver_name)
169+ return driver
170+
171+ def test_register_retrieve_driver(self):
172+ """Register driver and retrieve it again"""
173+ driver = self.register_empty_driver('some_type', 'some_name')
174+ self.assertTrue(drivers.get_driver('some_type', 'some_name') is driver)
175+
176+ def test_register_duplicate_driver(self):
177+ """Registering duplicate driver fails"""
178+ self.register_empty_driver('some_type', 'some_name')
179+ self.assertRaises(exceptions.DuplicateDriverError,
180+ self.register_empty_driver,
181+ 'some_type',
182+ 'some_name')
183+
184+ def test_get_invalid_driver_type(self):
185+ """Retrieve driver of unknown type fails"""
186+ self.assertRaises(exceptions.UnknownDriverTypeError,
187+ drivers.get_driver,
188+ 'unknown_type',
189+ 'some_name')
190+
191+ def test_get_invalid_driver(self):
192+ """Retrieve unknown driver of known type fails"""
193+ self.register_empty_driver('some_type', 'some_name')
194+ self.assertRaises(exceptions.UnknownDriverError,
195+ drivers.get_driver,
196+ 'some_type',
197+ 'unknown_name')
198+
199+ def test_unregister_unknown_driver_type(self):
200+ """Unregister driver of unknown type fails"""
201+ self.assertRaises(exceptions.UnknownDriverTypeError,
202+ drivers.unregister_driver,
203+ 'unknown_type',
204+ 'some_name')
205+
206+ def test_unregister_unknown_driver(self):
207+ """Unregister unknown driver of known type fails"""
208+ self.register_empty_driver('some_type', 'some_name')
209+ self.assertRaises(exceptions.UnknownDriverError,
210+ drivers.unregister_driver,
211+ 'some_type',
212+ 'unknown_name')
213+
214+ def test_unregister_then_retrieve(self):
215+ """When last driver of a type is removed, so it the type"""
216+ self.register_empty_driver('some_type', 'some_name', add_cleanup=False)
217+ drivers.unregister_driver('some_type', 'some_name')
218+ self.assertRaises(exceptions.UnknownDriverTypeError,
219+ drivers.unregister_driver,
220+ 'some_type',
221+ 'some_name')

Subscribers

People subscribed via source and target branches

to all changes: