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
class FileContentType(Enum):
    """File Content type for read/write."""

    # JSON formatted content
    JSON = 1

    # YAML formatted content
    YAML = 2

    # No extension and possibly a DIR
    DIRLIKE = 3

    # Type could not be determined
    UNKNOWN = 4

    @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 == FileContentType.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 == '.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_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 == FileContentType.JSON or content_type == FileContentType.YAML
Attributes¤
DIRLIKE = 3 class-attribute instance-attribute ¤
JSON = 1 class-attribute instance-attribute ¤
UNKNOWN = 4 class-attribute instance-attribute ¤
YAML = 2 class-attribute instance-attribute ¤
Functions¤
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
69
70
71
72
73
74
75
76
77
78
79
80
@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
93
94
95
96
@classmethod
def is_readable_file(cls, content_type: 'FileContentType') -> bool:
    """Is the file a type that can be read directly."""
    return content_type == FileContentType.JSON or content_type == FileContentType.YAML
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
58
59
60
61
62
63
64
65
66
67
@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
82
83
84
85
86
87
88
89
90
91
@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
46
47
48
49
50
51
52
53
54
55
56
@classmethod
def to_content_type(cls, file_extension: str) -> 'FileContentType':
    """Get content type form file extension, including the dot."""
    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
37
38
39
40
41
42
43
44
@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 == FileContentType.JSON:
        return '.json'
    raise TrestleError(f'Invalid file content type {content_type}')

handler: python