Skip to content

Commit

Permalink
Revamp the entire library for 4.0.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
joowani committed Apr 30, 2018
1 parent 390e9cd commit 4fc0800
Show file tree
Hide file tree
Showing 81 changed files with 9,827 additions and 6,029 deletions.
23 changes: 11 additions & 12 deletions .travis.yml
@@ -1,23 +1,22 @@
language: python
sudo: false
language: python
python:
- 2.7
- 3.4
- 3.5
- 3.6
- 3.7
services:
- docker
before_install:
- sh scripts/setup_arangodb_travis.sh
- docker run --name arango -d -p 8529:8529 -e ARANGO_ROOT_PASSWORD=passwd arangodb/arangodb:3.3.8
- docker cp tests/static/service.zip arango:/tmp/service.zip
install:
- pip install coverage
- pip install pytest
- pip install pytest-cov
- pip install python-coveralls
- python setup.py install
- pip install flake8 mock pytest pytest-cov python-coveralls sphinx
- pip install .
script:
- if [[ $TRAVIS_PYTHON_VERSION == 2.7 || $TRAVIS_PYTHON_VERSION == 3.4 ]];
then py.test --cov=arango --ignore=tests/test_asyncio.py;
else py.test --cov=arango;
fi
- python -m flake8
- python -m sphinx -b doctest docs build
- py.test --complete -s -v --cov=arango
after_success:
- coveralls
- pkill -9 -f arango
1 change: 1 addition & 0 deletions MANIFEST.in
@@ -0,0 +1 @@
include README.rst LICENSE
68 changes: 38 additions & 30 deletions README.rst
Expand Up @@ -38,16 +38,17 @@ Welcome to the GitHub page for **python-arango**, a Python driver for
Features
========

- Clean, Pythonic interface
- Clean and Pythonic interface
- Lightweight
- High ArangoDB REST API coverage
- 95%+ ArangoDB REST API coverage

Compatibility
=============

- Python versions 2.7.x, 3.4.x, 3.5.x and 3.6.x are supported
- Latest version of python-arango (3.x) supports ArangoDB 3.x only
- Older versions of python-arango support ArangoDB 1.x ~ 2.x only
- Python versions 2.7, 3.4, 3.5 and 3.6 are supported
- python-arango 4.x supports ArangoDB 3.3+ (recommended)
- python-arango 3.x supports ArangoDB 3.0 ~ 3.2 only
- python-arango 2.x supports ArangoDB 1.x ~ 2.x only

Installation
============
Expand Down Expand Up @@ -80,32 +81,31 @@ Here is a simple usage example:
from arango import ArangoClient
# Initialize the client for ArangoDB
client = ArangoClient(
protocol='http',
host='localhost',
port=8529,
username='root',
password='',
enable_logging=True
)
client = ArangoClient(protocol='http', host='localhost', port=8529)
# Connect to "_system" database as root user.
system = client.db('_system', username='root', password='passwd')
# Create a new database named "school"
system.create_database('school')
# Create a new database named "my_database"
db = client.create_database('my_database')
# Connect to "school" database as root user.
db = client.db('school', username='root', password='passwd')
# Create a new collection named "students"
students = db.create_collection('students')
# Create a new collection named "teachers"
teachers = db.create_collection('teachers')
# Add a hash index to the collection
students.add_hash_index(fields=['name'], unique=True)
teachers.add_hash_index(fields=['name'], unique=True)
# Insert new documents into the collection
students.insert({'name': 'jane', 'age': 19})
students.insert({'name': 'josh', 'age': 18})
students.insert({'name': 'jake', 'age': 21})
teachers.insert({'name': 'jane', 'age': 19})
teachers.insert({'name': 'josh', 'age': 18})
teachers.insert({'name': 'jake', 'age': 21})
# Execute an AQL query
result = db.aql.execute('FOR s IN students RETURN s')
print([student['name'] for student in result])
cursor = db.aql.execute('FOR s IN teachers RETURN s')
print([document['name'] for document in cursor])
Here is another example involving graphs:
Expand All @@ -114,23 +114,31 @@ Here is another example involving graphs:
from arango import ArangoClient
client = ArangoClient()
client = ArangoClient(protocol='http', host='localhost', port=8529)
# Create a new graph
graph = client.db('my_database').create_graph('my_graph')
# Connect to "school" database as root user.
db = client.db('school', username='root', password='passwd')
# Create a new graph named "registration"
graph = db.create_graph('registration')
# Create vertex collections for the graph
students = graph.create_vertex_collection('students')
courses = graph.create_vertex_collection('courses')
# Create edge definition for the graph
takes = graph.create_edge_definition(
name='takes',
from_collections=['students'],
to_collections=['courses']
)
# Insert vertices
# Insert "from" vertices
students.insert({'_key': '01', 'full_name': 'Anna Smith'})
students.insert({'_key': '02', 'full_name': 'Jake Clark'})
students.insert({'_key': '03', 'full_name': 'Lisa Jones'})
# Insert "to" vertices
courses.insert({'_key': 'MAT101', 'title': 'Calculus'})
courses.insert({'_key': 'STA101', 'title': 'Statistics'})
courses.insert({'_key': 'CSC101', 'title': 'Algorithms'})
Expand All @@ -144,19 +152,19 @@ Here is another example involving graphs:
takes.insert({'_from': 'students/03', '_to': 'courses/CSC101'})
# Traverse the graph in outbound direction, breadth-first
traversal_results = graph.traverse(
result = graph.traverse(
start_vertex='students/01',
strategy='bfs',
direction='outbound'
)
print(traversal_results['vertices'])
print(result['vertices'])
Refer to the full `API documentation`_ for more details!

Contributing
============

Please have a look at this page_ before submitting a pull request. Thanks!
Please take a look at this page_ before submitting a pull request. Thanks!

.. _API documentation:
http://python-driver-for-arangodb.readthedocs.io/en/master/index.html
Expand Down
4 changes: 3 additions & 1 deletion arango/__init__.py
@@ -1 +1,3 @@
from arango.client import ArangoClient
from arango.client import ArangoClient # noqa: F401
from arango.exceptions import * # noqa: F401 F403
from arango.http import * # noqa: F401 F403
71 changes: 30 additions & 41 deletions arango/api.py
@@ -1,71 +1,60 @@
from __future__ import absolute_import, unicode_literals

__all__ = ['APIWrapper']


class APIWrapper(object):
"""API wrapper which facilitates access to the executor.
"""API wrapper base class.
This is a utility class which wraps the API executor.
:param connection: HTTP connection.
:type connection: arango.connection.Connection
:param executor: API executor.
:type executor: arango.api.APIExecutor
:type executor: arango.executor.DefaultExecutor
"""

def __init__(self, connection, executor):
self._conn = connection
self._executor = executor
self._context = {
'APIExecutor': 'default',
'AsyncExecutor': 'async',
'BatchExecutor': 'batch',
'TransactionExecutor': 'transaction',
}[self._executor.__class__.__name__]
self._is_transaction = self.context == 'transaction'

@property
def context(self):
"""Return the API execution context (e.g. "async", "batch").
def db_name(self):
"""Return the database name.
:return: API execution context.
:rtype: str or unicode
:return: Database name.
"""
return self._context
return self._conn.db_name

def _execute(self, request, response_handler):
"""Execute an API call.
@property
def username(self):
"""Return the username.
:param request: HTTP request.
:type request: arango.request.Request
:param response_handler: HTTP response handler.
:type response_handler: callable
:return: API execution result.
:rtype: dict or list or int or bool or str or unicode
:returns: Username.
:rtype: str | unicode
"""
return self._executor.execute(self._conn, request, response_handler)

return self._conn.username

class APIExecutor(object):
"""Base API executor.
@property
def context(self):
"""Return the API execution context.
API executor dictates how an API request is executed depending on the
context (e.g. async, batch). See :class:`arango.async.AsyncExecutor`
or :class:`arango.batch.BatchExecutor` for more examples.
"""
Possible values are "default", "async", "batch" and "transaction".
def execute(self, connection, request, response_handler):
"""Execute an API request.
:return: API execution context.
:rtype: str | unicode
"""
return self._executor.context

This method is meant to be overridden by subclasses and its behaviour
re-defined depending on the execution context (e.g. async, batch). For
more concrete examples, see :class:`arango.async.AsyncExecutor` or
:class:`arango.batch.BatchExecutor`.
def _execute(self, request, response_handler):
"""Execute an API according to the execution context.
:param connection: HTTP connection.
:type connection: arango.connection.Connection
:param request: HTTP request.
:type request: arango.request.Request
:param response_handler: HTTP response handler.
:type response_handler: callable
:return: API call result or its future.
:rtype: bool or list or dict or arango.connection.Future
:return: API execution result.
:rtype: str | unicode | bool | int | list | dict
"""
response = connection.send_request(request)
return response_handler(response)
return self._executor.execute(request, response_handler)

0 comments on commit 4fc0800

Please sign in to comment.