• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python2
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
31import EddParser
32from PfwBaseTranslator import PfwBaseTranslator
33
34import argparse
35import sys
36
37class PfwScriptTranslator(PfwBaseTranslator):
38
39    def __init__(self):
40        super(PfwScriptTranslator, self).__init__()
41
42        self._script = []
43
44    def getScript(self):
45        return self._script
46
47    def _appendCommand(self, *args):
48        self._script.append(list(args))
49
50    def _doCreateDomain(self, name):
51        self._appendCommand("createDomain", name)
52
53    def _doSetSequenceAware(self):
54        self._appendCommand("setSequenceAwareness", self._ctx_domain, "true")
55
56    def _doAddElement(self, path):
57        self._appendCommand("addElement", self._ctx_domain, path)
58
59    def _doCreateConfiguration(self, name):
60        self._appendCommand("createConfiguration", self._ctx_domain, name)
61
62    def _doSetElementSequence(self, paths):
63        self._appendCommand("setElementSequence", self._ctx_domain, self._ctx_configuration, *paths)
64
65    def _doSetRule(self, rule):
66        self._appendCommand("setRule", self._ctx_domain, self._ctx_configuration, rule)
67
68    def _doSetParameter(self, path, value):
69        self._appendCommand("setConfigurationParameter", self._ctx_domain, self._ctx_configuration, path, value)
70
71class ArgparseArgumentParser(object) :
72    """class that parse command line arguments with argparse library
73
74    result of parsing are the class atributs"""
75    def __init__(self):
76
77        myArgParser = argparse.ArgumentParser(description='Process domain scripts.')
78
79        myArgParser.add_argument('input', nargs='?',
80                type=argparse.FileType('r'), default=sys.stdin,
81                help="the domain script file, default stdin")
82
83        myArgParser.add_argument('-o', '--output',
84                type=argparse.FileType('w'), default=sys.stdout,
85                help="the output file, default stdout")
86
87        myArgParser.add_argument('-d', '--debug',
88                action='store_true',
89                help="print debug warnings")
90
91        myArgParser.add_argument('--output-kind',
92                choices=['pfw', 'raw'],
93                default='pfw',
94                help="output kind; can be either 'raw' (debug only) or 'pfw' (pfw commands; default choice)")
95
96
97        # process command line arguments
98        options = myArgParser.parse_args()
99
100        # maping to atributs
101        self.input = options.input
102        self.output = options.output
103
104        self.debug = options.debug
105
106        self.output_kind = options.output_kind
107
108
109# ==============
110# main function
111# ==============
112
113def printE(s):
114    """print in stderr"""
115    sys.stderr.write(str(s))
116
117def main ():
118
119    options = ArgparseArgumentParser()
120
121    myparser = EddParser.Parser()
122    try:
123        myroot = myparser.parse(options.input, options.debug)
124
125    except EddParser.MySyntaxError as ex:
126        printE(ex)
127        printE("EXIT ON FAILURE")
128        exit(2)
129
130    if options.output_kind == 'raw':
131        options.output.write(str(myroot))
132    else:
133        try:
134            myroot.propagate()
135
136        except EddParser.MyPropagationError, ex :
137            printE(ex)
138            printE("EXIT ON FAILURE")
139            exit(1)
140
141        if options.output_kind == 'pfw':
142            translator = PfwScriptTranslator()
143            myroot.translate(translator)
144            options.output.write("\n".join(translator.getScript()))
145
146# execute main function if the python interpreter is running this module as the main program
147if __name__ == "__main__" :
148    main()
149
150