2
# Copyright 2008 Sun Microsystems, Inc. All rights reserved.
3
# Use is subject to license terms.
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
27
Base class for particular config file format definitions of
30
Warning: this interface is not (yet) considered stable and may
35
def identify_file(input_file):
37
Return True if the given file is of this format.
39
raise NotImplementedError
42
def import_file(input_file):
44
Import a configuration file. Raises if the file couldn't be
45
opened, or parsing otherwise failed.
47
raise NotImplementedError
50
def export_file(vm, output_file):
52
Export a configuration file.
53
@vm vm configuration instance
54
@output_file Output file
56
Raises ValueError if configuration is not suitable, or another
57
exception on failure to write the output file.
59
raise NotImplementedError
62
def register_parser(parser):
64
Register a particular config format parser. This should be called by each
65
config plugin on import.
69
_parsers += [ parser ]
71
def parser_by_name(name):
73
Return the parser of the given name.
75
parsers = [p for p in _parsers if p.name == name]
79
def find_parser_by_file(input_file):
81
Return the parser that is capable of comprehending the given file.
84
if p.identify_file(input_file):
90
Return a list of supported formats.
92
return [p.name for p in _parsers]
96
Return a list of supported input formats.
98
return [p.name for p in _parsers if p.can_import]
100
def output_formats():
102
Return a list of supported output formats.
104
return [p.name for p in _parsers if p.can_export]
106
def find_input(path, format = None):
108
Search for a configuration file automatically. If @format is given,
109
then only search using a matching format parser.
112
if os.path.isdir(path):
113
files = os.listdir(path)
116
if not p.can_identify:
118
if format and format != p.name:
121
if os.path.isfile(path):
122
if p.identify_file(path):
123
return (path, p.name)
124
elif os.path.isdir(path):
125
for cfgfile in [ x for x in files if x.endswith(p.suffix) ]:
126
if p.identify_file(os.path.join(path, cfgfile)):
127
return (os.path.join(path, cfgfile), p.name)
129
raise StandardError("unknown format")