示例#1
0
def all_files_with_ext(topdir, ext, cs=False):
    """Iterates over files with extension ``ext`` recursively from ``topdir``
    """
    if not isinstance(topdir, str):
        msg = PARAM_TYPE_ERR.format(param="topdir",
                                    func="all_files_with_ext", type="str")
        LOG.error(msg)
        raise ValueError(msg)
    if not isinstance(ext, str):
        msg = PARAM_TYPE_ERR.format(param="ext",
                                    func="all_files_with_ext", type="str")
        LOG.error(msg)
        raise ValueError(msg)
    if not isinstance(cs, bool):
        msg = PARAM_TYPE_ERR.format(param="cs",
                                    func="all_files_with_ext", type="bool")
        LOG.error(msg)
        raise ValueError(msg)
    # Trim any leading spaces from the extension we've been given
    if ext.startswith("."):
        ext = ext[1:]
    # For speed, we pre-convert the ext to lowercase here if we're being case
    # insensitive
    if not cs:
        ext = ext.lower()
    # OK, walk the dir. we only care about files, hence why dirs never gets
    # touched
    for root, folders, files in os.walk(topdir):
        # These *must* be in place, not f = sorted(f)
        folders.sort()
        files.sort()
        for folder in folders:
            if folder.startswith("_"):
                folders.remove(folder)
        for fpath in files:
            # split out ext, and do any case-conversion we need
            fname, fext = path.splitext(fpath)
            if not cs:
                fext = fext.lower()
            # remove the dot at the start, as splitext returns ("fn", ".ext")
            fext = fext[1:]
            if fext == ext:
                # we give the whole path to  the file
                yield path.join(root, fpath)
示例#2
0
def ts_format_date(dt):
    if isinstance(dt, str):
        return dt
    elif isinstance(dt, datetime):
        return dt.strftime(TS_DATE_FORMAT)
    else:
        msg = PARAM_TYPE_ERR.format(param="dt", func="ts_format_date",
                                    type="datetime.datetime")
        LOG.error(msg)
        raise TypeError(msg)
示例#3
0
def all_files_with_exts(topdir, exts, cs=False):
    """Creates a dictionary of {"ext": [files]} for each ext in exts
    """
    if not isinstance(exts, list):
        msg = PARAM_TYPE_ERR.format(param="exts",
                                    func="all_files_with_exts", type="list")
        LOG.error(msg)
        raise ValueError(msg)
    ext_dict = {}
    for ext in exts:
        ext_dict[ext] = sorted(list(all_files_with_ext(topdir, ext, cs)))
    return ext_dict
示例#4
0
def ts_get_image(ts_path, date, n=0, write_manifest=False):
    """Get the image path of the image in ``ts_path`` at ``date``
    """
    if isinstance(date, datetime):
        date = ts_format_date(date)
    if not isinstance(date, str):
        msg = PARAM_TYPE_ERR.format(param="date",
                                    func="ts_get_image",
                                    type="datetime.datetime or str")
        LOG.error(msg)
        raise ValueError(msg)
    if not isinstance(ts_path, str):
        msg = PARAM_TYPE_ERR.format(param="ts_path",
                                    func="all_files_with_ext", type="str")
        LOG.error(msg)
        raise ValueError(msg)
    # Get ts_info from manifest
    ts_info = ts_get_manifest(ts_path)
    # Bail early if we know it's missing
    if date in ts_info["missing"]:
        return None
    # Format the path below the ts root (ts_path)
    relpath = _ts_date_to_path(ts_info["name"], ts_info["extension"],
                               ts_parse_date(date), n)
    # Join to make "absolute" path, i.e. path including ts_path
    abspath = path.join(ts_path, relpath)
    # not-so-silently fail if we can't find the image
    if path.exists(abspath):
        LOG.debug("Image at {} in {} is {}.".format(date, ts_path, abspath))
        return abspath
    else:
        LOG.warn("Expected image {} at {} in {} did not exist.".format(
            abspath, date, ts_path))
        if write_manifest:
            ts_info["missing"].append(date)
            ts_update_manifest(ts_path, ts_info)
            ts_info = ts_get_manifest(ts_path)
        return None
示例#5
0
def traypos_to_chamber_index(traypos, tray_cap=20, col_cap=5):
    if not isinstance(traypos, str):
        msg = PARAM_TYPE_ERR.format(func='traypos_to_chamber_index',
                                    param='traypos', type='str')
        LOG.error(msg)
        raise TypeError(msg)
    extractor = re.compile(r'^(\d{1,2})([a-zA-Z])([1-9])$')
    match = extractor.match(traypos)
    if match is None:
        msg = "Tray Pos '{}' is invalid".format(traypos)
        LOG.error(msg)
        raise ValueError(msg)
    tray, col, row = match.groups()
    tray = int(tray)
    col = ord(col.upper()) - 65  # Numericise the col num, 0-based
    row = int(row)
    index = (tray - 1) * tray_cap + col * col_cap + row
    return index