Exemple #1
0
def load_sql_settings():
    global config
    # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    # Get SQL settings
    # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    if is_sql_connection_defined():
        script_dir = os.path.dirname(__file__)
        rel_path = "preferences/sql_connection.cnf"
        abs_file_path = os.path.join(script_dir, rel_path)

        with open(abs_file_path, 'r') as document:
            config = {}
            for line in document:
                line = line.split()
                if not line:
                    continue
                try:
                    config[line[0]] = line[1:][0]
                except:
                    config[line[0]] = ''

        if 'user' not in list(config):
            config['user'] = ''
            config['password'] = ''

    else:
        config = {
            'host': 'localhost',
            'dbname': 'dvh',
            'port': '5432',
            'user': '',
            'password': ''
        }
def test_import_sql_cnx_definitions():
    if not is_import_settings_defined() and not is_sql_connection_defined():
        print("ERROR: Import and SQL settings are not yet defined.",
              "Please run:\n",
              "    $ dvh settings_simple",
              sep='')
    elif not is_import_settings_defined():
        print("ERROR: Import settings are not yet defined.",
              "Please run:\n",
              "    $ dvh settings_simple --dir",
              sep='')
    elif not is_sql_connection_defined():
        print("ERROR: Invalid or empty SQL settings.",
              "Please run:\n",
              "    $ dvh settings_simple --sql",
              sep='')
    else:
        return True

    return False
Exemple #3
0
def load_sql_settings():
    global config
    # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    # Get SQL settings
    # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    if is_sql_connection_defined():
        config = parse_settings_file(get_settings('sql'))

        if 'user' not in list(config):
            config['user'] = ''
            config['password'] = ''

        if 'password' not in list(config):
            config['password'] = ''

    else:
        config = {'host': 'localhost',
                  'dbname': 'dvh',
                  'port': '5432',
                  'user': '',
                  'password': ''}
Exemple #4
0
from __future__ import print_function
from dicom_to_sql import dicom_to_sql
from utilities import Temp_DICOM_FileSet
from sql_connector import DVH_SQL
from analysis_tools import DVH
from utilities import is_import_settings_defined, is_sql_connection_defined,\
    write_import_settings, write_sql_connection_settings, validate_import_settings, validate_sql_connection
import os
from getpass import getpass
import argparse
from subprocess import call

script_dir = os.path.dirname(__file__)

if is_sql_connection_defined():
    try:
        DVH_SQL().initialize_database()
    except:
        print("Warning: could not initialize SQL database")


def settings(**kwargs):
    if not kwargs:
        set_import_settings()
        set_sql_connection_parameters()
    else:
        if 'dir' in kwargs and kwargs['dir']:
            set_import_settings()
        if 'sql' in kwargs and kwargs['sql']:
            set_sql_connection_parameters()
Exemple #5
0
def test_dvh_code():

    if not is_import_settings_defined() and not is_sql_connection_defined():
        print("ERROR: Import and SQL settings are not yet defined.",
              "Please run:\n",
              "    $ dvh settings_simple",
              sep='')
    elif not is_import_settings_defined():
        print("ERROR: Import settings are not yet defined.",
              "Please run:\n",
              "    $ dvh settings_simple --dir",
              sep='')
    elif not is_sql_connection_defined():
        print("ERROR: Invalid or empty SQL settings.",
              "Please run:\n",
              "    $ dvh settings_simple --sql",
              sep='')
    else:
        is_import_valid = validate_import_settings()
        is_sql_connection_valid = validate_sql_connection()
        if not is_import_valid and not is_sql_connection_valid:
            print(
                "ERROR: Create the directories listed above or input valid directories.\n",
                "ERROR: Cannot connect to SQL.\n",
                "Please run:\n    $ dvh settings",
                sep='')
        elif not is_import_valid:
            print(
                "ERROR: Create the directories listed above or input valid directories by running:\n",
                "    $ dvh settings --dir",
                sep='')
        elif not is_sql_connection_valid:
            print(
                "ERROR: Cannot connect to SQL.\n",
                "Verify database is active and/or update SQL connection information with:\n",
                "    $ dvh settings --sql",
                sep='')

        else:
            print("Importing test files")
            dicom_to_sql(start_path="test_files/",
                         organize_files=False,
                         move_files=False,
                         force_update=False)

            print("Reading data from SQL DB with analysis_tools.py")
            test = DVH()

            print(
                "Reading dicom information from test files with utilities.py (for plan review module)"
            )
            test_files = Temp_DICOM_FileSet(start_path="test_files/")

            print("Deleting test data from SQL database")
            for i in range(0, test_files.count):
                cond_str = "mrn = '" + test_files.mrn[i]
                cond_str += "' and study_instance_uid = '" + test_files.study_instance_uid[
                    i] + "'"
                DVH_SQL().delete_rows(cond_str)

            print("Tests successful!")