Skip to content

Cleanup (PEP8) #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 35 additions & 26 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import codecs
import os
import re

try:
from setuptools import setup
except ImportError:
from distutils.core import setup

import os

os.chdir(os.path.abspath(os.path.dirname(__file__)))

# Read package version without importing it
for line in open(os.path.join("tarantool", "__init__.py")):
if line.startswith("__version__"):
exec line
break

# Extra commands for documentation management
cmdclass = {}
command_options = {}
Expand All @@ -37,7 +30,7 @@
from sphinx_pypi_upload import UploadDoc
cmdclass["upload_sphinx"] = UploadDoc
command_options["upload_sphinx"] = {
'upload_dir': ('setup.py', os.path.join("build", "sphinx", "html"))
'upload_dir': ('setup.py', os.path.join("build", "sphinx", "html"))
}
except ImportError:
pass
Expand All @@ -51,28 +44,44 @@
except ImportError:
pass


def read(*parts):
filename = os.path.join(os.path.dirname(__file__), *parts)
with codecs.open(filename, encoding='utf-8') as fp:
return fp.read()


def find_version(*file_paths):
version_file = read(*file_paths)
version_match = re.search(r"""^__version__\s*=\s*(['"])(.+)\1""",
version_file, re.M)
if version_match:
return version_match.group(2)
raise RuntimeError("Unable to find version string.")


setup(
name = "tarantool",
packages = ["tarantool"],
package_dir = {"tarantool": os.path.join("tarantool")},
version = __version__,
platforms = ["all"],
author = "Konstantin Cherkasoff",
author_email = "k.cherkasoff@gmail.com",
url = "https://github.com/coxx/tarantool-python",
license = "BSD",
description = "Python client library for Tarantool Database",
long_description = open("README.rst").read(),
classifiers = [
name="tarantool",
packages=["tarantool"],
package_dir={"tarantool": os.path.join("tarantool")},
version=find_version('tarantool', '__init__.py'),
platforms=["all"],
author="Konstantin Cherkasoff",
author_email="k.cherkasoff@gmail.com",
url="https://github.com/tarantool/tarantool-python",
license="BSD",
description="Python client library for Tarantool Database",
long_description=read('README.rst'),
classifiers=[
"Intended Audience :: Developers",
"License :: OSI Approved :: BSD License",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Topic :: Database :: Front-Ends"
],
cmdclass = cmdclass,
command_options = command_options,
install_requires = [
cmdclass=cmdclass,
command_options=command_options,
install_requires=[
'msgpack-python>=0.4',
]
)
4 changes: 2 additions & 2 deletions tarantool/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
)

from tarantool.schema import (
Schema,
SchemaError
Schema,
SchemaError
)


Expand Down
62 changes: 30 additions & 32 deletions tarantool/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import msgpack

import base64
from const import IPROTO_GREETING_SIZE

try:
from ctypes import c_ssize_t
Expand Down Expand Up @@ -53,9 +52,9 @@
from tarantool.schema import Schema
from tarantool.utils import check_key

class Connection(object):

'''\
class Connection(object):
'''
Represents connection to the Tarantool server.

This class is responsible for connection and network exchange with
Expand All @@ -81,7 +80,7 @@ def __init__(self, host, port,
reconnect_max_attempts=RECONNECT_MAX_ATTEMPTS,
reconnect_delay=RECONNECT_DELAY,
connect_now=True):
'''\
'''
Initialize a connection to the server.

:param str host: Server hostname or IP-address
Expand All @@ -104,16 +103,15 @@ def __init__(self, host, port,
if connect_now:
self.connect()


def close(self):
'''\
'''
Close connection to the server
'''
self._socket.close()
self._socket = None

def connect_basic(self):
'''\
'''
Create connection to the host and port specified in __init__().
:raise: `NetworkError`
'''
Expand All @@ -136,7 +134,7 @@ def handshake(self):
self.authenticate(self.user, self.password)

def connect(self):
'''\
'''
Create connection to the host and port specified in __init__().
Usually there is no need to call this method directly,
since it is called when you create an `Connection` instance.
Expand All @@ -161,7 +159,7 @@ def _recv(self, to_read):
tmp = self._socket.recv(to_read)
if not tmp:
raise NetworkError(socket.error(errno.ECONNRESET,
"Lost connection to server during query"))
"Lost connection to server during query"))
to_read -= len(tmp)
buf += tmp
return buf
Expand All @@ -179,7 +177,7 @@ def _read_response(self):
return self._recv(length)

def _send_request_wo_reconnect(self, request):
'''\
'''
:rtype: `Response` instance

:raise: NetworkError
Expand All @@ -200,7 +198,7 @@ def _send_request_wo_reconnect(self, request):
raise DatabaseError(response.return_code, response.return_message)

def _opt_reconnect(self):
'''\
'''
Check that connection is alive using low-level recv from libc(ctypes)
**Due to bug in python - timeout is internal python construction.
'''
Expand All @@ -210,7 +208,7 @@ def _opt_reconnect(self):
def check(): # Check that connection is alive
buf = ctypes.create_string_buffer(2)
self._sys_recv(self._socket.fileno(), buf, 1,
socket.MSG_DONTWAIT | socket.MSG_PEEK)
socket.MSG_DONTWAIT | socket.MSG_PEEK)
if ctypes.get_errno() == errno.EAGAIN:
ctypes.set_errno(0)
return errno.EAGAIN
Expand All @@ -234,7 +232,8 @@ def check(): # Check that connection is alive
warn("Reconnect attempt %d of %d" %
(attempt, self.reconnect_max_attempts), NetworkWarning)
if attempt == self.reconnect_max_attempts:
raise NetworkError(socket.error(last_errno, errno.errorcode[last_errno]))
raise NetworkError(
socket.error(last_errno, errno.errorcode[last_errno]))
attempt += 1

self.handshake()
Expand All @@ -245,7 +244,7 @@ def check(): # Check that connection is alive
self._socket.settimeout(self.socket_timeout)

def _send_request(self, request):
'''\
'''
Send the request to the server through the socket.
Return an instance of `Response` class.

Expand All @@ -266,7 +265,7 @@ def flush_schema(self):
self.schema.flush()

def call(self, func_name, *args):
'''\
'''
Execute CALL request. Call stored Lua function.

:param func_name: stored Lua function name
Expand All @@ -287,7 +286,7 @@ def call(self, func_name, *args):
return response

def eval(self, expr, *args):
'''\
'''
Execute EVAL request. Eval Lua expression.

:param expr: Lua expression
Expand All @@ -307,7 +306,6 @@ def eval(self, expr, *args):
response = self._send_request(request)
return response


def replace(self, space_name, values):
'''
Execute REPLACE request.
Expand All @@ -327,12 +325,12 @@ def replace(self, space_name, values):
return self._send_request(request)

def authenticate(self, user, password):
self.user = user;
self.user = user
self.password = password
if not self._socket:
return self._opt_reconnect()

request = RequestAuthenticate(self, self._salt, self.user, \
request = RequestAuthenticate(self, self._salt, self.user,
self.password)
return self._send_request_wo_reconnect(request)

Expand All @@ -341,21 +339,21 @@ def join(self, server_uuid):
resp = self._send_request(request)
while True:
yield resp
if resp.code == REQUEST_TYPE_OK or \
resp.code >= REQUEST_TYPE_ERROR:
if resp.code == REQUEST_TYPE_OK or resp.code >= REQUEST_TYPE_ERROR:
return
resp = Response(self, self._read_response())
self.close() # close connection after JOIN
self.close() # close connection after JOIN

def subscribe(self, cluster_uuid, server_uuid, vclock = {}):
def subscribe(self, cluster_uuid, server_uuid, vclock={}):
# FIXME rudnyh: ^ 'vclock={}'? really? sure?
request = RequestSubscribe(self, cluster_uuid, server_uuid, vclock)
resp = self._send_request(request)
while True:
yield resp
if resp.code >= REQUEST_TYPE_ERROR:
return
resp = Response(self, self._read_response())
self.close() # close connection after SUBSCRIBE
self.close() # close connection after SUBSCRIBE

def insert(self, space_name, values):
'''
Expand All @@ -376,7 +374,7 @@ def insert(self, space_name, values):
return self._send_request(request)

def delete(self, space_name, key, **kwargs):
'''\
'''
Execute DELETE request.
Delete single record identified by `key` (using primary index).

Expand All @@ -398,7 +396,7 @@ def delete(self, space_name, key, **kwargs):
return self._send_request(request)

def update(self, space_name, key, op_list, **kwargs):
'''\
'''
Execute UPDATE request.
Update single record identified by `key` (using primary index).

Expand Down Expand Up @@ -428,7 +426,7 @@ def update(self, space_name, key, op_list, **kwargs):
return self._send_request(request)

def ping(self, notime=False):
'''\
'''
Execute PING request.
Send empty request and receive empty response from server.

Expand All @@ -438,15 +436,15 @@ def ping(self, notime=False):

request = RequestPing(self)
t0 = time.time()
response = self._send_request(request)
self._send_request(request)
t1 = time.time()

if notime:
return "Success"
return t1 - t0

def select(self, space_name, key=None, **kwargs):
'''\
'''
Execute SELECT request.
Select and retrieve data from the database.

Expand Down Expand Up @@ -500,13 +498,13 @@ def select(self, space_name, key=None, **kwargs):
space_name = self.schema.get_space(space_name).sid
if isinstance(index_name, basestring):
index_name = self.schema.get_index(space_name, index_name).iid
request = RequestSelect(
self, space_name, index_name, key, offset, limit, iterator_type)
request = RequestSelect(self, space_name, index_name, key, offset,
limit, iterator_type)
response = self._send_request(request)
return response

def space(self, space_name):
'''\
'''
Create `Space` instance for particular space

`Space` instance encapsulates the identifier of the space and provides
Expand Down
30 changes: 15 additions & 15 deletions tarantool/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,28 @@
REQUEST_TYPE_ERROR = 1 << 15


SPACE_SCHEMA = 272
SPACE_SPACE = 280
SPACE_INDEX = 288
SPACE_FUNC = 296
SPACE_USER = 304
SPACE_PRIV = 312
SPACE_SCHEMA = 272
SPACE_SPACE = 280
SPACE_INDEX = 288
SPACE_FUNC = 296
SPACE_USER = 304
SPACE_PRIV = 312
SPACE_CLUSTER = 320

INDEX_SPACE_PRIMARY = 0
INDEX_SPACE_NAME = 2
INDEX_SPACE_NAME = 2
INDEX_INDEX_PRIMARY = 0
INDEX_INDEX_NAME = 2
INDEX_INDEX_NAME = 2

ITERATOR_EQ = 0
ITERATOR_EQ = 0
ITERATOR_REQ = 1
ITERATOR_ALL = 2
ITERATOR_LT = 3
ITERATOR_LE = 4
ITERATOR_GE = 5
ITERATOR_GT = 6
ITERATOR_BITSET_ALL_SET = 7
ITERATOR_BITSET_ANY_SET = 8
ITERATOR_LT = 3
ITERATOR_LE = 4
ITERATOR_GE = 5
ITERATOR_GT = 6
ITERATOR_BITSET_ALL_SET = 7
ITERATOR_BITSET_ANY_SET = 8
ITERATOR_BITSET_ALL_NOT_SET = 9

# Default value for socket timeout (seconds)
Expand Down
Loading