<commit_before>import os
import re
import warnings
from marvin.tools.core.exceptions import MarvinUserWarning
from marvin.utils.general.general import getDbMachine

# Inits the log
from marvin.tools.core.logger import initLog
log = initLog()

warnings.simplefilter('once')


class Config(object):
    def __init__(self):

        self._mode = 'auto'
        self._drpall = None

        self.drpver = None
        self.dapver = None
        self.mplver = None
        self.vermode = None
        self.download = False
        self.sasurl = os.getenv('SAS_URL') if 'SAS_URL' in os.environ else 'https://sas.sdss.org/'
        self._setDbConfig()
        self._checkConfig()

        self.setDefaultDrpAll()

    def setDefaultDrpAll(self, drpver=None):
        """Tries to set the default location of drpall."""

        if not drpver and not self.drpver:
            return

        self.drpall = self._getDrpAllPath(drpver=drpver)

    def _getDrpAllPath(self, drpver=None):
        """Returns the default path for drpall, give a certain `drpver`."""

        drpver = drpver if drpver else self.drpver

        if 'MANGA_SPECTRO_REDUX' in os.environ and drpver:
            return os.path.join(os.environ['MANGA_SPECTRO_REDUX'], str(drpver),
                                'drpall-{0}.fits'.format(drpver))
        else:
            return None

    @property
    def mode(self):
        return self._mode

    @mode.setter
    def mode(self, value):
        if value in ['local', 'remote', 'auto']:
            self._mode = value
        else:
            raise ValueError('config.mode must be "local" or "remote".')

    @property
    def drpall(self):
        return self._drpall

    @drpall.setter
    def drpall(self, value):
        if os.path.exists(value):
            self._drpall = value
        else:
            warnings.warn('path {0} cannot be found. Setting drpall to None.'
                          .format(value), MarvinUserWarning)

    def _setDbConfig(self):
        ''' Set the db configuration '''
        self.db = getDbMachine()

    def _checkConfig(self):
        ''' Check the config '''
        if not self.mplver or not (self.drpver and self.dapver):
            warnings.warn('No MPL or DRP/DAP version set. Setting default to MPL-4', MarvinUserWarning)
            self.setMPL('MPL-4')

    def setMPL(self, mplver):
        ''' Set the data version by MPL '''

        from marvin.utils.general import lookUpVersions

        m = re.search('MPL-([0-9])', mplver)
        assert m is not None, 'MPL version must be of form "MPL-[X]"'
        if m:
            self.mplver = mplver
            self.drpver, self.dapver = lookUpVersions(mplver)

    def setVersions(self, drpver=None, dapver=None):
        ''' Set the data version by DRPVER and DAPVER '''

        from marvin.utils.general import lookUpMpl

        if drpver:
            assert type(drpver) == str, 'drpver needs to be a string'
            drpre = re.search('v([0-9][_]([0-9])[_]([0-9]))', drpver)
            assert drpre is not None, 'DRP version must be of form "v[X]_[X]_[X]"'
            if drpre:
                self.drpver = drpver
                self.mplver = lookUpMpl(drpver)

        if dapver:
            assert type(dapver) == str, 'dapver needs to be a string'
            dapre1 = re.search('v([0-9][_]([0-9])[_]([0-9]))', dapver)
            dapre2 = re.search('([0-9][.]([0-9])[.]([0-9]))', dapver)
            assert (dapre1 or dapre2) is not None, 'DAP version must be of form "v[X]_[X]_[X]", or [X].[X].[X]'
            if any([dapre1, dapre2]):
                self.dapver = dapver

config = Config()

# Inits the Database session and ModelClasses
session = None
datadb = None
if config.db:
    try:
        from marvin.db.database import db
    except RuntimeError as e:
        print('RuntimeError raised: Problem importing db: {0}'.format(e))
    else:
        try:<commit_msg>added a config setdefault outside of config init as well<commit_after>import os
import re
import warnings
from marvin.tools.core.exceptions import MarvinUserWarning
from marvin.utils.general.general import getDbMachine

# Inits the log
from marvin.tools.core.logger import initLog
log = initLog()

warnings.simplefilter('once')


class Config(object):
    def __init__(self):

        self._mode = 'auto'
        self._drpall = None

        self.drpver = None
        self.dapver = None
        self.mplver = None
        self.vermode = None
        self.download = False
        self.sasurl = os.getenv('SAS_URL') if 'SAS_URL' in os.environ else 'https://sas.sdss.org/'
        self._setDbConfig()
        self._checkConfig()

        self.setDefaultDrpAll()

    def setDefaultDrpAll(self, drpver=None):
        """Tries to set the default location of drpall."""

        if not drpver and not self.drpver:
            return

        self.drpall = self._getDrpAllPath(drpver=drpver)

    def _getDrpAllPath(self, drpver=None):
        """Returns the default path for drpall, give a certain `drpver`."""

        drpver = drpver if drpver else self.drpver

        if 'MANGA_SPECTRO_REDUX' in os.environ and drpver:
            return os.path.join(os.environ['MANGA_SPECTRO_REDUX'], str(drpver),
                                'drpall-{0}.fits'.format(drpver))
        else:
            return None

    @property
    def mode(self):
        return self._mode

    @mode.setter
    def mode(self, value):
        if value in ['local', 'remote', 'auto']:
            self._mode = value
        else:
            raise ValueError('config.mode must be "local" or "remote".')

    @property
    def drpall(self):
        return self._drpall

    @drpall.setter
    def drpall(self, value):
        if os.path.exists(value):
            self._drpall = value
        else:
            warnings.warn('path {0} cannot be found. Setting drpall to None.'
                          .format(value), MarvinUserWarning)

    def _setDbConfig(self):
        ''' Set the db configuration '''
        self.db = getDbMachine()

    def _checkConfig(self):
        ''' Check the config '''
        if not self.mplver or not (self.drpver and self.dapver):
            warnings.warn('No MPL or DRP/DAP version set. Setting default to MPL-4', MarvinUserWarning)
            self.setMPL('MPL-4')

    def setMPL(self, mplver):
        ''' Set the data version by MPL '''

        from marvin.utils.general import lookUpVersions

        m = re.search('MPL-([0-9])', mplver)
        assert m is not None, 'MPL version must be of form "MPL-[X]"'
        if m:
            self.mplver = mplver
            self.drpver, self.dapver = lookUpVersions(mplver)

    def setVersions(self, drpver=None, dapver=None):
        ''' Set the data version by DRPVER and DAPVER '''

        from marvin.utils.general import lookUpMpl

        if drpver:
            assert type(drpver) == str, 'drpver needs to be a string'
            drpre = re.search('v([0-9][_]([0-9])[_]([0-9]))', drpver)
            assert drpre is not None, 'DRP version must be of form "v[X]_[X]_[X]"'
            if drpre:
                self.drpver = drpver
                self.mplver = lookUpMpl(drpver)

        if dapver:
            assert type(dapver) == str, 'dapver needs to be a string'
            dapre1 = re.search('v([0-9][_]([0-9])[_]([0-9]))', dapver)
            dapre2 = re.search('([0-9][.]([0-9])[.]([0-9]))', dapver)
            assert (dapre1 or dapre2) is not None, 'DAP version must be of form "v[X]_[X]_[X]", or [X].[X].[X]'
            if any([dapre1, dapre2]):
                self.dapver = dapver

config = Config()
config._checkConfig()

# Inits the Database session and ModelClasses
session = None
datadb = None
if config.db:
    try:
        from marvin.db.database import db
    except RuntimeError as e:
        print('RuntimeError raised: Problem importing db: {0}'.format(e))
    else:
        try:
{'fraction_non_alphanumeric': 0.11962450843587466, 'fraction_numerical': 0.007865026005327921, 'mean_word_length': 2.1586538461538463, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5686819', 'n_tokens_mistral': 2625, 'n_tokens_neox': 2546, 'n_words': 691}