예제 #1
0
    def do_select(self, line):
        """
        Select a data source
        :param line: <String> Source name
        :return: None
        :Author: Zhiming Liu
        """
        # Available data sources
        options = "-csv", "-db"
        args = list(arg.lower() for arg in str(line).split())

        try:
            # Check if the input data source is available in this program or not
            if args[0] not in options:
                raise ValueError("The data resource is not available.")
            else:
                # Code for initialise CSV data source
                if args[0] == "-csv":
                    try:
                        if len(args) == 1:
                            self._std.select_source(args[0][1:],
                                                    "staffinfo.csv")
                            View.warning(
                                "No CSV file path specified. A default file \"staffinfo.csv\" will be used."
                            )
                        elif len(args) == 2:
                            self._std.select_source(args[0][1:], args[1])
                        elif len(args) == 3:
                            if args[1] == "-a":
                                self._std.select_source(
                                    args[0][1:], args[2], True)
                    except (CSVError, OSError) as e:
                        View.error(e)
                    except Exception as e:
                        View.error(e)
                    else:
                        View.success("Data source CSV is selected.")

                # Code for initialise database source
                elif args[0] == "-db":
                    try:
                        self._std.select_source(args[0][1:])
                    except (ConnectionError, TypeError) as e:
                        View.error(e)
                    except Exception as e:
                        View.error(e)
                    else:
                        View.success("Data source Database is selected.")

                # Code for initialise XXXX data source
                else:
                    pass
        # Catch and display error message
        except ValueError as e:
            View.error(str(e) + "\n")
            View.help_select()
        except Exception as e:
            View.error(e)
예제 #2
0
    def select_csv(self, cmd):
        try:
            if not cmd.is_valid_command:
                View.error("Invalid command.")
                View.help_select()

            elif cmd.file_name is None:
                self._std.select_source(cmd.resource_type,
                                        cmd.default_csv_file)
                View.warning("No CSV file path specified. "
                             "A default file \"{0}\" "
                             "will be used.".format(cmd.default_csv_file))
            else:
                self._std.select_source(cmd.resource_type, cmd.file_name,
                                        cmd.create_file)
        except Exception as e:
            View.error(e)
        else:
            View.success("Data source CSV is selected.")
예제 #3
0
 def help_select():
     View.help_select()
예제 #4
0
 def help_select():
     View.display("Select a source of data for reading "
                  "and saving staff information.\n")
     View.help_select()