Exemplo n.º 1
0
    def configure(self, argv):
        """Takes an argv list, and gives it straight to optparser.parse_args.

        The argv list should not include the executable name.

        """

        self.__names = []  # keep track of what configuration we configure

        # Parse argv.
        # ===========

        opts, args = optparser.parse_args(argv)

        # Orient ourselves.
        # =================

        self.root = root = find_root(args)
        os.chdir(root)

        self.dotaspen = dotaspen = join(self.root, ".aspen")
        if isdir(dotaspen):
            if sys.path[0] != dotaspen:
                sys.path.insert(0, dotaspen)

        self.__names.extend(["root", "dotaspen"])

        # Set some attributes.
        # ====================

        self.conf = load_conf(expanduser, dotaspen)
        self.template_loader = Loader(dotaspen)
        self.__names.extend(["conf", "template_loader"])

        init_mimetypes(mimetypes, dotaspen)
        self.default_mimetype = load_default_mimetype(self.conf)
        self.default_filenames = load_default_filenames(self.conf)
        self.json_content_type = load_json_content_type(self.conf)
        self.show_tracebacks = self.conf.aspen.no("show_tracebacks")
        self.__names.extend(["default_mimetype", "default_filenames", "json_content_type", "show_tracebacks"])

        self.hooks = load_hooks(expanduser, dotaspen)
        self.__names.append("hooks")

        self.engine = load_engine(opts, self)
        self.changes_kill = self.conf["aspen.cli"].no("changes_kill")
        self.__names.extend(["engine", "changes_kill"])

        self.address, self.sockfam = load_address_sockfam(opts, self.conf)
        self.port = load_port(self.address, self.sockfam)
        self.sock = None
        self.__names.extend(["address", "sockfam", "port", "sock"])

        r = configure_logging(opts, dotaspen, self.conf)
        self.log_filename, self.log_filter, self.log_format, self.log_level = r
        self.__names.extend(["log_filename", "log_filter", "log_format", "log_level"])
Exemplo n.º 2
0
    def __init__(self, argv):
        """Takes an argv list, and gives it straight to optparser.parse_args.
        """

        # Initialize parsers.
        # ===================

        opts, args = optparser.parse_args(argv)

        # Root
        # ====
        # This can only be passed on the command line.

        if args:
            root = args[0]
        else:
            try:
                # Under supervisord, the following raises
                #   OSError: [Errno 2] No such file or directory
                # So be sure to pass a directory in on the command line, or cwd
                # using supervisord's own facility for that.
                root = os.getcwd()
            except OSError:
                raise ConfigurationError(
                    "Could not get a current working "
                    "directory. You can specify the site "
                    "root on the command line."
                )
        root = realpath(root)
        if not isdir(root):
            msg = "%s does not point to a directory" % root
            raise ConfigurationError(msg)

        # sys.path
        # ========

        dotaspen = join(root, ".aspen")
        if isdir(dotaspen):
            sys.path.insert(0, dotaspen)

        # aspen.conf
        # ==========

        conf = AspenConf(
            "/etc/aspen/aspen.conf",
            "/usr/local/etc/aspen/aspen.conf",
            expanduser("~/.aspen/aspen.conf"),
            join(dotaspen, "aspen.conf"),
        )  # later overrides earlier

        self.root = root
        self.args = args
        self.conf = conf
        self.optparser = optparser
        self.opts = opts

        # hooks
        # =====

        self.hooks = HooksConf(
            "/etc/aspen/hooks.conf",
            "/usr/local/etc/aspen/hooks.conf",
            expanduser("~/.aspen/hooks.conf"),
            join(dotaspen, "hooks.conf"),
        )  # later comes after earlier, per section

        # address/sockfam
        # ===============
        # These can be set either on the command line or in the conf file.

        if getattr(opts, "have_address", False):  # first check CLI
            address = opts.address
            sockfam = opts.sockfam
        elif "address" in conf.main:  # then check conf
            address, sockfam = validate_address(conf.main["address"])
        else:  # default from optparse
            address = opts.address
            sockfam = socket.AF_INET

        self.address = address
        self.sockfam = sockfam

        # Logging
        # =======
        # Logging can be configured from four places, in this order of
        # precedence:
        #
        #   1) some other module (theoretically, I'm not sure an aspen user
        #      could make this happen easily)
        #   2) the command line
        #   3) a logging.conf file
        #   4) a [logging] section in the aspen.conf file
        #
        # These are not layered; only one is used.

        # FMT = "%(process)-6s%(levelname)-9s%(name)-14s%(message)s"

        logging_configured = bool(len(logging.getLogger().handlers))
        # this test is taken from logging.basicConfig.

        if logging_configured:  # some other module
            log.warn("logging is already configured")

        if not logging_configured:  # command line
            kw = dict()
            kw["filename"] = opts.log_file
            kw["filter"] = opts.log_filter
            kw["format"] = opts.log_format
            kw["level"] = opts.log_level
            if kw.values() != [None, None, None, None]:  # at least one knob set
                if kw["format"] is None:
                    kw["format"] = LOG_FORMAT
                if kw["level"] is None:
                    kw["level"] = LOG_LEVEL
                self.configure_logging(**kw)
                log.info("logging configured from the command line")
                logging_configured = True

        if not logging_configured:  # logging.conf
            # TODO /etc/aspen/logging.conf
            # TODO /usr/local/etc/aspen/logging.conf
            logging_conf = join(root, ".aspen", "logging.conf")
            if exists(logging_conf):
                logging.config.fileConfig(logging_conf)
                log.info("logging configured from logging.conf")
                logging_configured = True

        if not logging_configured:  # aspen.conf [logging]
            kw = dict()
            kw["filename"] = conf.logging.get("file")
            kw["filter"] = conf.logging.get("filter")
            kw["format"] = conf.logging.get("format", LOG_FORMAT)

            log_level = conf.logging.get("level")
            if log_level is not None:
                log_level = validate_log_level(log_level)
            else:
                log_level = LOG_LEVEL
            kw["level"] = log_level

            self.configure_logging(**kw)
            log.info("logging configured from aspen.conf")
            logging_configured = True
Exemplo n.º 3
0
    def configure(self, argv):
        """Takes an argv list, and gives it straight to optparser.parse_args.

        The argv list should not include the executable name.

        """

        self.__names = [] # keep track of what configuration we configure


        # Parse argv.
        # ===========

        opts, args = optparser.parse_args(argv)


        # Orient ourselves.
        # =================

        self.root = root = find_root(args)
        os.chdir(root)

        self.dotaspen = dotaspen = join(self.root, '.aspen')
        if isdir(dotaspen):
            if sys.path[0] != dotaspen:
                sys.path.insert(0, dotaspen)

        self.__names.extend(['root', 'dotaspen'])


        # Set some attributes.
        # ====================

        self.conf = load_conf(expanduser, dotaspen)
        self.template_loader = Loader(dotaspen)
        self.__names.extend(['conf', 'template_loader'])

        init_mimetypes(mimetypes, dotaspen)
        self.default_mimetype = load_default_mimetype(self.conf)
        self.default_filenames = load_default_filenames(self.conf)
        self.json_content_type = load_json_content_type(self.conf)
        self.show_tracebacks = self.conf.aspen.no('show_tracebacks')
        self.__names.extend(['default_mimetype', 'default_filenames', 
            'json_content_type', 'show_tracebacks'])

        self.hooks = load_hooks(expanduser, dotaspen)
        self.__names.append('hooks')

        self.engine = load_engine(opts, self.conf)
        self.changes_kill = self.conf['aspen.cli'].no('changes_kill')
        self.__names.extend(['engine', 'changes_kill'])

        self.address, self.sockfam = load_address_sockfam(opts, self.conf)
        self.port = load_port(self.address, self.sockfam)
        self.sock = None
        self.__names.extend(['address', 'sockfam', 'port', 'sock'])

        r = configure_logging(opts, dotaspen, self.conf)
        self.log_filename, self.log_filter, self.log_format, self.log_level = r
        self.__names.extend(['log_filename', 'log_filter', 'log_format', 
            'log_level'])