from malcolm.core import Info

from .util import DatasetType, AttributeDatasetType


class FilePathTranslatorInfo(Info):
    """Translate linux filepath to windows equivalent

    Args:
        windows_drive_letter: The drive letter assigned to the windows mount
        path_prefix: The location of the mount in linux (i.e. /dls or /dls_sw)
    """

    def __init__(self, windows_drive_letter, path_prefix):
        self.windows_drive_letter = windows_drive_letter
        self.path_prefix = path_prefix

    @classmethod
    def translate_filepath(cls, part_info, filepath):
        error_msg = "No or multiple FilePathTranslatorPart found:" + \
                    "must have exactly 1 if any part in the AD chain is running on Windows"
        translator = cls.filter_single_value(part_info, error_msg)
        if not filepath.startswith(translator.path_prefix):
            raise AssertionError(
                "filepath %s does not start with expected prefix %s" % (filepath, translator.path_prefix)
            )
        return filepath.replace(translator.path_prefix, translator.windows_drive_letter + ":").replace("/", "\\")


class ExposureDeadtimeInfo(Info):
    """Detector exposure time should be generator.duration - deadtime

    Args:
        readout_time: The per frame readout time of the detector
        frequency_accuracy: The crystal accuracy in ppm
    """

    def __init__(self, readout_time, frequency_accuracy):
        # type: (float, float) -> None
        self.readout_time = readout_time
        self.frequency_accuracy = frequency_accuracy

    def calculate_exposure(self, duration):
        # type: (float) -> float
        """Calculate the exposure to set the detector to given the duration of
        the frame and the readout_time and frequency_accuracy"""
        exposure = duration - self.frequency_accuracy * duration / 1000000.0 - \
                   self.readout_time
        assert exposure > 0.0, \
            "Exposure time %s too small when deadtime taken into account" % (
                exposure,)
        return exposure


class NDArrayDatasetInfo(Info):
    """Declare the NDArray data this produces as being a useful dataset to store
    to file

    Args:
        rank: The rank of the dataset, e.g. 2 for a 2D detector
    """

    def __init__(self, rank):
        # type: (int) -> None
        self.rank = rank


class CalculatedNDAttributeDatasetInfo(Info):
    """Declare that we have calculated some statistics from the main dataset
    and these will be available

    Args:
        name: Dataset name that should be written to
        attr: NDAttribute name to get data from
    """

    def __init__(self, name, attr):
        # type: (str, str) -> None
        self.name = name
        self.attr = attr


class NDAttributeDatasetInfo(Info):
    """Declare an NDAttribute attached to this NDArray to produce a useful
    dataset to store to file

    Args:
        name: Dataset name that should be written to
        type: What NeXuS dataset type it produces
        attr: NDAttribute name to get data from
        rank: The rank of the dataset
    """

    def __init__(self, name, type, attr, rank):
        # type: (str, AttributeDatasetType, str, int) -> None
        self.name = name
        self.type = type
        self.attr = attr
        self.rank = rank


class DatasetProducedInfo(Info):
    """Declare that we will write the following dataset to file

    Args:
        name: Dataset name
        filename: Filename relative to the fileDir we were given
        type: What NeXuS dataset type it produces
        rank: The rank of the dataset including generator dims
        path: The path of the dataset within the file
        uniqueid: The path of the UniqueID dataset within the file
    """

    def __init__(self, name, filename, type, rank, path, uniqueid):
        # type: (str, str, DatasetType, int, str, str) -> None
        self.name = name
        self.filename = filename
        self.type = type
        self.rank = rank
        self.path = path
        self.uniqueid = uniqueid

@@ -335,33 +335,32 @@
 dls_sw)%0A    %22%22%22%0A
-%0A
     def __init__
@@ -812,13 +812,13 @@
     
-if no
+asser
 t fi
@@ -862,48 +862,12 @@
 fix)
-:%0A            raise AssertionError(%0A    
+, %5C%0A
     
@@ -968,22 +968,8 @@
 ix)%0A
-            )%0A
     
@@ -1316,33 +1316,32 @@
  in ppm%0A    %22%22%22%0A
-%0A
     def __init__
@@ -1818,23 +1818,16 @@
 0.0 - %5C%0A
-       
         
@@ -1840,32 +1840,32 @@
 lf.readout_time%0A
+
         assert e
@@ -2212,33 +2212,32 @@
 etector%0A    %22%22%22%0A
-%0A
     def __init__
@@ -2574,33 +2574,32 @@
 ta from%0A    %22%22%22%0A
-%0A
     def __init__
@@ -3045,33 +3045,32 @@
 dataset%0A    %22%22%22%0A
-%0A
     def __init__
@@ -3707,17 +3707,16 @@
     %22%22%22%0A
-%0A
     def 
@@ -3971,16 +3971,16 @@
  = path%0A
-
         
@@ -4004,8 +4004,9 @@
 niqueid%0A
+%0A

Revert "Removed run-time assert"