Skip to content

Commit

Permalink
save full project information to .mergin/mergin.json file (fix #83)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Oct 16, 2023
1 parent 5610a91 commit 7f8d27e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 38 deletions.
10 changes: 1 addition & 9 deletions mergin/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,15 +519,7 @@ def create_project_and_push(self, project_name, directory, is_public=False, name
self.create_project(project_name, is_public)
if directory:
project_info = self.project_info(project_name)
MerginProject.write_metadata(
directory,
{
"name": project_name,
"version": "v0",
"files": [],
"project_id": project_info["id"],
},
)
MerginProject.write_metadata(directory, project_info)
mp = MerginProject(directory)
if mp.inspect_files():
self.push_project(directory)
Expand Down
19 changes: 2 additions & 17 deletions mergin/client_pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,7 @@ def download_project_finalize(job):
task.apply(job.directory, job.mp)

# final update of project metadata
# TODO: why not exact copy of project info JSON ?
job.mp.update_metadata(
{
"name": job.project_path,
"version": job.version,
"project_id": job.project_info["id"],
"files": job.project_info["files"],
}
)
job.mp.update_metadata(job.project_info)


def download_project_cancel(job):
Expand Down Expand Up @@ -613,14 +605,7 @@ def pull_project_finalize(job):
job.mp.log.info("--- pull aborted")
raise ClientError("Failed to apply pull changes: " + str(e))

job.mp.update_metadata(
{
"name": job.project_path,
"version": job.version if job.version else "v0", # for new projects server version is ""
"project_id": job.project_info["id"],
"files": job.project_info["files"],
}
)
job.mp.update_metadata(job.project_info)

if job.mp.has_unfinished_pull():
job.mp.log.info("--- failed to complete pull -- project left in the unfinished pull state")
Expand Down
9 changes: 1 addition & 8 deletions mergin/client_push.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,7 @@ def push_project_finalize(job):
job.mp.log.info("cancel response: " + str(err2))
raise err

job.mp.update_metadata(
{
"name": job.project_path,
"version": job.server_resp["version"],
"project_id": job.server_resp["id"],
"files": job.server_resp["files"],
}
)
job.mp.update_metadata(job.server_resp)
try:
job.mp.apply_push_changes(job.changes)
except Exception as e:
Expand Down
20 changes: 16 additions & 4 deletions mergin/merginproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,11 @@ def project_full_name(self) -> str:
"""Returns fully qualified project name: <workspace>/<name>"""
if self._metadata is None:
self._read_metadata()
return self._metadata["name"]
return (
self._metadata["name"]
if "/" in self._metadata["name"]
else f"{self._metadata['namespace']}/{self._metadata['name']}"
)

def project_name(self) -> str:
"""Returns only project name, without its workspace name"""
Expand All @@ -154,10 +158,18 @@ def workspace_name(self) -> str:
return full_name[:slash_index]

def project_id(self) -> str:
"""Returns ID of the project (UUID using 8-4-4-4-12 formatting without braces)"""
"""Returns ID of the project (UUID using 8-4-4-4-12 formatting without braces)
Raises ClientError if project id is not present in the project metadata.
"""
if self._metadata is None:
self._read_metadata()
return self._metadata["project_id"]

# "id" or "project_id" may not exist in projects downloaded with old client version
if "id" not in self._metadata and "project_id" not in self._metadata:
raise ClientError("Missed project id metadata. Please re-download your project.")

return self._metadata["project_id"] if "/" in self._metadata["name"] else self._metadata["id"]

def workspace_id(self) -> int:
"""Returns ID of the workspace where the project belongs"""
Expand All @@ -169,7 +181,7 @@ def version(self) -> str:
"""Returns project version (e.g. "v123")"""
if self._metadata is None:
self._read_metadata()
return self._metadata["version"]
return self._metadata["version"] if self._metadata["version"] else "v0"

def files(self) -> list:
"""Returns project's list of files (each file being a dictionary)"""
Expand Down

0 comments on commit 7f8d27e

Please sign in to comment.