Example #1
0
class PuppetConverter():
    def __init__(self, minput, filters=None):
        self.__output = {}
        self.__input = copy.deepcopy(minput)
        self.__filter = Filter(filters)
        self.__prev_obj = None

    # main method
    @GeneralException
    def run(self):
        Tools.l(INFO, "running", 'run', self)

        #empty imput
        if not self.__input:
            Tools.l(ERR, "empty input", 'run', self)
            return {}

        # convert
        self.__generate_classes(self.__input)

        # add exceptions
        if GLOBAL_EXEC_EQ:
            self.__add_global_exec()

        Tools.l(INFO, "complete", 'run', self)
        return self.__output

    # generate global exec
    @GeneralException
    def __add_global_exec(self):
        Tools.l(INFO, "adding Exec section", 'add_global_exec', self)
        self.__output[GLOBAL_SEC_EQ['Exec']] = {MAIN_SECTION: {}}
        for key in GLOBAL_EXEC_EQ:
            Tools.l(INFO, "adding key %s" % (key), 'add_global_exec', self)
            self.__output[GLOBAL_SEC_EQ['Exec']][MAIN_SECTION][
                key] = self.__process_values('', 'Exec', key,
                                             GLOBAL_EXEC_EQ[key])

    # generate sub execs
    @GeneralException
    def __add_top_class(self, key):
        c = {}
        for order in REQUIRE_EQ:
            if key in order[0]: break
            elif key in order[1]:
                req = []
                for r in order[0]:
                    req.append(r)
                if req:
                    c = Tools.dict_merging(c, {GLOBAL_SEC_EQ['require']: req})
        if key in EXEC_EQ:
            Tools.l(INFO, "adding exec section for %s" % (key), 'add_exec',
                    self)
            c = Tools.dict_merging(
                c, {GLOBAL_SEC_EQ['exec']: {
                        EXEC_EQ[key]: GLOBAL_EXEC_EQ
                    }})
        return c

    # processing on values
    @GeneralException
    def __process_values(self, gclass, name, key, val):
        if type(val) is int:
            val = "%s" % (val)
        elif (type(val) is not str) or (not val):
            return val
        if (gclass in APPSEC_EQ) and (key == APPSEC_EQ[gclass][0]):
            val = APPSEC_EQ[gclass][1] + val
        return self.__filter.item_replace(gclass, key, val, name)

    # processing on data
    @GeneralException
    def __process_data(self, input, gclass, name, cur_class):
        Tools.l(INFO, "processing data", 'process_data', self)
        # modifications
        kcontent = Tools.list_merging(AVOIDSEC_EQ.get(MAIN_SECTION),
                                      AVOIDSEC_EQ.get(gclass))
        for key in kcontent:
            if key in input:
                input[key] = None
        kcontent = Tools.s_dict_merging(CONTENTADD_EQ.get(MAIN_SECTION),
                                        CONTENTADD_EQ.get(gclass), False)
        for key in kcontent:
            input[key] = kcontent[key]
        kcontent = Tools.s_dict_merging(CONTENTKEY_EQ.get(MAIN_SECTION),
                                        CONTENTKEY_EQ.get(gclass), False)
        for key in kcontent:
            if key in input:
                input[kcontent[key]] = input.pop(key)
        kcontent = Tools.s_dict_merging(CONTENTVAL_EQ.get(MAIN_SECTION),
                                        CONTENTVAL_EQ.get(gclass), False)
        for key in kcontent:
            if key in input:
                if input[key] == kcontent[key][0]:
                    input[key] = kcontent[key][1]

        # exec dependency
        if cur_class in EXEC_EQ:
            input['require'] = Tools.dict_merging(
                input.get('require'), {
                    GLOBAL_SEC_EQ['exec'][len(ACTION_ID):].capitalize():
                    [EXEC_EQ[cur_class]]
                })

        # main loop
        for key in input:
            if type(input[key]) is list:
                store = []
                for d in input[key]:
                    store.append(self.__process_values(gclass, name, key, d))
                input[key] = store
            else:
                input[key] = self.__process_values(gclass, name, key,
                                                   input[key])
        return input

    # processing on section
    @GeneralException
    def __process_sec(self, data, gclass, name, cur_class):
        Tools.l(INFO, "creating section %s" % (SECTION_EQ[gclass]),
                'process_sec', self)
        if (SECTION_EQ[gclass] == ACTION_ID + 'package'):
            data[gclass][name] = self.__filter.update_package(
                data[gclass][name], name, 'latest')
        return self.__process_data(data[gclass][name], gclass, name, cur_class)

    # class generation
    @GeneralException
    def __generate_classes(self, data):
        for gclass in data:
            if gclass not in SECTION_EQ:
                Tools.l(INFO, "Ignored unknown class %s" % (gclass),
                        'generate_classes', self)
                continue
            Tools.l(INFO, "creating class %s" % (gclass), 'generate_classes',
                    self)
            self.__prev_obj = None
            self.__output[gclass] = self.__add_top_class(gclass)
            for name in sorted(data[gclass]):
                if gclass in SUBCLASS_EQ:
                    subkey = data[gclass][name][SUBCLASS_EQ[gclass]
                                                [MAIN_SECTION]]
                    Tools.l(INFO, "creating sub class %s" % (subkey),
                            'generate_classes', self)
                    self.__output[gclass].setdefault(
                        subkey, self.__add_top_class(subkey))
                    self.__output[gclass][subkey].setdefault(
                        SECTION_EQ[gclass], {})
                    self.__output[gclass][subkey][
                        SECTION_EQ[gclass]][name] = self.__process_sec(
                            data, gclass, name, subkey)
                else:
                    self.__output[gclass].setdefault(SECTION_EQ[gclass], {})
                    self.__output[gclass][
                        SECTION_EQ[gclass]][name] = self.__process_sec(
                            data, gclass, name, gclass)
                self.__prev_obj = name
Example #2
0
class PuppetConverter():
    def __init__(self, minput, filters = None):
        self.__output = {}
        self.__input = copy.deepcopy(minput)
        self.__filter = Filter(filters)
        self.__prev_obj = None

    # main method
    @GeneralException
    def run(self):
        Tools.l(INFO, "running", 'run', self)

        #empty imput
        if not self.__input:
            Tools.l(ERR, "empty input", 'run', self)
            return {}

        # convert
        self.__generate_classes(self.__input)

        # add exceptions
        if GLOBAL_EXEC_EQ:
            self.__add_global_exec()

        Tools.l(INFO, "complete", 'run', self)
        return self.__output

    # generate global exec
    @GeneralException
    def __add_global_exec(self):
        Tools.l(INFO, "adding Exec section", 'add_global_exec', self)
        self.__output[GLOBAL_SEC_EQ['Exec']] = {MAIN_SECTION : {}}
        for key in GLOBAL_EXEC_EQ:
            Tools.l(INFO, "adding key %s" % (key), 'add_global_exec', self)
            self.__output[GLOBAL_SEC_EQ['Exec']][MAIN_SECTION][key] = self.__process_values('', 'Exec', key, GLOBAL_EXEC_EQ[key])

    # generate sub execs
    @GeneralException
    def __add_top_class(self, key):
        c = {}
        for order in REQUIRE_EQ:
            if key in order[0]: break
            elif key in order[1]:
                req = []
                for r in order[0]:
                    req.append(r)
                if req:
                    c = Tools.dict_merging(c, {
                            GLOBAL_SEC_EQ['require'] : req
                            })
        if key in EXEC_EQ:
            Tools.l(INFO, "adding exec section for %s" % (key), 'add_exec', self)
            c = Tools.dict_merging(c, {
                    GLOBAL_SEC_EQ['exec'] : {
                        EXEC_EQ[key] : GLOBAL_EXEC_EQ
                        }
                    })
        return c

    # processing on values
    @GeneralException
    def __process_values(self, gclass, name, key, val):
        if type(val) is int:
            val = "%s" % (val)
        elif (type(val) is not str) or (not val):
            return val
        if (gclass in APPSEC_EQ) and (key == APPSEC_EQ[gclass][0]):
            val = APPSEC_EQ[gclass][1] + val
        return self.__filter.item_replace(gclass, key, val, name)


    # processing on data
    @GeneralException
    def __process_data(self, input, gclass, name, cur_class):
        Tools.l(INFO, "processing data", 'process_data', self)
        # modifications
        kcontent = Tools.list_merging(AVOIDSEC_EQ.get(MAIN_SECTION), AVOIDSEC_EQ.get(gclass))
        for key in kcontent:
            if key in input:
                input[key] = None
        kcontent = Tools.s_dict_merging(CONTENTADD_EQ.get(MAIN_SECTION), CONTENTADD_EQ.get(gclass), False)
        for key in kcontent:
            input[key] = kcontent[key]
        kcontent = Tools.s_dict_merging(CONTENTKEY_EQ.get(MAIN_SECTION), CONTENTKEY_EQ.get(gclass), False)
        for key in kcontent:
            if key in input:
                input[kcontent[key]] = input.pop(key)
        kcontent = Tools.s_dict_merging(CONTENTVAL_EQ.get(MAIN_SECTION), CONTENTVAL_EQ.get(gclass), False)
        for key in kcontent:
            if key in input:
                if input[key] == kcontent[key][0]:
                    input[key] = kcontent[key][1]

        # exec dependency
        if cur_class in EXEC_EQ:
            input['require'] = Tools.dict_merging(input.get('require'), {
                    GLOBAL_SEC_EQ['exec'][len(ACTION_ID):].capitalize() : [
                        EXEC_EQ[cur_class]
                        ]
                    })

        # main loop
        for key in input:
            if type(input[key]) is list:
                store = []
                for d in input[key]:
                    store.append(self.__process_values(gclass, name, key, d))
                input[key] = store
            else:
                input[key] = self.__process_values(gclass, name, key, input[key])
        return input

    # processing on section
    @GeneralException
    def __process_sec(self, data, gclass, name, cur_class):
        Tools.l(INFO, "creating section %s" % (SECTION_EQ[gclass]), 'process_sec', self)
        if (SECTION_EQ[gclass] == ACTION_ID+'package'):
            data[gclass][name] = self.__filter.update_package(data[gclass][name], name, 'latest')
        return self.__process_data(data[gclass][name], gclass, name, cur_class)

    # class generation
    @GeneralException
    def __generate_classes(self, data):
        for gclass in data:
            if gclass not in SECTION_EQ:
                Tools.l(INFO, "Ignored unknown class %s" % (gclass), 'generate_classes', self)
                continue
            Tools.l(INFO, "creating class %s" % (gclass), 'generate_classes', self)
            self.__prev_obj = None
            self.__output[gclass] = self.__add_top_class(gclass)
            for name in sorted(data[gclass]):
                if gclass in SUBCLASS_EQ:
                    subkey = data[gclass][name][SUBCLASS_EQ[gclass][MAIN_SECTION]]
                    Tools.l(INFO, "creating sub class %s" % (subkey), 'generate_classes', self)
                    self.__output[gclass].setdefault(subkey, self.__add_top_class(subkey))
                    self.__output[gclass][subkey].setdefault(SECTION_EQ[gclass], {})
                    self.__output[gclass][subkey][SECTION_EQ[gclass]][name] = self.__process_sec(data, gclass, name, subkey)
                else:
                    self.__output[gclass].setdefault(SECTION_EQ[gclass], {})
                    self.__output[gclass][SECTION_EQ[gclass]][name] = self.__process_sec(data, gclass, name, gclass)
                self.__prev_obj = name
Example #3
0
class SaltConverter():
    def __init__(self, minput, filters=None):
        self.__output = {}
        self.__input = copy.deepcopy(minput)
        self.__filter = Filter(filters)

    # main method
    @GeneralException
    def run(self):
        Tools.l(INFO, "running", 'run', self)

        #empty imput
        if not self.__input:
            Tools.l(ERR, "empty input", 'run', self)
            return {}

        self.__curent_state = None

        # convert
        self.__generate_classes(self.__input)

        Tools.l(INFO, "complete", 'run', self)
        return self.__output

    # processing on values
    @GeneralException
    def __process_values(self, manifest, name, key, val):
        if type(val) is int:
            val = "%s" % (val)
        elif (type(val) is not str) or (not val):
            return val
        if (manifest in APPSEC_EQ) and (key == APPSEC_EQ[manifest][0]):
            val = APPSEC_EQ[manifest][1] + val
        return self.__filter.item_replace(manifest, key, val, name)

    # processing on data
    # recursive
    @GeneralException
    def __structure_data(self, data, manifest, name):
        Tools.l(INFO, "building data structure", 'structure_data', self)
        for key in data:
            if type(data[key]) is dict:
                data[key] = self.__structure_data(data[key], manifest, name)
            elif type(data[key]) is list:
                store = []
                for d in data[key]:
                    store.append(self.__process_values(manifest, name, key, d))
                data[key] = store
            else:
                data[key] = self.__process_values(manifest, name, key,
                                                  data[key])
        Tools.l(INFO, "building data structure complete", 'structure_data',
                self)
        return data

    # processing on data
    @GeneralException
    def __process_data(self, data, manifest, name):
        Tools.l(INFO, "processing data", 'process_data', self)
        sec_key = (self.__curent_state
                   if type(SECTION_EQ[manifest]) is dict else manifest)

        # modifications
        kcontent = Tools.list_merging(AVOIDSEC_EQ.get(MAIN_SECTION),
                                      AVOIDSEC_EQ.get(sec_key))
        for key in kcontent:
            if key in data:
                data[key] = None
        kcontent = Tools.s_dict_merging(CONTENTADD_EQ.get(MAIN_SECTION),
                                        CONTENTADD_EQ.get(sec_key), False)
        for key in kcontent:
            data[key] = kcontent[key]
        kcontent = Tools.s_dict_merging(CONTENTKEY_EQ.get(MAIN_SECTION),
                                        CONTENTKEY_EQ.get(sec_key), False)
        for key in kcontent:
            if key in data:
                data[kcontent[key]] = Tools.merge_string_list(
                    data.get(kcontent[key]), data.pop(key))
        kcontent = Tools.s_dict_merging(CONTENTVAL_EQ.get(MAIN_SECTION),
                                        CONTENTVAL_EQ.get(sec_key), False)
        for key in kcontent:
            if key in data:
                if (data[key] == kcontent[key][0]) or (kcontent[key][0]
                                                       == MAIN_SECTION):
                    if type(kcontent[key][1]) is str:
                        data[key] = kcontent[key][1]
                    else:
                        data[key] = kcontent[key][1](data[key], data)

        # set action key
        kcontent = ACTIONKEY_EQ.get(sec_key)
        if type(kcontent) is str:
            data[kcontent] = MAIN_SECTION
        elif kcontent:
            data[kcontent(data)] = MAIN_SECTION

        # main loop
        data = self.__structure_data(data, manifest, name)
        Tools.l(INFO, "processing data complete", 'process_data', self)
        return data

    # ganaration method
    @GeneralException
    def __generate_classes(self, data):
        for manifest in sorted(data):
            if manifest not in SECTION_EQ:
                Tools.l(INFO, "Ignored unknown class %s" % (manifest),
                        'generate_classes', self)
                continue
            Tools.l(INFO, "creating manifest %s" % (manifest),
                    'generate_classes', self)
            for name in sorted(data[manifest]):
                if type(SECTION_EQ[manifest]) is dict:
                    ref = data[manifest][name].get(SECTION_EQ[manifest]['key'])
                    if not ref:
                        Tools.l(
                            ERR, "Reference key not found %s" %
                            (SECTION_EQ[manifest]['key']), 'generate_classes',
                            self)
                        continue
                    state = SECTION_EQ[manifest].get(ref)
                    if not state:
                        Tools.l(
                            ERR, "State not found ref %s, manifest %s" %
                            (ref, manifest), 'generate_classes', self)
                        continue
                else:
                    state = SECTION_EQ.get(manifest)
                    if not state:
                        Tools.l(ERR,
                                "State not found manifest %s" % (manifest),
                                'generate_classes', self)
                        continue
                if (manifest == 'packages'):
                    data[manifest][name] = self.__filter.update_package(
                        data[manifest][name], name, 'latest')
                self.__output.setdefault(manifest, {})
                self.__output[manifest].setdefault(state, {})
                self.__curent_state = state
                self.__output[manifest][state][name] = self.__process_data(
                    data[manifest][name], manifest, name)
Example #4
0
class SaltConverter:
    def __init__(self, minput, filters=None):
        self.__output = {}
        self.__input = copy.deepcopy(minput)
        self.__filter = Filter(filters)

    # main method
    @GeneralException
    def run(self):
        Tools.l(INFO, "running", "run", self)

        # empty imput
        if not self.__input:
            Tools.l(ERR, "empty input", "run", self)
            return {}

        self.__curent_state = None

        # convert
        self.__generate_classes(self.__input)

        Tools.l(INFO, "complete", "run", self)
        return self.__output

    # processing on values
    @GeneralException
    def __process_values(self, manifest, name, key, val):
        if type(val) is int:
            val = "%s" % (val)
        elif (type(val) is not str) or (not val):
            return val
        if (manifest in APPSEC_EQ) and (key == APPSEC_EQ[manifest][0]):
            val = APPSEC_EQ[manifest][1] + val
        return self.__filter.item_replace(manifest, key, val, name)

    # processing on data
    # recursive
    @GeneralException
    def __structure_data(self, data, manifest, name):
        Tools.l(INFO, "building data structure", "structure_data", self)
        for key in data:
            if type(data[key]) is dict:
                data[key] = self.__structure_data(data[key], manifest, name)
            elif type(data[key]) is list:
                store = []
                for d in data[key]:
                    store.append(self.__process_values(manifest, name, key, d))
                data[key] = store
            else:
                data[key] = self.__process_values(manifest, name, key, data[key])
        Tools.l(INFO, "building data structure complete", "structure_data", self)
        return data

    # processing on data
    @GeneralException
    def __process_data(self, data, manifest, name):
        Tools.l(INFO, "processing data", "process_data", self)
        sec_key = self.__curent_state if type(SECTION_EQ[manifest]) is dict else manifest

        # modifications
        kcontent = Tools.list_merging(AVOIDSEC_EQ.get(MAIN_SECTION), AVOIDSEC_EQ.get(sec_key))
        for key in kcontent:
            if key in data:
                data[key] = None
        kcontent = Tools.s_dict_merging(CONTENTADD_EQ.get(MAIN_SECTION), CONTENTADD_EQ.get(sec_key), False)
        for key in kcontent:
            data[key] = kcontent[key]
        kcontent = Tools.s_dict_merging(CONTENTKEY_EQ.get(MAIN_SECTION), CONTENTKEY_EQ.get(sec_key), False)
        for key in kcontent:
            if key in data:
                data[kcontent[key]] = Tools.merge_string_list(data.get(kcontent[key]), data.pop(key))
        kcontent = Tools.s_dict_merging(CONTENTVAL_EQ.get(MAIN_SECTION), CONTENTVAL_EQ.get(sec_key), False)
        for key in kcontent:
            if key in data:
                if (data[key] == kcontent[key][0]) or (kcontent[key][0] == MAIN_SECTION):
                    if type(kcontent[key][1]) is str:
                        data[key] = kcontent[key][1]
                    else:
                        data[key] = kcontent[key][1](data[key], data)

        # set action key
        kcontent = ACTIONKEY_EQ.get(sec_key)
        if type(kcontent) is str:
            data[kcontent] = MAIN_SECTION
        elif kcontent:
            data[kcontent(data)] = MAIN_SECTION

        # main loop
        data = self.__structure_data(data, manifest, name)
        Tools.l(INFO, "processing data complete", "process_data", self)
        return data

    # ganaration method
    @GeneralException
    def __generate_classes(self, data):
        for manifest in sorted(data):
            if manifest not in SECTION_EQ:
                Tools.l(INFO, "Ignored unknown class %s" % (manifest), "generate_classes", self)
                continue
            Tools.l(INFO, "creating manifest %s" % (manifest), "generate_classes", self)
            for name in sorted(data[manifest]):
                if type(SECTION_EQ[manifest]) is dict:
                    ref = data[manifest][name].get(SECTION_EQ[manifest]["key"])
                    if not ref:
                        Tools.l(
                            ERR, "Reference key not found %s" % (SECTION_EQ[manifest]["key"]), "generate_classes", self
                        )
                        continue
                    state = SECTION_EQ[manifest].get(ref)
                    if not state:
                        Tools.l(ERR, "State not found ref %s, manifest %s" % (ref, manifest), "generate_classes", self)
                        continue
                else:
                    state = SECTION_EQ.get(manifest)
                    if not state:
                        Tools.l(ERR, "State not found manifest %s" % (manifest), "generate_classes", self)
                        continue
                if manifest == "packages":
                    data[manifest][name] = self.__filter.update_package(data[manifest][name], name, "latest")
                self.__output.setdefault(manifest, {})
                self.__output[manifest].setdefault(state, {})
                self.__curent_state = state
                self.__output[manifest][state][name] = self.__process_data(data[manifest][name], manifest, name)