-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiles_handler.py
More file actions
71 lines (59 loc) · 2.12 KB
/
Copy pathfiles_handler.py
File metadata and controls
71 lines (59 loc) · 2.12 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import math
import os
import shutil
from exception_handler import exception_handler
from logger import Logger
import logging
from wit_exception import WitException
class FileHandler:
_working_directory = None
_base_path = "C:\\"
@classmethod
@property
def base_path(cls):
cls._base_path = cls.__find_base_dir()
return cls._base_path
@classmethod
@property
# @exception_handler
def working_directory(cls):
cls._working_directory = os.getcwd()
return cls._working_directory
@classmethod
@exception_handler
def __find_base_dir(cls):
item_path = cls.working_directory
parent_path = ""
drive = os.path.splitdrive(item_path)[0]
while not parent_path == f"{drive}\\":
doubt_wit_dir_path = os.path.join(item_path, ".wit")
if os.path.exists(doubt_wit_dir_path):
return doubt_wit_dir_path
parent_path = os.path.dirname(item_path)
item_path = parent_path
raise WitException("There is no .wit dir in project")
@classmethod
@exception_handler
def create_dir(cls, path):
os.mkdir(path)
message = f"A new folder {os.path.basename(os.path.normpath(path))} was created in the path: {path}"
Logger.log_to(level=logging.INFO, message=message)
@classmethod
@exception_handler
def get_full_path(cls, routing_end, routing_start=None):
if not routing_start:
routing_start = cls.working_directory
full_path = os.path.join(routing_start, routing_end)
if not os.path.exists(full_path):
message = f"No such path exists: {full_path}"
raise FileExistsError(message)
return full_path
@classmethod
@exception_handler
def copy_item(cls, origin, target):
if os.path.isfile(origin):
shutil.copy(origin, target)
else:
target = os.path.join(target, os.path.basename(os.path.normpath(origin)))
shutil.copytree(origin, target)
Logger.log_to(level=logging.INFO, message=f"An item has been copied from {origin} to {target}")