4 from http
import HTTPStatus
5 from shutil
import rmtree
6 from fsbase
import FsBase
, FsException
8 __author__
= "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
11 class FsLocal(FsBase
):
13 def __init__(self
, logger_name
='fs'):
14 self
.logger
= logging
.getLogger(logger_name
)
18 return {"fs": "local", "path": self
.path
}
20 def fs_connect(self
, config
):
22 if "logger_name" in config
:
23 self
.logger
= logging
.getLogger(config
["logger_name"])
24 self
.path
= config
["path"]
25 if not self
.path
.endswith("/"):
27 if not os
.path
.exists(self
.path
):
28 raise FsException("Invalid configuration param at '[storage]': path '{}' does not exist".format(
32 except Exception as e
: # TODO refine
33 raise FsException(str(e
))
35 def fs_disconnect(self
):
38 def mkdir(self
, folder
):
40 Creates a folder or parent object location
42 :return: None or raises and exception
45 os
.mkdir(self
.path
+ folder
)
46 except Exception as e
:
47 raise FsException(str(e
), http_code
=HTTPStatus
.INTERNAL_SERVER_ERROR
)
49 def file_exists(self
, storage
):
51 Indicates if "storage" file exist
52 :param storage: can be a str or a str list
55 if isinstance(storage
, str):
59 return os
.path
.exists(self
.path
+ f
)
61 def file_size(self
, storage
):
64 :param storage: can be a str or a str list
67 if isinstance(storage
, str):
71 return os
.path
.getsize(self
.path
+ f
)
73 def file_extract(self
, tar_object
, path
):
76 :param tar_object: object of type tar
77 :param path: can be a str or a str list, or a tar object where to extract the tar_object
80 if isinstance(path
, str):
83 f
= self
.path
+ "/".join(path
)
84 tar_object
.extractall(path
=f
)
86 def file_open(self
, storage
, mode
):
89 :param storage: can be a str or list of str
90 :param mode: file mode
93 if isinstance(storage
, str):
97 return open(self
.path
+ f
, mode
)
99 def file_delete(self
, storage
, ignore_non_exist
=False):
101 Delete storage content recursivelly
102 :param storage: can be a str or list of str
103 :param ignore_non_exist: not raise exception if storage does not exist
107 if isinstance(storage
, str):
108 f
= self
.path
+ storage
110 f
= self
.path
+ "/".join(storage
)
111 if os
.path
.exists(f
):
113 elif not ignore_non_exist
:
114 raise FsException("File {} does not exist".format(storage
), http_code
=HTTPStatus
.BAD_REQUEST
)