コード例 #1
0
ファイル: gui_threads.py プロジェクト: Fxhnd/macleod
    def run(self):
        # We need to capture the print statements from the parser
        backup = sys.stdout
        sys.stdout = self.error

        # Create a place to read the text from
        buffer = tempfile.mkstemp(".macleod")
        with open(buffer[1], 'w') as f:
            f.write(self.text)

        try:
            self.ontology = Parser.parse_file(buffer[1],
                                              filemgt.read_config('cl', 'prefix'),
                                              os.path.abspath(filemgt.read_config('system', 'path')),
                                              self.resolve,
                                              self.path)

        except Exception as e:
            print(e)
            # If it's not a CL error we need to find the problem in Python
            if not isinstance(e, TypeError):
                # This prints to the Python console, not to the console in the window
                traceback.print_exc()
            self.ontology = None

        # return to the previous output
        sys.stdout = backup

        # leave no trace of the buffer
        os.close(buffer[0])
        os.remove(buffer[1])
コード例 #2
0
def main():
    deprec()
    #licence.print_terms()

    parser = argparse.ArgumentParser(
        description=
        'Utility function to convert Common Logic Interchange Format (.clif) files to the TPTP or LADR syntax.'
    )
    parser.add_argument('-f',
                        '--file',
                        type=str,
                        help='Clif file or folder to parse',
                        required=True)
    parser.add_argument(
        '-o',
        '--output',
        type=str,
        help='Output format (currently supported are tptp and ladr)',
        default=tptp_output)
    parser.add_argument('-n',
                        '--noresolve',
                        action="store_true",
                        help='Prevent from automatically resolving imports',
                        default=False)
    parser.add_argument('--loc',
                        type=str,
                        help='Path to directory containing ontology files',
                        default=default_dir)
    parser.add_argument(
        '--prefix',
        type=str,
        help='String to replace with basepath found in imports',
        default=default_prefix)
    args = parser.parse_args()

    path = os.path.normpath(os.path.join(args.loc, args.file))

    print(not (args.noresolve))

    if os.path.isfile(path):
        ontology = Parser.parse_file(args.file, args.prefix, args.loc,
                                     not (args.noresolve))
        convert_single_clif_file(ontology, args.output, not (args.noresolve),
                                 args.loc, args.prefix)
    elif os.path.isdir(path):
        convert_all_clif_files(path, args.output, not (args.noresolve),
                               args.loc, args.prefix)
    else:
        logging.getLogger(__name__).error(
            "Attempted to parse non-existent file or directory: " + path)
コード例 #3
0
    def resolve_imports(self, resolve=False):
        """
        Look over our list of imports and tokenize and parse any that haven't
        already been parsed
        """

        self.resolve = resolve

        logging.getLogger(__name__).debug("Resolving imports")

        # Cyclic imports are kind of painful in Python
        import macleod.parsing.parser as Parser

        for path in self.imports:

            logging.getLogger(__name__).debug("Working on import " + path)

            if self.imports[path] is None:

                if path in Ontology.imported:
                    print("Cyclic import found: {} imports {}".format(
                        self.name, path))
                    self.imports[path] = Ontology.imported[path]
                else:
                    sub, base = self.basepath
                    subbed_path = path.replace(self.basepath[0],
                                               self.basepath[1])
                    logging.getLogger(__name__).debug(
                        "Subbed path for import " + path)
                    # Need to immediately keep track of the ontology path in processing to not visit it again
                    # we later update the value with the actual created ontology object
                    Ontology.imported[path] = None
                    try:
                        logging.getLogger(__name__).info("Starting to parse " +
                                                         subbed_path)
                        new_ontology = Parser.parse_file(
                            subbed_path, sub, base, resolve)
                    except TypeError as e:
                        logging.getLogger(__name__).error("Error parsing + " +
                                                          subbed_path + ": " +
                                                          str(e))

                    new_ontology.basepath = self.basepath
                    self.imports[path] = new_ontology
                    # update the import information with the created ontology object
                    Ontology.imported[path] = new_ontology
コード例 #4
0
def convert_all_clif_files(folder,
                           output,
                           resolve,
                           loc=default_dir,
                           prefix=default_prefix):

    tempfolder = Filemgt.read_config('converters', 'tempfolder')
    ignores = [tempfolder]
    cl_ending = Filemgt.read_config('cl', 'ending')
    logging.getLogger(__name__).info("Traversing folder " + folder)

    for directory, subdirs, files in os.walk(folder):
        if any(ignore in directory for ignore in ignores):
            pass
        else:
            for single_file in files:
                if single_file.endswith(cl_ending):
                    file = os.path.join(directory, single_file)
                    logging.getLogger(__name__).info("Found CL file " + file)
                    ontology = Parser.parse_file(file, prefix, loc, resolve)
                    convert_single_clif_file(ontology, output, resolve, loc,
                                             prefix)
コード例 #5
0
def convert_file(file, args):

    ontology = Parser.parse_file(file, args.sub, args.base, args.resolve)

    if ontology is None:
        exit(-1)

    # producing OWL output
    if args.owl:
        onto = ontology.to_owl()
        print("\n-- Translation --\n")

        print(onto.tostring())

        # producing OWL file
        if args.output:
            filename = write_owl_file(onto, args.resolve)
            logging.getLogger(__name__).info("Produced OWL file " + filename)

        exit(0)

    # producing TPTP ouput
    if args.tptp:
        to_tptp(ontology, args.resolve, args.output)

    # producing LADR output
    if args.ladr:
        to_ladr(ontology, args.resolve, args.output)

    # producing LaTeX output
    if args.latex:
        to_latex(ontology, args.resolve, args.output, args.enum)

    # Just converting to Function-free Prenex-Conjunctive-Normalform
    if args.ffpcnf:
        print(ontology.to_ffpcnf())