Commit 5d274f01 authored by BO ZHANG's avatar BO ZHANG 🏀
Browse files

deprecate FileRecorder

parent 7b9bcc4a
Loading
Loading
Loading
Loading
Loading
+40 −15
Original line number Diff line number Diff line
"""
Identifier:     KSC-SJ4-csst_common/__init__.py
Name:           __init__.py
Description:    csst_common package
Author:         Bo Zhang
Created:        2022-09-13
Modified-History:
    2022-09-13, Bo Zhang, created
    2022-09-13, Bo Zhang, fixed a bug
    2023-12-39, Bo Zhang, deprecated
"""

import os
from collections import namedtuple

from astropy import table

FileRecord = namedtuple(
    "FileRecord",
    ["filepath", "db", "comment", "existence"]
)
FileRecord = namedtuple("FileRecord", ["filepath", "db", "comment", "existence"])


class FileRecorder(list):
@@ -64,14 +73,21 @@ class FileRecorder(list):

    @staticmethod
    def FileRecord(filepath: str = "", db: bool = False, comment: str = ""):
        return FileRecord(filepath=filepath, db=db, comment=comment, existence=os.path.exists(filepath))
        return FileRecord(
            filepath=filepath,
            db=db,
            comment=comment,
            existence=os.path.exists(filepath),
        )

    def add_record(self, filepath: str = "", db: bool = False, comment: str = ""):
        existence = os.path.exists(filepath)
        assert isinstance(filepath, str)
        assert isinstance(db, bool)
        assert isinstance(comment, str)
        super().append(FileRecord(filepath=filepath, db=db, comment=comment, existence=existence))
        super().append(
            FileRecord(filepath=filepath, db=db, comment=comment, existence=existence)
        )

    def to_table(self):
        return table.Table([_._asdict() for _ in self])
@@ -87,20 +103,29 @@ class FileRecorder(list):
    def __repr__(self):
        t = self.to_table()
        lines, outs = t.formatter._pformat_table(
            t, max_lines=-1, max_width=-1, show_name=True, show_unit=None, show_dtype=False, align="<")
        if outs['show_length']:
            lines.append(f'Length = {len(self)} rows')
            t,
            max_lines=-1,
            max_width=-1,
            show_name=True,
            show_unit=None,
            show_dtype=False,
            align="<",
        )
        if outs["show_length"]:
            lines.append(f"Length = {len(self)} rows")
        return "\n".join(lines)

    @property
    def summary(self):
        if len(self) == 0:
            return '0 alleged, 0 missing, 0 to db'
            return "0 alleged, 0 missing, 0 to db"
        else:
            return f"{len(self)} alleged, " \
                   f"{len(self) - sum(self.to_table()['existence'])} missing, " \
            return (
                f"{len(self)} alleged, "
                f"{len(self) - sum(self.to_table()['existence'])} missing, "
                f"{sum(self.to_table()['existence'])} to db"
            )

    def is_good(self):
        """Check if all alleged files exist."""
        return all(self.to_table()['existence'])
        return all(self.to_table()["existence"])