Skip to main content

Python package for working in the Global Knowledge Commons (Wikiverse, OpenStreetMap, etc.)

Project description

GKC - Global Knowledge Commons

CI PyPI version Python Versions

The "Global Knowledge Commons" is a term of art describing the de facto confederation of community built and maintained data, information and knowledge assets. These include Wikipedia, Wikidata, Wikimedia Commons (and other parts of the "Wikiverse") along with OpenStreetMap for mapping data and services. This Python project is designed to provide a full working suite of capabilities for contributing to the Commons in a robust and documented fashion. Read more in the background document.

Features

  • 🔐 Authentication support for Wikidata, Wikipedia, and OpenStreetMap
  • 🌍 Easy-to-use interface for Global Knowledge Commons services
  • 📦 Built with Poetry for modern Python development
  • ✅ Comprehensive test coverage
  • 🚀 CI/CD with GitHub Actions

Installation

Using pip

pip install gkc

Using Poetry

poetry add gkc

Development Installation

# Clone the repository
git clone https://github.com/skybristol/gkc.git
cd gkc

# Install Poetry (if not already installed)
curl -sSL https://install.python-poetry.org | python3 -

# Install dependencies
poetry install

# Run pre-merge checks (recommended before PRs)
./scripts/pre-merge-check.sh

Usage

Authentication

The package provides authentication classes for Global Knowledge Commons services. You can provide credentials directly, via environment variables, or through interactive prompts.

Wikiverse (Wikidata, Wikipedia, Wikimedia Commons)

The WikiverseAuth class provides unified authentication for all Wikimedia projects using bot passwords. The same credentials work across Wikidata, Wikipedia, and Wikimedia Commons due to Wikimedia's Single User Login (SUL) system.

Setting up Bot Passwords:

  1. Go to Special:BotPasswords on Wikidata
  2. Create a new bot password with appropriate permissions
  3. Your username will be in the format YourUsername@BotName
  4. Save the generated password (you won't see it again!)

Basic Usage:

from gkc import WikiverseAuth, AuthenticationError

# Method 1: Using environment variables (recommended)
auth = WikiverseAuth()
auth.login()

# Method 2: Using explicit credentials
auth = WikiverseAuth(
    username="YourUsername@BotName",
    password="abc123def456ghi789"
)
auth.login()

# Method 3: Interactive prompt
auth = WikiverseAuth(interactive=True)
auth.login()

# Check login status
if auth.is_logged_in():
    print(f"Logged in as: {auth.get_account_name()}")
    print(f"Bot name: {auth.get_bot_name()}")

Targeting Different Wikimedia Projects:

# Wikidata (default)
auth = WikiverseAuth(
    username="User@Bot",
    password="secret",
    api_url="wikidata"  # or just omit for default
)
auth.login()

# English Wikipedia
auth = WikiverseAuth(
    username="User@Bot",
    password="secret",
    api_url="wikipedia"
)
auth.login()

# Wikimedia Commons
auth = WikiverseAuth(
    username="User@Bot",
    password="secret",
    api_url="commons"
)
auth.login()

# Custom MediaWiki instance (e.g., enterprise wiki)
auth = WikiverseAuth(
    username="User@Bot",
    password="secret",
    api_url="https://wiki.mycompany.com/w/api.php"
)
auth.login()

Making Authenticated API Requests:

# After login, use the session for API requests
auth = WikiverseAuth()
auth.login()

# Example: Query user information
response = auth.session.get(auth.api_url, params={
    "action": "query",
    "meta": "userinfo",
    "format": "json"
})
print(response.json())

# For editing, get a CSRF token
try:
    csrf_token = auth.get_csrf_token()
    print(f"CSRF Token: {csrf_token}")
except AuthenticationError as e:
    print(f"Error: {e}")

# When done, logout
auth.logout()

Helper Methods:

auth = WikiverseAuth(username="Alice@MyBot", password="secret")

# Extract account name
print(auth.get_account_name())  # Output: "Alice"

# Extract bot name
print(auth.get_bot_name())  # Output: "MyBot"

# Check authentication state
print(auth.is_authenticated())  # Has credentials
print(auth.is_logged_in())      # Successfully logged in to API

OpenStreetMap

from gkc import OpenStreetMapAuth

# Method 1: Using explicit credentials
auth = OpenStreetMapAuth(username="your_username", password="your_password")

# Method 2: Using environment variables (OPENSTREETMAP_USERNAME, OPENSTREETMAP_PASSWORD)
auth = OpenStreetMapAuth()

# Method 3: Interactive prompt
auth = OpenStreetMapAuth(interactive=True)

# Check authentication status
if auth.is_authenticated():
    print("Authenticated successfully!")

Environment Variables

You can set the following environment variables for automatic authentication:

Wikimedia Projects:

  • WIKIVERSE_USERNAME - Bot password username (format: Username@BotName)
  • WIKIVERSE_PASSWORD - Bot password
  • WIKIVERSE_API_URL - (Optional) MediaWiki API endpoint. Defaults to Wikidata. Can use shortcuts: "wikidata", "wikipedia", "commons"

OpenStreetMap:

  • OPENSTREETMAP_USERNAME - OpenStreetMap username
  • OPENSTREETMAP_PASSWORD - OpenStreetMap password

Example:

# Wikidata (default)
export WIKIVERSE_USERNAME="Alice@MyBot"
export WIKIVERSE_PASSWORD="abc123def456ghi789"

# Wikipedia
export WIKIVERSE_USERNAME="Alice@MyBot"
export WIKIVERSE_PASSWORD="abc123def456ghi789"
export WIKIVERSE_API_URL="wikipedia"

# Custom MediaWiki instance
export WIKIVERSE_USERNAME="Alice@MyBot"
export WIKIVERSE_PASSWORD="abc123def456ghi789"
export WIKIVERSE_API_URL="https://wiki.mycompany.com/w/api.php"

# OpenStreetMap
export OPENSTREETMAP_USERNAME="your_osm_username"
export OPENSTREETMAP_PASSWORD="your_osm_password"

Development

Running Tests

# Run all tests
poetry run pytest

# Run with coverage
poetry run pytest --cov=gkc --cov-report=html

# Run specific test file
poetry run pytest tests/test_auth.py

Code Quality

# Format code with Black
poetry run black gkc tests

# Lint with Ruff
poetry run ruff check gkc tests

# Type checking with mypy
poetry run mypy gkc

Building the Package

# Build distribution packages
poetry build

# The built packages will be in the dist/ directory

Publishing to PyPI

The package is configured to automatically publish to PyPI when a new release is created on GitHub. The workflow uses PyPI's trusted publisher feature for secure authentication.

Manual Publishing (if needed)

# Build the package
poetry build

# Publish to PyPI
poetry publish

CI/CD

The project uses GitHub Actions for continuous integration and deployment:

  • CI Workflow: Runs on every push and pull request

    • Tests on Python 3.9, 3.10, 3.11, and 3.12
    • Runs linting (Ruff), formatting (Black), and type checking (mypy)
    • Runs test suite with coverage reporting
  • Publish Workflow: Runs on release creation

    • Builds the package
    • Publishes to PyPI using PyPI Trusted Publishing (OIDC)

Before merging or creating a PR, run the pre-merge checks:

./scripts/pre-merge-check.sh

See CI/CD Documentation for detailed setup instructions and release process.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes
  4. Run pre-merge checks: ./scripts/pre-merge-check.sh
  5. Commit your changes (git commit -m 'Add some amazing feature')
  6. Push to the branch (git push origin feature/amazing-feature)
  7. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • Wikidata community
  • Wikipedia community
  • OpenStreetMap community

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gkc-0.1.0.tar.gz (26.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gkc-0.1.0-py3-none-any.whl (27.3 kB view details)

Uploaded Python 3

File details

Details for the file gkc-0.1.0.tar.gz.

File metadata

  • Download URL: gkc-0.1.0.tar.gz
  • Upload date:
  • Size: 26.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for gkc-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f5f97b8c0a6fd929c9f35052dd4f60d061fdbbad28f1321275654a8cf928d424
MD5 78cfcd7a6ff5ada87c5c84a3b864ae48
BLAKE2b-256 e722f1f12976d33705b51928b44535cbf6219afe9186d082fcfb28335aaf2e67

See more details on using hashes here.

Provenance

The following attestation bundles were made for gkc-0.1.0.tar.gz:

Publisher: publish.yml on skybristol/gkc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file gkc-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: gkc-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 27.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for gkc-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f6f55ff2c5d02e354954720f4d318a31a6908c28e7f0b219beba46cb41b2080d
MD5 5de7889aa856d64575ef3bbeee9fcbd9
BLAKE2b-256 78c6d490b6231e4e45e94e8d508b5823f8c6ff5620bccfec4617fbaa5053573d

See more details on using hashes here.

Provenance

The following attestation bundles were made for gkc-0.1.0-py3-none-any.whl:

Publisher: publish.yml on skybristol/gkc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page