Source code for vdat.database.core

# Virus Data Analysis Tool: a data reduction GUI for HETDEX/VIRUS data
# Copyright (C) 2015, 2016, 2017  "The HETDEX collaboration"
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
"""Database management for VDAT
"""
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import os

from pyhetdex.tools.db_helpers import SQLiteConnector

from .base import database


DB_NAME = 'vdat.db'
MEMORY_DB = ':memory:'


# initialise the database
[docs]def init(redux_dir=None, remove_old=True): """Initialise the database Parameters ---------- redux_dir : string, optional if given, create a physical database, otherwise creates it in memory remove_old : bool, optional drop existing tables if true """ if redux_dir is None: location = MEMORY_DB else: location = os.path.join(redux_dir, DB_NAME) # initialise and create the database database.init(location) with connect(): # create the tables import vdat.database.models as m if remove_old and m.VDATDir.table_exists(): m.VDATDir.drop_table() if not m.VDATDir.table_exists(): m.VDATDir.create_table() if remove_old and m.VDATExposures.table_exists(): m.VDATExposures.drop_table() if not m.VDATExposures.table_exists(): m.VDATExposures.create_table()
connect = SQLiteConnector(database, keep_open=MEMORY_DB)