3
The prefix loader reads a lazr.config file, looks for the oops_prefix entry and
4
loads that prefix into the database, if it's not there yet.
6
>>> from django.conf import settings
7
>>> from oopstools.oops.prefixloader import PrefixLoader
9
Initialize the PrefixLoader with the path to the lazr.config file or a
10
directory containing lazr.config files.
12
>>> prefix_loader = PrefixLoader(settings.LAZR_CONFIG)
14
The correct config files are loaded by the PrefixLoader.
15
sorted() is used here so a predictable result is always returned.
17
>>> from operator import attrgetter
18
>>> for schema in sorted(prefix_loader._schemas, key=attrgetter('filename')):
19
... print schema.filename
20
/.../lazr-configs/edge-lazr.conf
21
/.../lazr-configs/edge/launchpad-lazr.conf
22
/.../lazr-configs/edge1/launchpad-lazr.conf
23
/.../lazr-configs/lpnet-lazr.conf
24
/.../lazr-configs/lpnet2/launchpad-lazr.conf
25
/.../lazr-configs/production/launchpad-lazr.conf
27
And a number of prefixes were found in those config files.
29
>>> found_prefixes = prefix_loader.iter_prefixes()
30
>>> print sorted(found_prefixes)
31
['B', 'BZR', 'EA', 'EBZR', 'SMESSH', 'SMS']
33
Now let's make sure those are correctly loaded into the database, but first
34
let's grab all prefixes already in the database
36
>>> from oopstools.oops.models import Prefix
37
>>> db_prefixes_before = set()
38
>>> for prefix in Prefix.objects.all():
39
... db_prefixes_before.add(prefix.value)
41
And check that not all prefixes found by the prefix_loader are in the list of
44
>>> set(found_prefixes).issubset(db_prefixes_before)
47
Load the prefixes found in the config files into the database. The method
48
return only those that weren't previously in the database.
50
>>> prefix_loader.load_prefixes_into_database()
51
['BZR', 'EBZR', 'SMESSH']
53
Check all prefixes found by the prefix_loader are now in the list of
56
>>> db_prefixes_after = set()
57
>>> for prefix in Prefix.objects.all():
58
... db_prefixes_after.add(prefix.value)
60
>>> set(found_prefixes).issubset(db_prefixes_after)
65
If we try to initialize the PrefixLoader with a non-existent file we get a
66
ConfigError exception.
68
>>> PrefixLoader('/does/not/exist.conf')
69
Traceback (most recent call last):
73
Or initialize with a file that's not a real lazr.config file.
76
>>> PrefixLoader(os.path.join(settings.ROOT, 'Makefile'))
77
Traceback (most recent call last):
79
MissingSectionHeaderError: File contains no section headers.