示例#1
0
    def __init__(
        self, url="localhost:9200", path="", timeout=30, connection_type=None, connection=None, except_on_error=False
    ):
        super(Elastic, self).__init__()
        url_parts = url.split(":")
        self.host = url_parts[0]
        self.port = int(url_parts[1]) if len(url_parts) == 2 else 9200
        self.url = "%s:%s" % (self.host, self.port)
        self.timeout = timeout  # seconds
        self.path = path

        if connection_type is None:
            if self.port >= 9500 and self.port <= 9600:
                self.connection_type = "thrift"
            else:
                self.connection_type = "http"
        else:
            self.connection_type = connection_type

        if connection is None:
            if self.connection_type == "http":
                self.connection = HttpConnection(
                    self.host, self.port, timeout=self.timeout, except_on_error=except_on_error
                )
            else:
                self.connection = ThriftConnection(
                    self.host, self.port, timeout=self.timeout, except_on_error=except_on_error
                )
        else:
            self.connection = connection
示例#2
0
文件: elastic.py 项目: drue/rawes
    def __init__(self,
                 url='localhost:9200',
                 path='',
                 timeout=30,
                 connection_type=None,
                 connection=None):
        super(Elastic, self).__init__()
        url_parts = url.split(':')
        self.host = url_parts[0]
        self.port = int(url_parts[1]) if len(url_parts) == 2 else 9200
        self.url = '%s:%s' % (self.host, self.port)
        self.timeout = None if timeout is None else timeout * 1000
        self.path = path

        if connection_type is None:
            if thrift_installed and self.port >= 9500 and self.port <= 9600:
                self.connection_type = 'thrift'
            else:
                self.connection_type = 'http'
        else:
            self.connection_type = connection_type

        if connection is None:
            if self.connection_type == 'http':
                self.connection = HttpConnection(self.host,
                                                 self.port,
                                                 timeout=self.timeout)
            else:
                self.connection = ThriftConnection(self.host,
                                                   self.port,
                                                   timeout=self.timeout)
        else:
            self.connection = connection
示例#3
0
文件: elastic.py 项目: atkinson/rawes
    def __init__(self, url='localhost:9200', path='', timeout=30, connection_type=None, connection=None):
        super(Elastic, self).__init__()
        url_parts = url.split(':')
        self.host = url_parts[0]
        self.port = int(url_parts[1]) if len(url_parts) == 2 else 9200
        self.url = '%s:%s' % (self.host, self.port)
        self.timeout = None if timeout is None else timeout * 1000
        self.path = path

        if connection_type is None:
            if self.port >= 9500 and self.port <= 9600:
                self.connection_type = 'thrift'
            else:
                self.connection_type = 'http'
        else:
            self.connection_type = connection_type

        if connection is None:
            if self.connection_type == 'http':
                self.connection = HttpConnection(self.host, self.port, timeout=self.timeout)
            else:
                self.connection = ThriftConnection(self.host, self.port, timeout=self.timeout)
        else:
            self.connection = connection
示例#4
0
文件: elastic.py 项目: fsys/rawes
    def _get_connection_from_url(self, url, timeout, **kwargs):
        """Returns a connection object given a string url"""

        url = self._decode_url(url, "")

        if url.scheme == 'http' or url.scheme == 'https':
            return HttpConnection(url.geturl(), timeout=timeout, **kwargs)
        else:
            if sys.version_info[0] > 2:
                raise ValueError("Thrift transport is not available "
                                 "for Python 3")

            try:
                from thrift_connection import ThriftConnection
            except ImportError:
                raise ImportError("The 'thrift' python package "
                                  "does not seem to be installed.")
            return ThriftConnection(url.hostname,
                                    url.port,
                                    timeout=timeout,
                                    **kwargs)
示例#5
0
    def __init__(self, url='localhost:9200', path='', timeout=30, connection=None, json_encoder=encode_date_optional_time, **kwargs):
        super(Elastic, self).__init__()

        self.url = self._decode_url(url,path)
        self.timeout = timeout  # seconds
        self.json_encoder = json_encoder

        if connection is None:
            if self.url.scheme == 'http' or self.url.scheme == 'https':
                connection = HttpConnection(self.url.geturl(), timeout=self.timeout, **kwargs)
            else:
                if sys.version_info[0] > 2:
                    raise ValueError("Thrift transport not available for Python 3")

                try:
                    from thrift_connection import ThriftConnection
                except ImportError:
                    raise ImportError("The 'thrift' python package does not seem to be installed.")
                connection = ThriftConnection(self.url.hostname, self.url.port, timeout=self.timeout, **kwargs)

        self.connection = connection
示例#6
0
文件: elastic.py 项目: atkinson/rawes
class Elastic(object):
    """Connect to an elasticsearch instance"""
    def __init__(self, url='localhost:9200', path='', timeout=30, connection_type=None, connection=None):
        super(Elastic, self).__init__()
        url_parts = url.split(':')
        self.host = url_parts[0]
        self.port = int(url_parts[1]) if len(url_parts) == 2 else 9200
        self.url = '%s:%s' % (self.host, self.port)
        self.timeout = None if timeout is None else timeout * 1000
        self.path = path

        if connection_type is None:
            if self.port >= 9500 and self.port <= 9600:
                self.connection_type = 'thrift'
            else:
                self.connection_type = 'http'
        else:
            self.connection_type = connection_type

        if connection is None:
            if self.connection_type == 'http':
                self.connection = HttpConnection(self.host, self.port, timeout=self.timeout)
            else:
                self.connection = ThriftConnection(self.host, self.port, timeout=self.timeout)
        else:
            self.connection = connection

    def put(self, path='', **kwargs):
        new_path = self._build_path(self.path, path)
        return self.connection.put(new_path, **kwargs)

    def get(self, path='', **kwargs):
        new_path = self._build_path(self.path, path)
        return self.connection.get(new_path, **kwargs)

    def post(self, path='', **kwargs):
        new_path = self._build_path(self.path, path)
        return self.connection.post(new_path, **kwargs)

    def delete(self, path='', **kwargs):
        new_path = self._build_path(self.path, path)
        return self.connection.delete(new_path, **kwargs)

    def head(self, path='', **kwargs):
        new_path = self._build_path(self.path, path)
        return self.connection.head(new_path, **kwargs)

    def __getattr__(self, path_item):
        return self.__getitem__(path_item)

    def __getitem__(self, path_item):
        new_path = self._build_path(self.path, path_item)
        return Elastic(
            url=self.url,
            timeout=self.timeout,
            connection_type=self.connection_type,
            path=new_path,
            connection=self.connection
        )

    def _build_path(self, base_path, path_item):
        return '%s/%s' % (base_path, path_item) if base_path != '' else path_item
示例#7
0
class Elastic(object):
    """Connect to an elasticsearch instance"""

    def __init__(
        self, url="localhost:9200", path="", timeout=30, connection_type=None, connection=None, except_on_error=False
    ):
        super(Elastic, self).__init__()
        url_parts = url.split(":")
        self.host = url_parts[0]
        self.port = int(url_parts[1]) if len(url_parts) == 2 else 9200
        self.url = "%s:%s" % (self.host, self.port)
        self.timeout = timeout  # seconds
        self.path = path

        if connection_type is None:
            if self.port >= 9500 and self.port <= 9600:
                self.connection_type = "thrift"
            else:
                self.connection_type = "http"
        else:
            self.connection_type = connection_type

        if connection is None:
            if self.connection_type == "http":
                self.connection = HttpConnection(
                    self.host, self.port, timeout=self.timeout, except_on_error=except_on_error
                )
            else:
                self.connection = ThriftConnection(
                    self.host, self.port, timeout=self.timeout, except_on_error=except_on_error
                )
        else:
            self.connection = connection

    def put(self, path="", **kwargs):
        return self.request("put", path, **kwargs)

    def get(self, path="", **kwargs):
        return self.request("get", path, **kwargs)

    def post(self, path="", **kwargs):
        return self.request("post", path, **kwargs)

    def delete(self, path="", **kwargs):
        return self.request("delete", path, **kwargs)

    def head(self, path="", **kwargs):
        return self.request("head", path, **kwargs)

    def request(self, method, path, **kwargs):
        new_path = self._build_path(self.path, path)

        # Look for a custom json encoder
        if "json_encoder" in kwargs:
            json_encoder = kwargs["json_encoder"]
            del kwargs["json_encoder"]
        else:
            json_encoder = encode_date_optional_time

        # Encode data dict to json if necessary
        if "data" in kwargs and type(kwargs["data"]) == dict:
            kwargs["data"] = json.dumps(kwargs["data"], default=json_encoder)

        return self.connection.request(method, new_path, **kwargs)

    def __getattr__(self, path_item):
        return self.__getitem__(path_item)

    def __getitem__(self, path_item):
        new_path = self._build_path(self.path, path_item)
        return Elastic(
            url=self.url,
            timeout=self.timeout,
            connection_type=self.connection_type,
            path=new_path,
            connection=self.connection,
        )

    def _build_path(self, base_path, path_item):
        return "%s/%s" % (base_path, path_item) if base_path != "" else path_item
示例#8
0
class Elastic(object):
    """Connect to an elasticsearch instance"""
    def __init__(self, url='localhost:9200', path='', timeout=30, connection_type=None, connection=None):
        super(Elastic, self).__init__()
        url_parts = url.split(':')
        self.host = url_parts[0]
        self.port = int(url_parts[1]) if len(url_parts) == 2 else 9200
        self.url = '%s:%s' % (self.host, self.port)
        self.timeout = None if timeout is None else timeout * 1000
        self.path = path

        if connection_type is None:
            if self.port >= 9500 and self.port <= 9600:
                self.connection_type = 'thrift'
            else:
                self.connection_type = 'http'
        else:
            self.connection_type = connection_type

        if connection is None:
            if self.connection_type == 'http':
                self.connection = HttpConnection(self.host, self.port, timeout=self.timeout)
            else:
                self.connection = ThriftConnection(self.host, self.port, timeout=self.timeout)
        else:
            self.connection = connection

    def put(self, path='', **kwargs):
        return self.request('put', path, **kwargs)

    def get(self, path='', **kwargs):
        return self.request('get', path, **kwargs)

    def post(self, path='', **kwargs):
        return self.request('post', path, **kwargs)

    def delete(self, path='', **kwargs):
        return self.request('delete', path, **kwargs)

    def head(self, path='', **kwargs):
        return self.request('head', path, **kwargs)

    def request(self, method, path, **kwargs):
        new_path = self._build_path(self.path, path)

        # Look for a custom json encoder
        if 'json_encoder' in kwargs:
            json_encoder = kwargs['json_encoder']
            del kwargs['json_encoder']
        else:
            json_encoder = encode_date_optional_time

        # Encode data dict to json if necessary
        if 'data' in kwargs and type(kwargs['data']) == dict:
            kwargs['data'] = json.dumps(kwargs['data'], default=json_encoder)

        return self.connection.request(method, new_path, **kwargs)

    def __getattr__(self, path_item):
        return self.__getitem__(path_item)

    def __getitem__(self, path_item):
        new_path = self._build_path(self.path, path_item)
        return Elastic(
            url=self.url,
            timeout=self.timeout,
            connection_type=self.connection_type,
            path=new_path,
            connection=self.connection
        )

    def _build_path(self, base_path, path_item):
        return '%s/%s' % (base_path, path_item) if base_path != '' else path_item
示例#9
0
文件: elastic.py 项目: drue/rawes
class Elastic(object):
    """Connect to an elasticsearch instance"""
    def __init__(self,
                 url='localhost:9200',
                 path='',
                 timeout=30,
                 connection_type=None,
                 connection=None):
        super(Elastic, self).__init__()
        url_parts = url.split(':')
        self.host = url_parts[0]
        self.port = int(url_parts[1]) if len(url_parts) == 2 else 9200
        self.url = '%s:%s' % (self.host, self.port)
        self.timeout = None if timeout is None else timeout * 1000
        self.path = path

        if connection_type is None:
            if thrift_installed and self.port >= 9500 and self.port <= 9600:
                self.connection_type = 'thrift'
            else:
                self.connection_type = 'http'
        else:
            self.connection_type = connection_type

        if connection is None:
            if self.connection_type == 'http':
                self.connection = HttpConnection(self.host,
                                                 self.port,
                                                 timeout=self.timeout)
            else:
                self.connection = ThriftConnection(self.host,
                                                   self.port,
                                                   timeout=self.timeout)
        else:
            self.connection = connection

    def put(self, path='', **kwargs):
        return self.request('put', path, **kwargs)

    def get(self, path='', **kwargs):
        return self.request('get', path, **kwargs)

    def post(self, path='', **kwargs):
        return self.request('post', path, **kwargs)

    def delete(self, path='', **kwargs):
        return self.request('delete', path, **kwargs)

    def head(self, path='', **kwargs):
        return self.request('head', path, **kwargs)

    def request(self, method, path, **kwargs):
        new_path = self._build_path(self.path, path)

        # Look for a custom json encoder
        if 'json_encoder' in kwargs:
            json_encoder = kwargs['json_encoder']
            del kwargs['json_encoder']
        else:
            json_encoder = encode_date_optional_time

        # Encode data dict to json if necessary
        if 'data' in kwargs and type(kwargs['data']) == dict:
            kwargs['data'] = json.dumps(kwargs['data'], default=json_encoder)
        elif 'data' in kwargs and type(kwargs['data']) == list:
            kwargs['data'] = '\n'.join(
                [json.dumps(d, default=json_encoder)
                 for d in kwargs['data']]) + '\n'

        return self.connection.request(method, new_path, **kwargs)

    def __getattr__(self, path_item):
        return self.__getitem__(path_item)

    def __getitem__(self, path_item):
        new_path = self._build_path(self.path, path_item)
        return Elastic(url=self.url,
                       timeout=self.timeout,
                       connection_type=self.connection_type,
                       path=new_path,
                       connection=self.connection)

    def _build_path(self, base_path, path_item):
        return '%s/%s' % (base_path,
                          path_item) if base_path != '' else path_item