Debian Codegraph @

lib/utils/file.py

raw
1 # -*- coding: utf-8 -*-
2 # This program is free software; you can redistribute it and/or modify
3 # it under the terms of the GNU General Public License as published by
4 # the Free Software Foundation; either version 2 of the License, or
5 # (at your option) any later version.
6 #
7 # This program is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 # GNU General Public License for more details.
11 #
12 # You should have received a copy of the GNU General Public License
13 # along with this program; if not, write to the Free Software
14 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
15 # MA 02110-1301, USA.
16 #
17 # Author: Mauro Soria
18
19 from __future__ import annotations
20
21 import os
22 import os.path
23
24
25 class File:
26 def __init__(self, *path_components):
27 self._path = FileUtils.build_path(*path_components)
28
29 @property
30 def path(self):
31 return self._path
32
33 @path.setter
34 def path(self, value):
35 raise NotImplementedError
36
37 def is_valid(self):
38 return FileUtils.is_file(self.path)
39
40 def exists(self):
41 return FileUtils.exists(self.path)
42
43 def can_read(self):
44 return FileUtils.can_read(self.path)
45
46 def can_write(self):
47 return FileUtils.can_write(self.path)
48
49 def read(self):
50 return FileUtils.read(self.path)
51
52 def get_lines(self):
53 return FileUtils.get_lines(self.path)
54
55 def __enter__(self):
56 return self
57
58 def __exit__(self, type, value, tb):
59 pass
60
61
62 class FileUtils:
63 @staticmethod
64 def build_path(*path_components: str) -> str:
65 if path_components:
66 path = os.path.join(*path_components)
67 else:
68 path = ""
69
70 return path
71
72 @staticmethod
73 def get_abs_path(file_name):
74 return os.path.abspath(file_name)
75
76 @staticmethod
77 def exists(file_name):
78 return os.access(file_name, os.F_OK)
79
80 @staticmethod
81 def is_empty(file_name):
82 return os.stat(file_name).st_size == 0
83
84 @staticmethod
85 def can_read(file_name):
86 try:
87 with open(file_name):
88 pass
89 except OSError:
90 return False
91
92 return True
93
94 @classmethod
95 def can_write(cls, path):
96 while not cls.exists(path):
97 path = cls.parent(path)
98
99 return os.access(path, os.W_OK)
100
101 @staticmethod
102 def read(file_name):
103 return open(file_name, "r").read()
104
105 @classmethod
106 def get_files(cls, directory):
107 files = []
108
109 for path in os.listdir(directory):
110 path = os.path.join(directory, path)
111 if cls.is_dir(path):
112 files.extend(cls.get_files(path))
113 else:
114 files.append(path)
115
116 return files
117
118 @staticmethod
119 def get_lines(file_name: str) -> list[str]:
120 with open(file_name, "r", errors="replace") as fd:
121 return fd.read().splitlines()
122
123 @staticmethod
124 def is_dir(path):
125 return os.path.isdir(path)
126
127 @staticmethod
128 def is_file(path):
129 return os.path.isfile(path)
130
131 @staticmethod
132 def parent(path, depth=1):
133 for _ in range(depth):
134 path = os.path.dirname(path)
135
136 return path
137
138 @classmethod
139 def create_dir(cls, directory):
140 if not cls.exists(directory):
141 os.makedirs(directory, exist_ok=True)
142
143 @staticmethod
144 def write_lines(file_name, lines, overwrite=False):
145 if isinstance(lines, list):
146 lines = os.linesep.join(lines)
147 with open(file_name, "w" if overwrite else "a") as f:
148 f.writelines(lines)