Skip to content

Commit 5e57c33

Browse files
authored
Merge pull request #276 from mapswipe/circular_imports
Circular imports
2 parents c73bdca + 843c60d commit 5e57c33

File tree

5 files changed

+45
-57
lines changed

5 files changed

+45
-57
lines changed

mapswipe_workers/Dockerfile

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
# This image contains Python 3 and the latest pre-compiled version of GDAL
2-
# Based on Debian (Including apt-get update && upgrade)
3-
FROM thinkwhere/gdal-python:3.7-shippable
1+
# https://github.com/OSGeo/gdal/tree/master/gdal/docker
2+
# Image includes python3.6, gdal-python, gdal-bin
3+
FROM osgeo/gdal:ubuntu-small-latest
44

5-
# Install gdal-bin to get ogr2ogr tool
5+
ENV LC_ALL=C.UTF-8
6+
ENV LANG=C.UTF-8
7+
8+
# Install pip
69
RUN apt-get update
7-
RUN apt-get --yes install gdal-bin
10+
RUN apt-get --yes install python3-pip
811

9-
# create directories for config, logs and data
12+
# Create directories for config, logs and data
1013
ARG config_dir=/usr/share/config/mapswipe_workers/
1114
ARG repo_dir=/usr/local/mapswipe_workers/
1215
ARG data_dir=/var/lib/mapswipe_workers/
@@ -26,7 +29,7 @@ RUN mkdir -p $data_dir"api-data/tasks/"
2629
RUN mkdir -p $data_dir"api-data/yes_maybe/"
2730
RUN mkdir -p $data_dir"api-data/hot_tm/"
2831

29-
# copy mapswipe workers repo from local repo
32+
# Copy mapswipe workers repo from local repo
3033
WORKDIR $repo_dir
3134
COPY mapswipe_workers/ mapswipe_workers/
3235
COPY sample_data/ sample_data/
@@ -35,8 +38,8 @@ COPY requirements.txt .
3538
COPY setup.py .
3639
COPY config $config_dir
3740

38-
# Install dependencies and mapswipe-workers
39-
# RUN python setup.py install
40-
RUN pip install .
41+
# Update setuptools and install mapswipe-workers with dependencies (requirements.txt)
42+
RUN pip3 install --upgrade setuptools
43+
RUN pip3 install .
4144

42-
# we don't use a CMD here, this will be defined in docker-compose.yaml
45+
# Don't use a CMD here, this will be defined in docker-compose.yaml

mapswipe_workers/mapswipe_workers/definitions.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,6 @@
55

66
import sentry_sdk
77

8-
from mapswipe_workers.project_types.build_area.build_area_project import (
9-
BuildAreaProject,
10-
)
11-
from mapswipe_workers.project_types.change_detection.change_detection_project import (
12-
ChangeDetectionProject,
13-
)
14-
from mapswipe_workers.project_types.footprint.footprint_project import FootprintProject
15-
168

179
class CustomError(Exception):
1810
pass
@@ -38,18 +30,6 @@ def load_config(CONFIG_PATH) -> dict:
3830

3931
DATA_PATH = os.path.abspath("/var/lib/mapswipe_workers/")
4032

41-
PROJECT_TYPE_CLASSES = {
42-
1: BuildAreaProject,
43-
2: FootprintProject,
44-
3: ChangeDetectionProject,
45-
}
46-
47-
PROJECT_TYPE_NAMES = {
48-
1: BuildAreaProject.project_type_name,
49-
2: FootprintProject.project_type_name,
50-
3: ChangeDetectionProject.project_type_name,
51-
}
52-
5333
logging.config.fileConfig(fname=LOGGING_CONFIG_PATH, disable_existing_loggers=True)
5434
logger = logging.getLogger("Mapswipe Workers")
5535

mapswipe_workers/mapswipe_workers/firebase_to_postgres/transfer_results.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
import dateutil.parser
55

66
from mapswipe_workers import auth
7-
from mapswipe_workers.definitions import logger
7+
from mapswipe_workers.definitions import logger, sentry
88
from mapswipe_workers.firebase_to_postgres import update_data
9-
from mapswipe_workers.utils import sentry
109

1110

1211
def transfer_results(project_id_list=None):
@@ -224,4 +223,4 @@ def get_projects_from_postgres():
224223
project_ids = [i[0] for i in raw_ids]
225224

226225
del pg_db
227-
return project_ids
226+
return project_ids

mapswipe_workers/mapswipe_workers/mapswipe_workers.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,25 @@
44
import json
55
import time
66

7-
import schedule as sched
8-
97
import click
8+
import schedule as sched
109
from mapswipe_workers import auth
11-
from mapswipe_workers.definitions import (
12-
PROJECT_TYPE_CLASSES,
13-
CustomError,
14-
logger,
15-
sentry,
16-
)
10+
from mapswipe_workers.definitions import CustomError, logger, sentry
1711
from mapswipe_workers.firebase_to_postgres import (
1812
archive_project,
1913
transfer_results,
2014
update_data,
2115
)
2216
from mapswipe_workers.generate_stats import generate_stats
2317
from mapswipe_workers.project_types.build_area import build_area_tutorial
18+
from mapswipe_workers.project_types.build_area.build_area_project import (
19+
BuildAreaProject,
20+
)
2421
from mapswipe_workers.project_types.change_detection import change_detection_tutorial
22+
from mapswipe_workers.project_types.change_detection.change_detection_project import (
23+
ChangeDetectionProject,
24+
)
25+
from mapswipe_workers.project_types.footprint.footprint_project import FootprintProject
2526
from mapswipe_workers.utils import user_management
2627
from mapswipe_workers.utils.slack_helper import send_slack_message
2728

@@ -53,6 +54,13 @@ def run_create_projects():
5354
Create projects with groups and tasks.
5455
Save created projects, groups and tasks to Firebase and Postgres.
5556
"""
57+
58+
project_type_classes = {
59+
1: BuildAreaProject,
60+
2: FootprintProject,
61+
3: ChangeDetectionProject,
62+
}
63+
5664
fb_db = auth.firebaseDB()
5765
ref = fb_db.reference("v2/projectDrafts/")
5866
project_drafts = ref.get()
@@ -66,7 +74,7 @@ def run_create_projects():
6674
project_name = project_draft["name"]
6775
try:
6876
# Create a project object using appropriate class (project type).
69-
project = PROJECT_TYPE_CLASSES[project_type](project_draft)
77+
project = project_type_classes[project_type](project_draft)
7078
project.geometry = project.validate_geometries()
7179
project.create_groups()
7280
project.calc_required_results()
@@ -143,7 +151,7 @@ def run_user_management(email, manager) -> None:
143151
"--input-file",
144152
help=(f"A JSON file of the tutorial."),
145153
required=True,
146-
type=click.Path,
154+
type=str,
147155
)
148156
def run_create_tutorial(input_file) -> None:
149157
"""Create a tutorial project from provided JSON file."""
@@ -165,7 +173,7 @@ def run_create_tutorial(input_file) -> None:
165173
sentry.capture_exception()
166174

167175

168-
@click.command("archive")
176+
@cli.command("archive")
169177
@click.option(
170178
"--project-id", "-i", help=("Archive project with giving project id"), type=str,
171179
)
@@ -187,7 +195,7 @@ def run_archive_project(project_id, project_ids):
187195
archive_project.archive_project(project_ids)
188196

189197

190-
@click.command("run")
198+
@cli.command("run")
191199
@click.option(
192200
"--schedule", is_flag=True, help=("Schedule jobs to run every 10 minutes.")
193201
)

mapswipe_workers/mapswipe_workers/utils/user_management.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
from requests.exceptions import HTTPError
66

77
from mapswipe_workers.auth import firebaseDB
8-
from mapswipe_workers.definitions import CustomError
9-
from mapswipe_workers.definitions import logger
10-
from mapswipe_workers.auth import load_config
8+
from mapswipe_workers.definitions import (
9+
CustomError,
10+
logger,
11+
CONFIG
12+
)
1113

1214

1315
def set_project_manager_rights(email):
@@ -92,8 +94,7 @@ def delete_user(email):
9294

9395

9496
def sign_in_with_email_and_password(email, password):
95-
config = load_config()
96-
api_key = config['firebase']['api_key']
97+
api_key = CONFIG['firebase']['api_key']
9798
request_ref = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key={0}".format(api_key)
9899
headers = {"content-type": "application/json; charset=UTF-8"}
99100
data = json.dumps({"email": email, "password": password, "returnSecureToken": True})
@@ -104,8 +105,7 @@ def sign_in_with_email_and_password(email, password):
104105

105106

106107
def get_firebase_db(path, custom_arguments=None, token=None):
107-
config = load_config()
108-
databaseName = config['firebase']['database_name']
108+
databaseName = CONFIG['firebase']['database_name']
109109
database_url = f'https://{databaseName}.firebaseio.com'
110110
request_ref = '{0}{1}.json?{3}auth={2}'.format(database_url, path, token, custom_arguments)
111111
headers = {"content-type": "application/json; charset=UTF-8"}
@@ -122,8 +122,7 @@ def get_firebase_db(path, custom_arguments=None, token=None):
122122

123123

124124
def set_firebase_db(path, data, token=None):
125-
config = load_config()
126-
databaseName = config['firebase']['database_name']
125+
databaseName = CONFIG['firebase']['database_name']
127126
database_url = f'https://{databaseName}.firebaseio.com'
128127
request_ref = '{0}{1}.json?auth={2}'.format(database_url, path, token)
129128
headers = {"content-type": "application/json; charset=UTF-8"}
@@ -141,8 +140,7 @@ def set_firebase_db(path, data, token=None):
141140

142141

143142
def update_firebase_db(path, data, token=None):
144-
config = load_config()
145-
databaseName = config['firebase']['database_name']
143+
databaseName = CONFIG['firebase']['database_name']
146144
database_url = f'https://{databaseName}.firebaseio.com'
147145
request_ref = '{0}{1}.json?auth={2}'.format(database_url, path, token)
148146
headers = {"content-type": "application/json; charset=UTF-8"}

0 commit comments

Comments
 (0)