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

Add presets for metadata-extraction script #146

Merged
merged 4 commits into from
Feb 17, 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
8 changes: 7 additions & 1 deletion admin/scripts/common/models/notion.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,20 @@ class Contact(BaseModel):
organisationName: str

name: Optional[str] = Field(default=None)
websites: List[str]
summary: str
websites: List[str]
contact: Contact

def test(self):
return self.contact.name


class Overview(BaseModel):
name: Optional[str] = Field(default=None)
summary: str
websites: List[str]


class Project(BaseModel):
name: str = ""
subgrants: List[str] = []
32 changes: 30 additions & 2 deletions admin/scripts/extract_project_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,31 @@

import ijson
from common.models.dashboard import Fund
from common.models.notion import Subgrant
from common.models.notion import Overview, Subgrant
from common.utils import dir_path
from pydantic import ValidationError
from tqdm import tqdm

# fmt: off
choices = {
"default": lambda subgrants: {
s.name: s.model_dump()
for s in subgrants
},
"emails": lambda subgrants: sorted(
set(s.contact.email for s in subgrants),
key=lambda email: email.split("@")[1]
),
"overview": lambda subgrants: {
o.name: o.model_dump()
for o in [
Overview(name=s.name, websites=s.websites, summary=s.summary)
for s in subgrants
]
},
}
# fmt: on


class Cli:
def __init__(self) -> None:
Expand All @@ -32,6 +52,13 @@ def __init__(self) -> None:
action="store_true",
help="Print debugging information",
)
self.parser.add_argument(
"-p",
"--preset",
choices=choices.keys(),
default="default",
help="Output the metadata in a specific format",
)

if len(sys.argv) == 1:
self.parser.print_help()
Expand Down Expand Up @@ -79,7 +106,8 @@ def main():
except ValidationError as e:
logger.error(e)

content = {s.name: s.model_dump() for s in subgrants}
content = choices[args.preset](subgrants)

json.dump(content, sys.stdout, indent=2)


Expand Down