Skip to main content

A Python library for working with IATI XML data according to the IATI 2.03 standard

Project description

Python Tests

OKFN IATI XML Handler

A Python library for working with IATI XML data according to the IATI 2.03 standard.

Features

  • Data models that represent IATI XML elements with validation
  • XML generator to create valid IATI XML from Python objects
  • Support for IATI 2.03 standard
  • Enums for standardized code lists
  • Data validation to ensure compliance with the standard

Installation

pip install okfn-iati

Usage

Creating an IATI Activity

from okfn_iati import (
    # Main models
    Activity, Narrative, OrganizationRef, ParticipatingOrg, ActivityDate,
    Location, LocationIdentifier, DocumentLink,
    Budget, Transaction, IatiActivities,

    # Enums - use these constants instead of strings
    ActivityStatus, ActivityDateType, TransactionType, BudgetType, BudgetStatus,
    OrganisationRole, OrganisationType, LocationID, DocumentCategory,
    SectorCategory,

    # Generator
    IatiXmlGenerator
)

# Define reporting organization identifier (following IATI standard format)
reporting_org_id = "XM-DAC-12345"

# Create an IATI Activity
activity = Activity(
    # The activity identifier should begin with the reporting org identifier 
    # followed by a hyphen and a unique string: {org-id}-{activity-unique-id}
    iati_identifier=f"{reporting_org_id}-PROJECT001",
    reporting_org=OrganizationRef(
        ref=reporting_org_id,  # Must match the prefix of the activity identifier
        type=OrganisationType.GOVERNMENT.value,
        narratives=[Narrative(text="Example Organization")]
    ),
    title=[Narrative(text="Example Project")],
    description=[{
        "type": "1", 
        "narratives": [
            Narrative(text="This is an example project description")
        ]
    }],
    activity_status=ActivityStatus.IMPLEMENTATION,
    activity_dates=[
        ActivityDate(
            type=ActivityDateType.PLANNED_START,
            iso_date="2023-01-01",
            narratives=[Narrative(text="Planned start date")]
        ),
        ActivityDate(
            type=ActivityDateType.ACTUAL_START,
            iso_date="2023-01-15"
        )
    ],
    participating_orgs=[
        ParticipatingOrg(
            role=OrganisationRole.FUNDING,
            ref="XM-EXAMPLE-FUNDER",
            type=OrganisationType.GOVERNMENT.value,
            narratives=[Narrative(text="Example Funding Organization")]
        ),
        ParticipatingOrg(
            role=OrganisationRole.IMPLEMENTING,
            ref="XM-EXAMPLE-IMPL",
            narratives=[Narrative(text="Example Implementing Organization")]
        )
    ],
    # Required: recipient country or region
    recipient_countries=[
        {
            "code": "KE",
            "percentage": 100,
            "narratives": [Narrative(text="Kenya")]
        }
    ],
    locations=[
        Location(
            location_id=LocationIdentifier(
                vocabulary=LocationID.GEONAMES,
                code="1234567"
            ),
            name=[Narrative(text="Example Location")]
        )
    ],
    # Required: sector information
    sectors=[
        {
            "code": SectorCategory.EDUCATION_GENERAL.value,  # "11110"
            "vocabulary": "1",  # DAC vocabulary
            "percentage": 100
        }
    ],
    budgets=[
        Budget(
            type=BudgetType.ORIGINAL,
            status=BudgetStatus.INDICATIVE,
            period_start="2023-01-01",
            period_end="2023-12-31",
            value=100000.00,
            currency="USD",
            value_date="2023-01-01"  # Added required value_date
        )
    ],
    transactions=[
        Transaction(
            type=TransactionType.DISBURSEMENT,
            date="2023-03-15",
            value=50000.00,
            currency="USD",
            value_date="2023-03-15"  # Added required value_date
        )
    ],
    document_links=[
        DocumentLink(
            url="https://example.org/docs/report.pdf",
            format="application/pdf",
            title=[Narrative(text="Project Report")],
            categories=[DocumentCategory.OBJECTIVES]
        )
    ],
    default_currency="USD",
)

# Create an IATI Activities container
iati_activities = IatiActivities(
    version="2.03",
    activities=[activity]
)

# Generate XML
generator = IatiXmlGenerator()
xml_string = generator.generate_iati_activities_xml(iati_activities)

# Save to file
generator.save_to_file(iati_activities, "example_activity.xml")

Validation with IATI Validator

The XML generated by this library follows the IATI schema and ruleset requirements. To validate your XML:

  1. Generate your XML file using this library
  2. Upload it to the IATI Validator
  3. Check that it passes both schema validation and IATI ruleset validation

You can validate IATI XML using the built-in IatiValidator class:

from okfn_iati import IatiValidator

# Create a validator
validator = IatiValidator()

# Validate XML string
with open("example_activity.xml", "r") as f:
    xml_string = f.read()
    is_valid, errors = validator.validate(xml_string)
    
    if is_valid:
        print("XML is valid!")
    else:
        print("XML validation errors:")
        for schema_error in errors['schema_errors']:
            print(f"  Schema error: {schema_error}")
        for ruleset_error in errors['ruleset_errors']:
            print(f"  Ruleset error: {ruleset_error}")

Start your IATI project

You can start by creating a CSV file and this library will process it to generate valid IATI XML files.
Read the docs in englsh or spanish to learn how to do it.

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

okfn_iati-0.2.0.tar.gz (64.5 kB view details)

Uploaded Source

Built Distribution

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

okfn_iati-0.2.0-py3-none-any.whl (61.7 kB view details)

Uploaded Python 3

File details

Details for the file okfn_iati-0.2.0.tar.gz.

File metadata

  • Download URL: okfn_iati-0.2.0.tar.gz
  • Upload date:
  • Size: 64.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for okfn_iati-0.2.0.tar.gz
Algorithm Hash digest
SHA256 a3c4dcffe1f20e0b18cbe1970e6ea843c8d064534cd59c9730861af23a9e4871
MD5 f9f439ce335a68064004f5d8ede3c20b
BLAKE2b-256 53391566759b7bf3dd7eba9ed60417d7313c4c4e2e52141b63f60ad235cd0e28

See more details on using hashes here.

File details

Details for the file okfn_iati-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: okfn_iati-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 61.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for okfn_iati-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2ecf1355bbcad139171971b703c9f9f6f783d4b8c10d7fa15e5f3ba17c924e42
MD5 6675bcb6259488bc37a2fb5217120082
BLAKE2b-256 a6e66273a8ec1995a00bd43a820b504fd0337264f48e32f9c561374e2e7e4789

See more details on using hashes here.

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