2013-03-18 05:06:52 +08:00
|
|
|
#
|
|
|
|
# Copyright 2008 Sun Microsystems, Inc. All rights reserved.
|
|
|
|
# Use is subject to license terms.
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
# MA 02110-1301 USA.
|
|
|
|
#
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
2013-04-14 02:34:52 +08:00
|
|
|
_parsers = []
|
2013-03-18 05:06:52 +08:00
|
|
|
|
2013-03-18 06:18:22 +08:00
|
|
|
|
2013-03-18 05:06:52 +08:00
|
|
|
class parser(object):
|
|
|
|
"""
|
|
|
|
Base class for particular config file format definitions of
|
|
|
|
a VM instance.
|
|
|
|
|
|
|
|
Warning: this interface is not (yet) considered stable and may
|
|
|
|
change at will.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def identify_file(input_file):
|
|
|
|
"""
|
|
|
|
Return True if the given file is of this format.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def import_file(input_file):
|
|
|
|
"""
|
|
|
|
Import a configuration file. Raises if the file couldn't be
|
|
|
|
opened, or parsing otherwise failed.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def export(vm):
|
|
|
|
"""
|
|
|
|
Export a configuration file as a string.
|
|
|
|
@vm vm configuration instance
|
|
|
|
|
|
|
|
Raises ValueError if configuration is not suitable.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2013-04-14 02:34:52 +08:00
|
|
|
|
2013-03-18 05:06:52 +08:00
|
|
|
def register_parser(new_parser):
|
|
|
|
"""
|
|
|
|
Register a particular config format parser. This should be called by each
|
|
|
|
config plugin on import.
|
|
|
|
"""
|
|
|
|
|
|
|
|
global _parsers
|
2013-04-14 02:34:52 +08:00
|
|
|
_parsers += [new_parser]
|
|
|
|
|
2013-03-18 05:06:52 +08:00
|
|
|
|
|
|
|
def parser_by_name(name):
|
|
|
|
"""
|
|
|
|
Return the parser of the given name.
|
|
|
|
"""
|
|
|
|
parsers = [p for p in _parsers if p.name == name]
|
|
|
|
if len(parsers):
|
|
|
|
return parsers[0]
|
|
|
|
|
2013-04-14 02:34:52 +08:00
|
|
|
|
2013-03-18 05:06:52 +08:00
|
|
|
def find_parser_by_file(input_file):
|
|
|
|
"""
|
|
|
|
Return the parser that is capable of comprehending the given file.
|
|
|
|
"""
|
|
|
|
for p in _parsers:
|
|
|
|
if p.identify_file(input_file):
|
|
|
|
return p
|
|
|
|
return None
|
|
|
|
|
2013-04-14 02:34:52 +08:00
|
|
|
|
2013-03-18 05:06:52 +08:00
|
|
|
def formats():
|
|
|
|
"""
|
|
|
|
Return a list of supported formats.
|
|
|
|
"""
|
|
|
|
return [p.name for p in _parsers]
|
|
|
|
|
2013-04-14 02:34:52 +08:00
|
|
|
|
2013-03-18 05:06:52 +08:00
|
|
|
def input_formats():
|
|
|
|
"""
|
|
|
|
Return a list of supported input formats.
|
|
|
|
"""
|
|
|
|
return [p.name for p in _parsers if p.can_import]
|
|
|
|
|
2013-04-14 02:34:52 +08:00
|
|
|
|
2013-03-18 05:06:52 +08:00
|
|
|
def output_formats():
|
|
|
|
"""
|
|
|
|
Return a list of supported output formats.
|
|
|
|
"""
|
|
|
|
return [p.name for p in _parsers if p.can_export]
|
|
|
|
|
2013-04-14 02:34:52 +08:00
|
|
|
|
2013-04-12 20:26:21 +08:00
|
|
|
def find_input(path, fmt=None):
|
2013-03-18 05:06:52 +08:00
|
|
|
"""
|
|
|
|
Search for a configuration file automatically. If @format is given,
|
|
|
|
then only search using a matching format parser.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if os.path.isdir(path):
|
|
|
|
files = os.listdir(path)
|
|
|
|
|
|
|
|
for p in _parsers:
|
|
|
|
if not p.can_identify:
|
|
|
|
continue
|
2013-04-12 20:26:21 +08:00
|
|
|
if fmt and fmt != p.name:
|
2013-03-18 05:06:52 +08:00
|
|
|
continue
|
|
|
|
|
|
|
|
if os.path.isfile(path):
|
|
|
|
if p.identify_file(path):
|
|
|
|
return (path, p.name)
|
|
|
|
elif os.path.isdir(path):
|
2013-04-14 02:34:52 +08:00
|
|
|
for cfgfile in [x for x in files if x.endswith(p.suffix)]:
|
2013-03-18 05:06:52 +08:00
|
|
|
if p.identify_file(os.path.join(path, cfgfile)):
|
|
|
|
return (os.path.join(path, cfgfile), p.name)
|
|
|
|
|
|
|
|
raise StandardError(_("Unknown format"))
|