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
=== added file 'surveilr/drivers.py'
--- surveilr/drivers.py 1970-01-01 00:00:00 +0000
+++ surveilr/drivers.py 2011-11-22 09:03:23 +0000
@@ -0,0 +1,58 @@
1"""
2 Surveilr - Log aggregation, analysis and visualisation
3
4 Copyright (C) 2011 Linux2Go
5
6 This program is free software: you can redistribute it and/or
7 modify it under the terms of the GNU Affero General Public License
8 as published by the Free Software Foundation, either version 3 of
9 the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Affero General Public License for more details.
15
16 You should have received a copy of the GNU Affero General Public
17 License along with this program. If not, see
18 <http://www.gnu.org/licenses/>.
19
20 Driver infrastructure
21"""
22
23from surveilr import exceptions
24
25registry = {}
26
27
28def register_driver(driver_type, name, driver):
29 if driver_type not in registry:
30 registry[driver_type] = {}
31
32 if name in registry[driver_type]:
33 raise exceptions.DuplicateDriverError()
34
35 registry[driver_type][name] = driver
36
37
38def unregister_driver(driver_type, name):
39 if driver_type not in registry:
40 raise exceptions.UnknownDriverTypeError()
41
42 if name not in registry[driver_type]:
43 raise exceptions.UnknownDriverError()
44
45 del registry[driver_type][name]
46
47 if not registry[driver_type]:
48 del registry[driver_type]
49
50
51def get_driver(driver_type, name):
52 if driver_type not in registry:
53 raise exceptions.UnknownDriverTypeError()
54
55 if name not in registry[driver_type]:
56 raise exceptions.UnknownDriverError()
57
58 return registry[driver_type][name]
059
=== added file 'surveilr/exceptions.py'
--- surveilr/exceptions.py 1970-01-01 00:00:00 +0000
+++ surveilr/exceptions.py 2011-11-22 09:03:23 +0000
@@ -0,0 +1,33 @@
1"""
2 Surveilr - Log aggregation, analysis and visualisation
3
4 Copyright (C) 2011 Linux2Go
5
6 This program is free software: you can redistribute it and/or
7 modify it under the terms of the GNU Affero General Public License
8 as published by the Free Software Foundation, either version 3 of
9 the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Affero General Public License for more details.
15
16 You should have received a copy of the GNU Affero General Public
17 License along with this program. If not, see
18 <http://www.gnu.org/licenses/>.
19
20 Exception classes
21"""
22
23
24class DuplicateDriverError(Exception):
25 pass
26
27
28class UnknownDriverTypeError(Exception):
29 pass
30
31
32class UnknownDriverError(Exception):
33 pass
034
=== added file 'surveilr/tests/__init__.py'
--- surveilr/tests/__init__.py 1970-01-01 00:00:00 +0000
+++ surveilr/tests/__init__.py 2011-11-22 09:03:23 +0000
@@ -0,0 +1,19 @@
1"""
2 Surveilr - Log aggregation, analysis and visualisation
3
4 Copyright (C) 2011 Linux2Go
5
6 This program is free software: you can redistribute it and/or
7 modify it under the terms of the GNU Affero General Public License
8 as published by the Free Software Foundation, either version 3 of
9 the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Affero General Public License for more details.
15
16 You should have received a copy of the GNU Affero General Public
17 License along with this program. If not, see
18 <http://www.gnu.org/licenses/>.
19"""
020
=== added file 'surveilr/tests/test_drivers.py'
--- surveilr/tests/test_drivers.py 1970-01-01 00:00:00 +0000
+++ surveilr/tests/test_drivers.py 2011-11-22 09:03:23 +0000
@@ -0,0 +1,92 @@
1"""
2 Surveilr - Log aggregation, analysis and visualisation
3
4 Copyright (C) 2011 Linux2Go
5
6 This program is free software: you can redistribute it and/or
7 modify it under the terms of the GNU Affero General Public License
8 as published by the Free Software Foundation, either version 3 of
9 the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Affero General Public License for more details.
15
16 You should have received a copy of the GNU Affero General Public
17 License along with this program. If not, see
18 <http://www.gnu.org/licenses/>.
19
20 Tests for driver infrastructure
21"""
22
23import unittest
24
25from surveilr import drivers
26from surveilr import exceptions
27
28
29class DriversTests(unittest.TestCase):
30 def register_empty_driver(self, driver_type, driver_name,
31 add_cleanup=True):
32 class SomeDriver(object):
33 pass
34
35 driver = SomeDriver()
36 drivers.register_driver(driver_type, driver_name, driver)
37 if add_cleanup:
38 self.addCleanup(drivers.unregister_driver,
39 driver_type, driver_name)
40 return driver
41
42 def test_register_retrieve_driver(self):
43 """Register driver and retrieve it again"""
44 driver = self.register_empty_driver('some_type', 'some_name')
45 self.assertTrue(drivers.get_driver('some_type', 'some_name') is driver)
46
47 def test_register_duplicate_driver(self):
48 """Registering duplicate driver fails"""
49 self.register_empty_driver('some_type', 'some_name')
50 self.assertRaises(exceptions.DuplicateDriverError,
51 self.register_empty_driver,
52 'some_type',
53 'some_name')
54
55 def test_get_invalid_driver_type(self):
56 """Retrieve driver of unknown type fails"""
57 self.assertRaises(exceptions.UnknownDriverTypeError,
58 drivers.get_driver,
59 'unknown_type',
60 'some_name')
61
62 def test_get_invalid_driver(self):
63 """Retrieve unknown driver of known type fails"""
64 self.register_empty_driver('some_type', 'some_name')
65 self.assertRaises(exceptions.UnknownDriverError,
66 drivers.get_driver,
67 'some_type',
68 'unknown_name')
69
70 def test_unregister_unknown_driver_type(self):
71 """Unregister driver of unknown type fails"""
72 self.assertRaises(exceptions.UnknownDriverTypeError,
73 drivers.unregister_driver,
74 'unknown_type',
75 'some_name')
76
77 def test_unregister_unknown_driver(self):
78 """Unregister unknown driver of known type fails"""
79 self.register_empty_driver('some_type', 'some_name')
80 self.assertRaises(exceptions.UnknownDriverError,
81 drivers.unregister_driver,
82 'some_type',
83 'unknown_name')
84
85 def test_unregister_then_retrieve(self):
86 """When last driver of a type is removed, so it the type"""
87 self.register_empty_driver('some_type', 'some_name', add_cleanup=False)
88 drivers.unregister_driver('some_type', 'some_name')
89 self.assertRaises(exceptions.UnknownDriverTypeError,
90 drivers.unregister_driver,
91 'some_type',
92 'some_name')

Subscribers

People subscribed via source and target branches

to all changes: