Skip to content

Commit

Permalink
Merge pull request #2 from opengisch/path_starts_with
Browse files Browse the repository at this point in the history
Fix hidden username requirement and rename 'subdir' parameter to 'path-starts-with'
  • Loading branch information
suricactus authored Sep 10, 2021
2 parents 022d1a8 + a42df61 commit 20c2a26
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ Download QFieldCloud project files.
qfieldcloud-cli download-files [OPTIONS] PROJECT_ID LOCAL_DIR
Options:
--subdir TEXT Do not download the whole project, but only
the subdirectory passed.
--path-starts-with TEXT Do not download the whole project, but only
the files which path starts with the string.
--exit-on-error / --no-exit-on-error
If any project file download fails stop
Expand Down
11 changes: 5 additions & 6 deletions src/bin/qfieldcloud-cli
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,9 @@ def login(ctx, username, password) -> None:
help="Includes the public project in the list. Default: False",
)
@click.pass_context
def list_projects(ctx, username, include_public):
def list_projects(ctx, include_public):
"""List QFieldCloud projects."""
projects = ctx.obj["client"].list_projects(
username=username,
include_public=include_public,
)

Expand Down Expand Up @@ -157,18 +156,18 @@ def list_files(ctx, project_id):
@click.argument("project_id")
@click.argument("local_dir")
@click.option(
"--subdir",
help="Do not download the whole project, but only the subdirectory passed.",
"--path-starts-with",
help="Do not download the whole project, but only the files which path starts with the string.",
)
@click.option(
"--exit-on-error/--no-exit-on-error",
help="If any project file download fails stop downloading the rest. Default: False",
)
@click.pass_context
def download_files(ctx, project_id, local_dir, subdir, exit_on_error):
def download_files(ctx, project_id, local_dir, path_starts_with, exit_on_error):
"""Download QFieldCloud project files."""
files = ctx.obj["client"].download_files(
project_id, local_dir, subdir, exit_on_error
project_id, local_dir, path_starts_with, exit_on_error
)

if ctx.obj["format_json"]:
Expand Down
15 changes: 9 additions & 6 deletions src/qfieldcloud_sdk/sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ def list_projects(
"GET",
"projects",
params={
"username": username or "",
"include-public": "1" if include_public else "0",
},
)
Expand All @@ -70,15 +69,15 @@ def download_files(
self,
project_id: str,
local_dir: str,
subdir: str = None,
path_starts_with: str = None,
continue_on_error: bool = False,
) -> List[Dict]:
"""Download the specified project files into the destination dir
"""Download the specified project files into the destination dir.
Args:
project_id: id of the project to be downloaded
local_dir: destination directory where the files will be downloaded
subdir: if specified, download only files that are withing that subdirectory, otherwise download all
path_starts_with: if specified, download only files that are within that path starts with, otherwise download all
"""

files = self.list_files(project_id)
Expand All @@ -88,13 +87,17 @@ def download_files(
file["status"] = DownloadStatus.PENDING
file["status_reason"] = ""

files_to_download = []

for file in files:
local_file = Path(f'{local_dir}/{file["name"]}')
resp = None

if subdir and not file["name"].startswith(subdir):
if path_starts_with and not file["name"].startswith(path_starts_with):
continue

files_to_download.append(file)

try:
resp = self._request(
"GET",
Expand Down Expand Up @@ -126,7 +129,7 @@ def download_files(
f.write(chunk)
files_count += 1

return files
return files_to_download

def _request(
self,
Expand Down

0 comments on commit 20c2a26

Please sign in to comment.