Пример #1
0
def section_from_yaml_file(uri, key='__section', config_dir=None):
    from sonicprobe.libs.urisup import uri_help_split, uri_help_unsplit

    section = None

    u = list(uri_help_split(uri))
    if not u[0] and u[2]:
        u[0] = 'file'
        if config_dir and not u[2].startswith('/'):
            u[2] = "%s/%s" % (config_dir, u[2])

    if u[3]:
        q = []
        for k, v in u[3]:
            if k == key:
                section = v
            else:
                q.append((k, v))
        u[3] = q

    r = load_yaml_file(uri_help_unsplit(u))

    if section \
       and isinstance(r, dict) \
       and section in r:
        return r[section]

    return r
Пример #2
0
    def _mk_uri(self, cfg, path):
        uri    = list(urisup.uri_help_split(cfg['url']))
        uri[2] = path
        uri[3] = None

        if 'path' in cfg:
            if isinstance(cfg['path'], dict):
                uri[2] = self._regex(cfg['path'], path)
            del cfg['path']

        return urisup.uri_help_unsplit(uri)
Пример #3
0
    def _build_uri(self, achall, xtype = 'perform'):   # pylint: disable=missing-docstring
        key = achall.chall.path
        uri = list(self._uri[xtype])

        if self._config[xtype]['param_challenge']:
            uri[3] += [(self._config[xtype]['param_challenge'], key)]
        elif uri[2]:
            uri[2] = "/%s" % ("%s%s" % (uri[2].strip('/'), key)).lstrip('/')
        else:
            uri[2] = key

        return urisup.uri_help_unsplit(uri)
Пример #4
0
    def _do_request(self, method, path, params, payload, headers):
        uri = list(self.api_uri)
        uri[2] = path or None

        h = {}
        for k, v in headers.iteritems():
            if k.lower() != 'content-length':
                h[k.lower()] = v

        return getattr(requests, method.lower())(urisup.uri_help_unsplit(uri),
                                                 params=params,
                                                 json=payload,
                                                 headers=h)
Пример #5
0
    def reconnect(self, query=None, log_reconnect=False):
        """
        Reconnect to the database.
        """
        uri = list(urisup.uri_help_split(self.sqluri))
        if uri[1]:
            authority = list(uri[1])
            if authority[1]:
                authority[1] = None
            uri[1] = authority

        if log_reconnect:
            LOG.warning('reconnecting to %r database (query: %r)',
                        urisup.uri_help_unsplit(uri), query)
        self.__connect()
Пример #6
0
    def _build_uri(self, path = None, query = None, fragment = None):
        uri = list(urisup.uri_help_split(self.endpoint))
        uri[2:5] = (path, query, fragment)

        return urisup.uri_help_unsplit(uri)
Пример #7
0
    def _do_call(self, obj, targets=None, registry=None):  # pylint: disable=unused-argument
        if not targets:
            targets = self.targets

        param_target = obj.get_params().get('target')

        for target in targets:
            (data, req) = (None, None)

            cfg = target.config
            method = 'get'
            xformat = None

            if 'format' in cfg:
                xformat = cfg.pop('format').lower()

            if 'uri' in cfg and not cfg.get('url'):
                cfg['url'] = cfg.pop('uri')

            if param_target and not cfg.get('url'):
                cfg['url'] = param_target

            if not cfg.get('url'):
                raise CovenantConfigurationError(
                    "missing uri or target in configuration")

            if 'path' in cfg:
                url = list(urisup.uri_help_split(cfg['url']))
                if url[2]:
                    url[2] = "%s/%s" % (url[2].rstrip('/'), cfg.pop(
                        'path', '').lstrip('/'))
                else:
                    url[2] = cfg.pop('path')
                cfg['url'] = urisup.uri_help_unsplit(url)

            if 'method' in cfg:
                method = cfg.pop('method').lower()

            if 'ssl_verify' in cfg:
                cfg['verify'] = bool(cfg.pop('ssl_verify'))

            if cfg.get('timeout') is not None:
                cfg['timeout'] = float(cfg['timeout'])
            else:
                cfg['timeout'] = None

            if not isinstance(cfg.get('headers'), dict):
                cfg['headers'] = None

            if target.credentials:
                cfg['auth'] = (target.credentials['username'],
                               target.credentials['password'])

            if method not in _ALLOWED_METHODS:
                raise CovenantConfigurationError("invalid http method: %r" %
                                                 method)

            try:
                req = getattr(requests, method)(**cfg)

                if req.status_code != requests.codes['ok']:
                    raise LookupError("invalid status code: %r. (error: %r)" %
                                      (req.status_code, req.text))

                if xformat == 'json':
                    data = req.json()
                else:
                    data = req.text
            except Exception as e:
                data = CovenantTargetFailed(e)
                LOG.exception("error on target: %r. exception: %r",
                              target.name, e)
            finally:
                if req:
                    req.close()

            target(data)

        return self.generate_latest(registry)
Пример #8
0
 def _build_uri(self):  # pylint: disable=missing-docstring
     return urisup.uri_help_unsplit(self._uri)
Пример #9
0
def c14n_uri(uri):
    puri = list(uri_help_split(uri))
    puri[PATH] = os.path.abspath(puri[PATH])
    return uri_help_unsplit(tuple(puri))