<commit_before># Copyright 2013 BrewPi
# This file is part of BrewPi.

# BrewPi 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.

# BrewPi 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 BrewPi.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import print_function
import time
import sys
import os
import serial
import autoSerial
import tcpSerial

import app.models as models  # This SHOULD work due to the sys.path.append above.


try:
    import configobj
except ImportError:
    print("BrewPi requires ConfigObj to run, please install it with 'sudo apt-get install python-configobj")
    sys.exit(1)


def addSlash(path):
    """
    Adds a slash to the path, but only when it does not already have a slash at the end
    Params: a string
    Returns: a string
    """
    if not path.endswith('/'):
        path += '/'
    return path


def read_config_file_with_defaults(cfg):
    """
    Reads a config file with the default config file as fallback

    Params:
    cfg: string, path to cfg file

    Returns:
    ConfigObj of settings
    """
    if not cfg:
        cfg = addSlash(sys.path[0]) + 'settings/config.cfg'

    defaultCfg = scriptPath() + '/settings/defaults.cfg'
    config = configobj.ConfigObj(defaultCfg)

    if cfg:
        try:
            userConfig = configobj.ConfigObj(cfg)
            config.merge(userConfig)
        except configobj.ParseError:
            logMessage("ERROR: Could not parse user config file %s" % cfg)
        except IOError:
            logMessage("Could not open user config file %s. Using only default config file" % cfg)
    return config


def read_config_from_database_without_defaults(db_config_object):
    """
    Reads configuration parameters from the database without

    Params:
    db_config_object: models.BrewPiDevice, BrewPiDevice object

    Returns:
    ConfigObj of settings
    """

    config = {}

    # Unlike the above, we don't have defaults (because we assume the database enforces defaults). Load everything.
    config['scriptPath'] = db_config_object.script_path
    config['port'] = db_config_object.serial_port
    if db_config_object.serial_alt_port <> 'None':
        config['altport'] = db_config_object.serial_alt_port
    config['boardType'] = db_config_object.board_type
    config['beerName'] = db_config_object.get_active_beer_name()
    config['interval'] = float(db_config_object.data_point_log_interval)  # Converting to a float to match config file
    config['dataLogging'] = db_config_object.logging_status
    config['socket_name'] = db_config_object.socket_name
    config['profileName'] = None  # TODO - Implement this
    config['connection_type'] = db_config_object.connection_type
    config['wifiHost'] = db_config_object.wifi_host
    config['wifiPort'] = db_config_object.wifi_port
    config['useInetSocket'] = db_config_object.useInetSocket
    config['socketPort'] = db_config_object.socketPort
    config['socketHost'] = db_config_object.socketHost

    return config


def configSet(configFile, db_config_object, settingName, value):
    if configFile:
        if not os.path.isfile(configFile):
            logMessage("User config file %s does not exist yet, creating it..." % configFile)
        try:
            config = configobj.ConfigObj(configFile)
            config[settingName] = value
            config.write()
        except IOError as e:
            logMessage("I/O error(%d) while updating %s: %s " % (e.errno, configFile, e.strerror))
            logMessage("Probably your permissions are not set correctly. " +
                       "To fix this, run 'sudo sh /home/brewpi/fixPermissions.sh'")
        return read_config_file_with_defaults(configFile)  # return updated ConfigObj
    else:
        # Assuming we have a valid db_config_object here
        if settingName == "scriptPath":
            db_config_object.script_path = value
        elif settingName == "port":
            db_config_object.serial_port = value
        elif settingName == "altport":
            db_config_object.serial_alt_port = value
        elif settingName == "boardType":
            db_config_object.board_type = value
        elif settingName == "beerName":
            # As of right now, setting the beerName doesn't do anything, as setting the beer is done in brewpi.startBeer
            # TODO - Make sure this actually works as intended
            pass
        elif settingName == "profileName":
            # TODO - Implement this
            pass
        elif settingName == "socket_name":
            db_config_object.socket_name = value
        elif settingName == "interval":
            # TODO - Check if the interval gets updated in the script. If so, make sure we do proper type conversion.
            db_config_object.data_point_log_interval = value<commit_msg>Remove profileName option from configuration<commit_after># Copyright 2013 BrewPi
# This file is part of BrewPi.

# BrewPi 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.

# BrewPi 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 BrewPi.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import print_function
import time
import sys
import os
import serial
import autoSerial
import tcpSerial

import app.models as models  # This SHOULD work due to the sys.path.append above.


try:
    import configobj
except ImportError:
    print("BrewPi requires ConfigObj to run, please install it with 'sudo apt-get install python-configobj")
    sys.exit(1)


def addSlash(path):
    """
    Adds a slash to the path, but only when it does not already have a slash at the end
    Params: a string
    Returns: a string
    """
    if not path.endswith('/'):
        path += '/'
    return path


def read_config_file_with_defaults(cfg):
    """
    Reads a config file with the default config file as fallback

    Params:
    cfg: string, path to cfg file

    Returns:
    ConfigObj of settings
    """
    if not cfg:
        cfg = addSlash(sys.path[0]) + 'settings/config.cfg'

    defaultCfg = scriptPath() + '/settings/defaults.cfg'
    config = configobj.ConfigObj(defaultCfg)

    if cfg:
        try:
            userConfig = configobj.ConfigObj(cfg)
            config.merge(userConfig)
        except configobj.ParseError:
            logMessage("ERROR: Could not parse user config file %s" % cfg)
        except IOError:
            logMessage("Could not open user config file %s. Using only default config file" % cfg)
    return config


def read_config_from_database_without_defaults(db_config_object):
    """
    Reads configuration parameters from the database without

    Params:
    db_config_object: models.BrewPiDevice, BrewPiDevice object

    Returns:
    ConfigObj of settings
    """

    config = {}

    # Unlike the above, we don't have defaults (because we assume the database enforces defaults). Load everything.
    config['scriptPath'] = db_config_object.script_path
    config['port'] = db_config_object.serial_port
    if db_config_object.serial_alt_port <> 'None':
        config['altport'] = db_config_object.serial_alt_port
    config['boardType'] = db_config_object.board_type
    config['beerName'] = db_config_object.get_active_beer_name()
    config['interval'] = float(db_config_object.data_point_log_interval)  # Converting to a float to match config file
    config['dataLogging'] = db_config_object.logging_status
    config['socket_name'] = db_config_object.socket_name
    config['connection_type'] = db_config_object.connection_type
    config['wifiHost'] = db_config_object.wifi_host
    config['wifiPort'] = db_config_object.wifi_port
    config['useInetSocket'] = db_config_object.useInetSocket
    config['socketPort'] = db_config_object.socketPort
    config['socketHost'] = db_config_object.socketHost

    return config


def configSet(configFile, db_config_object, settingName, value):
    if configFile:
        if not os.path.isfile(configFile):
            logMessage("User config file %s does not exist yet, creating it..." % configFile)
        try:
            config = configobj.ConfigObj(configFile)
            config[settingName] = value
            config.write()
        except IOError as e:
            logMessage("I/O error(%d) while updating %s: %s " % (e.errno, configFile, e.strerror))
            logMessage("Probably your permissions are not set correctly. " +
                       "To fix this, run 'sudo sh /home/brewpi/fixPermissions.sh'")
        return read_config_file_with_defaults(configFile)  # return updated ConfigObj
    else:
        # Assuming we have a valid db_config_object here
        if settingName == "scriptPath":
            db_config_object.script_path = value
        elif settingName == "port":
            db_config_object.serial_port = value
        elif settingName == "altport":
            db_config_object.serial_alt_port = value
        elif settingName == "boardType":
            db_config_object.board_type = value
        elif settingName == "beerName":
            # As of right now, setting the beerName doesn't do anything, as setting the beer is done in brewpi.startBeer
            # TODO - Make sure this actually works as intended
            pass
        elif settingName == "socket_name":
            db_config_object.socket_name = value
        elif settingName == "interval":
            # TODO - Check if the interval gets updated in the script. If so, make sure we do proper type conversion.
            db_config_object.data_point_log_interval = value
{'fraction_non_alphanumeric': 0.07746411022256479, 'fraction_numerical': 0.0013488775411889392, 'mean_word_length': 3.0326340326340326, 'pattern_counts': {'":': 15, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2982947', 'n_tokens_mistral': 2889, 'n_tokens_neox': 2816, 'n_words': 969}