b88475f5d0cc597c3e14dcec45506b36ed26b671
3 from http
import HTTPStatus
4 from shutil
import rmtree
5 from fsbase
import FsBase
, FsException
14 return {"fs": "local", "path": self
.path
}
16 def fs_connect(self
, config
):
18 self
.path
= config
["path"]
19 if not self
.path
.endswith("/"):
21 if not os
.path
.exists(self
.path
):
22 raise FsException("Invalid configuration param at '[storage]': path '{}' does not exist".format(
26 except Exception as e
: # TODO refine
27 raise FsException(str(e
))
29 def fs_disconnect(self
):
32 def mkdir(self
, folder
):
34 Creates a folder or parent object location
36 :return: None or raises and exception
39 os
.mkdir(self
.path
+ folder
)
40 except Exception as e
:
41 raise FsException(str(e
), http_code
=HTTPStatus
.INTERNAL_SERVER_ERROR
.value
)
43 def file_exists(self
, storage
):
45 Indicates if "storage" file exist
46 :param storage: can be a str or a str list
49 if isinstance(storage
, str):
53 return os
.path
.exists(self
.path
+ f
)
55 def file_size(self
, storage
):
58 :param storage: can be a str or a str list
61 if isinstance(storage
, str):
65 return os
.path
.getsize(self
.path
+ f
)
67 def file_extract(self
, tar_object
, path
):
70 :param tar_object: object of type tar
71 :param path: can be a str or a str list, or a tar object where to extract the tar_object
74 if isinstance(path
, str):
77 f
= self
.path
+ "/".join(path
)
78 tar_object
.extractall(path
=f
)
80 def file_open(self
, storage
, mode
):
83 :param storage: can be a str or list of str
84 :param mode: file mode
87 if isinstance(storage
, str):
91 return open(self
.path
+ f
, mode
)
93 def file_delete(self
, storage
, ignore_non_exist
=False):
95 Delete storage content recursivelly
96 :param storage: can be a str or list of str
97 :param ignore_non_exist: not raise exception if storage does not exist
101 if isinstance(storage
, str):
102 f
= self
.path
+ storage
104 f
= self
.path
+ "/".join(storage
)
105 if os
.path
.exists(f
):
107 elif not ignore_non_exist
:
108 raise FsException("File {} does not exist".format(storage
), http_code
=HTTPStatus
.BAD_REQUEST
.value
)