Skip to content

Commit

Permalink
simplify config override
Browse files Browse the repository at this point in the history
  • Loading branch information
kizniche committed Oct 10, 2024
1 parent 0007f23 commit f9abdc3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 24 deletions.
3 changes: 0 additions & 3 deletions alembic_db/alembic/versions/ef49f6644e0c_save_user_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

from alembic_db.alembic_post_utils import write_revision_post_alembic

from mycodo.config import SQL_DATABASE_MYCODO
from mycodo.config import ID_FILE
from mycodo.config import STATS_CSV

Expand All @@ -24,8 +23,6 @@
branch_labels = None
depends_on = None

MYCODO_DB_PATH = f'sqlite:///{SQL_DATABASE_MYCODO}'


def upgrade():
write_revision_post_alembic(revision)
Expand Down
27 changes: 9 additions & 18 deletions mycodo/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,16 @@
# config.py - Global Mycodo settings
#
import binascii
import sys
import os
import subprocess
import sys
from datetime import timedelta

import os
from flask_babel import lazy_gettext as lg

# Append proper path for other software reading this config file
sys.path.append(os.path.abspath(os.path.dirname(__file__)))

from config_translations import TRANSLATIONS as T
try:
import config_override
except:
pass

MYCODO_VERSION = '8.16.0'
ALEMBIC_VERSION = '5966b3569c89'
Expand All @@ -40,25 +35,21 @@
# Install path (the parent directory of this file)
INSTALL_DIRECTORY = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + '/..')

# Database
# Settings database
DATABASE_PATH = os.path.join(INSTALL_DIRECTORY, 'databases')
DATABASE_NAME = "mycodo.db"
SQL_DATABASE_MYCODO = os.path.join(DATABASE_PATH, DATABASE_NAME)
ALEMBIC_PATH = os.path.join(INSTALL_DIRECTORY, 'alembic_db')
ALEMBIC_UPGRADE_POST = os.path.join(ALEMBIC_PATH, 'alembic_post_upgrade_versions')

try:
import config_override
MYCODO_DB_PATH = config_override.MYCODO_DB_PATH
except:
MYCODO_DB_PATH = f'sqlite:///{SQL_DATABASE_MYCODO}'

# Alembic
ALEMBIC_PATH = os.path.join(INSTALL_DIRECTORY, 'alembic_db')
ALEMBIC_UPGRADE_POST = os.path.join(ALEMBIC_PATH, 'alembic_post_upgrade_versions')
try: # Ensure the correct alembic url is set
ALEMBIC_URL = config_override.ALEMBIC_URL
cmd = f'/opt/Mycodo/env/bin/crudini --set /opt/Mycodo/alembic_db/alembic.ini alembic sqlalchemy.url {ALEMBIC_URL}'
subprocess.Popen(cmd, shell=True)
except:
cmd = '/opt/Mycodo/env/bin/crudini --set /opt/Mycodo/alembic_db/alembic.ini alembic sqlalchemy.url sqlite:///../databases/mycodo.db'
subprocess.Popen(cmd, shell=True)
cmd = f'/opt/Mycodo/env/bin/crudini --set /opt/Mycodo/alembic_db/alembic.ini alembic sqlalchemy.url {MYCODO_DB_PATH}'
subprocess.Popen(cmd, shell=True)

# Misc paths
PATH_1WIRE = '/sys/bus/w1/devices/'
Expand Down
2 changes: 1 addition & 1 deletion mycodo/functions/example_function_all_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ def loop(self):
# Deactivate controller in the SQL database
self.logger.debug(
"Deactivating (SQL) Custom controller select_device_2 with ID {}".format(self.select_device_2_id))
from mycodo.config import MYCODO_DB_PATH, SQL_DATABASE_MYCODO
from mycodo.config import MYCODO_DB_PATH
from mycodo.databases.utils import session_scope
with session_scope(MYCODO_DB_PATH) as new_session:
mod_cont = new_session.query(CustomController).filter(
Expand Down
8 changes: 6 additions & 2 deletions mycodo/utils/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,12 @@ def create_settings_export(save_path=None):
try:
data = io.BytesIO()
with zipfile.ZipFile(data, mode='w') as z:
z.write(SQL_DATABASE_MYCODO,
os.path.basename(SQL_DATABASE_MYCODO))
try:
z.write(SQL_DATABASE_MYCODO,
os.path.basename(SQL_DATABASE_MYCODO))
except:
logger.error(f"Could not find database file {SQL_DATABASE_MYCODO}")

export_directories = [
(PATH_FUNCTIONS_CUSTOM, "custom_functions"),
(PATH_ACTIONS_CUSTOM, "custom_actions"),
Expand Down

0 comments on commit f9abdc3

Please sign in to comment.