<commit_before># This file is part of the ISIS IBEX application.
# Copyright (C) 2012-2016 Science & Technology Facilities Council.
# All rights reserved.
#
# This program is distributed in the hope that it will be useful.
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License v1.0 which accompanies this distribution.
# EXCEPT AS EXPRESSLY SET FORTH IN THE ECLIPSE PUBLIC LICENSE V1.0, THE PROGRAM
# AND ACCOMPANYING MATERIALS ARE PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES
# OR CONDITIONS OF ANY KIND.  See the Eclipse Public License v1.0 for more details.
#
# You should have received a copy of the Eclipse Public License v1.0
# along with this program; if not, you can obtain a copy from
# https://www.eclipse.org/org/documents/epl-v10.php or
# http://opensource.org/licenses/eclipse-1.0.php

import os
from server_common.utilities import print_and_log, compress_and_hex
from BlockServer.core.file_path_manager import FILEPATH_MANAGER
from BlockServer.fileIO.schema_checker import ConfigurationSchemaChecker, ConfigurationInvalidUnderSchema
from BlockServer.core.constants import FILENAME_SCREENS as SCREENS_FILE
from xml.dom import minidom

GET_SCREENS = "GET_SCREENS"
SET_SCREENS = "SET_SCREENS"

SCREENS_SCHEMA = "screens.xsd"


class DevicesManager(object):
    """Class for managing the PVs associated with devices"""
    def __init__(self, block_server, cas, schema_folder, vc_manager):
        """Constructor.

        Args:
            block_server (BlockServer): A reference to the BlockServer instance.
            cas (CAServer): The channel access server for creating PVs on-the-fly
            schema_folder (string): The filepath for the devices schema
            vc_manager (ConfigVersionControl): The manager to allow version control modifications
        """
        self._schema_folder = schema_folder
        self._cas = cas
        self._devices_pvs = dict()
        self._vc = vc_manager
        self._bs = block_server
        self._current_config = ""

    def load_current(self):
        """Create the PVs for all the devices found in the devices directory."""
        try:
            devices_file_name = self.get_devices_filename()
            with open(devices_file_name, 'r') as devfile:
                data = devfile.read()
        except IOError as err:
            data = self.get_blank_devices()
            print_and_log("Unable to load devices file. " + str(err) + ". The PV data will default to a blank set of devices.","MINOR")

        try:
            ConfigurationSchemaChecker.check_xml_matches_schema(
                os.path.join(self._schema_folder, SCREENS_SCHEMA),
                data, "Screens")
        except ConfigurationInvalidUnderSchema as err:
            print_and_log(err)

        try:
            self._create_pv(data)
        except Exception as err:
            print_and_log("Error creating device PV: %s" % str(err), "MAJOR")

        try:
            self._add_to_version_control("New change found in devices file %s" % self._current_config)
        except Exception as err:
            print_and_log("Unable to add new data to version control. " + str(err),"MINOR")

        self._vc.commit("Blockserver started, devices updated")

    def _create_pv(self, data):
        """Creates a single PV based on a name and data.

        Args:<commit_msg>Only check vc is device file exists<commit_after># This file is part of the ISIS IBEX application.
# Copyright (C) 2012-2016 Science & Technology Facilities Council.
# All rights reserved.
#
# This program is distributed in the hope that it will be useful.
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License v1.0 which accompanies this distribution.
# EXCEPT AS EXPRESSLY SET FORTH IN THE ECLIPSE PUBLIC LICENSE V1.0, THE PROGRAM
# AND ACCOMPANYING MATERIALS ARE PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES
# OR CONDITIONS OF ANY KIND.  See the Eclipse Public License v1.0 for more details.
#
# You should have received a copy of the Eclipse Public License v1.0
# along with this program; if not, you can obtain a copy from
# https://www.eclipse.org/org/documents/epl-v10.php or
# http://opensource.org/licenses/eclipse-1.0.php

import os
from server_common.utilities import print_and_log, compress_and_hex
from BlockServer.core.file_path_manager import FILEPATH_MANAGER
from BlockServer.fileIO.schema_checker import ConfigurationSchemaChecker, ConfigurationInvalidUnderSchema
from BlockServer.core.constants import FILENAME_SCREENS as SCREENS_FILE
from xml.dom import minidom

GET_SCREENS = "GET_SCREENS"
SET_SCREENS = "SET_SCREENS"

SCREENS_SCHEMA = "screens.xsd"


class DevicesManager(object):
    """Class for managing the PVs associated with devices"""
    def __init__(self, block_server, cas, schema_folder, vc_manager):
        """Constructor.

        Args:
            block_server (BlockServer): A reference to the BlockServer instance.
            cas (CAServer): The channel access server for creating PVs on-the-fly
            schema_folder (string): The filepath for the devices schema
            vc_manager (ConfigVersionControl): The manager to allow version control modifications
        """
        self._schema_folder = schema_folder
        self._cas = cas
        self._devices_pvs = dict()
        self._vc = vc_manager
        self._bs = block_server
        self._current_config = ""

    def load_current(self):
        """Create the PVs for all the devices found in the devices directory."""

        devices_file_name = None
        try:
            devices_file_name = self.get_devices_filename()
            with open(devices_file_name, 'r') as devfile:
                data = devfile.read()
        except IOError as err:
            data = self.get_blank_devices()
            print_and_log("Unable to load devices file. " + str(err) + ". The PV data will default to a blank set of devices.","MINOR")

        try:
            ConfigurationSchemaChecker.check_xml_matches_schema(
                os.path.join(self._schema_folder, SCREENS_SCHEMA),
                data, "Screens")
        except ConfigurationInvalidUnderSchema as err:
            print_and_log(err)

        try:
            self._create_pv(data)
        except Exception as err:
            print_and_log("Error creating device PV: %s" % str(err), "MAJOR")

        if devices_file_name is not None:
            try:
                self._add_to_version_control("New change found in devices file %s" % self._current_config)
            except Exception as err:
                print_and_log("Unable to add new data to version control. " + str(err), "MINOR")

        self._vc.commit("Blockserver started, devices updated")

    def _create_pv(self, data):
        """Creates a single PV based on a name and data.

        Args:
{'fraction_non_alphanumeric': 0.07297572321558365, 'fraction_numerical': 0.005814798662596307, 'mean_word_length': 2.9654178674351583, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, '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': '4650495', 'n_tokens_mistral': 1952, 'n_tokens_neox': 1860, 'n_words': 667}