-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_handler.py
More file actions
134 lines (107 loc) · 3.82 KB
/
file_handler.py
File metadata and controls
134 lines (107 loc) · 3.82 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# -*- coding: utf-8 -*-
"""
The MIT License (MIT)
Copyright (c) 2020 Nortxort
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
from pathlib import Path
class FileHandler:
"""
FileHandler class for various operation at the given path.
"""
def __init__(self, path: str):
"""
Initialize the FileHandler class.
:param path: The work path.
"""
self._path = Path(path)
self._old_samples = []
self._dir_created = False
@property
def path(self):
"""
Returns the absolute path
"""
return self._path.resolve()
@property
def was_dir_created(self) -> bool:
"""
Check if a directory was created at the work path.
:return: True if a directory was created, else False.
"""
return self._dir_created
def is_valid_path(self) -> bool:
"""
Check if the path is valid.
:return: True if valid, else False.
"""
if self._path.exists():
return True
return False
def is_dir(self) -> bool:
"""
Check directory.
:return: True if valid directory, else False.
"""
if self._path.is_dir():
return True
return False
def create_dir(self):
"""
Create a directory.
"""
self._path.mkdir()
self._dir_created = True
def iter_root_dir(self) -> list:
"""
Iter the work path directory.
"""
if self._path.is_dir():
for d in self._path.iterdir():
if d.is_file() and d.name.endswith('.zip'):
# *.zip files in this folder is sample packs
self._old_samples.append(d.name)
if d.is_dir():
self._old_samples.append(d.name)
return self._old_samples
def compare(self, new_samples: list, old_samples: list) -> tuple:
"""
Compare two lists.
:param new_samples: list of SamplePack objects.
:param old_samples: list of sample pack names.
:return: tuple of list(a) not in old samples and list(b) in old samples.
"""
to_download = []
ignored = []
if len(old_samples) == 0:
to_download = new_samples
return to_download, ignored
for sample in new_samples:
name = sample.file_name.replace('.zip', '')
if name not in self._remove_extension(old_samples): # check old samples for .zip
to_download.append(sample)
else:
ignored.append(sample)
return to_download, ignored
@staticmethod
def _remove_extension(sample_list: list) -> list:
_ = []
for s in sample_list:
if s.endswith('.zip'):
s = s.replace('.zip', '')
_.append(s)
return _