Module psdi_data_conversion.converters.atomsk.converter

@file psdi_data_conversion/converters/script_template/converter.py

Atomsk file converter

Classes

class AtomskFileConverter (filename: str,
to_format: str,
from_format: str | None = None,
data: dict[str, typing.Any] | None = None,
abort_callback: Callable[[int], None] = <function abort_raise>,
use_envvars=False,
input_dir='./psdi_data_conversion/static/uploads',
output_dir='./psdi_data_conversion/static/downloads',
no_clobber=False,
max_file_size=None,
no_check=False,
log_file: str | None = None,
log_mode='full',
log_level: int | None = None,
refresh_local_log: bool = True,
delete_input=False)
Expand source code
class AtomskFileConverter(ScriptFileConverter):
    """File converter specialised to use Atomsk for conversions"""

    meta: FileConverterMeta = FileConverterMeta.load(__file__)

    script = "atomsk.sh"
    required_bin = "atomsk"

    has_in_format_flags_or_options = False
    has_out_format_flags_or_options = False

    allowed_flags = ()
    allowed_options = ()

    def _get_script_args(self):
        """Override the standard script arguments so we can set the different format names expected by atomsk
        """
        l_script_args = super()._get_script_args()

        # Update the formats to Atomsk style if relevant
        if self.from_format_info.name != self.from_format_name:
            l_script_args[1] = replace_extension(l_script_args[1], self.from_format_name)
        if self.to_format_info.name != self.to_format_name:
            l_script_args[2] = replace_extension(l_script_args[2], self.to_format_name)

        return l_script_args

    def _convert(self):
        """Atomsk has a bug with some formats which results in the file being created with the wrong extension, so
        we check for that here and correct it
        """

        # Delete the output file if it already exists
        if os.path.exists(self.out_filename):
            os.remove(self.out_filename)

        # Check if we might need to delete an intermediate file too, and do so
        intermediate_filename: str = ""
        backup_filename: str = ""
        if self.to_format_info.primary_name != self.to_format_name:
            intermediate_filename = replace_extension(self.out_filename, self.to_format_info.primary_name)
            if os.path.exists(intermediate_filename):
                # Create a name for a backup file we'll keep of any intermediate file if one exists
                backup_filename = intermediate_filename + ".bak"
                while os.path.exists(backup_filename):
                    backup_filename = backup_filename + ".bak"
                os.rename(intermediate_filename, backup_filename)

        try:

            super()._convert()

            # If an intermediate filename was used, rename to the proper output filename now
            if intermediate_filename:
                os.rename(intermediate_filename, self.out_filename)

        finally:
            if intermediate_filename and backup_filename:
                os.rename(backup_filename, intermediate_filename)

File converter specialised to use Atomsk for conversions

Initialize the object, storing needed data and setting up loggers.

Parameters

filename : str
The filename of the input file to be converted, either relative to current directory or fully-qualified
to_format : str
The desired format to convert to, as the file extension (e.g. "cif")
from_format : str | None
The format to convert from, as the file extension (e.g. "pdb"). If None is provided (default), will be determined from the extension of filename
data : dict[str | Any] | None
A dict of any other data needed by a converter or for extra logging information, default empty dict. See the docstring of each converter for supported keys and values that can be passed to data here
abort_callback : Callable[[int], None]
Function to be called if the conversion hits an error and must be aborted, default abort_raise, which raises an appropriate exception
use_envvars : bool
If set to True, environment variables will be checked for any that set options for this class and used, default False
input_dir : str
The location of input files relative to the current directory
output_dir : str
The location of output files relative to the current directory
max_file_size : float
The maximum allowed file size for input/output files, in MB. If 0, will be unlimited. Default 0 (unlimited)
no_check : bool
If False (default), will check at setup whether or not a conversion between the desired file formats is supported with the specified converter
log_file : str | None
If provided, all logging will go to a single file or stream. Otherwise, logs will be split up among multiple files for server-style logging.
log_mode : str
How logs should be stores. Allowed values are: - 'full' - Multi-file logging, only recommended when running as a public web app - 'full-force' - Multi-file logging, only recommended when running as a public web app, with the log file name forced to be used for the output log - 'simple' - Logs saved to one file - 'stdout' - Output logs and errors only to stdout - 'none' - Output only errors to stdout
log_level : int | None
The level to log output at. If None (default), the level will depend on the chosen log_mode: - 'full', 'full-force', or 'simple': INFO - 'stdout' - INFO to stdout, no logging to file - 'none' - ERROR to stdout, no logging to file
refresh_local_log : bool
If True, the local log generated from this run will be overwritten. If False it will be appended to. Default True
delete_input : bool
Whether or not to delete input files after conversion, default False

Ancestors

Class variables

var allowed_flags : tuple[tuple[str, dict, collections.abc.Callable], ...] | None
var allowed_options : tuple[tuple[str, dict, collections.abc.Callable], ...] | None
var has_in_format_flags_or_options
var has_out_format_flags_or_options
var meta : FileConverterMeta
var required_bin : str | None
var script : str | None
class converter (filename: str,
to_format: str,
from_format: str | None = None,
data: dict[str, typing.Any] | None = None,
abort_callback: Callable[[int], None] = <function abort_raise>,
use_envvars=False,
input_dir='./psdi_data_conversion/static/uploads',
output_dir='./psdi_data_conversion/static/downloads',
no_clobber=False,
max_file_size=None,
no_check=False,
log_file: str | None = None,
log_mode='full',
log_level: int | None = None,
refresh_local_log: bool = True,
delete_input=False)
Expand source code
class AtomskFileConverter(ScriptFileConverter):
    """File converter specialised to use Atomsk for conversions"""

    meta: FileConverterMeta = FileConverterMeta.load(__file__)

    script = "atomsk.sh"
    required_bin = "atomsk"

    has_in_format_flags_or_options = False
    has_out_format_flags_or_options = False

    allowed_flags = ()
    allowed_options = ()

    def _get_script_args(self):
        """Override the standard script arguments so we can set the different format names expected by atomsk
        """
        l_script_args = super()._get_script_args()

        # Update the formats to Atomsk style if relevant
        if self.from_format_info.name != self.from_format_name:
            l_script_args[1] = replace_extension(l_script_args[1], self.from_format_name)
        if self.to_format_info.name != self.to_format_name:
            l_script_args[2] = replace_extension(l_script_args[2], self.to_format_name)

        return l_script_args

    def _convert(self):
        """Atomsk has a bug with some formats which results in the file being created with the wrong extension, so
        we check for that here and correct it
        """

        # Delete the output file if it already exists
        if os.path.exists(self.out_filename):
            os.remove(self.out_filename)

        # Check if we might need to delete an intermediate file too, and do so
        intermediate_filename: str = ""
        backup_filename: str = ""
        if self.to_format_info.primary_name != self.to_format_name:
            intermediate_filename = replace_extension(self.out_filename, self.to_format_info.primary_name)
            if os.path.exists(intermediate_filename):
                # Create a name for a backup file we'll keep of any intermediate file if one exists
                backup_filename = intermediate_filename + ".bak"
                while os.path.exists(backup_filename):
                    backup_filename = backup_filename + ".bak"
                os.rename(intermediate_filename, backup_filename)

        try:

            super()._convert()

            # If an intermediate filename was used, rename to the proper output filename now
            if intermediate_filename:
                os.rename(intermediate_filename, self.out_filename)

        finally:
            if intermediate_filename and backup_filename:
                os.rename(backup_filename, intermediate_filename)

File converter specialised to use Atomsk for conversions

Initialize the object, storing needed data and setting up loggers.

Parameters

filename : str
The filename of the input file to be converted, either relative to current directory or fully-qualified
to_format : str
The desired format to convert to, as the file extension (e.g. "cif")
from_format : str | None
The format to convert from, as the file extension (e.g. "pdb"). If None is provided (default), will be determined from the extension of filename
data : dict[str | Any] | None
A dict of any other data needed by a converter or for extra logging information, default empty dict. See the docstring of each converter for supported keys and values that can be passed to data here
abort_callback : Callable[[int], None]
Function to be called if the conversion hits an error and must be aborted, default abort_raise, which raises an appropriate exception
use_envvars : bool
If set to True, environment variables will be checked for any that set options for this class and used, default False
input_dir : str
The location of input files relative to the current directory
output_dir : str
The location of output files relative to the current directory
max_file_size : float
The maximum allowed file size for input/output files, in MB. If 0, will be unlimited. Default 0 (unlimited)
no_check : bool
If False (default), will check at setup whether or not a conversion between the desired file formats is supported with the specified converter
log_file : str | None
If provided, all logging will go to a single file or stream. Otherwise, logs will be split up among multiple files for server-style logging.
log_mode : str
How logs should be stores. Allowed values are: - 'full' - Multi-file logging, only recommended when running as a public web app - 'full-force' - Multi-file logging, only recommended when running as a public web app, with the log file name forced to be used for the output log - 'simple' - Logs saved to one file - 'stdout' - Output logs and errors only to stdout - 'none' - Output only errors to stdout
log_level : int | None
The level to log output at. If None (default), the level will depend on the chosen log_mode: - 'full', 'full-force', or 'simple': INFO - 'stdout' - INFO to stdout, no logging to file - 'none' - ERROR to stdout, no logging to file
refresh_local_log : bool
If True, the local log generated from this run will be overwritten. If False it will be appended to. Default True
delete_input : bool
Whether or not to delete input files after conversion, default False

Ancestors

Class variables

var has_in_format_flags_or_options
var has_out_format_flags_or_options

Inherited members