Beispiel #1
0
    def load_config(self, namespace=None, rtorrent_rc=None):
        """ Load file given in "rtorrent_rc".
        """
        def cfgkey(key):
            "Sanitize rtorrent config keys"
            return key.replace('.', '_')

        if namespace is None:
            namespace = config

        # Only load when needed (also prevents multiple loading)
        if not all(getattr(namespace, key, False) for key in self.RTORRENT_RC_KEYS):
            # Get and check config file name
            if not rtorrent_rc:
                rtorrent_rc = getattr(config, "rtorrent_rc", None)
            if not rtorrent_rc:
                raise error.UserError("No 'rtorrent_rc' path defined in configuration!")
            if not os.path.isfile(rtorrent_rc):
                raise error.UserError("Config file %r doesn't exist!" % (rtorrent_rc,))

            # Parse the file
            self.LOG.debug("Loading rtorrent config from %r" % (rtorrent_rc,))
            with closing(open(rtorrent_rc)) as handle:
                for line in handle.readlines():
                    # Skip comments and empty lines
                    line = line.strip()
                    if not line or line.startswith("#"):
                        continue

                    # Be lenient about errors, after all it's not our own config file
                    try:
                        key, val = line.split("=", 1)
                    except ValueError:
                        self.LOG.warning("Ignored invalid line %r in %r!" % (line, rtorrent_rc))
                        continue
                    key, val = key.strip(), val.strip()
                    key = self.RTORRENT_RC_ALIASES.get(key, key)

                    # Copy values we're interested in
                    if key in self.RTORRENT_RC_THROTTLE_KEYS:
                        val = val.split(',')[0].strip()
                        self.LOG.debug("rtorrent.rc: added throttle %r" % (val,))
                        namespace.throttle_names.add(val)
                    elif key in self.RTORRENT_RC_KEYS and not getattr(namespace, cfgkey(key), None):
                        self.LOG.debug("rtorrent.rc: %s = %s" % (key, val))
                        setattr(namespace, cfgkey(key), val)

        # Validate fields
        for key in self.RTORRENT_RC_KEYS:
            key = cfgkey(key)
            setattr(namespace, key, load_config.validate(key, getattr(namespace, key, None)))
        if config.scgi_local and config.scgi_local.startswith("/"):
            config.scgi_local = "scgi://" + config.scgi_local
        if config.scgi_port and not config.scgi_port.startswith("scgi://"):
            config.scgi_port = "scgi://" + config.scgi_port

        # Prefer UNIX domain sockets over TCP sockets
        config.scgi_url = config.scgi_local or config.scgi_port
Beispiel #2
0
    def get_options(self):
        """ Get program options.
        """
        super(ScriptBaseWithConfig, self).get_options()
        load_config.ConfigLoader(self.options.config_dir).load(self.OPTIONAL_CFG_FILES + self.options.config_file)
        if self.options.debug:
            config.debug = True

        for key_val in self.options.defines:
            try:
                key, val = key_val.split('=', 1)
            except ValueError, exc:
                raise error.UserError("Bad config override %r (%s)" % (key_val, exc))
            else:
                setattr(config, key, load_config.validate(key, val))
Beispiel #3
0
    def get_options(self):
        """ Get program options.
        """
        super(ScriptBaseWithConfig, self).get_options()
        load_config.ConfigLoader(
            self.options.config_dir).load(self.OPTIONAL_CFG_FILES +
                                          self.options.config_file)
        if self.options.debug:
            config.debug = True

        for key_val in self.options.defines:
            try:
                key, val = key_val.split('=', 1)
            except ValueError, exc:
                raise error.UserError("Bad config override %r (%s)" %
                                      (key_val, exc))
            else:
                setattr(config, key, load_config.validate(key, val))
Beispiel #4
0
    def get_options(self):
        """ Get program options.
        """
        super(ScriptBaseWithConfig, self).get_options()

        self.config_dir = os.path.abspath(os.path.expanduser(self.options.config_dir
            or os.environ.get('PYRO_CONFIG_DIR', None)
            or self.CONFIG_DIR_DEFAULT))
        load_config.ConfigLoader(self.config_dir).load(self.OPTIONAL_CFG_FILES + self.options.config_file)
        if self.options.debug:
            config.debug = True

        for key_val in self.options.defines:
            try:
                key, val = key_val.split('=', 1)
            except ValueError, exc:
                raise error.UserError("Bad config override %r (%s)" % (key_val, exc))
            else:
                setattr(config, key, load_config.validate(key, val))
Beispiel #5
0
    def _open_proxy(self):
        """ Open proxy, if enabled and not yet open.
        """
        ##LOG.warn(repr(self.global_config))
        cfg = getattr(self, "config", self.global_config)

        if cfg and cfg["enabled"] and self.proxy is None:
            try:
                # Load config from disc
                load_config.ConfigLoader(cfg["config_dir"]).load()

                # Set overrides
                for key, val in cfg["overrides"].items():
                    setattr(pyrocfg, key, load_config.validate(key, val))

                # Open the connection
                self.proxy = pyrocfg.engine.open()
                LOG.info(pyrocfg.engine)  # where are we connected?
            except error.LoggableError, exc:
                raise plugin.PluginError(str(exc))
Beispiel #6
0
    def _open_proxy(self):
        """ Open proxy, if enabled and not yet open.
        """
        ##LOG.warn(repr(self.global_config))
        cfg = getattr(self, "config", self.global_config)
        
        if cfg and cfg["enabled"] and self.proxy is None:
            try:
                # Load config from disc
                load_config.ConfigLoader(cfg["config_dir"]).load()

                # Set overrides
                for key, val in cfg["overrides"].items():
                    setattr(pyrocfg, key, load_config.validate(key, val))

                # Open the connection
                self.proxy = pyrocfg.engine.open()
                LOG.info(pyrocfg.engine) # where are we connected?
            except error.LoggableError, exc:
                raise plugin.PluginError(str(exc))
Beispiel #7
0
    def load_config(self, namespace=None, rtorrent_rc=None):
        """ Load file given in "rtorrent_rc".
        """
        def cfgkey(key):
            "Sanitize rtorrent config keys"
            return key.replace('.', '_')

        if namespace is None:
            namespace = config

        # Only load when needed (also prevents multiple loading)
        if not all(
                getattr(namespace, key, False)
                for key in self.RTORRENT_RC_KEYS):
            # Get and check config file name
            if not rtorrent_rc:
                rtorrent_rc = getattr(config, "rtorrent_rc", None)
            if not rtorrent_rc:
                raise error.UserError(
                    "No 'rtorrent_rc' path defined in configuration!")
            if not os.path.isfile(rtorrent_rc):
                raise error.UserError("Config file %r doesn't exist!" %
                                      (rtorrent_rc, ))

            # Parse the file
            self.LOG.debug("Loading rtorrent config from %r" % (rtorrent_rc, ))
            with closing(open(rtorrent_rc)) as handle:
                for line in handle.readlines():
                    # Skip comments and empty lines
                    line = line.strip()
                    if not line or line.startswith("#"):
                        continue

                    # Be lenient about errors, after all it's not our own config file
                    try:
                        key, val = line.split("=", 1)
                    except ValueError:
                        self.LOG.warning("Ignored invalid line %r in %r!" %
                                         (line, rtorrent_rc))
                        continue
                    key, val = key.strip(), val.strip()
                    key = self.RTORRENT_RC_ALIASES.get(key, key)

                    # Copy values we're interested in
                    if key in self.RTORRENT_RC_THROTTLE_KEYS:
                        val = val.split(',')[0].strip()
                        self.LOG.debug("rtorrent.rc: added throttle %r" %
                                       (val, ))
                        namespace.throttle_names.add(val)
                    elif key in self.RTORRENT_RC_KEYS and not getattr(
                            namespace, cfgkey(key), None):
                        self.LOG.debug("rtorrent.rc: %s = %s" % (key, val))
                        setattr(namespace, cfgkey(key), val)

        # Validate fields
        for key in self.RTORRENT_RC_KEYS:
            key = cfgkey(key)
            setattr(namespace, key,
                    load_config.validate(key, getattr(namespace, key, None)))
        if config.scgi_local and config.scgi_local.startswith("/"):
            config.scgi_local = "scgi://" + config.scgi_local
        if config.scgi_port and not config.scgi_port.startswith("scgi://"):
            config.scgi_port = "scgi://" + config.scgi_port

        # Prefer UNIX domain sockets over TCP sockets
        config.scgi_url = config.scgi_local or config.scgi_port