Example #1
0
def load_config(extra_args=None,
                doc=None,
                filenames=None,
                invalidate_cache=False,
                fedmsg_command=False):
    """ Setup a runtime config dict by integrating the following sources
    (ordered by precedence):

      - defaults
      - config file
      - command line arguments

    If the ``fedmsg_command`` argument is False, no command line arguments are
    checked.

    """
    global __cache

    if invalidate_cache:
        __cache = {}

    if __cache:
        return __cache

    # Coerce defaults if arguments are not supplied.
    extra_args = extra_args or []
    doc = doc or ""

    config = copy.deepcopy(defaults)
    config.update(_process_config_file(filenames=filenames))

    # This is optional (and defaults to false) so that only 'fedmsg-*' commands
    # are required to provide these arguments.
    # For instance, the moksha-hub command takes a '-v' argument and internally
    # makes calls to fedmsg.  We don't want to impose all of fedmsg's CLI
    # option constraints on programs that use fedmsg, so we make it optional.
    if fedmsg_command:
        config.update(_process_arguments(extra_args, doc, config))

    # If the user specified a config file on the command line, then start over
    # but read in that file instead.
    if not filenames and config.get('config_filename', None):
        return load_config(extra_args,
                           doc,
                           filenames=[config['config_filename']])

    # Just a little debug option.  :)
    if config['print_config']:
        print pretty_dumps(config)
        sys.exit(0)

    if config['environment'] not in VALID_ENVIRONMENTS:
        raise ValueError("%r not one of %r" %
                         (config['environment'], VALID_ENVIRONMENTS))

    if 'endpoints' not in config:
        raise ValueError("No config value 'endpoints' found.")

    __cache = config
    return config
Example #2
0
def load_config(extra_args=None,
                doc=None,
                filenames=None,
                invalidate_cache=False,
                fedmsg_command=False):
    """ Setup a runtime config dict by integrating the following sources
    (ordered by precedence):

      - defaults
      - config file
      - command line arguments

    If the ``fedmsg_command`` argument is False, no command line arguments are
    checked.

    """
    global __cache

    if invalidate_cache:
        __cache = {}

    if __cache:
        return __cache

    # Coerce defaults if arguments are not supplied.
    extra_args = extra_args or []
    doc = doc or ""

    config = copy.deepcopy(defaults)
    config.update(_process_config_file(filenames=filenames))

    # This is optional (and defaults to false) so that only 'fedmsg-*' commands
    # are required to provide these arguments.
    # For instance, the moksha-hub command takes a '-v' argument and internally
    # makes calls to fedmsg.  We don't want to impose all of fedmsg's CLI
    # option constraints on programs that use fedmsg, so we make it optional.
    if fedmsg_command:
        config.update(_process_arguments(extra_args, doc, config))

    # If the user specified a config file on the command line, then start over
    # but read in that file instead.
    if not filenames and config.get('config_filename', None):
        return load_config(extra_args, doc,
                           filenames=[config['config_filename']])

    # Just a little debug option.  :)
    if config['print_config']:
        print pretty_dumps(config)
        sys.exit(0)

    if config['environment'] not in VALID_ENVIRONMENTS:
        raise ValueError("%r not one of %r" % (
            config['environment'], VALID_ENVIRONMENTS))

    if 'endpoints' not in config:
        raise ValueError("No config value 'endpoints' found.")

    __cache = config
    return config
Example #3
0
    def run(self):
        m.init(self.config['datanommer.sqlalchemy.url'])
        config = self.config

        query = m.Message.query
        if config.get('before', None):
            query = query.filter(m.Message.timestamp<=config.get('before'))

        if config.get('since', None):
            query = query.filter(m.Message.timestamp>=config.get('since'))

        results = query.all()

        self.log.info(pretty_dumps(results))
Example #4
0
    def run(self):
        m.init(self.config['datanommer.sqlalchemy.url'])
        config = self.config

        query = m.Message.query
        if config.get('before', None):
            query = query.filter(m.Message.timestamp <= config.get('before'))

        if config.get('since', None):
            query = query.filter(m.Message.timestamp >= config.get('since'))

        results = query.all()

        self.log.info(pretty_dumps(results))
Example #5
0
 def formatter(key, val):
     if config.get('timestamp', None) and config.get('human', None):
         return pretty_dumps(str(val.timestamp))
     elif config.get('timestamp', None):
         return pretty_dumps(time.mktime(val.timestamp.timetuple()))
     elif config.get('timesince', None) and config.get('human', None):
         return pretty_dumps(str(datetime.datetime.now()-val.timestamp))
     elif config.get('timesince', None):
         timedelta = datetime.datetime.now() - val.timestamp
         return pretty_dumps(str((timedelta.days * 86400)+timedelta.seconds))
     else:
         return "{%s: %s}" % (pretty_dumps(key), pretty_dumps(val))
Example #6
0
 def formatter(key, val):
     if config.get('timestamp', None) and config.get('human', None):
         return pretty_dumps(str(val.timestamp))
     elif config.get('timestamp', None):
         return pretty_dumps(time.mktime(val.timestamp.timetuple()))
     elif config.get('timesince', None) and config.get('human', None):
         return pretty_dumps(
             str(datetime.datetime.now() - val.timestamp))
     elif config.get('timesince', None):
         timedelta = datetime.datetime.now() - val.timestamp
         return pretty_dumps(
             str((timedelta.days * 86400) + timedelta.seconds))
     else:
         return "{%s: %s}" % (pretty_dumps(key), pretty_dumps(val))
Example #7
0
def load_config(extra_args=None,
                doc=None,
                filenames=None,
                invalidate_cache=False,
                fedmsg_command=False,
                disable_defaults=False):
    """ Setup a runtime config dict by integrating the following sources
    (ordered by precedence):

      - defaults (unless disable_defaults = True)
      - config file
      - command line arguments

    If the ``fedmsg_command`` argument is False, no command line arguments are
    checked.

    """
    global __cache

    if invalidate_cache:
        __cache = {}

    if __cache:
        return __cache

    # Coerce defaults if arguments are not supplied.
    extra_args = extra_args or []
    doc = doc or ""

    if not disable_defaults:
        config = copy.deepcopy(defaults)
    else:
        config = {}

    config.update(_process_config_file(filenames=filenames))

    # This is optional (and defaults to false) so that only 'fedmsg-*' commands
    # are required to provide these arguments.
    # For instance, the moksha-hub command takes a '-v' argument and internally
    # makes calls to fedmsg.  We don't want to impose all of fedmsg's CLI
    # option constraints on programs that use fedmsg, so we make it optional.
    if fedmsg_command:
        config.update(_process_arguments(extra_args, doc, config))

    # If the user specified a config file on the command line, then start over
    # but read in that file instead.
    if not filenames and config.get('config_filename', None):
        return load_config(extra_args,
                           doc,
                           filenames=[config['config_filename']])

    # Just a little debug option.  :)
    if config.get('print_config'):
        print(pretty_dumps(config))
        sys.exit(0)

    if config.get('environment', 'prod') not in VALID_ENVIRONMENTS:
        raise ValueError("%r not one of %r" %
                         (config['environment'], VALID_ENVIRONMENTS))

    if not disable_defaults and 'endpoints' not in config:
        raise ValueError("No config value 'endpoints' found.")

    if not isinstance(config.get('endpoints', {}), dict):
        raise ValueError("The 'endpoint' config value must be a dict.")

    if 'endpoints' in config:
        config['endpoints'] = dict([(k, list(iterate(v)))
                                    for k, v in config['endpoints'].items()])

    if 'srv_endpoints' in config and len(config['srv_endpoints']) > 0:
        from dns.resolver import query, NXDOMAIN, Timeout, NoNameservers
        for e in config['srv_endpoints']:
            urls = []
            try:
                records = query('_fedmsg._tcp.{0}'.format(e), 'SRV')
            except NXDOMAIN:
                warnings.warn("There is no appropriate SRV records " +
                              "for {0}".format(e))
                continue
            except Timeout:
                warnings.warn("The DNS query for the SRV records of" +
                              " {0} timed out.".format(e))
                continue
            except NoNameservers:
                warnings.warn("No name server is available, please " +
                              "check the configuration")
                break

            for rec in records:
                urls.append('tcp://{hostname}:{port}'.format(
                    hostname=rec.target.to_text(), port=rec.port))
            config['endpoints'][e] = list(iterate(urls))

    if 'topic_prefix_re' not in config and 'topic_prefix' in config:
        # Turn "org.fedoraproject" into "org\.fedoraproject\.(dev|stg|prod)"
        config['topic_prefix_re'] = config['topic_prefix'].replace('.', '\.')\
            + '\.(%s)' % '|'.join(VALID_ENVIRONMENTS)

    __cache = config
    return config
Example #8
0
def load_config(extra_args=None,
                doc=None,
                filenames=None,
                invalidate_cache=False,
                fedmsg_command=False,
                disable_defaults=False):
    """ Setup a runtime config dict by integrating the following sources
    (ordered by precedence):

      - defaults (unless disable_defaults = True)
      - config file
      - command line arguments

    If the ``fedmsg_command`` argument is False, no command line arguments are
    checked.

    """
    global __cache

    if invalidate_cache:
        __cache = {}

    if __cache:
        return __cache

    # Coerce defaults if arguments are not supplied.
    extra_args = extra_args or []
    doc = doc or ""

    if not disable_defaults:
        config = copy.deepcopy(defaults)
    else:
        config = {}

    config.update(_process_config_file(filenames=filenames))

    # This is optional (and defaults to false) so that only 'fedmsg-*' commands
    # are required to provide these arguments.
    # For instance, the moksha-hub command takes a '-v' argument and internally
    # makes calls to fedmsg.  We don't want to impose all of fedmsg's CLI
    # option constraints on programs that use fedmsg, so we make it optional.
    if fedmsg_command:
        config.update(_process_arguments(extra_args, doc, config))

    # If the user specified a config file on the command line, then start over
    # but read in that file instead.
    if not filenames and config.get('config_filename', None):
        return load_config(extra_args, doc,
                           filenames=[config['config_filename']])

    # Just a little debug option.  :)
    if config.get('print_config'):
        print(pretty_dumps(config))
        sys.exit(0)

    if config.get('environment', 'prod') not in VALID_ENVIRONMENTS:
        raise ValueError("%r not one of %r" % (
            config['environment'], VALID_ENVIRONMENTS))

    if not disable_defaults and 'endpoints' not in config:
        raise ValueError("No config value 'endpoints' found.")

    if not isinstance(config.get('endpoints', {}), dict):
        raise ValueError("The 'endpoint' config value must be a dict.")

    if 'endpoints' in config:
        config['endpoints'] = dict([
            (k, list(iterate(v))) for k, v in config['endpoints'].items()
        ])

    if 'srv_endpoints' in config and len(config['srv_endpoints']) > 0:
        from dns.resolver import query, NXDOMAIN, Timeout, NoNameservers
        for e in config['srv_endpoints']:
            urls = []
            try:
                records = query('_fedmsg._tcp.{0}'.format(e), 'SRV')
            except NXDOMAIN:
                warnings.warn("There is no appropriate SRV records " +
                              "for {0}".format(e))
                continue
            except Timeout:
                warnings.warn("The DNS query for the SRV records of" +
                              " {0} timed out.".format(e))
                continue
            except NoNameservers:
                warnings.warn("No name server is available, please " +
                              "check the configuration")
                break

            for rec in records:
                urls.append('tcp://{hostname}:{port}'.format(
                    hostname=rec.target.to_text(),
                    port=rec.port
                ))
            config['endpoints'][e] = list(iterate(urls))

    if 'topic_prefix_re' not in config and 'topic_prefix' in config:
        # Turn "org.fedoraproject" into "org\.fedoraproject\.(dev|stg|prod)"
        config['topic_prefix_re'] = config['topic_prefix'].replace('.', '\.')\
            + '\.(%s)' % '|'.join(VALID_ENVIRONMENTS)

    __cache = config
    return config