summaryrefslogtreecommitdiff
path: root/py04/ex2/ft_stream_management.py
blob: 4c2544bcba8f70e82643f0656229e15f1a00e404 (plain)
1
2
3
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
#!/usr/bin/env python3

import sys


def read_content() -> None:
    file = None
    try:
        file = open(sys.argv[1])
        content = file.read()
        print(f"Accessing file {sys.argv[1]}")
        print("---")
        print()
        print(content)
    except IndexError:
        print("[STDERR] Usage: ft_ancient_text.py <file>", file=sys.stderr)
    except FileNotFoundError as e:
        print(f"[STDERR] Error opening the file '{sys.argv[1]}':",
              e, file=sys.stderr)
    # trying to write when disk is full
    # also catch permission errors
    except OSError as e:
        print("[STDERR]", e, file=sys.stderr)
    # No permission to read or write
    except PermissionError as e:
        print("[STDERR]", e, file=sys.stderr)
    # file encoded in a format other than UTF-8
    except UnicodeDecodeError as e:
        print("[STDERR]", e, file=sys.stderr)
    except IsADirectoryError as e:
        print("[STDERR]", e, file=sys.stderr)
    finally:
        if file:
            file.close()
            print("---")
            print(f"File '{sys.argv[1]}' closed.")


def new_preservation_protocol() -> None:
    # give files a value so I can close them anyway
    original_file = None
    new_file = None
    # create a list to store modified lines
    data = []
    try:
        original_file = open(sys.argv[1], 'r')
        print("Transform data")
        print("---")
        for line in original_file:
            line = line.rstrip('\n')
            line = line + "#"
            data.append(line)
            print(line)
        original_file.close()
        sys.stdout.write("Enter a new file name (or empty): ")
        # flush buffer
        sys.stdout.flush()
        new_file_name = sys.stdin.readline()
        new_file_name = new_file_name.rstrip('\n')
        if not new_file_name:
            print("Not saving data")
        else:
            print(f"Saving data to '{new_file_name}'")
            new_file = open(new_file_name, 'w')
            for line in data:
                new_file.write(line)
                new_file.write('\n')
            print(f"Date saved in file '{new_file_name}'")
            new_file.close()
    except IndexError:
        print("[STDERR] No file to copy", file=sys.stderr)
    except FileNotFoundError as e:
        print(f"[STDERR] Error opening the file '{sys.argv[1]}':",
              e, file=sys.stderr)
    # trying to write when disk is full
    # also catch permission errors
    except OSError as e:
        print("[STDERR]", e, file=sys.stderr)
    # No permission to read or write
    except PermissionError as e:
        print("[STDERR]", e, file=sys.stderr)
    # file encoded in a format other than UTF-8
    except UnicodeDecodeError as e:
        print("[STDERR]", e, file=sys.stderr)
    except IsADirectoryError as e:
        print("[STDERR]", e, file=sys.stderr)
    finally:
        if original_file:
            original_file.close()
        if new_file:
            new_file.close()


def main() -> None:
    print("=== Cyber Archives Recovery & Preservation ===")
    read_content()
    print()
    new_preservation_protocol()


if __name__ == "__main__":
    main()