[go: up one dir, main page]

File: any.py

package info (click to toggle)
kiwi 10.2.33-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,528 kB
  • sloc: python: 67,299; sh: 3,980; xml: 3,379; ansic: 391; makefile: 354
file content (123 lines) | stat: -rw-r--r-- 4,197 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# Copyright (c) 2020 SUSE LLC.  All rights reserved.
#
# This file is part of kiwi.
#
# kiwi 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 3 of the License, or
# (at your option) any later version.
#
# kiwi 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 kiwi.  If not, see <http://www.gnu.org/licenses/>
#
from typing import Any
import importlib
import importlib.util

# project
from kiwi.utils.temporary import Temporary
from kiwi.markup.base import MarkupBase

from kiwi.exceptions import (
    KiwiDescriptionInvalid,
    KiwiAnyMarkupPluginError
)


class MarkupAny(MarkupBase):
    """
    **Implements markup handling for XML, YAML, JSON and INI**
    """
    def post_init(self) -> None:
        """
        Convert given description file into XML

        The anymarkup module supports auto detection of the given
        input format and can convert YAML, JSON and INI to XML
        """
        try:
            anymarkup_mod = 'anymarkup'
            if importlib.util.find_spec('anymarkup_core'):
                anymarkup_mod = 'anymarkup_core'
            self.anymarkup: Any = importlib.import_module(anymarkup_mod)
        except Exception as issue:
            raise KiwiAnyMarkupPluginError(issue)
        try:
            self.description_markup_processed = Temporary().new_file()
            markup = self.anymarkup.parse_file(
                self.description, force_types=None
            )
            self.anymarkup.serialize_file(
                markup, self.description_markup_processed.name, format='xml'
            )
        except Exception as issue:
            raise KiwiDescriptionInvalid(issue)

    def get_xml_description(self) -> str:
        """
        Return XML description file name

        :return: file path name

        :rtype: str
        """
        return self.apply_xslt_stylesheets(
            self.description_markup_processed.name
        )

    def get_toml_description(self) -> str:
        """
        Return TOML description file name

        :return: file path name

        :rtype: str
        """
        xml_description_xslt_transformed = self.apply_xslt_stylesheets(
            self.description_markup_processed.name
        )
        markup = self.anymarkup.parse_file(
            xml_description_xslt_transformed, force_types=None
        )
        self.anymarkup.serialize_file(
            markup, xml_description_xslt_transformed, format='toml'
        )
        return xml_description_xslt_transformed

    def get_yaml_description(self) -> str:
        """
        Return YAML description file name

        :return: file path name

        :rtype: str
        """
        xml_description_xslt_transformed = self.apply_xslt_stylesheets(
            self.description_markup_processed.name
        )
        markup = self.anymarkup.parse_file(
            xml_description_xslt_transformed, force_types=None
        )
        # The translation to yaml runs from a translation to json first
        # This is done because a direct translation from xml to yaml
        # causes the ordered mapping to be included. See
        # http://yaml.org/type/omap.html. In the context of kiwi this
        # ordering information is however not needed. The order of
        # sections and attributes doesn't play a role. In json no
        # such ordering concept exists and this is way to convert to
        # yaml without the !!omap definitions.
        self.anymarkup.serialize_file(
            markup, xml_description_xslt_transformed, format='json'
        )
        markup = self.anymarkup.parse_file(
            xml_description_xslt_transformed, force_types=None
        )
        self.anymarkup.serialize_file(
            markup, xml_description_xslt_transformed, format='yaml'
        )
        return xml_description_xslt_transformed