-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.py
More file actions
48 lines (37 loc) · 1.73 KB
/
base.py
File metadata and controls
48 lines (37 loc) · 1.73 KB
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
import os
from abc import abstractmethod
from typing import Optional
from .interface import IFileInfoExtractor
class TBaseFileInfoExtractor(IFileInfoExtractor):
def get_dir(self, file_path_abs: str) -> str:
dir_path: str = os.path.dirname(file_path_abs)
if dir_path and not dir_path.endswith(os.sep):
dir_path += os.sep
return dir_path
def get_dir_without_trailing_slash(self, file_path_abs: str) -> str:
return os.path.dirname(file_path_abs).rstrip(os.sep)
def get_drive_letter(self, file_path_abs: str) -> str:
drive, _ = os.path.splitdrive(file_path_abs)
return drive
def get_file_ext(self, file_path_abs: str) -> str:
base: str = self.get_file_name(file_path_abs)
dot_pos: int = base.rfind(".")
return base[dot_pos : len(base)].lower() if dot_pos != -1 else ""
def get_file_name(self, file_path_abs: str) -> str:
return os.path.basename(file_path_abs)
def get_file_name_without_ext(self, file_path_abs: str) -> str:
base: str = self.get_file_name(file_path_abs)
dot_pos: int = base.rfind(".")
return base[:dot_pos] if dot_pos != -1 else base
@abstractmethod
def get_file_type(self, file_path_abs: str) -> Optional[str]:
raise NotImplementedError
def get_shebang(self, file_path_abs: str) -> Optional[str]:
if not os.path.exists(file_path_abs):
return None
try:
with open(file_path_abs, "r", encoding="utf-8") as fin:
first_line: str = fin.readline().strip()
return first_line[2:] if first_line.startswith("#!") and not first_line.startswith("#![") else None
except (IOError, UnicodeDecodeError):
return None