Skip to content

Commit

Permalink
black style
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Caha committed Mar 20, 2024
1 parent 9da311a commit ed127bb
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 21 deletions.
18 changes: 14 additions & 4 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class ConfigError(Exception):
def validate_config(config):
"""Validate config - make sure values are consistent"""

if not (config.mergin.username and config.mergin.password and config.mergin.project_name):
if not (
config.mergin.username and config.mergin.password and config.mergin.project_name
):
raise ConfigError("Config error: Incorrect mergin settings")

if config.driver not in ["local", "minio"]:
Expand All @@ -36,7 +38,10 @@ def validate_config(config):
raise ConfigError("Config error: Incorrect Local driver settings")

if config.driver == "minio" and not (
config.minio.endpoint and config.minio.access_key and config.minio.secret_key and config.minio.bucket
config.minio.endpoint
and config.minio.access_key
and config.minio.secret_key
and config.minio.bucket
):
raise ConfigError("Config error: Incorrect MinIO driver settings")

Expand All @@ -50,10 +55,15 @@ def validate_config(config):
config.update({"references": []})

if not isinstance(config.references, list):
raise ConfigError("Config error: Incorrect reference settings. Needs to be list of references.")
raise ConfigError(
"Config error: Incorrect reference settings. Needs to be list of references."
)

for ref in config.references:
if not all(hasattr(ref, attr) for attr in ["file", "table", "local_path_column", "driver_path_column"]):
if not all(
hasattr(ref, attr)
for attr in ["file", "table", "local_path_column", "driver_path_column"]
):
raise ConfigError("Config error: Incorrect media reference settings")


Expand Down
33 changes: 25 additions & 8 deletions media_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@ def _get_project_version():

def _check_has_working_dir():
if not os.path.exists(config.project_working_dir):
raise MediaSyncError("The project working directory does not exist: " + config.project_working_dir)
raise MediaSyncError(
"The project working directory does not exist: "
+ config.project_working_dir
)

if not os.path.exists(os.path.join(config.project_working_dir, ".mergin")):
raise MediaSyncError(
"The project working directory does not seem to contain Mergin project: " + config.project_working_dir
"The project working directory does not seem to contain Mergin project: "
+ config.project_working_dir
)


Expand All @@ -46,17 +50,24 @@ def _check_pending_changes():
status_push = mp.get_push_changes()
if status_push["added"] or status_push["updated"] or status_push["removed"]:
raise MediaSyncError(
"There are pending changes in the local directory - please review and push manually! " + str(status_push)
"There are pending changes in the local directory - please review and push manually! "
+ str(status_push)
)


def _get_media_sync_files(files):
"""Return files relevant to media sync from project files"""
allowed_extensions = config.allowed_extensions
files_to_upload = [f for f in files if os.path.splitext(f["path"])[1].lstrip(".") in allowed_extensions]
files_to_upload = [
f
for f in files
if os.path.splitext(f["path"])[1].lstrip(".") in allowed_extensions
]
# filter out files which are not under particular directory in mergin project
if "base_path" in config and config.base_path:
filtered_files = [f for f in files_to_upload if f["path"].startswith(config.base_path)]
filtered_files = [
f for f in files_to_upload if f["path"].startswith(config.base_path)
]
files_to_upload = filtered_files
return files_to_upload

Expand Down Expand Up @@ -130,7 +141,9 @@ def mc_pull(mc):
raise MediaSyncError("Mergin client error on pull: " + str(e))

print("Pulled new version from Mergin: " + _get_project_version())
files_to_upload = _get_media_sync_files(status_pull["added"] + status_pull["updated"])
files_to_upload = _get_media_sync_files(
status_pull["added"] + status_pull["updated"]
)
return files_to_upload


Expand All @@ -148,7 +161,9 @@ def _update_references(files):

print("Updating references ...")
try:
gpkg_conn = sqlite3.connect(os.path.join(config.project_working_dir, ref.file))
gpkg_conn = sqlite3.connect(
os.path.join(config.project_working_dir, ref.file)
)
gpkg_conn.enable_load_extension(True)
gpkg_cur = gpkg_conn.cursor()
gpkg_cur.execute('SELECT load_extension("mod_spatialite")')
Expand Down Expand Up @@ -210,7 +225,9 @@ def media_sync_push(mc, driver, files):
mp = MerginProject(config.project_working_dir)
status_push = mp.get_push_changes()
if status_push["added"]:
raise MediaSyncError("There are changes to be added - it should never happen")
raise MediaSyncError(
"There are changes to be added - it should never happen"
)
if status_push["updated"] or status_push["removed"]:
mc.push_project(config.project_working_dir)
version = _get_project_version()
Expand Down
16 changes: 12 additions & 4 deletions test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,30 @@ def test_config():
validate_config(config)

_reset_config()
with pytest.raises(ConfigError, match="Config error: Incorrect Local driver settings"):
with pytest.raises(
ConfigError, match="Config error: Incorrect Local driver settings"
):
config.update({"DRIVER": "local", "LOCAL__DEST": None})
validate_config(config)

_reset_config()
with pytest.raises(ConfigError, match="Config error: Incorrect MinIO driver settings"):
with pytest.raises(
ConfigError, match="Config error: Incorrect MinIO driver settings"
):
config.update({"DRIVER": "minio", "MINIO__ENDPOINT": None})
validate_config(config)

_reset_config()
with pytest.raises(ConfigError, match="Config error: Allowed extensions can not be empty"):
with pytest.raises(
ConfigError, match="Config error: Allowed extensions can not be empty"
):
config.update({"ALLOWED_EXTENSIONS": []})
validate_config(config)

_reset_config()
with pytest.raises(ConfigError, match="Config error: Incorrect media reference settings"):
with pytest.raises(
ConfigError, match="Config error: Incorrect media reference settings"
):
config.update({"REFERENCES": [{"file": "survey.gpkg"}]})
validate_config(config)

Expand Down
13 changes: 8 additions & 5 deletions test/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
WORKSPACE,
TMP_DIR,
USER_PWD,
USER_PWD,
SERVER_URL,
MINIO_URL,
MINIO_ACCESS_KEY,
Expand Down Expand Up @@ -512,14 +511,18 @@ def test_multiple_tables(mc):
),
],
)
def test_sync_without_references(mc: MerginClient, project_name: str, config_update: dict):
def test_sync_without_references(mc, project_name: str, config_update: dict):
"""
Test media sync running sync without references. It should not fail and just copy files.
The test just checks that main() runs without errors.
"""
full_project_name = API_USER + "/" + project_name
work_project_dir = os.path.join(TMP_DIR, project_name + "_work") # working dir for mediasync
driver_dir = os.path.join(TMP_DIR, project_name + "_driver") # destination dir for 'local' driver
full_project_name = WORKSPACE + "/" + project_name
work_project_dir = os.path.join(
TMP_DIR, project_name + "_work"
) # working dir for mediasync
driver_dir = os.path.join(
TMP_DIR, project_name + "_driver"
) # destination dir for 'local' driver

cleanup(mc, full_project_name, [work_project_dir, driver_dir])
prepare_mergin_project(mc, full_project_name)
Expand Down

0 comments on commit ed127bb

Please sign in to comment.