Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[12.0][BPRT] storage_backend: add _find_files + _move_files #367

Open
wants to merge 1 commit into
base: 12.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions storage_backend/components/base_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import os
import re

from odoo.addons.component.core import AbstractComponent

Expand All @@ -26,5 +27,34 @@ def get(self, relative_path, **kwargs):
def list(self, relative_path=""):
raise NotImplementedError

def find_files(self, pattern, relative_path="", **kwargs):
"""Find files matching given pattern.

:param pattern: regex expression
:param relative_path: optional relative path containing files
:return: list of file paths as full paths from the root
"""
regex = re.compile(pattern)
filelist = self.list(relative_path)
files_matching = [
regex.match(file_).group() for file_ in filelist if regex.match(file_)
]
filepaths = []
if files_matching:
filepaths = [
os.path.join(self._fullpath(relative_path) or "", filename)
for filename in files_matching
]
return filepaths

def move_files(self, files, destination_path, **kwargs):
"""Move files to given destination.

:param files: list of file paths to be moved
:param destination_path: directory path where to move files
:return: None
"""
raise NotImplementedError

def delete(self, relative_path):
raise NotImplementedError
15 changes: 11 additions & 4 deletions storage_backend/models/storage_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,27 @@ def _list(self, relative_path="", pattern=False):
names = fnmatch.filter(names, pattern)
return names

def _find_files(self, pattern, relative_path="", **kw):
return self._forward("find_files", pattern, relative_path=relative_path)

def _move_files(self, files, destination_path, **kw):
return self._forward("move_files", files, destination_path)

def _delete(self, relative_path):
return self._forward("delete", relative_path)

def _forward(self, method, relative_path, *args, **kwargs):
def _forward(self, method, *args, **kwargs):
_logger.debug(
"Backend Storage ID: %s type %s: %s file %s",
"Backend Storage ID: %s type %s: %s file %s %s",
self.backend_type,
self.id,
method,
relative_path,
args,
kwargs,
)
self.ensure_one()
adapter = self._get_adapter()
return getattr(adapter, method)(relative_path, *args, **kwargs)
return getattr(adapter, method)(*args, **kwargs)

def _get_adapter(self):
with self.work_on(self._name) as work:
Expand Down
Loading