Skip to content

abstract

FileItem

File item that represents a file.

Can be used for both Workarea and Published workfile. Workarea file will always exist on disk which is not the case for Published workfile.

Parameters:

Name Type Description Default
dirpath str

Directory path of file.

required
filename str

Filename.

required
modified float

Modified timestamp.

required
created_by Optional[str]

Username.

None
representation_id Optional[str]

Representation id of published workfile.

None
filepath Optional[str]

Prepared filepath.

None
exists Optional[bool]

If file exists on disk.

None
Source code in client/ayon_wrap/workfiles/abstract.py
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
class FileItem:
    """File item that represents a file.

    Can be used for both Workarea and Published workfile. Workarea file
    will always exist on disk which is not the case for Published workfile.

    Args:
        dirpath (str): Directory path of file.
        filename (str): Filename.
        modified (float): Modified timestamp.
        created_by (Optional[str]): Username.
        representation_id (Optional[str]): Representation id of published
            workfile.
        filepath (Optional[str]): Prepared filepath.
        exists (Optional[bool]): If file exists on disk.
    """

    def __init__(
        self,
        dirpath,
        filename,
        modified,
        created_by=None,
        updated_by=None,
        representation_id=None,
        filepath=None,
        exists=None
    ):
        self.filename = filename
        self.dirpath = dirpath
        self.modified = modified
        self.created_by = created_by
        self.updated_by = updated_by
        self.representation_id = representation_id
        self._filepath = filepath
        self._exists = exists

    @property
    def filepath(self):
        """Filepath of file.

        Returns:
            str: Full path to a file.
        """

        if self._filepath is None:
            self._filepath = os.path.join(self.dirpath, self.filename)
        return self._filepath

    @property
    def exists(self):
        """File is available.

        Returns:
            bool: If file exists on disk.
        """

        if self._exists is None:
            self._exists = os.path.exists(self.filepath)
        return self._exists

    def to_data(self):
        """Converts file item to data.

        Returns:
            dict[str, Any]: File item data.
        """

        return {
            "filename": self.filename,
            "dirpath": self.dirpath,
            "modified": self.modified,
            "created_by": self.created_by,
            "representation_id": self.representation_id,
            "filepath": self.filepath,
            "exists": self.exists,
        }

    @classmethod
    def from_data(cls, data):
        """Re-creates file item from data.

        Args:
            data (dict[str, Any]): File item data.

        Returns:
            FileItem: File item.
        """

        required_keys = {
            "filename",
            "dirpath",
            "modified",
            "representation_id"
        }
        missing_keys = required_keys - set(data.keys())
        if missing_keys:
            raise KeyError("Missing keys: {}".format(missing_keys))

        return cls(**{
            key: data[key]
            for key in required_keys
        })

exists property

File is available.

Returns:

Name Type Description
bool

If file exists on disk.

filepath property

Filepath of file.

Returns:

Name Type Description
str

Full path to a file.

from_data(data) classmethod

Re-creates file item from data.

Parameters:

Name Type Description Default
data dict[str, Any]

File item data.

required

Returns:

Name Type Description
FileItem

File item.

Source code in client/ayon_wrap/workfiles/abstract.py
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
@classmethod
def from_data(cls, data):
    """Re-creates file item from data.

    Args:
        data (dict[str, Any]): File item data.

    Returns:
        FileItem: File item.
    """

    required_keys = {
        "filename",
        "dirpath",
        "modified",
        "representation_id"
    }
    missing_keys = required_keys - set(data.keys())
    if missing_keys:
        raise KeyError("Missing keys: {}".format(missing_keys))

    return cls(**{
        key: data[key]
        for key in required_keys
    })

to_data()

Converts file item to data.

Returns:

Type Description

dict[str, Any]: File item data.

Source code in client/ayon_wrap/workfiles/abstract.py
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
def to_data(self):
    """Converts file item to data.

    Returns:
        dict[str, Any]: File item data.
    """

    return {
        "filename": self.filename,
        "dirpath": self.dirpath,
        "modified": self.modified,
        "created_by": self.created_by,
        "representation_id": self.representation_id,
        "filepath": self.filepath,
        "exists": self.exists,
    }