Example #1
0
def load_component(file):
    """Load a component. This handles various special cases,
    including old-style C++ recognizers (soon to be gotten rid of),
    python expressions ("=package.ObjectName(arg1,arg2)"),
    and simple pickled Python objects (default)."""
    if file[0]=="=":
        return pyconstruct(file[1:])
    elif file[0]=="@":
        file = file[1:]
    with open(file,"r") as stream:
        # FIXME -- get rid of this eventually
        start = stream.read(128)
    if start.startswith("<object>\nlinerec\n"):
        # FIXME -- get rid of this eventually
        warnings.warn("loading old-style linerec: %s"%file)
        result = RecognizeLine()
        import ocropus
        result.comp = ocropus.load_IRecognizeLine(file)
        return result
    if start.startswith("<object>"):
        # FIXME -- get rid of this eventually
        warnings.warn("loading old-style cmodel: %s"%file)
        import ocroold
        result = ocroold.Model()
        import ocropus
        result.comp = ocropus.load_IModel(file)
        return result
    return load_object(file)
Example #2
0
def load_component(file):
    """Load a component from disk.  If file starts with "@", it is
    taken as a Python expression and evaluated, but this can be overridden
    by starting file with "=".  Otherwise, the contents of the file are
    examined.  If it looks like a native component, it is loaded as a line
    recognizers if it can be identified as such, otherwise it is loaded
    with load_Imodel as a model.  Anything else is loaded with Python's
    pickle.load."""

    if file[0]=="=":
        return pyconstruct(file[1:])
    elif file[0]=="@":
        file = file[1:]
    with open(file,"r") as stream:
        # FIXME -- get rid of this eventually
        start = stream.read(128)
    if start.startswith("<object>\nlinerec\n"):
        # FIXME -- get rid of this eventually
        warnings.warn("loading old-style linerec: %s"%file)
        result = RecognizeLine()
        import ocropus
        result.comp = ocropus.load_IRecognizeLine(file)
        return result
    if start.startswith("<object>"):
        # FIXME -- get rid of this eventually
        warnings.warn("loading old-style cmodel: %s"%file)
        import ocroold
        result = ocroold.Model()
        import ocropus
        result.comp = ocropus.load_IModel(file)
        return result
    with open(file,"rb") as stream:
        return pickle.load(stream)