From 6271fd3a666b931faf1a386c92f07060a19fb690 Mon Sep 17 00:00:00 2001 From: lasseedfast Date: Fri, 19 Sep 2025 08:04:42 +0200 Subject: [PATCH] Initial commit --- README.md | 50 +++++++++++++++++++++++++++++ __init__.py | 1 + arango.py | 83 ++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 3 ++ setup.py | 14 ++++++++ 5 files changed, 151 insertions(+) create mode 100644 README.md create mode 100644 __init__.py create mode 100644 arango.py create mode 100644 requirements.txt create mode 100644 setup.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..0e36679 --- /dev/null +++ b/README.md @@ -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 + ``` + diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..cda57b5 --- /dev/null +++ b/__init__.py @@ -0,0 +1 @@ +from .arango import arango diff --git a/arango.py b/arango.py new file mode 100644 index 0000000..42935d8 --- /dev/null +++ b/arango.py @@ -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") diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..ab6a5f9 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +arango +python-dotenv +env_manager @ git+https://github.com/lasseedfast/env_manager.git diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..f14d5f7 --- /dev/null +++ b/setup.py @@ -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", +)