# Path: sloth/utils/strings.py
# def str2rng(rngstr, keeporder=True, rebin=None):
#     """Convert a string to a range of intergers
# 
#     Description
#     -----------
#     This utility converts a string representing list or range of intergers (=scans)
#     to a sorted list of integers
# 
#     Parameters
#     ----------
#     rngstr : str
#         Strig following a given syntax (see Example below)
#     keeporder : boolean
#         Control the order output [True -> keep the original order]
#         keeporder=False -> turn into a sorted list
#     rebin : int
#         if set, force rebinning of the final range [None]
#         .. note: useful for selecting scans every a given bunch.
# 
#     Example
#     -------
#     > _str2rng('100, 7:9, 130:140:5, 14, 16:18:1')
#     > [7, 8, 9, 14, 16, 17, 18, 100, 130, 135, 140]
# 
#     """
#     if rngstr is None:
#         raise NameError("Provide a string or list of scans to load")
#     if type(rngstr) is list:
#         assert all([type(elem) is int for elem in rngstr]), "'rngstr' not list of ints"
#         _logger.info("[str2rng] 'rngstr' given as list of integers")
#         return rngstr
#     else:
#         assert type(rngstr) is str, "'rngstr' should be a string"
#     _rng = []
#     for _r in rngstr.split(", "):  # the space is important!
#         if len(_r.split(",")) > 1:
#             raise NameError("Space after comma(s) is missing in '{0}'".format(_r))
#         _rsplit2 = _r.split(":")
#         if len(_rsplit2) == 1:
#             _rng.append(_r)
#         elif len(_rsplit2) == 2 or len(_rsplit2) == 3:
#             if len(_rsplit2) == 2:
#                 _rsplit2.append("1")
#             if _rsplit2[0] == _rsplit2[1]:
#                 raise NameError("Wrong range '{0}' in string '{1}'".format(_r, rngstr))
#             if int(_rsplit2[0]) > int(_rsplit2[1]):
#                 raise NameError("Wrong range '{0}' in string '{1}'".format(_r, rngstr))
#             _rng.extend(range(int(_rsplit2[0]), int(_rsplit2[1]) + 1, int(_rsplit2[2])))
#         else:
#             raise NameError("Too many colon in {0}".format(_r))
# 
#     # create the list and return it (removing the duplicates)
#     _rngout = [int(x) for x in _rng]
# 
#     if rebin is not None:
#         try:
#             _rngout = _rngout[:: int(rebin)]
#         except:
#             raise NameError("Wrong rebin={0}".format(int(rebin)))
# 
#     def uniquify(seq):
#         # Order preserving uniquifier by Dave Kirby
#         seen = set()
#         return [x for x in seq if x not in seen and not seen.add(x)]
# 
#     if keeporder:
#         return uniquify(_rngout)
#     else:
#         return list(set(_rngout))


import os
import numpy as np
from scipy.interpolate import interp1d
    from ..math.gridxyz import gridxyz
    from silx.io import specfilewrapper as specfile
    from PyMca5.PyMcaMath import SimpleMath
    from PyMca5.PyMcaMath import SGModule
    from .specfile_writer import SpecfileDataWriter
from sloth.utils.strings import str2rng as _str2rng  # noqa
    s = SpecfileData(fname)
    group = _larch.symtable.create_group()
    group.__name__ = "SPEC data file {0}; scans {1}; action {2}".format(
        fname, scans, action
    )
    x, y = s.get_mrg(
        scans=scans,
        cntx=cntx,
        csig=csig,
        cmon=cmon,
        csec=csec,
        norm=norm,
        action=action,
    )
    setattr(group, "x", x)
    setattr(group, "y", y)

    return group


spec_getmrg2group.__doc__ += _specfiledata_getdoc("get_mrg")


def str2rng_larch(rngstr, keeporder=True, _larch=None):
    """ larch equivalent of _str2rng() """
    if _larch is None:
        raise Warning("larch broken?")
    return _str2rng(rngstr, keeporder=keeporder)



# Path: sloth/utils/strings.py
# def str2rng(rngstr, keeporder=True, rebin=None):
#     """Convert a string to a range of intergers
# 
#     Description
#     -----------
#     This utility converts a string representing list or range of intergers (=scans)
#     to a sorted list of integers
# 
#     Parameters
#     ----------
#     rngstr : str
#         Strig following a given syntax (see Example below)
#     keeporder : boolean
#         Control the order output [True -> keep the original order]
#         keeporder=False -> turn into a sorted list
#     rebin : int
#         if set, force rebinning of the final range [None]
#         .. note: useful for selecting scans every a given bunch.
# 
#     Example
#     -------
#     > _str2rng('100, 7:9, 130:140:5, 14, 16:18:1')
#     > [7, 8, 9, 14, 16, 17, 18, 100, 130, 135, 140]
# 
#     """
#     if rngstr is None:
#         raise NameError("Provide a string or list of scans to load")
#     if type(rngstr) is list:
#         assert all([type(elem) is int for elem in rngstr]), "'rngstr' not list of ints"
#         _logger.info("[str2rng] 'rngstr' given as list of integers")
#         return rngstr
#     else:
#         assert type(rngstr) is str, "'rngstr' should be a string"
#     _rng = []
#     for _r in rngstr.split(", "):  # the space is important!
#         if len(_r.split(",")) > 1:
#             raise NameError("Space after comma(s) is missing in '{0}'".format(_r))
#         _rsplit2 = _r.split(":")
#         if len(_rsplit2) == 1:
#             _rng.append(_r)
#         elif len(_rsplit2) == 2 or len(_rsplit2) == 3:
#             if len(_rsplit2) == 2:
#                 _rsplit2.append("1")
#             if _rsplit2[0] == _rsplit2[1]:
#                 raise NameError("Wrong range '{0}' in string '{1}'".format(_r, rngstr))
#             if int(_rsplit2[0]) > int(_rsplit2[1]):
#                 raise NameError("Wrong range '{0}' in string '{1}'".format(_r, rngstr))
#             _rng.extend(range(int(_rsplit2[0]), int(_rsplit2[1]) + 1, int(_rsplit2[2])))
#         else:
#             raise NameError("Too many colon in {0}".format(_r))
# 
#     # create the list and return it (removing the duplicates)
#     _rngout = [int(x) for x in _rng]
# 
#     if rebin is not None:
#         try:
#             _rngout = _rngout[:: int(rebin)]
#         except:
#             raise NameError("Wrong rebin={0}".format(int(rebin)))
# 
#     def uniquify(seq):
#         # Order preserving uniquifier by Dave Kirby
#         seen = set()
#         return [x for x in seq if x not in seen and not seen.add(x)]
# 
#     if keeporder:
#         return uniquify(_rngout)
#     else:
#         return list(set(_rngout))


# Path: sloth/io/specfile_reader.py
import os
import numpy as np
from scipy.interpolate import interp1d
    from ..math.gridxyz import gridxyz
    from silx.io import specfilewrapper as specfile
    from PyMca5.PyMcaMath import SimpleMath
    from PyMca5.PyMcaMath import SGModule
    from .specfile_writer import SpecfileDataWriter
from sloth.utils.strings import str2rng as _str2rng  # noqa

    s = SpecfileData(fname)
    group = _larch.symtable.create_group()
    group.__name__ = "SPEC data file {0}; scans {1}; action {2}".format(
        fname, scans, action
    )
    x, y = s.get_mrg(
        scans=scans,
        cntx=cntx,
        csig=csig,
        cmon=cmon,
        csec=csec,
        norm=norm,
        action=action,
    )
    setattr(group, "x", x)
    setattr(group, "y", y)

    return group


spec_getmrg2group.__doc__ += _specfiledata_getdoc("get_mrg")


def str2rng_larch(rngstr, keeporder=True, _larch=None):
    """ larch equivalent of _str2rng() """
    if _larch is None:
        raise Warning("larch broken?")
    return _str2rng(rngstr, keeporder=keeporder)



str2rng_larch.__doc__ = _str2rng.__doc__