summaryrefslogtreecommitdiff
path: root/database/prefabs.py
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2022-04-04 17:00:31 +0200
committerGitHub <noreply@github.com>2022-04-04 17:00:31 +0200
commit38769373c7e89783d33849283586bfa0b62e8251 (patch)
tree4fda128ee6b30018c1aa14c584cc53ade80e67f7 /database/prefabs.py
parent6021aa4278bebb34bf5603ead4b5daeabcdc4c19 (diff)
parent527ae2230f5c2dd22f496f45d5d8e3bd4acdb854 (diff)
merge: Migrate to Quarkus-based web API
This pull request changes the web API to a Quarkus-based version. Currently, the OpenDC web API is written in Python (using Flask). Although Python is a powerful language to develop web services, having another language next to Kotlin/Java and JavaScript introduces some challenges. For instance, the web API and UI lack integration with our Gradle-based build pipeline and require additional steps from the developer to start working with. Furthermore, deploying OpenDC requires having Python installed in addition to the JVM. By converting the web API into a Quarkus application, we can enjoy further integration with our Gradle-based build pipeline and simplify the development/deployment process of OpenDC, by requiring only the JVM and Node to work with OpenDC. ## Implementation Notes :hammer_and_pick: * Move build dependencies into version catalog * Design unified communication protocol * Add Quarkus API implementation * Add new web client implementation * Update runner to use new web client * Fix compatibility with React.js UI * Remove Python build steps from CI pipeline * Update Docker deployment for new web API * Remove obsolete database configuration ## External Dependencies :four_leaf_clover: * Quarkus ## Breaking API Changes :warning: * The new web API only supports SQL-based databases for storing user-data, as opposed to MongoDB currently. We intend to use H2 for development and Postgres for production.
Diffstat (limited to 'database/prefabs.py')
-rwxr-xr-xdatabase/prefabs.py122
1 files changed, 0 insertions, 122 deletions
diff --git a/database/prefabs.py b/database/prefabs.py
deleted file mode 100755
index ed308d84..00000000
--- a/database/prefabs.py
+++ /dev/null
@@ -1,122 +0,0 @@
-# encoding: utf-8
-"""
-prefabs
-
-Python Library for interacting with mongoDB prefabs collection.
-
-"""
-import urllib.parse
-import pprint
-import sys
-import os
-import json
-import re
-import ujson
-#import pyyaml
-
-from pymongo import MongoClient
-from bson.json_util import loads, dumps, RELAXED_JSON_OPTIONS, CANONICAL_JSON_OPTIONS
-
-#mongodb_opendc_db = os.environ['OPENDC_DB']
-#mongodb_opendc_user = os.environ['OPENDC_DB_USERNAME']
-#mongodb_opendc_password = os.environ['OPENDC_DB_PASSWORD']
-
-#if mongodb_opendc_db == None or mongodb_opendc_user == None or mongodb_opendc_password == None:
-# print("One or more environment variables are not set correctly. \nYou may experience issues connecting to the mongodb database.")
-
-user = urllib.parse.quote_plus('opendc') #TODO: replace this with environment variable
-password = urllib.parse.quote_plus('opendcpassword') #TODO: same as above
-database = urllib.parse.quote_plus('opendc')
-
-client = MongoClient('mongodb://%s:%s@localhost/default_db?authSource=%s' % (user, password, database))
-opendcdb = client.opendc
-prefabs_collection = opendcdb.prefabs
-
-
-def add(prefab_file, name):
- if(re.match(r"\w+(\\\ \w*)*\.json", prefab_file)):
- try:
- with open(prefab_file, "r") as json_file:
- json_prefab = json.load(json_file)
- #print(json_prefab)
- if name != None:
- json_prefab["name"] = name
- try:
- prefab_id = prefabs_collection.insert(json_prefab)
- except ConnectionFailure:
- print("ERROR: Could not connect to the mongoDB database.")
- except DuplicateKeyError:
- print("ERROR: A prefab with the same unique ID already exists in the database. \nPlease remove the '_id' before trying again.\nYour prefab has not been imported.")
- except:
- print("ERROR: A general error has occurred. Your prefab has not been imported.")
- if prefab_id != None:
- if name != None:
- print(f'Prefab "{name}" has been imported successfully.')
- else:
- print(f'Prefab "{prefab_file}" has been imported successfully.')
- except FileNotFoundError:
- print(f"ERROR: {prefab_file} could not be found in the specified path. No prefabs have been imported.")
- elif(re.match(r"\w+(\\\ \w*)*\.yml", prefab_file)):
- print("expecting a yaml file here")
- #yaml
- else:
- print("The filetype provided is an unsupported filetype.")
- #unsupported filetype
-
-def clone(prefab_name, new_name):
- bson = prefabs_collection.find_one({'name': prefab_name})
- json_string = dumps(bson) #convert BSON representation to JSON
- chosen_prefab = json.loads(json_string) #load as a JSON object
-
- chosen_prefab.pop("_id") # clean out our _id field from the export: mongo will generate a new one if this is imported back in
-
- if new_name != None:
- chosen_prefab["name"] = new_name
- try:
- prefab_id = prefabs_collection.insert_one(chosen_prefab)
- except ConnectionFailure:
- print("ERROR: Could not connect to the mongoDB database.")
- except:
- print("ERROR: A general error has occurred. Your selected prefab has not been cloned.")
- if prefab_id != None:
- if new_name != None:
- print(f'Prefab "{prefab_name}" has been cloned successfully as {new_name}.')
- else:
- print(f'Prefab "{prefab_name}" has been cloned successfully.')
-
-def export(prefab_name, type):
- bson = prefabs_collection.find_one({'name': prefab_name})
- json_string = dumps(bson) #convert BSON representation to JSON
- chosen_prefab = json.loads(json_string) #load as a JSON object
-
- chosen_prefab.pop("_id") # clean out our _id field from the export: mongo will generate a new one if this is imported back in
-
- with open(f'{prefab_name}.json', 'w', encoding='utf8') as f:
- json.dump(chosen_prefab, f, ensure_ascii=False, indent=4)
- print(f'Prefab {prefab_name} written to {os.getcwd()}/{prefab_name}.json.')
- #pprint.pprint(json_string)
- #pprint.pprint(json.loads(str(json_string)))
-
-def list():
- #TODO: why does it output in single quotations?
- cursor = prefabs_collection.find()
- prefabs = []
- for record in cursor:
- #pprint.pprint(record)
- #print(record)
- json_string = dumps(record, json_options=RELAXED_JSON_OPTIONS) ##pymongo retrieves BSON objects, which need to be converted to json for pythons json module
- prefabs.append(json.loads(json_string))
-
- #print(f'There are {str(len(prefabs))} prefabs in the database. They are:')
- print("Name Author")
- for prefab in prefabs:
- if(prefab['visibility'] == "private"):
- continue
- print(f"{prefab['name']} {prefab['author']}")
- #pprint.pprint(prefab)
-
-
-def remove(prefab_name):
- prefabs_collection.delete_one({'name': prefab_name})
-
-