def save_devices_xml(self, xml_data):
        """ Saves the xml in the current "screens.xml" config file.

        Args:
            xml_data (string): The XML to be saved
        """
        try:
            ConfigurationSchemaChecker.check_xml_data_matches_schema(os.path.join(self._schema_folder, SCREENS_SCHEMA),
                                                                     xml_data)
        except ConfigurationInvalidUnderSchema as err:
            print_and_log(err.message)
            return

        try:
            if not os.path.exists(FILEPATH_MANAGER.devices_dir):
                os.makedirs(FILEPATH_MANAGER.devices_dir)
            self._file_io.save_devices_file(self.get_devices_filename(), xml_data)
        except IOError as err:
            print_and_log("Unable to save devices file. %s. The PV data will not be updated." % err,
                          "MINOR")
            return

        # Update PVs
        self.update(xml_data, "Device screens modified by client")
        print_and_log("Devices saved to " + self.get_devices_filename())
    def _load_current(self):
        """ Gets the devices XML for the current instrument"""
        # Read the data from file
        try:
            self._data = self._file_io.load_devices_file(self.get_devices_filename())
        except IOError as err:
            self._data = self.get_blank_devices()
            print_and_log("Unable to load devices file. %s. The PV data will default to a blank set of devices." % err,
                          "MINOR")
            return

        try:
            # Check against the schema
            ConfigurationSchemaChecker.check_xml_data_matches_schema(os.path.join(self._schema_folder,
                                                                                  SCREENS_SCHEMA), self._data)
        except ConfigurationInvalidUnderSchema as err:
            self._data = self.get_blank_devices()
            print_and_log(err.message)
            return

        try:
            self._add_to_version_control("New change found in devices file")
        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 test_valid_blocks_xml_matches_schema(self):
        self.cs.set_config_details(TEST_CONFIG)
        xml = ConfigurationXmlConverter.blocks_to_xml(self.cs.get_block_details(), MACROS)

        try:
            ConfigurationSchemaChecker.check_xml_data_matches_schema(os.path.join(self.schema_dir, "blocks.xsd"), xml)
        except:
            self.fail()
    def test_valid_components_xml_matches_schema(self):
        conf = Configuration(MACROS)
        conf.components["COMP1"] = "COMP1"
        conf.components["COMP2"] = "COMP2"

        xml = ConfigurationXmlConverter.components_to_xml(conf.components)

        try:
            ConfigurationSchemaChecker.check_xml_data_matches_schema(os.path.join(self.schema_dir, "components.xsd"), xml)
        except:
            self.fail()
    def _check_valid(self, path):
        """
        Check the validity of the device screens file and return the xml data contained within if valid

        Args:
            path (string): The location of the file

        Returns: The device screens data as a string of xml

        """
        extension = path[-4:]
        if extension != ".xml":
            raise NotConfigFileException("File not xml")

        xml_data = self._manager.load_devices(path)

        with self._schema_lock:
            ConfigurationSchemaChecker.check_xml_data_matches_schema(self._schema_filepath, xml_data)

        return xml_data
    def _load_initial(self):
        """Create the PVs for all the synoptics found in the synoptics directory."""
        for f in self._file_io.get_list_synoptic_files(self._directory):
            # Load the data, checking the schema
            try:
                data = self._file_io.read_synoptic_file(self._directory, f)
                ConfigurationSchemaChecker.check_xml_matches_schema(os.path.join(self._schema_folder,
                                                                                 SYNOPTIC_SCHEMA_FILE), data, "Synoptic")
                # Get the synoptic name
                self._create_pv(data)

                self._add_to_version_control(f[0:-4])
            except VersionControlException as err:
                print_and_log(str(err), "MAJOR")
            except Exception as err:
                print_and_log("Error creating synoptic PV: %s" % str(err), "MAJOR")
        try:
            self._vc.commit("Blockserver started, synoptics updated")
        except Exception as err:
            print_and_log("Unable to update synoptics while starting the blockserver: %s" % str(err), "MAJOR")
    def save_synoptic_xml(self, xml_data):
        """Saves the xml under the filename taken from the xml name tag.

        Args:
            xml_data (string): The XML to be saved
        """
        try:
            # Check against schema
            ConfigurationSchemaChecker.check_xml_matches_schema(os.path.join(self._schema_folder, SYNOPTIC_SCHEMA_FILE),
                                                                xml_data, "Synoptic")
            # Update PVs
            self._create_pv(xml_data)

        except Exception as err:
            print_and_log(err)
            raise

        name = self._get_synoptic_name_from_xml(xml_data)

        save_path = FILEPATH_MANAGER.get_synoptic_path(name)

        self._file_io.write_synoptic_file(name, save_path, xml_data)
        self._add_to_version_control(name, "%s modified by client" % name)
        print_and_log("Synoptic saved: " + name)
 def _check_againgst_schema(self, xml, filename):
     regex = re.compile(re.escape(".xml"), re.IGNORECASE)
     name = regex.sub(".xsd", filename)
     schema_path = os.path.join(FILEPATH_MANAGER.schema_dir, name)
     ConfigurationSchemaChecker.check_xml_data_matches_schema(schema_path, xml)