prepare_xep_list.py

Mon, 13 Mar 2023 17:19:11 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Mon, 13 Mar 2023 17:19:11 +0000
changeset 1
75449093fdb6
parent 0
8e1675826e46
permissions
-rwxr-xr-x

Dockerfile: Switch user before running

'''
This file is used to download the XEP list and convert it to JSON
'''
from typing import Any

import json
import os
import sys

import requests
from defusedxml.ElementTree import fromstring
from defusedxml.ElementTree import ParseError

XEP_LIST_URL = 'https://xmpp.org/extensions/xeplist.xml'


def build_xep_list() -> None:
    '''
    Download and parse xeplist.xml and build xeplist.json
    '''
    try:
        xeplist_request = requests.get(XEP_LIST_URL)
    except requests.exceptions.RequestException as err:
        sys.exit(f'Error while requesting xeplist.xml ({err}')

    if not 200 >= xeplist_request.status_code < 400:
        sys.exit(f'Error while downloading xeplist.xml '
                 f'({xeplist_request.status_code}')

    try:
        root = fromstring(xeplist_request.content)
    except ParseError:
        sys.exit('Error while parsing xeplist.xml')

    def fix_status(status: str) -> str:
        if status == 'Draft':
            return 'Stable'
        return status

    xeps: list[dict[str, Any]] = []
    for xep in root.findall("xep"):
        xep_data = {
            'title': xep.find('title').text,
            'status': fix_status(xep.find('status').text),
            'number': int(xep.find('number').text) if xep.find('number').text != 'xxxx' else None,
            'last_updated': xep.find('last-revision').find('date').text,
            'version': xep.find('last-revision').find('version').text,
            'type': xep.find('type').text,
            'approver': xep.find('approver').text,
        }
        if xep.get("accepted") == "true":
            xep_data.update({
                'accepted': True,
                'short_name': xep.find('shortname').text if xep.find('shortname') else None,
                'url': f'https://xmpp.org/extensions/xep-{xep_data["number"]:04d}.html',
            })
        else:
            xep_data.update({
                'accepted': False,
                'short_name': xep.find("proto-name").text,
                'url': f'https://xmpp.org/extensions/inbox/{xep.find("proto-name").text}.html',
            })
        xeps.append(xep_data)

    base_path = os.path.dirname(os.path.abspath(sys.argv[0]))

    with open(f'{base_path}/../data/xeplist.json',
              'w',
              encoding='utf-8') as json_file:
        json.dump(xeps, json_file, indent=4)
    print('XEP List prepared successfully')


if __name__ == '__main__':
    build_xep_list()

mercurial