Skip to content

Commit 3af5064

Browse files
author
Kirill_Lekhov
committed
Add ignoring some fields during serialization/deserialization
1 parent 90c1493 commit 3af5064

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

django_spinproject/modules/gitlab_ci.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from ._base import BaseModule
2-
from .gitlab_ci_data import _V1_ENV, _V2_ENV, _V3_ENV, _V4_ENV, _V5_ENV
2+
from .gitlab_ci_data import _V1_ENV, _V2_ENV, _V3_ENV, _V4_ENV
33
from ..project.project_info import ProjectInfo
44

55

django_spinproject/project/memento.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,18 @@ def save(self, info: ProjectInfo, overwrite: bool = True) -> None:
4141
info: Project info which to be saved.
4242
overwrite: Flag for overwriting the project file.
4343
"""
44-
if not overwrite and self.does_project_file_exist():
45-
exit_with_output(f"{self.filename} file already exists", 1)
44+
serialization_settings = {}
45+
46+
if not overwrite:
47+
if self.does_project_file_exist():
48+
exit_with_output(f"{self.filename} file already exists", 1)
49+
50+
# If the project file is not overwritten, then a new project may be created.
51+
serialization_settings['is_initial'] = True
4652

4753
try:
4854
with open(self.filename, mode='w') as file:
49-
json.dump(info.serialize(), file, indent=2)
55+
json.dump(info.serialize(**serialization_settings), file, indent=2)
5056

5157
except PermissionError:
5258
exit_with_output("Unable to save project info. Permission denied", 1)

django_spinproject/project/project_info.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,23 @@ def deserialize(cls, raw_data: dict) -> 'ProjectInfo':
7070

7171
return instance
7272

73+
def serialize(self, **kwargs) -> dict:
74+
"""
75+
Serializes project info.
76+
77+
Args:
78+
**kwargs: serialization settings.
79+
80+
Notes:
81+
Not all project versions use all settings.
82+
Possible kwargs options:
83+
`is_initial` - Flag of the initial serialization of project information.
84+
85+
Returns:
86+
Project info in the form of a dictionary.
87+
"""
88+
return super().serialize()
89+
7390

7491
@VERSION_REGISTRY.register
7592
class ProjectInfoV1(ProjectInfo):
@@ -83,3 +100,35 @@ class ProjectInfoV2(ProjectInfo):
83100
ProjectConfig = ProjectConfigV2
84101
DockerConfig = DockerConfigV2
85102
VERSION = 2
103+
104+
def __init__(self, project_name: str, main: str = DEFAULT_MAIN):
105+
super().__init__(project_name, main)
106+
self.serialization_settings = {}
107+
108+
def serialize(self, **kwargs) -> dict:
109+
is_initial = kwargs.get('is_initial', False)
110+
serialized_data = super().serialize()
111+
112+
if is_initial or self.serialization_settings.get(self.DOCKER_LABEL, {}).get('ignore_username', False):
113+
self.serialization_settings.setdefault(self.DOCKER_LABEL, {})
114+
self.serialization_settings[self.DOCKER_LABEL]['ignore_username'] = True
115+
del serialized_data[self.CONFIG_LABEL][self.DOCKER_LABEL]['username']
116+
117+
del serialized_data['serialization_settings']
118+
119+
return serialized_data
120+
121+
@classmethod
122+
def deserialize(cls, raw_data: dict) -> 'ProjectInfoV2':
123+
if raw_data.get(cls.CONFIG_LABEL, {}).get(cls.DOCKER_LABEL, {}).get('username') is None:
124+
docker_username_is_missing = True
125+
else:
126+
docker_username_is_missing = False
127+
128+
obj = super().deserialize(raw_data)
129+
130+
if docker_username_is_missing:
131+
obj.serialization_settings.setdefault(cls.DOCKER_LABEL, {})
132+
obj.serialization_settings[cls.DOCKER_LABEL]['ignore_username'] = True
133+
134+
return obj

0 commit comments

Comments
 (0)