Initial commit

main
lasseedfast 5 months ago
commit 6271fd3a66
  1. 50
      README.md
  2. 1
      __init__.py
  3. 83
      arango.py
  4. 3
      requirements.txt
  5. 14
      setup.py

@ -0,0 +1,50 @@
# arango
A simple Python wrapper for connecting to and interacting with ArangoDB.
## Installation
Install dependencies:
```bash
pip install -r requirements.txt
```
You also need to install `env_manager` to help set up environment variables for ArangoDB:
```bash
pip install git+https://github.com/lasseedfast/env_manager.git
```
## Usage
Environment variables:
- `ARANGO_HOST`
- `ARANGO_DB`
- `ARANGO_USERNAME`
- `ARANGO_PWD`
Tthe package will use `env_manager` to set them automatically (first time you may need to provide a password).
Example:
```python
from _arango import arango
results = arango.execute_aql("FOR doc IN my_collection RETURN doc")
```
## Using as a Base for Your Own Project
If you want to use this package as a starting point for your own ArangoDB utilities, you can clone the repository:
```bash
git clone https://git.edfast.se/lasse/arango.git
```
You can then add your own methods and modify the code as needed. If you do not plan to sync changes back to the original repository, consider to remove or change the remote origin to avoid accidentally pushing changes to the original repo:
```bash
git remote remove origin
```

@ -0,0 +1 @@
from .arango import arango

@ -0,0 +1,83 @@
from arango import ArangoClient
from dotenv import load_dotenv
import os
import re
from typing import Optional
load_dotenv()
if 'ARANGO_HOST' not in os.environ:
import env_manager
env_manager.set_env()
class Arango:
"""
Provides a simple interface for connecting to and interacting with an ArangoDB database.
Environment variables required:
- ARANGO_HOST: The host URL of the ArangoDB server.
- ARANGO_DB: The name of the database to connect to.
- ARANGO_USERNAME: Username for authentication.
- ARANGO_PWD: Password for authentication.
"""
def __init__(self):
"""
Initializes the ArangoDB client and connects to the specified database using environment variables.
"""
self.client = ArangoClient(hosts=os.environ.get("ARANGO_HOST"))
self.db = self.client.db(
os.environ.get("ARANGO_DB"),
username=os.environ.get("ARANGO_USERNAME"),
password=os.environ.get("ARANGO_PWD"),
)
def fix_key(self, _key: str) -> str:
"""
Sanitizes a given key for use in ArangoDB by replacing disallowed characters with underscores.
Allowed characters: alphanumeric, underscore, hyphen, dot, at symbol, parentheses, plus, equals, semicolon,
dollar sign, asterisk, single quote, percent, or colon.
Args:
_key (str): The key to be sanitized.
Returns:
str: The sanitized key.
"""
return re.sub(r"[^A-Za-z0-9_\-\.@()+=;\$!*\'%:]", "_", _key)
def clear_collections(self):
"""
Truncates (empties) all non-system collections in the connected database.
System collections (names starting with '_') are skipped.
"""
for db in self.db.collections():
if not db["name"].startswith("_"):
col = self.db.collection(db["name"])
col.truncate()
print(f"Truncated collection: {db['name']}")
def execute_aql(self, query: str, bind_vars: Optional[dict] = None, batch_size: Optional[int] = None) -> list[dict]:
"""
Executes an AQL (Arango Query Language) query and returns the results as a list of dictionaries.
Args:
query (str): The AQL query string.
bind_vars (Optional[dict]): Optional dictionary of bind variables for the query.
batch_size (Optional[int]): Optional batch size for fetching results.
Returns:
list[dict]: The query results.
"""
cursor = self.db.aql.execute(query, bind_vars=bind_vars or {}, batch_size=batch_size)
return list(cursor)
arango = Arango()
if __name__ == "__main__":
arango = Arango()
print("Connected to ArangoDB")

@ -0,0 +1,3 @@
arango
python-dotenv
env_manager @ git+https://github.com/lasseedfast/env_manager.git

@ -0,0 +1,14 @@
from setuptools import setup, find_packages
setup(
name="arango",
version="0.1.0",
description="Simple ArangoDB client wrapper",
author="Your Name",
packages=find_packages(),
install_requires=[
"arango",
"python-dotenv"
],
python_requires=">=3.7",
)
Loading…
Cancel
Save