|
| 1 | +"""Configuration for the user-item sync tool.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import argparse |
| 6 | +from dataclasses import dataclass, field |
| 7 | +from typing import List, Optional |
| 8 | + |
| 9 | +# Category page titles of the target item types (see opensemantic.base.v1). |
| 10 | +USER_CATEGORY = "Category:OSWd9aa0bca9b0040d8af6f5c091bf9eec7" |
| 11 | +ORGANIZATION_CATEGORY = "Category:OSW1969007d5acf40539642877659a02c23" |
| 12 | + |
| 13 | +# Public ORCID API used to enrich ORCID users. |
| 14 | +ORCID_API_BASE_DEFAULT = "https://pub.orcid.org/v3.0" |
| 15 | + |
| 16 | +# MediaWiki group whose members are excluded from the sync (bots). |
| 17 | +BOT_GROUP = "bot" |
| 18 | + |
| 19 | + |
| 20 | +@dataclass |
| 21 | +class SyncConfig: |
| 22 | + """Runtime options for a single sync run.""" |
| 23 | + |
| 24 | + domain: Optional[str] = None |
| 25 | + cred_filepath: Optional[str] = None |
| 26 | + dry_run: bool = False |
| 27 | + assume_yes: bool = False |
| 28 | + limit: Optional[int] = None |
| 29 | + include_non_orcid: bool = True |
| 30 | + exclude_bot_group: bool = True |
| 31 | + create_redirects: bool = True |
| 32 | + link_organizations: bool = True |
| 33 | + orcid_api_base: str = ORCID_API_BASE_DEFAULT |
| 34 | + excluded_groups: List[str] = field(default_factory=lambda: [BOT_GROUP]) |
| 35 | + |
| 36 | + |
| 37 | +def build_arg_parser() -> argparse.ArgumentParser: |
| 38 | + """Build the argument parser used by the example wrapper.""" |
| 39 | + parser = argparse.ArgumentParser( |
| 40 | + description="Create or update OSW User items from MediaWiki and ORCID.", |
| 41 | + ) |
| 42 | + parser.add_argument("--domain", help="Target OSL domain, e.g. llm4eln.semos.dev.") |
| 43 | + parser.add_argument( |
| 44 | + "--cred-filepath", |
| 45 | + dest="cred_filepath", |
| 46 | + help="Path to accounts.pwd.yaml (defaults to the working directory).", |
| 47 | + ) |
| 48 | + parser.add_argument( |
| 49 | + "--dry-run", |
| 50 | + action="store_true", |
| 51 | + help="Show the preview only; do not write anything.", |
| 52 | + ) |
| 53 | + parser.add_argument( |
| 54 | + "--yes", |
| 55 | + dest="assume_yes", |
| 56 | + action="store_true", |
| 57 | + help="Non-interactive: apply new items and gap-fills, keep existing on conflicts.", |
| 58 | + ) |
| 59 | + parser.add_argument( |
| 60 | + "--limit", |
| 61 | + type=int, |
| 62 | + help="Process at most this many MediaWiki users (for testing).", |
| 63 | + ) |
| 64 | + parser.add_argument( |
| 65 | + "--no-redirects", |
| 66 | + dest="create_redirects", |
| 67 | + action="store_false", |
| 68 | + help="Do not create User: redirect pages.", |
| 69 | + ) |
| 70 | + parser.add_argument( |
| 71 | + "--no-organizations", |
| 72 | + dest="link_organizations", |
| 73 | + action="store_false", |
| 74 | + help="Do not resolve or link ORCID affiliations to Organization items.", |
| 75 | + ) |
| 76 | + parser.add_argument( |
| 77 | + "--include-non-orcid", |
| 78 | + dest="include_non_orcid", |
| 79 | + action="store_true", |
| 80 | + default=True, |
| 81 | + help="Also sync non-bot accounts without an ORCID username (default).", |
| 82 | + ) |
| 83 | + parser.add_argument( |
| 84 | + "--orcid-only", |
| 85 | + dest="include_non_orcid", |
| 86 | + action="store_false", |
| 87 | + help="Sync only accounts whose username is an ORCID iD.", |
| 88 | + ) |
| 89 | + return parser |
| 90 | + |
| 91 | + |
| 92 | +def config_from_args(argv: Optional[List[str]] = None) -> SyncConfig: |
| 93 | + """Parse command-line arguments into a SyncConfig.""" |
| 94 | + args = build_arg_parser().parse_args(argv) |
| 95 | + return SyncConfig( |
| 96 | + domain=args.domain, |
| 97 | + cred_filepath=args.cred_filepath, |
| 98 | + dry_run=args.dry_run, |
| 99 | + assume_yes=args.assume_yes, |
| 100 | + limit=args.limit, |
| 101 | + include_non_orcid=args.include_non_orcid, |
| 102 | + create_redirects=args.create_redirects, |
| 103 | + link_organizations=args.link_organizations, |
| 104 | + ) |
0 commit comments