• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python3
2
3# Copyright (c) 2015, Intel Corporation
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without modification,
7# are permitted provided that the following conditions are met:
8#
9# 1. Redistributions of source code must retain the above copyright notice, this
10# list of conditions and the following disclaimer.
11#
12# 2. Redistributions in binary form must reproduce the above copyright notice,
13# this list of conditions and the following disclaimer in the documentation and/or
14# other materials provided with the distribution.
15#
16# 3. Neither the name of the copyright holder nor the names of its contributors
17# may be used to endorse or promote products derived from this software without
18# specific prior written permission.
19#
20# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
24# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
27# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31
32import argparse
33import sys
34
35import EddParser
36from PfwBaseTranslator import PfwBaseTranslator
37
38class PfwScriptTranslator(PfwBaseTranslator):
39
40    def __init__(self):
41        super(PfwScriptTranslator, self).__init__()
42
43        self._script = []
44
45    def getScript(self):
46        return self._script
47
48    def _appendCommand(self, *args):
49        self._script.append(list(args))
50
51    def _doCreateDomain(self, name):
52        self._appendCommand("createDomain", name)
53
54    def _doSetSequenceAware(self):
55        self._appendCommand("setSequenceAwareness", self._ctx_domain, "true")
56
57    def _doAddElement(self, path):
58        self._appendCommand("addElement", self._ctx_domain, path)
59
60    def _doCreateConfiguration(self, name):
61        self._appendCommand("createConfiguration", self._ctx_domain, name)
62
63    def _doSetElementSequence(self, paths):
64        self._appendCommand("setElementSequence", self._ctx_domain, self._ctx_configuration, *paths)
65
66    def _doSetRule(self, rule):
67        self._appendCommand("setRule", self._ctx_domain, self._ctx_configuration, rule)
68
69    def _doSetParameter(self, path, value):
70        self._appendCommand("setConfigurationParameter", self._ctx_domain, self._ctx_configuration,
71                            path, value)
72
73class ArgparseArgumentParser(object):
74    """class that parse command line arguments with argparse library
75
76    result of parsing are the class atributs"""
77    def __init__(self):
78
79        myArgParser = argparse.ArgumentParser(description='Process domain scripts.')
80
81        myArgParser.add_argument('input', nargs='?',
82                                 type=argparse.FileType('r'), default=sys.stdin,
83                                 help="the domain script file, default stdin")
84
85        myArgParser.add_argument('-o', '--output',
86                                 type=argparse.FileType('w'), default=sys.stdout,
87                                 help="the output file, default stdout")
88
89        myArgParser.add_argument('-d', '--debug',
90                                 action='store_true',
91                                 help="print debug warnings")
92
93        myArgParser.add_argument('--output-kind',
94                                 choices=['pfw', 'raw'],
95                                 default='pfw',
96                                 help="output kind; can be either 'raw' (debug only) or 'pfw' \
97                                    (pfw commands; default choice)")
98
99
100        # process command line arguments
101        options = myArgParser.parse_args()
102
103        # maping to atributs
104        self.input = options.input
105        self.output = options.output
106
107        self.debug = options.debug
108
109        self.output_kind = options.output_kind
110
111
112# ==============
113# main function
114# ==============
115
116def printE(s):
117    """print in stderr"""
118    sys.stderr.write(str(s))
119
120def main():
121
122    options = ArgparseArgumentParser()
123
124    myparser = EddParser.Parser()
125    try:
126        myroot = myparser.parse(options.input, options.debug)
127
128    except EddParser.MySyntaxError as ex:
129        printE(ex)
130        printE("EXIT ON FAILURE")
131        exit(2)
132
133    if options.output_kind == 'raw':
134        options.output.write(str(myroot))
135    else:
136        try:
137            myroot.propagate()
138
139        except EddParser.MyPropagationError as ex:
140            printE(ex)
141            printE("EXIT ON FAILURE")
142            exit(1)
143
144        if options.output_kind == 'pfw':
145            translator = PfwScriptTranslator()
146            myroot.translate(translator)
147            options.output.write("\n".join(translator.getScript()))
148
149# execute main function if the python interpreter is running this module as the main program
150if __name__ == "__main__":
151    main()
152
153