Exemplo n.º 1
0
    def initConfig(self) -> None:
        try:
            configFile = cast(Path, cast(Mapping, self).get("configFile"))

            if configFile is None:
                configuration = Configuration(None)
            else:
                if not configFile.is_file():
                    exit(ExitStatus.EX_CONFIG, "Config file not found.")
                configuration = Configuration(configFile)

            options = cast(MutableMapping, self)

            if "overrides" in options:
                for _override in options["overrides"]:
                    raise NotImplementedError("Option overrides unimplemented")

            if "logFileName" in options:
                configuration.LogFilePath = Path(options["logFileName"])
            else:
                self.opt_log_file(str(configuration.LogFilePath))

            if "logFormat" in options:
                configuration.LogFormat = options["logFormat"]
            elif configuration.LogFormat is not None:
                self.opt_log_format(configuration.LogFormat)

            if "logLevel" in options:
                configuration.LogLevelName = options["logLevel"].name
            elif configuration.LogLevelName is not None:
                self.opt_log_level(configuration.LogLevelName)

            options["configuration"] = configuration
        except Exception as e:
            exit(ExitStatus.EX_CONFIG, str(e))
Exemplo n.º 2
0
    def initConfig(self) -> None:
        try:
            configFile = cast(Path, cast(Mapping, self).get("configFile"))

            if configFile is None:
                configuration = Configuration(None)
            else:
                if not configFile.is_file():
                    exit(ExitStatus.EX_CONFIG, "Config file not found.")
                configuration = Configuration(configFile)

            options = cast(Mapping, self)

            if "logFileName" in options:
                configuration.LogFilePath = Path(self["logFileName"])
            else:
                self.opt_log_file(str(configuration.LogFilePath))

            if "logFormat" in options:
                configuration.LogFormat = self["logFormat"]
            elif configuration.LogFormat is not None:
                self.opt_log_format(configuration.LogFormat)

            if "logLevel" in options:
                configuration.LogLevelName = self["logLevel"].name
            elif configuration.LogLevelName is not None:
                self.opt_log_level(configuration.LogLevelName)

            cast(MutableMapping, self)["configuration"] = configuration
        except Exception as e:
            exit(ExitStatus.EX_CONFIG, str(e))
Exemplo n.º 3
0
    def options(argv: Sequence[str]) -> ServerOptions:
        """
        Parse command line options.
        """
        options = ServerOptions()

        try:
            options.parseOptions(argv[1:])
        except UsageError as e:
            exit(ExitStatus.EX_USAGE, f"Error: {e}\n\n{options}")

        return options
Exemplo n.º 4
0
    def options(argv: Sequence[str]) -> IMSOptions:
        """
        Parse command line options.
        """
        options = IMSOptions()

        try:
            options.parseOptions(argv[1:])
        except UsageError as e:
            exit(ExitStatus.EX_USAGE, f"Error: {e}\n\n{options}")

        return options
Exemplo n.º 5
0
    def opt_log_file(self, fileName: str) -> None:
        """
        Log to file. ("-" for stdout, "+" for stderr; default: "-")
        """
        self["logFileName"] = fileName

        if fileName == "-":
            self["logFile"] = stdout
            return

        if fileName == "+":
            self["logFile"] = stderr
            return

        try:
            self["logFile"] = openFile(fileName, "a")
        except EnvironmentError as e:
            exit(ExitStatus.EX_IOERR,
                 f"Unable to open log file {fileName!r}: {e}")
Exemplo n.º 6
0
    def initConfig(self) -> None:
        try:
            configFile = cast(
                Optional[Path], cast(Mapping, self).get("configFile")
            )

            if configFile and not configFile.is_file():
                self.log.info("Config file not found.")
                configFile = None

            configuration = Configuration.fromConfigFile(configFile)

            options = cast(MutableMapping, self)

            if "overrides" in options:
                for _override in options["overrides"]:
                    raise NotImplementedError("Option overrides unimplemented")

            if "logFileName" in options:
                configuration = configuration.replace(
                    LogFilePath=Path(options["logFileName"])
                )
            else:
                self.opt_log_file(str(configuration.LogFilePath))

            if "logFormat" in options:
                configuration = configuration.replace(
                    LogFormat=options["logFormat"]
                )
            elif configuration.LogFormat is not None:
                self.opt_log_format(configuration.LogFormat)

            if "logLevel" in options:
                configuration = configuration.replace(
                    LogLevelName=options["logLevel"].name
                )
            elif configuration.LogLevelName is not None:
                self.opt_log_level(configuration.LogLevelName)

            options["configuration"] = configuration
        except Exception as e:
            exit(ExitStatus.EX_CONFIG, str(e))
Exemplo n.º 7
0
    def opt_log_file(self, fileName: str) -> None:
        """
        Log to file. ("-" for stdout, "+" for stderr; default: "-")
        """
        self["logFileName"] = fileName

        if fileName == "-":
            self["logFile"] = stdout
            return

        if fileName == "+":
            self["logFile"] = stderr
            return

        try:
            self["logFile"] = openFile(fileName, "a")
        except EnvironmentError as e:
            exit(
                ExitStatus.EX_IOERR,
                f"Unable to open log file {fileName!r}: {e}"
            )
Exemplo n.º 8
0
    def initConfig(self) -> None:
        try:
            configFile = cast(Optional[Path],
                              cast(Mapping, self).get("configFile"))

            if configFile and not configFile.is_file():
                self.log.info("Config file not found.")
                configFile = None

            configuration = Configuration.fromConfigFile(configFile)

            options = cast(MutableMapping, self)

            if "overrides" in options:
                for _override in options["overrides"]:
                    raise NotImplementedError("Option overrides unimplemented")

            if "logFileName" in options:
                configuration = configuration.replace(
                    logFilePath=Path(options["logFileName"]))

            self.opt_log_file(str(configuration.logFilePath))

            if "logFormat" in options:
                configuration = configuration.replace(
                    logFormat=options["logFormat"])
            elif configuration.logFormat is not None:
                self.opt_log_format(configuration.logFormat.name)

            if "logLevel" in options:
                configuration = configuration.replace(
                    logLevelName=options["logLevel"].name)
            elif configuration.logLevelName is not None:
                self.opt_log_level(configuration.logLevelName)

            options["configuration"] = configuration

        except Exception as e:
            exit(ExitStatus.EX_CONFIG, str(e))
Exemplo n.º 9
0
 def opt_version(self) -> None:
     """
     Print version and exit.
     """
     exit(ExitStatus.EX_OK, f"{version}")
Exemplo n.º 10
0
 def openNamedFile() -> IO:
     try:
         file = open(fileName, mode)
     except EnvironmentError as e:
         exit(ExitStatus.EX_IOERR, f"Unable to open file {fileName!r}: {e}")
     return file
Exemplo n.º 11
0
 def opt_version(self) -> None:
     """
     Print version and exit.
     """
     exit(ExitStatus.EX_OK, f"{version}")