Skip to content

trestle.core.models.file_content_type

trestle.core.models.file_content_type ¤

Action wrapper of a command.

Classes¤

FileContentType ¤

Bases: Enum


              flowchart TD
              trestle.core.models.file_content_type.FileContentType[FileContentType]

              

              click trestle.core.models.file_content_type.FileContentType href "" "trestle.core.models.file_content_type.FileContentType"
            

File Content type for read/write.

Source code in trestle/core/models/file_content_type.py
 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
107
108
109
110
111
112
113
114
class FileContentType(Enum):
    """File Content type for read/write."""

    # JSON formatted content
    JSON = 1

    # YAML formatted content
    YAML = 2

    # RFC 8785 canonical JSON formatted content. This is an explicit content type;
    # extension inference still treats canonical JSON files as JSON.
    CANONICAL_JSON = 3

    # No extension and possibly a DIR
    DIRLIKE = 4

    # Type could not be determined
    UNKNOWN = 5

    @classmethod
    def to_file_extension(cls, content_type: 'FileContentType') -> str:
        """Get file extension for the type, including the dot."""
        if content_type == FileContentType.YAML:
            return '.yaml'
        if content_type in [FileContentType.JSON, FileContentType.CANONICAL_JSON]:
            return '.json'
        raise TrestleError(f'Invalid file content type {content_type}')

    @classmethod
    def to_content_type(cls, file_extension: str) -> 'FileContentType':
        """Get content type form file extension, including the dot."""
        if file_extension == '.canonical.json':
            return FileContentType.CANONICAL_JSON
        if file_extension == '.json':
            return FileContentType.JSON
        if file_extension == '.yaml' or file_extension == '.yml':
            return FileContentType.YAML
        if not file_extension:
            return FileContentType.DIRLIKE

        raise TrestleError(f'Unsupported file extension {file_extension}')

    @classmethod
    def path_suffix_to_content_type(cls, file_path: Path) -> 'FileContentType':
        """Get content type from an explicit file path's suffixes without checking existence."""
        if cls._has_canonical_json_suffix(file_path):
            return FileContentType.CANONICAL_JSON
        return FileContentType.to_content_type(file_path.suffix)

    @classmethod
    def _has_canonical_json_suffix(cls, file_path: Path) -> bool:
        """Return True if the path ends with the canonical JSON double extension."""
        return ''.join(file_path.suffixes[-2:]) == '.canonical.json'

    @classmethod
    def path_to_content_type(cls, file_path: Path) -> 'FileContentType':
        """Get content type from file path looking for extension."""
        if file_path.with_suffix('.json').exists():
            return FileContentType.JSON
        if file_path.with_suffix('.yaml').exists():
            return FileContentType.YAML
        if file_path.with_suffix('.yml').exists():
            return FileContentType.YAML
        return FileContentType.UNKNOWN

    @classmethod
    def dir_to_content_type(cls, dir_path: Path) -> 'FileContentType':
        """Get content type by looking for json or yaml files in dir."""
        files = dir_path.glob('*')
        for file in files:
            if file.is_file():
                suffix = file.suffix
                if suffix == '.json':
                    return FileContentType.JSON
                if suffix in ['.yaml', '.yml']:
                    return FileContentType.YAML
        return FileContentType.UNKNOWN

    @classmethod
    def path_to_file_extension(cls, file_path: Path) -> str:
        """Get extension from file path looking for extension."""
        if file_path.with_suffix('.json').exists():
            return '.json'
        if file_path.with_suffix('.yaml').exists():
            return '.yaml'
        if file_path.with_suffix('.yml').exists():
            return '.yml'
        return ''

    @classmethod
    def is_readable_file(cls, content_type: 'FileContentType') -> bool:
        """Is the file a type that can be read directly."""
        return content_type in [FileContentType.JSON, FileContentType.YAML, FileContentType.CANONICAL_JSON]
Attributes¤
CANONICAL_JSON = 3 class-attribute instance-attribute ¤
DIRLIKE = 4 class-attribute instance-attribute ¤
JSON = 1 class-attribute instance-attribute ¤
UNKNOWN = 5 class-attribute instance-attribute ¤
YAML = 2 class-attribute instance-attribute ¤
Methods:¤
dir_to_content_type(dir_path) classmethod ¤

Get content type by looking for json or yaml files in dir.

Source code in trestle/core/models/file_content_type.py
87
88
89
90
91
92
93
94
95
96
97
98
@classmethod
def dir_to_content_type(cls, dir_path: Path) -> 'FileContentType':
    """Get content type by looking for json or yaml files in dir."""
    files = dir_path.glob('*')
    for file in files:
        if file.is_file():
            suffix = file.suffix
            if suffix == '.json':
                return FileContentType.JSON
            if suffix in ['.yaml', '.yml']:
                return FileContentType.YAML
    return FileContentType.UNKNOWN
is_readable_file(content_type) classmethod ¤

Is the file a type that can be read directly.

Source code in trestle/core/models/file_content_type.py
111
112
113
114
@classmethod
def is_readable_file(cls, content_type: 'FileContentType') -> bool:
    """Is the file a type that can be read directly."""
    return content_type in [FileContentType.JSON, FileContentType.YAML, FileContentType.CANONICAL_JSON]
path_suffix_to_content_type(file_path) classmethod ¤

Get content type from an explicit file path's suffixes without checking existence.

Source code in trestle/core/models/file_content_type.py
64
65
66
67
68
69
@classmethod
def path_suffix_to_content_type(cls, file_path: Path) -> 'FileContentType':
    """Get content type from an explicit file path's suffixes without checking existence."""
    if cls._has_canonical_json_suffix(file_path):
        return FileContentType.CANONICAL_JSON
    return FileContentType.to_content_type(file_path.suffix)
path_to_content_type(file_path) classmethod ¤

Get content type from file path looking for extension.

Source code in trestle/core/models/file_content_type.py
76
77
78
79
80
81
82
83
84
85
@classmethod
def path_to_content_type(cls, file_path: Path) -> 'FileContentType':
    """Get content type from file path looking for extension."""
    if file_path.with_suffix('.json').exists():
        return FileContentType.JSON
    if file_path.with_suffix('.yaml').exists():
        return FileContentType.YAML
    if file_path.with_suffix('.yml').exists():
        return FileContentType.YAML
    return FileContentType.UNKNOWN
path_to_file_extension(file_path) classmethod ¤

Get extension from file path looking for extension.

Source code in trestle/core/models/file_content_type.py
100
101
102
103
104
105
106
107
108
109
@classmethod
def path_to_file_extension(cls, file_path: Path) -> str:
    """Get extension from file path looking for extension."""
    if file_path.with_suffix('.json').exists():
        return '.json'
    if file_path.with_suffix('.yaml').exists():
        return '.yaml'
    if file_path.with_suffix('.yml').exists():
        return '.yml'
    return ''
to_content_type(file_extension) classmethod ¤

Get content type form file extension, including the dot.

Source code in trestle/core/models/file_content_type.py
50
51
52
53
54
55
56
57
58
59
60
61
62
@classmethod
def to_content_type(cls, file_extension: str) -> 'FileContentType':
    """Get content type form file extension, including the dot."""
    if file_extension == '.canonical.json':
        return FileContentType.CANONICAL_JSON
    if file_extension == '.json':
        return FileContentType.JSON
    if file_extension == '.yaml' or file_extension == '.yml':
        return FileContentType.YAML
    if not file_extension:
        return FileContentType.DIRLIKE

    raise TrestleError(f'Unsupported file extension {file_extension}')
to_file_extension(content_type) classmethod ¤

Get file extension for the type, including the dot.

Source code in trestle/core/models/file_content_type.py
41
42
43
44
45
46
47
48
@classmethod
def to_file_extension(cls, content_type: 'FileContentType') -> str:
    """Get file extension for the type, including the dot."""
    if content_type == FileContentType.YAML:
        return '.yaml'
    if content_type in [FileContentType.JSON, FileContentType.CANONICAL_JSON]:
        return '.json'
    raise TrestleError(f'Invalid file content type {content_type}')

handler: python