commit 6b53b41bc26b6d4740ae8d5e8f3856e478a99365 Author: lasseedfast Date: Thu Oct 10 10:28:05 2024 +0200 first commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..25d1b08 --- /dev/null +++ b/README.md @@ -0,0 +1,12 @@ +# env_manager + +This script initializes a connection to an ArangoDB database and retrieves all documents from the 'enviroment' collection. + +## Installation + +Clone the repository and install the package: + +```sh +git clone https://github.com/yourusername/env_manager.git +cd env_manager +pip install . \ No newline at end of file diff --git a/env_manager.py b/env_manager.py new file mode 100644 index 0000000..f7b563a --- /dev/null +++ b/env_manager.py @@ -0,0 +1,65 @@ +""" +env_manager.py + +This script initializes a connection to an ArangoDB database and retrieves all documents from the 'enviroment' collection. +It attempts to load environment variables from a .env file to get the ArangoDB password. If the .env file is not found, +it prompts the user to input the password manually. It then connects to the ArangoDB instance using the provided credentials +and returns the first document from the 'enviroment' collection. + +Modules: + os: Provides a way of using operating system dependent functionality. + arango: Provides a client for interacting with ArangoDB. + dotenv: Loads environment variables from a .env file. + +Functions: + get_env: Initializes a connection to an ArangoDB database and retrieves the first document from the 'enviroment' collection. + +Usage: + Run this script to connect to the ArangoDB database and print the first document from the 'enviroment' collection. +""" + +import os +try: + from arango import ArangoClient +except ModuleNotFoundError: + import subprocess + subprocess.run(["pip", "install", "python-arango"]) + from arango import ArangoClient +try: + from dotenv import load_dotenv +except ModuleNotFoundError: + import subprocess + subprocess.run(["pip", "install", "python-dotenv"]) + from dotenv import load_dotenv + +def get_env(): + """ + Initializes a connection to an ArangoDB database and retrieves all documents from the 'enviroment' collection. + + This function attempts to load environment variables from a .env file to get the ArangoDB password. If the .env file + is not found, it prompts the user to input the password manually. It then connects to the ArangoDB instance using + the provided credentials and returns the first document from the 'enviroment' collection. + + Returns: + dict: The first document from the 'enviroment' collection in the ArangoDB database. + + Raises: + FileNotFoundError: If the .env file is not found. + KeyError: If the 'ARANGO_PWD_ENV_MANAGER' environment variable is not set. + """ + try: + load_dotenv(".env") + pwd = os.environ['ARANGO_PWD_ENV_MANAGER'] + except FileNotFoundError: + print("No .env file found.") + pwd = input("Enter the ArangoDB password for the user 'env_manager': ") + host ="https://arango.lasseedfast.se" + username = "env_manager" + db='div' + + # Initialize the database for ArangoDB. + db = ArangoClient(hosts=host).db(db, username=username, password=pwd) + return db.collection('enviroment').all().next() + +env = get_env() +print(env) \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..25d1609 --- /dev/null +++ b/setup.py @@ -0,0 +1,16 @@ +from setuptools import setup, find_packages + +setup( + name='env_manager', + version='0.1', + packages=find_packages(), + install_requires=[ + 'python-arango', + 'python-dotenv' + ], + entry_points={ + 'console_scripts': [ + 'env_manager=env_manager:main', + ], + }, +) \ No newline at end of file