Skip to content
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
73 changes: 73 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
name: Main CI

on: [push]

permissions: {}

jobs:
lint:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v5
with:
persist-credentials: false
- uses: actions/setup-python@v6
with:
python-version: '3.14'
cache: 'pip'
- name: Install testing dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements-test.txt
- name: Lint
# FIXME
continue-on-error: true
run: |
flake8 lib/pyld tests --count --show-source --statistics
test:
runs-on: ubuntu-latest
needs: [lint]
timeout-minutes: 10
strategy:
matrix:
python-version:
- '3.10'
- '3.11'
- '3.12'
- '3.13'
- '3.14'
- 'pypy3.10'
- 'pypy3.11'
loader: [requests, aiohttp]
steps:
- uses: actions/checkout@v5
with:
persist-credentials: false
- name: Use Python ${{ matrix.python-version }}
uses: actions/setup-python@v6
with:
python-version: ${{ matrix.python-version }}
cache: 'pip'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Install testing dependencies
run: |
pip install -r requirements-test.txt
- name: Fetch test suites
run: |
git clone --depth 1 https://github.com/w3c/json-ld-api.git _json-ld-api
git clone --depth 1 https://github.com/w3c/json-ld-framing.git _json-ld-framing
git clone --depth 1 https://github.com/json-ld/normalization.git _normalization
- name: Test with Python=${{ matrix.python-version }} Loader=${{ matrix.loader }}
run: |
python tests/runtests.py ./_json-ld-api/tests -l ${{ matrix.loader }}
python tests/runtests.py ./_json-ld-framing/tests -l ${{ matrix.loader }}
python tests/runtests.py ./_normalization/tests -l ${{ matrix.loader }}
env:
LOADER: ${{ matrix.loader }}
#coverage:
# needs: [test]
# FIXME
42 changes: 0 additions & 42 deletions .travis.yml

This file was deleted.

9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# pyld ChangeLog

## 3.0.0 - 2025-xx-xx

### Changed
- **BREAKING**: Require supported Python version >= 3.10.
- Update aiohttp document loader to work with Python 3.14.
- Minimize async related changes to library code in this release.
- In sync environment use `asyncio.run`.
- In async environment use background thread.

## 2.0.4 - 2024-02-16

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ yet supported.
Requirements
------------

- Python_ (3.6 or later)
- Python_ (3.10 or later)
- Requests_ (optional)
- aiohttp_ (optional, Python 3.5 or later)

Expand Down
62 changes: 51 additions & 11 deletions lib/pyld/documentloader/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,54 @@
.. moduleauthor:: Olaf Conradi <olaf@conradi.org>
"""

import asyncio
import re
import string
import threading
import urllib.parse as urllib_parse

from pyld.jsonld import (JsonLdError, parse_link_header, LINK_HEADER_REL)


# Background event loop (used when inside an existing async environment)
_background_loop = None
_background_thread = None


def _ensure_background_loop():
"""Start a persistent background event loop if not running."""
global _background_loop, _background_thread
if _background_loop is None:
_background_loop = asyncio.new_event_loop()

def run_loop(loop):
asyncio.set_event_loop(loop)
loop.run_forever()

_background_thread = threading.Thread(
target=run_loop, args=(_background_loop,), daemon=True)
_background_thread.start()
return _background_loop


def aiohttp_document_loader(loop=None, secure=False, **kwargs):
"""
Create an Asynchronous document loader using aiohttp.

:param loop: the event loop used for processing HTTP requests.
:param loop: deprecated / ignored (kept for backward compatibility).
:param secure: require all requests to use HTTPS (default: False).
:param **kwargs: extra keyword args for the aiohttp request get() call.

:return: the RemoteDocument loader function.
"""
import asyncio
import aiohttp

if loop is None:
loop = asyncio.get_event_loop()

async def async_loader(url, headers):
"""
Retrieves JSON-LD at the given URL asynchronously.

:param url: the URL to retrieve.
:param headers: the request headers.

:return: the RemoteDocument.
"""
Expand All @@ -56,7 +77,7 @@ async def async_loader(url, headers):
'the URL\'s scheme is not "https".',
'jsonld.InvalidUrl', {'url': url},
code='loading document failed')
async with aiohttp.ClientSession(loop=loop) as session:
async with aiohttp.ClientSession() as session:
async with session.get(url,
headers=headers,
**kwargs) as response:
Expand Down Expand Up @@ -104,16 +125,35 @@ async def async_loader(url, headers):
'jsonld.LoadDocumentError', code='loading document failed',
cause=cause)

def loader(url, options={}):
def loader(url, options=None):
"""
Retrieves JSON-LD at the given URL.
Retrieves JSON-LD at the given URL synchronously.

Works safely in both synchronous and asynchronous environments.

:param url: the URL to retrieve.
:param options: the request options.

:return: the RemoteDocument.
"""
return loop.run_until_complete(
async_loader(url,
options.get('headers', {'Accept': 'application/ld+json, application/json'})))
if options is None:
options = {}
headers = options.get(
'headers', {'Accept': 'application/ld+json, application/json'})

# Detect whether we're already in an async environment
try:
running_loop = asyncio.get_running_loop()
except RuntimeError:
running_loop = None

# Sync environment
if not running_loop or not running_loop.is_running():
return asyncio.run(async_loader(url, headers))

# Inside async environment: use background event loop
loop = _ensure_background_loop()
future = asyncio.run_coroutine_threadsafe(async_loader(url, headers), loop)
return future.result()

return loader
1 change: 1 addition & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
flake8
26 changes: 26 additions & 0 deletions tests/runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,12 @@ def write(self, filename):
# see JSON-LD 1.0 Errata
'specVersion': ['json-ld-1.0'],
'idRegex': [
# uncategorized
'.*compact-manifest#t0112$',
'.*compact-manifest#tm023$',
'.*compact-manifest#t0111$',
'.*compact-manifest#t0113$',
'.*compact-manifest#tc028$',
]
},
'fn': 'compact',
Expand Down Expand Up @@ -707,6 +713,12 @@ def write(self, filename):
# see JSON-LD 1.0 Errata
'specVersion': ['json-ld-1.0'],
'idRegex': [
# uncategorized
'.*expand-manifest#tc036$',
'.*expand-manifest#tc037$',
'.*expand-manifest#tc038$',
'.*expand-manifest#ter54$',
'.*expand-manifest#ter56$',
]
},
'fn': 'expand',
Expand All @@ -725,6 +737,8 @@ def write(self, filename):
# see JSON-LD 1.0 Errata
'specVersion': ['json-ld-1.0'],
'idRegex': [
# uncategorized html
'.*html-manifest#tf004$',
]
},
'fn': 'flatten',
Expand All @@ -744,6 +758,8 @@ def write(self, filename):
# see JSON-LD 1.0 Errata
'specVersion': ['json-ld-1.0'],
'idRegex': [
# uncategorized
'.*frame-manifest#t0069$',
]
},
'fn': 'frame',
Expand All @@ -760,6 +776,8 @@ def write(self, filename):
# direction (compound-literal)
'.*fromRdf-manifest#tdi11$',
'.*fromRdf-manifest#tdi12$',
# uncategorized
'.*fromRdf-manifest#t0027$',
]
},
'fn': 'from_rdf',
Expand Down Expand Up @@ -792,6 +810,14 @@ def write(self, filename):
# well formed
'.*toRdf-manifest#twf05$',
'.*toRdf-manifest#twf06$',
# uncategorized
'.*toRdf-manifest#tc038$',
'.*toRdf-manifest#ter54$',
'.*toRdf-manifest#ter56$',
'.*toRdf-manifest#tli12$',
'.*toRdf-manifest#tli14$',
'.*toRdf-manifest#tc036$',
'.*toRdf-manifest#tc037$',
]
},
'skip': {
Expand Down
Loading