-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasset.py
More file actions
93 lines (76 loc) · 3.92 KB
/
Copy pathasset.py
File metadata and controls
93 lines (76 loc) · 3.92 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
import os
import skript_fs_util as fs_util
import skript_keyword
import unpack
import uri_fetch
class AssetError(Exception):
pass
class Asset:
def __init__(self, dict_obj=None, **kwargs):
self.__dict__.update(dict_obj or {})
combined_dict = {**self.__dict__, **(kwargs or {})}
if len(combined_dict) < 1:
raise AssetError("Asset was provided without items, this is implementation error")
self.special_keys = ['url', 'post-process', 'destination', skript_keyword.POSITIONAL_ARGUMENT]
self.arg_name = next(iter(combined_dict))
if self.arg_name != skript_keyword.POSITIONAL_ARGUMENT and self.arg_name in self.special_keys:
raise AssetError(
f"The 'asset' definition shall begin either with '{skript_keyword.POSITIONAL_ARGUMENT}' or real argument name")
self.arg_value = combined_dict[self.arg_name]
if len(self.arg_value) < 1:
raise AssetError("C'mon the asset gotta have some meaningful resource name")
keys_not_special = [key for key in combined_dict if key not in self.special_keys and key != self.arg_name]
if len(keys_not_special) > 0:
raise AssetError(f"Asset definition has extraneous fields: {', '.join(keys_not_special)}")
for key in self.special_keys:
if key in combined_dict:
setattr(self, key, combined_dict[key])
else:
setattr(self, key, None)
self.asset_path = None
def create_arg_or_args(self, root_path="") -> list:
splitted = self.arg_value.split()
arg_created = []
for s in splitted:
single_arg = ""
if self.arg_name != skript_keyword.POSITIONAL_ARGUMENT:
if len(self.arg_name) == 1:
single_arg += "-"
else:
single_arg += "--"
single_arg += self.arg_name + " "
if os.path.isabs(s):
single_arg += s
else:
single_arg += root_path + "/" + s
arg_created.append(single_arg)
return arg_created
def fetch(self, asset_store_dir="", fetch_store_dir=""):
if os.path.isabs(self.arg_value):
return self.arg_value
exist_entry = fs_util.file_exists_in_directory(asset_store_dir, self.arg_value)
if exist_entry is None:
if self.url is None:
raise RuntimeError(f"Given asset {self.arg_value} not found in {asset_store_dir} and no source defined to download")
content_file = uri_fetch.fetch(self.url, fetch_store_dir)
if content_file is None:
raise RuntimeError(f"Given asset {self.arg_value} could not be downloaded from {self.url}")
if os.path.basename(content_file) != os.path.basename(self.arg_value) and self.__dict__["post-process"] is None:
raise RuntimeError(f"The asset '{self.arg_value}' fetched from '{self.url}' has different form - {content_file}")
if self.__dict__["post-process"] is not None:
action = self.__dict__["post-process"]
if action == "UNPACK":
store_path = asset_store_dir
if self.destination is not None:
store_path = os.path.join(asset_store_dir, self.destination)
fs_util.create_dirs_if_needed_for(store_path)
print(f"Extracting {content_file} to {store_path}...")
unpack.extract(os.path.join(fetch_store_dir, content_file), store_path)
else:
raise RuntimeError(
f"The post-process action {action} is not currently supported for asset {self.arg_value}")
else:
self.asset_path = exist_entry
return self.asset_path
self.asset_path = fs_util.file_exists_in_directory(asset_store_dir, self.arg_value)
return self.asset_path