2 # Maemian::Data -- interface to query lists of keywords
4 # Copyright (C) 2008 Russ Allbery
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by the Free
8 # Software Foundation; either version 2 of the License, or (at your option)
11 # This program is distributed in the hope that it will be useful, but WITHOUT
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 # You should have received a copy of the GNU General Public License along with
17 # this program. If not, see <http://www.gnu.org/licenses/>.
19 package Maemian::Data;
24 # The constructor loads a list into a hash in %data, which is private to this
25 # module. Use %data as a cache to avoid loading the same list more than once
26 # (which means lintian doesn't support having the list change over the life of
27 # the proces. The returned object knows what list, stored in %data, it is
32 my ($class, $type, $separator) = @_;
33 croak('no data type specified') unless $type;
34 unless (exists $data{$type}) {
35 my $dir = $ENV{MAEMIAN_ROOT} . '/data';
36 open(LIST, '<', "$dir/$type")
37 or croak("unknown data type $type");
45 if (defined $separator) {
46 ($key, $val) = split(/$separator/, $_, 2);
48 ($key, $val) = ($_ => 1);
50 $data{$type}{$key} = $val;
53 my $self = { data => $data{$type} };
59 # Query a data object for whether a particular keyword is valid.
61 my ($self, $keyword) = @_;
62 return (exists $self->{data}{$keyword}) ? 1 : undef;
65 # Return all known keywords (in no particular order).
68 return keys(%{ $self->{data} });
71 # Query a data object for the value attached to a particular keyword.
73 my ($self, $keyword) = @_;
74 return (exists $self->{data}{$keyword}) ? $self->{data}{$keyword} : undef;
81 Maemian::Data - Maemian interface to query lists of keywords
85 my $list = Maemian::Data->new('type');
86 if ($list->known($keyword)) {
89 my $hash = Maemian::Data->new('another-type', '\s+');
90 if ($list->value($keyword) > 1) {
93 my @keywords = $list->all;
97 Maemian::Data provides a way of loading a list of keywords or key/value
98 pairs from a file in the Maemian root and then querying that list.
99 The lists are stored in the F<data> directory of the Maemian root and
100 consist of one keyword or key/value pair per line. Blank lines and
101 lines beginning with C<#> are ignored. Leading and trailing whitespace
104 If requested, the lines are split into key/value pairs with a given
105 separator regular expression. Otherwise, keywords are taken verbatim
106 as they are listed in the file and may include spaces.
108 This module allows lists such as menu sections, doc-base sections,
109 obsolete packages, package fields, and so forth to be stored in simple,
110 easily editable files.
116 =item new(TYPE [,SEPARATOR])
118 Creates a new Maemian::Data object for the given TYPE. TYPE is a partial
119 path relative to the F<data> directory and should correspond to a file in
120 that directory. The contents of that file will be loaded into memory and
121 returned as part of the newly created object. On error, new() throws an
124 If SEPARATOR is given, it will be used as a regular expression for splitting
125 the lines into key/value pairs.
127 A given file will only be loaded once. If new() is called again with the
128 same TYPE argument, the data previously loaded will be reused, avoiding
133 =head1 INSTANCE METHODS
139 Returns all keywords listed in the data file as a list (in no particular
140 order; the original order is not preserved). In a scalar context, returns
141 the number of keywords.
145 Returns true if KEYWORD was listed in the data file represented by this
146 Maemian::Data instance and false otherwise.
150 Returns the value attached to KEYWORD if it was listed in the data
151 file represented by this Maemian::Data instance and the undefined value
152 otherwise. If SEPARATOR was not given, the value will '1'.
160 =item no data type specified
162 new() was called without a TYPE argument.
164 =item unknown data type %s
166 The TYPE argument to new() did not correspond to a file in the F<data>
167 directory of the Maemian root.
175 =item MAEMIAN_ROOT/data
177 The files loaded by this module must be located in this directory.
178 Relative paths containing a C</> are permitted, so files may be organized
179 in subdirectories in this directory.
189 This variable must be set to Maemian's root directory (normally
190 F</usr/share/lintian> when Maemian is installed as a Debian package). The
191 B<lintian> program normally takes care of doing this. This module doesn't
192 care about the contents of this directory other than expecting the F<data>
193 subdirectory of this directory to contain its files.
199 Originally written by Russ Allbery <rra@debian.org> for Maemian.
210 # indent-tabs-mode: nil
211 # cperl-indent-level: 4
213 # vim: syntax=perl sw=4 sts=4 ts=4 et shiftround