Skip to content

Commit fd27250

Browse files
author
matthias_schaub
committed
Define project type and its class constructor in definition.py
1 parent 4569849 commit fd27250

File tree

2 files changed

+54
-30
lines changed

2 files changed

+54
-30
lines changed

mapswipe_workers/mapswipe_workers/definitions.py

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
11
import logging.config
22
import os
3-
import sentry_sdk
4-
from xdg import XDG_DATA_HOME
5-
from mapswipe_workers.config import SENTRY_DSN
63
from enum import Enum
74

5+
import sentry_sdk
6+
from xdg import XDG_DATA_HOME
87

9-
class CustomError(Exception):
10-
pass
11-
12-
13-
class MessageType(Enum):
14-
SUCCESS = 1
15-
FAIL = 2
16-
NOTIFICATION_90 = 3
17-
NOTIFICATION_100 = 4
18-
8+
from mapswipe_workers.config import SENTRY_DSN
199

2010
DATA_PATH = os.path.join(XDG_DATA_HOME, "mapswipe_workers")
2111
if not os.path.exists(DATA_PATH):
@@ -104,3 +94,44 @@ class MessageType(Enum):
10494
+ "tilematrix={z}&tilecol={x}&tilerow={y}&layer={layer}"
10595
),
10696
}
97+
98+
99+
class CustomError(Exception):
100+
pass
101+
102+
103+
class MessageType(Enum):
104+
SUCCESS = 1
105+
FAIL = 2
106+
NOTIFICATION_90 = 3
107+
NOTIFICATION_100 = 4
108+
109+
110+
class ProjectType(Enum):
111+
"""
112+
Definition of Project Type names, identifiers and constructors.
113+
114+
There are different project types with the same constructor.
115+
Get the class constructor of a project type with:
116+
ProjectType(1).constructor
117+
"""
118+
119+
BUILD_AREA = 1
120+
FOOTPRINT = 2
121+
CHANGE_DETECTION = 3
122+
123+
@property
124+
def constuctor(self):
125+
from mapswipe_workers.project_types.build_area.build_area_project import (
126+
BuildAreaProject,
127+
)
128+
from mapswipe_workers.project_types.footprint.footprint_project import (
129+
FootprintProject,
130+
)
131+
132+
project_type_classes = {
133+
1: BuildAreaProject,
134+
2: FootprintProject,
135+
3: BuildAreaProject,
136+
}
137+
return project_type_classes[self.value]

mapswipe_workers/mapswipe_workers/mapswipe_workers.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@
44
import json
55
import time
66

7+
import click
78
import schedule as sched
89

9-
import click
1010
from mapswipe_workers import auth
11-
from mapswipe_workers.definitions import CustomError, logger, sentry, MessageType
11+
from mapswipe_workers.definitions import (
12+
CustomError,
13+
MessageType,
14+
ProjectType,
15+
logger,
16+
sentry,
17+
)
1218
from mapswipe_workers.firebase_to_postgres import (
1319
archive_project,
1420
delete_project,
@@ -17,19 +23,12 @@
1723
)
1824
from mapswipe_workers.generate_stats import generate_stats
1925
from mapswipe_workers.project_types.build_area import build_area_tutorial
20-
from mapswipe_workers.project_types.build_area.build_area_project import (
21-
BuildAreaProject,
22-
)
2326
from mapswipe_workers.project_types.change_detection import change_detection_tutorial
24-
from mapswipe_workers.project_types.change_detection.change_detection_project import (
25-
ChangeDetectionProject,
26-
)
27-
from mapswipe_workers.project_types.footprint.footprint_project import FootprintProject
2827
from mapswipe_workers.utils import user_management
2928
from mapswipe_workers.utils.create_directories import create_directories
3029
from mapswipe_workers.utils.slack_helper import (
31-
send_slack_message,
3230
send_progress_notification,
31+
send_slack_message,
3332
)
3433

3534

@@ -63,12 +62,6 @@ def run_create_projects():
6362
Save created projects, groups and tasks to Firebase and Postgres.
6463
"""
6564

66-
project_type_classes = {
67-
1: BuildAreaProject,
68-
2: FootprintProject,
69-
3: ChangeDetectionProject,
70-
}
71-
7265
fb_db = auth.firebaseDB()
7366
ref = fb_db.reference("v2/projectDrafts/")
7467
project_drafts = ref.get()
@@ -83,7 +76,7 @@ def run_create_projects():
8376
project_name = project_draft["name"]
8477
try:
8578
# Create a project object using appropriate class (project type).
86-
project = project_type_classes[project_type](project_draft)
79+
project = ProjectType(project_type).constructor()
8780
project.geometry = project.validate_geometries()
8881
project.create_groups()
8982
project.calc_required_results()

0 commit comments

Comments
 (0)