def parse(self):
        """
        Based on the syntax of the file, parse the contents of the file into a python dictionary.
        :return: dictionary parsed from the file contents
        :raises TranslateException: if an error occurs
        """
        _method_name = 'parse'

        self.logger.entering(class_name=self._class_name, method_name=_method_name)
        # throws IllegalArgument if not a valid existing file
        model_file = JFileUtils.validateFileName(self.file_name)
        # yaml is the default. For now, if the file extension is not known, then parse the contents as yaml
        if JFileUtils.isJsonFile(model_file):
            result_dict = self._parse_json()
        else:
            result_dict = self._parse_yaml()

        # called method already logged result. don't log it again
        self.logger.exiting(class_name=self._class_name, method_name=_method_name)
        return result_dict
Ejemplo n.º 2
0
def main():
    """
    The main entry point for the discoverDomain tool.
    :param args: the command-line arguments
    """
    _method_name = 'main'

    _logger.entering(class_name=_class_name, method_name=_method_name)
    for index, arg in enumerate(sys.argv):
        _logger.finer('sys.argv[{0}] = {1}', str(index), str(arg), class_name=_class_name, method_name=_method_name)

    _outputdir = None

    try:
        model_context = __process_args(sys.argv)
        _outputdir = model_context.get_compare_model_output_dir()
        model1 = model_context.get_trailing_argument(0)
        model2 = model_context.get_trailing_argument(1)

        for f in [model1, model2]:
            if not os.path.exists(f):
                raise CLAException("Model %s does not exists" % f)
            if os.path.isdir(f):
                raise CLAException("Model %s is a directory" % f)

        model1_file = JFile(model1)
        model2_file = JFile(model2)

        if not (FileUtils.isYamlFile(model1_file) or FileUtils.isJsonFile(model1_file)):
            raise CLAException("Model extension must be either yaml or json")

        if not (FileUtils.isYamlFile(model1_file) and FileUtils.isYamlFile(model2_file)
                or FileUtils.isJsonFile(model1_file) and FileUtils.isJsonFile(model2_file)):
            ext = os.path.splitext(model1)[1]
            raise CLAException("Model %s is not a %s file " % (model2, ext))

        obj = ModelFileDiffer(model1, model2, model_context, _outputdir)
        rc = obj.compare()
        if rc == VALIDATION_FAIL:
            System.exit(2)

        if _outputdir:
            fos = None
            writer = None
            file_name = None
            if len(compare_msgs) > 0:
                try:
                    file_name = _outputdir + '/compare_model_stdout'
                    fos = JFileOutputStream(file_name, False)
                    writer = JPrintWriter(fos, True)
                    writer.println(BLANK_LINE)
                    writer.println(BLANK_LINE)
                    index = 1
                    for line in compare_msgs:
                        msg_key = line[0]
                        msg_value = line[1]
                        writer.println("%s. %s" % (index, format_message(msg_key,msg_value.replace(PATH_TOKEN, "-->"))))
                        index = index + 1
                        writer.println(BLANK_LINE)
                    fos.close()
                    writer.close()
                except JIOException, ioe:
                    if fos:
                        fos.close()
                    if writer:
                        writer.close()
                    _logger.severe('WLSDPLY-05708', file_name, ioe.getLocalizedMessage(),
                                   error=ioe, class_name=_class_name, method_name=_method_name)
        else: