Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download safeguarding files from Google Drive (optionally) #145

Merged
merged 1 commit into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ dependencies = [
"packaging~=21.3",
"rapidpro-abtesting @ git+https://github.com/IDEMSInternational/rapidpro_abtesting.git@master",
"requests~=2.31",
"rpft @ git+https://github.com/IDEMSInternational/rapidpro-flow-toolkit.git@1.6.0",
"rpft @ git+https://github.com/IDEMSInternational/rapidpro-flow-toolkit.git@1.7.0",
]

[project.scripts]
Expand Down
4 changes: 2 additions & 2 deletions src/parenttext_pipeline/extract_keywords.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ def process_keywords(sources):


def process_source(source):
input_file = source["path"]
input_file = source.get("location") or source["path"]
language = source["key"]
book = openpyxl.load_workbook(input_file)
book = openpyxl.load_workbook(input_file, read_only=True)
all_tables = {}

for sheet in book.worksheets:
Expand Down
31 changes: 25 additions & 6 deletions src/parenttext_pipeline/pull_data.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import os
import re
import shutil
import tempfile
from datetime import datetime, timezone
from pathlib import Path

import requests
from rpft.converters import convert_to_json
from rpft.google import Drive

from parenttext_pipeline.common import (
clear_or_create_folder,
Expand Down Expand Up @@ -139,16 +141,33 @@ def pull_json(config, source, source_name):
shutil.copyfile(filepath, source_input_path / f"{new_name}.json")


def is_google_drive_file_id(location):
return bool(re.fullmatch(r"[a-z0-9_-]{33}", location, re.IGNORECASE))


def pull_safeguarding(config, source, source_name):
# Safeguarding files
source_input_path = get_input_subfolder(
config, source_name, makedirs=True, in_temp=False
keywords_file_path = (
get_input_subfolder(config, source_name, makedirs=True, in_temp=False)
/ "safeguarding_words.json"
)
safeguarding_file_path = source_input_path / "safeguarding_words.json"

if source.sources:
process_keywords_to_file(source.sources, safeguarding_file_path)

with tempfile.TemporaryDirectory() as dest:

for s in source.sources:
location = s.get("location") or s["path"]

if is_google_drive_file_id(location):
name, content = Drive.fetch(location)
s["location"] = Path(dest) / (location + Path(name).suffix)

with open(s["location"], "wb") as f:
f.write(content)

process_keywords_to_file(source.sources, keywords_file_path)
else:
shutil.copyfile(source.filepath, safeguarding_file_path)
shutil.copyfile(source.filepath, keywords_file_path)


def unpack_archive(destination, location):
Expand Down