Skip to content

Commit

Permalink
Merge branch 'master' of github.com:aparo/pyes
Browse files Browse the repository at this point in the history
  • Loading branch information
Alberto Paro committed Feb 2, 2012
2 parents 607dbdf + e369285 commit 8a2ce30
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 13 deletions.
29 changes: 22 additions & 7 deletions pyes/es.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@ def default(self, value):
"""

if isinstance(value, datetime):
return value.strftime("%Y-%m-%dT%H:%M:%S")
return value.isoformat()
elif isinstance(value, date):
dt = datetime(value.year, value.month, value.day, 0, 0, 0)
return dt.strftime("%Y-%m-%dT%H:%M:%S")
return dt.isoformat()
elif isinstance(value, Decimal):
return float(str(value))
else:
Expand Down Expand Up @@ -481,7 +481,8 @@ def _validate_indices(self, indices=None):
If `indices` is not supplied, returns the default_indices.
"""
indices = indices or self.default_indices
if indices is None:
return self.default_indices
if isinstance(indices, basestring):
indices = [indices]
return indices
Expand All @@ -491,12 +492,26 @@ def _dump_curl_request(self, request):
params = {'pretty': 'true'}
params.update(request.parameters)
method = Method._VALUES_TO_NAMES[request.method]
url = urlunsplit(('http', self.servers[0], request.uri, urlencode(params), ''))
### using the new (prot,host,port) tuple, the below code throws a 'can not
### concatenate tuple + str' exception
#url = urlunsplit(('http', self.servers[0], request.uri, urlencode(params), ''))
url = urlunsplit(('http', ("%s:%s" % self.servers[0][1:3]), request.uri, urlencode(params), ''))
curl_cmd = "curl -X%s '%s'" % (method, url)
if request.body:
curl_cmd += " -d '%s'" % request.body
print >> self.dump_curl, curl_cmd

def _get_default_indices(self):
return self._default_indices

def _set_default_indices(self, default_indices):
if default_indices is not None:
default_indices = self._validate_indices(default_indices)
self._default_indices = default_indices

default_indices = property(_get_default_indices, _set_default_indices)
del _get_default_indices, _set_default_indices

#---- Admin commands
def status(self, indices=None):
"""
Expand Down Expand Up @@ -632,15 +647,15 @@ def change_aliases(self, commands):
}
return self._send_request('POST', "_aliases", body)

def add_alias(self, alias, indices):
def add_alias(self, alias, indices=None):
"""Add an alias to point to a set of indices.
"""
indices = self._validate_indices(indices)
return self.change_aliases(['add', index, alias]
for index in indices)

def delete_alias(self, alias, indices):
def delete_alias(self, alias, indices=None):
"""Delete an alias.
The specified index or indices are deleted from the alias, if they are
Expand All @@ -652,7 +667,7 @@ def delete_alias(self, alias, indices):
return self.change_aliases(['remove', index, alias]
for index in indices)

def set_alias(self, alias, indices):
def set_alias(self, alias, indices=None):
"""Set an alias.
This handles removing the old list of indices pointed to by the alias.
Expand Down
11 changes: 10 additions & 1 deletion pyes/tests/test_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

import unittest
from pyes.tests import ESTestCase
from pyes import TermQuery
from pyes import TermQuery, RangeQuery
from pyes.utils import ESRange
from datetime import datetime

"""
Expand Down Expand Up @@ -42,6 +43,7 @@ def setUp(self):
self.conn.put_mapping(self.document_type, {'properties':mapping}, self.index_name)
self.conn.index({"name":"Joe Tester", "parsedtext":"Joe Testere nice guy", "uuid":"11111", "position":1, 'inserted':datetime(2010, 10, 22, 12, 12, 12)}, self.index_name, self.document_type, 1)
self.conn.index({"name":"Bill Baloney", "parsedtext":"Joe Testere nice guy", "uuid":"22222", "position":2, 'inserted':datetime(2010, 10, 22, 12, 12, 10)}, self.index_name, self.document_type, 2)
self.conn.index({"name":"Jesus H Christ", "parsedtext":"Bible guy", "uuid":"33333", "position":3, 'inserted':datetime(1, 1, 1, 0, 0, 0)}, self.index_name, self.document_type, 3)
self.conn.refresh(self.index_name)

def test_TermQuery(self):
Expand All @@ -51,6 +53,13 @@ def test_TermQuery(self):
hit = resultset[0]
self.assertEquals(hit.inserted, datetime(2010, 10, 22, 12, 12, 12))

def test_DateBefore1900(self):
q = RangeQuery(ESRange("inserted", datetime(1, 1, 1), datetime(2, 1, 1)))
resultset = self.conn.search(query=q, indices=self.index_name)
self.assertEquals(resultset.total, 1)
hit = resultset[0]
self.assertEquals(hit.inserted, datetime(1, 1, 1, 0, 0, 0))


if __name__ == "__main__":
unittest.main()
44 changes: 39 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,41 @@
from setuptools import setup, find_packages, Command
from setuptools.command.test import test as TestCommand

import pyes as distmeta
# Extract distribution meta values, hint taken from Celery <http://celeryproject.org>

import re
re_meta = re.compile(r'__(\w+?)__\s*=\s*(.*)')
re_vers = re.compile(r'VERSION\s*=\s*\((.*?)\)')
re_doc = re.compile(r'^"""(.+?)"""')
rq = lambda s: s.strip("\"'")

def add_default(m):
attr_name, attr_value = m.groups()
return ((attr_name, rq(attr_value)), )


def add_version(m):
v = list(map(rq, m.groups()[0].split(", ")))
return (("VERSION", ".".join(v[0:3]) + "".join(v[3:])), )


def add_doc(m):
return (("doc", m.groups()[0]), )

pats = {re_meta: add_default,
re_vers: add_version,
re_doc: add_doc}
here = os.path.abspath(os.path.dirname(__file__))
meta_fh = open(os.path.join(here, "pyes/__init__.py"))
try:
meta = {}
for line in meta_fh:
for pattern, handler in pats.items():
m = pattern.match(line.strip())
if m:
meta.update(handler(m))
finally:
meta_fh.close()


class QuickRunTests(TestCommand):
Expand Down Expand Up @@ -53,11 +87,11 @@ def run(self, *args, **kwargs):

setup(
name='pyes',
version=distmeta.__version__,
version=meta['VERSION'],
description="Python Elastic Search driver",
author=distmeta.__author__,
author_email=distmeta.__contact__,
url=distmeta.__homepage__,
author=meta['author'],
author_email=meta['contact'],
url=meta['homepage'],
platforms=["any"],
license="BSD",
packages=find_packages(exclude=['ez_setup', 'tests', 'tests.*', "docs.*"]),
Expand Down

0 comments on commit 8a2ce30

Please sign in to comment.