Example #1
0
def mogu_import(args):
    results = []    # Holds the final result of all consumption operations
    redis_objects = [] #Holds the objects that can be written to Redis
    write = not args.testing # If testing, don't actually write to Redis

    for path in args.command[1:]:
        if path.endswith(".mogu"): # If the path points to a specific file
            results.extend(FileImporter.import_file(path,args.v))
        else:   # The path is a directory
            results.extend(PathImporter.import_path(path,args.v))

    converter = PythonObjectConverter.PythonObjectConverter()
    for result in results:
        redis_objects.extend(converter.convert(result))

    # SANITY CHECKS #

    # First, make sure that all symbols referenced are defined.
    for registry in [
            SymbolRegistry.widgetRegistry,
            SymbolRegistry.templateRegistry,
            SymbolRegistry.dataRegistry,
            SymbolRegistry.validatorRegistry,
            SymbolRegistry.policyRegistry
            ]:
        if not registry: # Returns false if a symbol in the registry is not defined
            sys.stderr.write(display_undefined_symbols(registry))
            sys.stderr.write("\n== REFUSING TO CONTINUE ==\n")
            sys.exit()

        if registry.nonreferenced(): # Warns the user if something was defined but never used
            i = None
            sys.stderr.write("\n== WARNING: %s contains the following symbols that are defined but never referenced == \n" %
                    registry.label)
            for symbol in registry.nonreferenced():
                sys.stderr.write("\t- %s\n" % symbol)
            # Give the user a chance to halt the import and fix the problem
            if not args.yes:
                i = raw_input("Continue anyway? [y to continue, anything else to cancel]: ")
            if i != 'y' and not args.yes:
                sys.stderr.write("Exiting...")
                sys.exit()

    # Sanity checks complete. Now the process of actually writing
    # to Redis 

    # TODO Don't forget to make RedisWriter deal with purging/merging
    if write: 
        writer = RedisWriter.RedisWriter(args) #TODO RedisWriter should read dbconfig.conf instead
        if args.v:
            sys.stderr.write("Writing imported files to database!\n")
        writer.write(redis_objects)
Example #2
0
    def __init__(self, paths, args=None):
        self.objectConverter = PythonObjectConverter.PythonObjectConverter()
        self.redisWriter = RedisWriter.RedisWriter(args)
        self.results = []
        self.redisObjects = []
        if args:
            if hasattr(args,"v"):
                pyboro.Lexer.VERBAL = args.v
                SharedData.VERBAL = args.v
            elif hasattr(args,"verbal"):
                pyboro.Lexer.VERBAL = args.verbal
                SharedData.VERBAL = args.verbal

        # Force the 'paths' into a list format.
        if isinstance(paths,tuple):
            paths = list(paths)
        elif isinstance(paths,str):
            paths = [paths,]
        elif not isinstance(paths,list):
            raise TypeError("Paths must be in list, tuple, or string format")
        num_paths = len(paths)
        # Lex the files in each of the paths. 
        for i,path in enumerate(paths):
            self.results.extend(PathImporter.import_path(path))

        # Determine if any references have unmet dependencies
        for registry in SymbolRegistry.registries:
            if not registry:
                raise ScriptImporter.RegistryError(registry)

        # Raise a warning if there are unreferenced symbols:
        for registry in SymbolRegistry.registries:
            if registry.nonreferenced():
                for symbol in registry.nonreferenced():
                    sys.stderr.write(
                            "WARNING: %s is defined but never referenced\n" % \
                            (symbol))