예제 #1
0
 def Run(self, force):
     """This public function is the main Run command which runs this module."""
     if force == True:
         self.truncateSchema()
     else:
         var = input(Messages().Get(128))
         if var == "y" or var == "Y":
             self.truncateSchema()
         else:
             self.helpers.Info(Messages().Get(129))
예제 #2
0
    def loadConfig(self):
        """This function loads the config file or errors if it is not available"""

        # passed this point a valid config file should be available
        try:
            configYaml = yaml.load(open(self.configFile))
            Helpers(configYaml).Info(Messages().Get(120))
            return configYaml
        except IOError:
            Helpers(None).Error(Messages().Get(206))
예제 #3
0
 def Run(self, force):
     """This public function is the main Run command which runs this module."""
     if force == True:
         self.emptyWeaviate("things")
         self.emptyWeaviate("actions")
     else:
         var = input(Messages().Get(124))
         if var == "y" or var == "Y":
             self.emptyWeaviate("things")
             self.emptyWeaviate("actions")
         else:
             self.helpers.Info(Messages().Get(123))
예제 #4
0
    def setConfig(self, options):
        """This function sets the config file"""

        configVars = {"url": ""}

        # Validate and set URL
        if validators.url(options.init_url) != True:
            Helpers(None).Error("No valid URL is set")

        # start creating the config file
        configVars["url"] = options.init_url

        # write to file
        try:
            with open(self.configFile, 'w') as configFile:
                yaml.dump(configVars, configFile, default_flow_style=False)
            Helpers(None).Info(Messages().Get(119))
        except IOError:
            Helpers(None).Error(Messages().Get(205) + self.configFile)
예제 #5
0
    def emptyWeaviate(self, conceptType):
        
        counter = 0
        # Get all concepts
        _, result = self.weaviate.Get("/" + conceptType)

        # check if there are concepts
        if "totalResults" in result:

            # loop and delete
            for concept in result[conceptType]:

                # delete the concept
                statusCode = self.weaviate.Delete("/" + conceptType + "/" + concept["id"])
                # validate if valid
                if statusCode != 204:
                    self.helpers.Error(Messages().Get(207))
                else:
                    self.helpers.Info(Messages().Get(125) + concept["id"])

            # restart the function
            self.emptyWeaviate(conceptType)
예제 #6
0
def main():
    """This class reads the command line arguments and loads the correct modules."""

    # Get parsed arguments
    args = argparse.ArgumentParser(description=Messages().Get(112))

    # Get the arguments for sinit
    args.add_argument('--init', help=Messages().Get(100), action="store_true")
    args.add_argument('--init-url', default="http://localhost", help=Messages().Get(101))

    # Get the arguments for schema import
    args.add_argument('--schema-import', help=Messages().Get(104), action="store_true")
    args.add_argument('--schema-import-ontology', default="./ontology.json", help=Messages().Get(104))
    args.add_argument('--schema-import-overwrite', help=Messages().Get(107), action="store_true")

    # Get the arguments for schema export
    args.add_argument('--schema-export', help=Messages().Get(108), action="store_true")
    args.add_argument('--schema-export-things', default="./things.json", help=Messages().Get(109))
    args.add_argument('--schema-export-actions', default="/actions.json", help=Messages().Get(110))

    # truncate the schema
    args.add_argument('--schema-truncate', help=Messages().Get(126), action="store_true")
    args.add_argument('--schema-truncate-force', help=Messages().Get(127), action="store_true")

    # Empty a weaviate
    args.add_argument('--empty', help=Messages().Get(121), action="store_true")
    args.add_argument('--empty-force', help=Messages().Get(122), action="store_true")

    options = args.parse_args()

    # Check init and validate if set
    if options.init is True:
        Init().setConfig(options)

    # Set the config
    config = Init().loadConfig()

    # Ping Weaviate to validate the connection
    Weaviate(config).Ping()

    # Check which items to load
    if options.schema_import is True:
        from modules.SchemaImport import SchemaImport
        SchemaImport(config).Run(options.schema_import_ontology, options.schema_import_overwrite)
    elif options.schema_export is True:
        from modules.SchemaExport import SchemaExport
        SchemaExport(config).Run()
    elif options.empty is True:
        from modules.Empty import Empty
        Empty(config).Run(options.empty_force)
    elif options.schema_truncate is True:
        from modules.Truncate import Truncate
        Truncate(config).Run(options.schema_truncate_force)
    else:
        exit(0)
예제 #7
0
def main():
    """This class reads the command line arguments and loads the correct modules."""

    # Get parsed arguments
    args = argparse.ArgumentParser(description=Messages().Get(112))

    # Get the arguments for sinit
    args.add_argument('--init', help=Messages().Get(100), action="store_true")
    args.add_argument('--init-url',
                      default="http://localhost",
                      help=Messages().Get(101))
    args.add_argument('--init-key', default="UNSET", help=Messages().Get(102))
    args.add_argument('--init-token',
                      default="UNSET",
                      help=Messages().Get(103))

    # Get the arguments for schema import
    args.add_argument('--schema-import',
                      help=Messages().Get(104),
                      action="store_true")
    args.add_argument('--schema-import-things',
                      default="./things.json",
                      help=Messages().Get(105))
    args.add_argument('--schema-import-actions',
                      default="/actions.json",
                      help=Messages().Get(106))
    args.add_argument('--schema-import-delete',
                      help=Messages().Get(107),
                      action="store_true")

    # Get the arguments for schema export
    args.add_argument('--schema-export',
                      help=Messages().Get(108),
                      action="store_true")
    args.add_argument('--schema-export-things',
                      default="./things.json",
                      help=Messages().Get(109))
    args.add_argument('--schema-export-actions',
                      default="/actions.json",
                      help=Messages().Get(110))

    # Get the arguments for bulk import
    args.add_argument('--data-import',
                      default="UNSET",
                      help=Messages().Get(111))

    options = args.parse_args()

    # Check init and validate if set
    if options.init is True:
        Init().setConfig(options)

    # Set the config
    config = Init().loadConfig()

    # Ping Weaviate to validate the connection
    Weaviate(config).Ping()

    # Check which items to load
    if options.schema_import is True:
        from modules.SchemaImport import SchemaImport
        SchemaImport(config).Run(options.schema_import_things, \
                                  options.schema_import_actions, \
                                  options.schema_import_delete)
    elif options.schema_export is True:
        from modules.SchemaExport import SchemaExport
        SchemaExport(config).Run()
    elif options.data_import is True:
        from modules.DataImport import DataImport
        DataImport(config).Run()
    else:
        exit(0)
예제 #8
0
    def Run(self, thingsFile, actionsFile, deleteIfFound):
        """This functions runs the import module."""

        # start the import
        self.helpers.Info(Messages().Get(113))

        # check if things files is url
        if validators.url(thingsFile) is True:
            thingsFile = self.downloadSchemaFiles('./things.json', thingsFile)

        # open the thingsfile
        try:
            with open(thingsFile, 'r') as file:
                things = json.load(file)
        except IOError:
            self.helpers.Error(Messages().Get(201) + thingsFile)

        # check actions files
        if validators.url(actionsFile) is True:
            actionsFile = self.downloadSchemaFiles('./things.json',
                                                   actionsFile)

        # open the actionsfile
        try:
            with open(actionsFile, 'r') as file:
                actions = json.load(file)
        except IOError:
            self.helpers.Error(Messages().Get(202) + actionsFile)

        # Validate if delete function would work
        if deleteIfFound is True:
            self.helpers.Info(Messages().Get(114))

            # check if there is data
            if self.checkIfThereIsData("things") is True \
            or self.checkIfThereIsData("actions") is True:
                self.helpers.Error(Messages().Get(203))

        # Render and create things
        self.helpers.Info(Messages().Get(115) + "things")
        self.helpers.CreateConceptClasses("things", things["classes"],
                                          deleteIfFound)

        # Render and create actions
        self.helpers.Info(Messages().Get(115) + "actions")
        self.helpers.CreateConceptClasses("actions", actions["classes"],
                                          deleteIfFound)

        # Add properties to things (needs to run after CreateConceptClasses()!)
        self.helpers.Info(Messages().Get(116) + "things")
        self.helpers.AddPropsToConceptClasses("things", things["classes"])

        # Add properties to things (needs to run after CreateConceptClasses()!)
        self.helpers.Info(Messages().Get(116) + "actions")
        self.helpers.AddPropsToConceptClasses("actions", actions["classes"])

        # Validate Things & Actions
        self.helpers.Info(Messages().Get(117))
        if self.helpers.ValidateConceptClasses(things["classes"],
                                               actions["classes"]) is True:
            self.helpers.Info(Messages().Get(118))
            exit(0)
        else:
            self.helpers.Error(Messages().Get(204))
예제 #9
0
    def Run(self, ontologyFile, deleteIfFound):
        """This functions runs the import module."""

        # start the import
        self.helpers.Info(Messages().Get(113))

        # Check if the schema is empty
        if deleteIfFound == False:
            schemaThingsCount, schemaActionsCount = self.helpers.SchemaCount()
            if schemaThingsCount != 0 or schemaActionsCount != 0:
                self.helpers.Error(Messages().Get(208))

        # check if things files is url
        if validators.url(ontologyFile) is True:
            ontologyFile = self.downloadSchemaFiles('./ontology.json',
                                                    ontologyFile)

        # open the thingsfile
        try:
            with open(ontologyFile, 'r') as file:
                ontology = json.load(file)
        except IOError:
            self.helpers.Error(Messages().Get(201) + ontologyFile)

        # Set things and actions from ontology file
        if "actions" not in ontology:
            self.helpers.Error(Messages().Get(209) + "actions")
        elif "things" not in ontology:
            self.helpers.Error(Messages().Get(209) + "things")

        actions = ontology["actions"]
        things = ontology["things"]

        # Validate if delete function would work
        if deleteIfFound is True:
            self.helpers.Info(Messages().Get(114))

            # check if there is data
            if self.checkIfThereIsData("things") is True \
            or self.checkIfThereIsData("actions") is True:
                self.helpers.Error(Messages().Get(203))

        # Render and create things
        self.helpers.Info(Messages().Get(115) + "things")
        self.helpers.CreateConceptClasses("things", things["classes"],
                                          deleteIfFound)

        # Render and create actions
        self.helpers.Info(Messages().Get(115) + "actions")
        self.helpers.CreateConceptClasses("actions", actions["classes"],
                                          deleteIfFound)

        # Add properties to things (needs to run after CreateConceptClasses()!)
        self.helpers.Info(Messages().Get(116) + "things")
        self.helpers.AddPropsToConceptClasses("things", things["classes"],
                                              deleteIfFound)

        # Add properties to actions (needs to run after CreateConceptClasses()!)
        self.helpers.Info(Messages().Get(116) + "actions")
        self.helpers.AddPropsToConceptClasses("actions", actions["classes"],
                                              deleteIfFound)

        # Validate Things & Actions
        self.helpers.Info(Messages().Get(117))
        if self.helpers.ValidateConceptClasses(things["classes"],
                                               actions["classes"]) is True:
            self.helpers.Info(Messages().Get(118))
            exit(0)
        else:
            self.helpers.Error(Messages().Get(204))