~ubuntu-branches/ubuntu/raring/virtinst/raring-proposed

« back to all changes in this revision

Viewing changes to virtconv/formats.py

  • Committer: Bazaar Package Importer
  • Author(s): Guido Günther
  • Date: 2008-11-19 09:19:29 UTC
  • mto: (1.4.1 sid)
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20081119091929-vwksujnqzo1utdln
Tags: upstream-0.400.0
Import upstream version 0.400.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 
3
# Use is subject to license terms.
 
4
#
 
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.
 
9
#
 
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.
 
14
#
 
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,
 
18
# MA 02110-1301 USA.
 
19
#
 
20
 
 
21
import os
 
22
 
 
23
_parsers = [ ]
 
24
 
 
25
class parser(object):
 
26
    """
 
27
    Base class for particular config file format definitions of
 
28
    a VM instance.
 
29
 
 
30
    Warning: this interface is not (yet) considered stable and may
 
31
    change at will.
 
32
    """
 
33
 
 
34
    @staticmethod
 
35
    def identify_file(input_file):
 
36
        """
 
37
        Return True if the given file is of this format.
 
38
        """
 
39
        raise NotImplementedError
 
40
 
 
41
    @staticmethod
 
42
    def import_file(input_file):
 
43
        """
 
44
        Import a configuration file.  Raises if the file couldn't be
 
45
        opened, or parsing otherwise failed.
 
46
        """
 
47
        raise NotImplementedError
 
48
 
 
49
    @staticmethod
 
50
    def export_file(vm, output_file):
 
51
        """
 
52
        Export a configuration file.
 
53
        @vm vm configuration instance
 
54
        @output_file Output file
 
55
 
 
56
        Raises ValueError if configuration is not suitable, or another
 
57
        exception on failure to write the output file.
 
58
        """
 
59
        raise NotImplementedError
 
60
            
 
61
 
 
62
def register_parser(parser):
 
63
    """
 
64
    Register a particular config format parser.  This should be called by each
 
65
    config plugin on import.
 
66
    """
 
67
 
 
68
    global _parsers
 
69
    _parsers += [ parser ]
 
70
 
 
71
def parser_by_name(name):
 
72
    """
 
73
    Return the parser of the given name.
 
74
    """
 
75
    parsers = [p for p in _parsers if p.name == name]
 
76
    if len(parsers):
 
77
        return parsers[0]
 
78
 
 
79
def find_parser_by_file(input_file):
 
80
    """
 
81
    Return the parser that is capable of comprehending the given file.
 
82
    """
 
83
    for p in _parsers:
 
84
        if p.identify_file(input_file):
 
85
            return p
 
86
    return None
 
87
 
 
88
def formats():
 
89
    """
 
90
    Return a list of supported formats.
 
91
    """
 
92
    return [p.name for p in _parsers]
 
93
 
 
94
def input_formats():
 
95
    """
 
96
    Return a list of supported input formats.
 
97
    """
 
98
    return [p.name for p in _parsers if p.can_import]
 
99
 
 
100
def output_formats():
 
101
    """
 
102
    Return a list of supported output formats.
 
103
    """
 
104
    return [p.name for p in _parsers if p.can_export]
 
105
 
 
106
def find_input(path, format = None):
 
107
    """
 
108
    Search for a configuration file automatically. If @format is given,
 
109
    then only search using a matching format parser.
 
110
    """
 
111
 
 
112
    if os.path.isdir(path):
 
113
        files = os.listdir(path)
 
114
 
 
115
    for p in _parsers:
 
116
        if not p.can_identify:
 
117
            continue
 
118
        if format and format != p.name:
 
119
            continue
 
120
 
 
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)
 
128
 
 
129
    raise StandardError("unknown format")