Skip to content

Commit

Permalink
Merge branch 'release/v0.6.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
ja573 committed Oct 22, 2021
2 parents f3938a7 + 41577ff commit 201a10b
Show file tree
Hide file tree
Showing 114 changed files with 5,216 additions and 96 deletions.
29 changes: 29 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Most components released under the Apache License.

Modified GraphQL client released under the MIT License.


Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
Expand Down Expand Up @@ -199,3 +204,27 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.



The MIT License (MIT)

Copyright (c) 2016 graph.cool

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
100 changes: 95 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,108 @@
Python client for Thoth's GraphQL API
Python client for Thoth's GraphQL and REST APIs

[![Build Status](https://travis-ci.org/openbookpublishers/thoth-client.svg?branch=master)](https://travis-ci.org/openbookpublishers/thoth-client) [![Release](https://img.shields.io/github/release/openbookpublishers/thoth-client.svg?colorB=58839b)](https://github.com/openbookpublishers/thoth-client/releases) [![License](https://img.shields.io/github/license/openbookpublishers/thoth-client.svg?colorB=ff0000)](https://github.com/openbookpublishers/thoth-client/blob/master/LICENSE)

## Usage

### Install
Install is either via pip or cloning the repository.

From pip:
```sh
python3 -m pip install thothlibrary==0.5.0
python3 -m pip install thothlibrary==0.6.0
```

Or from the repo:
```sh
git clone [email protected]:dqprogramming/thoth-client.git
cd thoth-client
pip3 install -r ./requirements.txt
```

### GraphQL Usage
```python
from thothlibrary import ThothClient

thoth = ThothClient()
all_works = thoth.works()
print(all_works)
thoth = ThothClient(version="0.4.2")
print(thoth.works())
```

### CLI GraphQL Usage
```sh
python3 -m thothlibrary.cli contribution --contribution_id=29e4f46b-851a-4d7b-bb41-e6f305fc2b11
python3 -m thothlibrary.cli contributions --limit=10
python3 -m thothlibrary.cli contribution_count
python3 -m thothlibrary.cli contributor --contributor_id=e8def8cf-0dfe-4da9-b7fa-f77e7aec7524
python3 -m thothlibrary.cli contributors --limit=10
python3 -m thothlibrary.cli contributor_count --search="Vincent"
python3 -m thothlibrary.cli funder --funder_id=194614ac-d189-4a74-8bf4-74c0c9de4a81
python3 -m thothlibrary.cli funders --limit=10
python3 -m thothlibrary.cli funder_count
python3 -m thothlibrary.cli funding --funding_id=5323d3e7-3ae9-4778-8464-9400fbbb959e
python3 -m thothlibrary.cli fundings --limit=10
python3 -m thothlibrary.cli imprint --imprint_id=78b0a283-9be3-4fed-a811-a7d4b9df7b25
python3 -m thothlibrary.cli imprints --limit=25 --offset=0 --publishers='["85fd969a-a16c-480b-b641-cb9adf979c3b" "9c41b13c-cecc-4f6a-a151-be4682915ef5"]'
python3 -m thothlibrary.cli imprint_count --publishers='["85fd969a-a16c-480b-b641-cb9adf979c3b" "9c41b13c-cecc-4f6a-a151-be4682915ef5"]'
python3 -m thothlibrary.cli issue --issue_id=6bd31b4c-35a9-4177-8074-dab4896a4a3d
python3 -m thothlibrary.cli issues --limit=10
python3 -m thothlibrary.cli issue_count
python3 -m thothlibrary.cli language --language_id=c19e68dd-c5a3-48f1-bd56-089ee732604c
python3 -m thothlibrary.cli languages --limit=10 --language_code=CHI
python3 -m thothlibrary.cli language_count --language_code=CHI
python3 -m thothlibrary.cli price --price_id=818567dd-7d3a-4963-8704-3381b5432877
python3 -m thothlibrary.cli prices --limit=10 --currency_code=GBP
python3 -m thothlibrary.cli price_count --currency_code=GBP
python3 -m thothlibrary.cli publication --publication_id=34712b75-dcdd-408b-8d0c-cf29a35be2e5
python3 -m thothlibrary.cli publications --limit=10 --publishers='["85fd969a-a16c-480b-b641-cb9adf979c3b"]'
python3 -m thothlibrary.cli publication_count --publication_type="HARDBACK"
python3 -m thothlibrary.cli publisher --publisher_id=85fd969a-a16c-480b-b641-cb9adf979c3b
python3 -m thothlibrary.cli publishers --limit=10 --order='{field: PUBLISHER_ID, direction: ASC}' --offset=0 --publishers='["85fd969a-a16c-480b-b641-cb9adf979c3b" "9c41b13c-cecc-4f6a-a151-be4682915ef5"]'
python3 -m thothlibrary.cli publisher_count --publishers='["85fd969a-a16c-480b-b641-cb9adf979c3b" "9c41b13c-cecc-4f6a-a151-be4682915ef5"]'
python3 -m thothlibrary.cli series --series_id=d4b47a76-abff-4047-a3c7-d44d85ccf009
python3 -m thothlibrary.cli serieses --limit=3 --search="Classics"
python3 -m thothlibrary.cli series_count --series_type=BOOK_SERIES
python3 -m thothlibrary.cli subject --subject_id=1291208f-fc43-47a4-a8e6-e132477ad57b
python3 -m thothlibrary.cli subjects --limit=10 --subject_type=BIC
python3 -m thothlibrary.cli subject_count --subject_type=THEMA
python3 -m thothlibrary.cli supported_versions
python3 -m thothlibrary.cli work --doi="https://doi.org/10.11647/OBP.0222"
python3 -m thothlibrary.cli work --work_id="e0f748b2-984f-45cc-8b9e-13989c31dda4"
python3 -m thothlibrary.cli works --limit=10 --order='{field: PUBLICATION_DATE, direction: DESC}' --work_status=ACTIVE --work_type=MONOGRAPH --offset=1 --publishers='["85fd969a-a16c-480b-b641-cb9adf979c3b"]'
python3 -m thothlibrary.cli work_count --publishers='["85fd969a-a16c-480b-b641-cb9adf979c3b"]'
```


### REST Usage
```python
from thothlibrary import ThothRESTClient

client = ThothRESTClient()
print(client.formats())
```

### CLI REST Usage
```sh
python3 -m thothrest.cli
python3 -m thothrest.cli formats
python3 -m thothrest.cli formats --return-json
python3 -m thothrest.cli work onix_3.0::project_muse e0f748b2-984f-45cc-8b9e-13989c31dda4
```

## Thoth Django
The thothdjango folder includes models, an import routine, subject-code support, and admin procedures to use Thoth in a django app. The import provides unidirectional synchronization from remote Thoth imports to a local database for use in a Django app.

## Test Suite
Tests for GraphQL queries are versioned in the thoth-[ver] folder of thothlibrary.

Tests confirm that current code produces good, known object outputs from stored GraphQL input.

## Versioning
The Thoth API is not yet considered stable and functionality changes between versions. The recommended way to add a new version compatibility is:

1. Read the latest Thoth changelog to understand the changes.
2. Copy the latest thoth-[ver] folder to the correctly named new version.
3. Find and replace the strings specified in genfixtures.sh and genjson.sh. Update the version string in tests and endpoints.
4. Run genjson.sh _only_ from inside the tests directory of the new version. This will fetch the latest server JSON responses and store it inside the fixtures directory for these tests. If there are any errors, then the command line CLI has encountered a breaking change that must first be fixed.
5. Run the test suite for the latest version and examine breakages. It is possible that breakages are not actually full breakdown, but merely a change in the serialized object. Nonetheless, fix these by subclassing the previous versions of the API and overriding broken methods. In the cases of total breakage, a non-subclassed rewrite may be more appropriate. (May also apply at major version breaks.)
6. When the test suite passes, or a new object format has been decided and tests rewritten, run genfixtures.sh to freeze the current test suite.

5 changes: 4 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
graphqlclient==0.2.4
requests==2.24.0
fire==0.4.0
munch==2.5.0
requests_mock==1.9.3
ascii_magic==1.6
12 changes: 6 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import os
from setuptools import setup
from thothlibrary import __version__, __license__
from thothlibrary import __version__

ROOTDIR = os.path.abspath(os.path.dirname(__file__))

Expand All @@ -14,14 +14,14 @@
setup(
name="thothlibrary",
version=__version__,
description="Python client for Thoth's GraphQL API",
author="Javier Arias",
description="Python client for Thoth's APIs",
author="Javier Arias, Martin Paul Eve",
author_email="[email protected]",
packages=["thothlibrary"],
install_requires=["graphqlclient", "requests"],
packages=["thothlibrary", "thothrest", "thothdjango"],
install_requires=["graphqlclient", "requests", "munch"],
long_description=LONG_DESCRIPTION,
long_description_content_type="text/markdown",
license=__license__,
license="Apache 2.0",
platforms=["any"],
classifiers=[
"Programming Language :: Python :: 3",
Expand Down
Empty file added thothdjango/__init__.py
Empty file.
71 changes: 71 additions & 0 deletions thothdjango/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"""
(c) ΔQ Programming LLP, 2021
This program is free software; you may redistribute and/or modify
it under the terms of the Apache License v2.0.
"""
from django.contrib import admin

from thoth import models


class WorkAdmin(admin.ModelAdmin):
list_display = ('pk', 'full_title', 'doi', 'publisher')
list_filter = ('publisher',)


class ThemaAdmin(admin.ModelAdmin):
list_display = ('pk', 'code', 'heading')


class BICAdmin(admin.ModelAdmin):
list_display = ('pk', 'code', 'heading')


class SubjectAdmin(admin.ModelAdmin):
list_display = ('pk', 'subject_display', 'subject_code', 'subject_type',
'work')
list_filter = ('subject_type',)


class PublisherAdmin(admin.ModelAdmin):
list_display = ('pk', 'publisher_name')


class ContributorAdmin(admin.ModelAdmin):
list_display = ('pk', 'full_name')


class ContributionAdmin(admin.ModelAdmin):
list_display = ('pk', 'contributor_name', 'contribution_type', 'work_name')
list_filter = ('work__publisher',)

def contributor_name(self, obj):
if obj.contributor:
return obj.contributor.full_name
else:
""

contributor_name.admin_order_field = 'full_name'
contributor_name.short_description = 'Contributor Name'

def work_name(self, obj):
if obj.contributor:
return obj.work.full_title
else:
return ""

work_name.admin_order_field = 'full_title'
work_name.short_description = 'Work Name'


admin_list = [
(models.Work, WorkAdmin),
(models.Publisher, PublisherAdmin),
(models.Contributor, ContributorAdmin),
(models.Contribution, ContributionAdmin),
(models.Subject, SubjectAdmin),
(models.Thema, ThemaAdmin),
(models.BIC, BICAdmin),
]

[admin.site.register(*t) for t in admin_list]
Empty file.
Empty file.
52 changes: 52 additions & 0 deletions thothdjango/management/commands/install_bic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""
(c) ΔQ Programming LLP, 2021
This program is free software; you may redistribute and/or modify
it under the terms of the Apache License v2.0.
"""
import os
import pathlib

from django.core.management.base import BaseCommand
import csv

from thoth.models import BIC


class Command(BaseCommand):
"""
A management command that installs BIC code support
"""

help = "Installs BIC code functionality into Thoth components"

def handle(self, *args, **options):
script_dir = pathlib.Path(__file__).parent.parent.parent.resolve()
path = os.path.join(script_dir, 'fixtures', 'BIC.csv')
path_quals = os.path.join(script_dir, 'fixtures', 'BICQuals.csv')

if not os.path.isfile(path) or not os.path.isfile(path_quals):
print('Please place BIC.csv and BICQuals.csv, converted from '
'https://www.bic.org.uk/7/BIC-Standard-Subject-Categories/, '
'in the thoth/fixtures directory.')
return

with open(path, newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
bic_model = BIC.objects.get_or_create(
code=row['Code'],
heading=row['Heading'])[0]

bic_model.save()

with open(path_quals, newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
bic_model = BIC.objects.get_or_create(
code=row['Code'],
heading=row['Heading'])[0]

bic_model.save()

print("BIC fixtures installed. At next Thoth sync, subject codes will "
"be linked to BIC entries.")
43 changes: 43 additions & 0 deletions thothdjango/management/commands/install_bisac.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""
(c) ΔQ Programming LLP, 2021
This program is free software; you may redistribute and/or modify
it under the terms of the Apache License v2.0.
"""
import os
import pathlib

from django.core.management.base import BaseCommand
import csv

from thoth.models import BISAC


class Command(BaseCommand):
"""
A management command that installs Bisac code support
"""

help = "Installs Bisac code functionality into Thoth components"

def handle(self, *args, **options):
script_dir = pathlib.Path(__file__).parent.parent.parent.resolve()
path = os.path.join(script_dir, 'fixtures', 'bisac.csv')

if not os.path.isfile(path):
print('Please place bisac.csv, converted from the BISAC mapping at '
'https://www.editeur.org/151/Thema/, '
'in the thoth/fixtures directory.')
return

with open(path, newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:

bisac_model = BISAC.objects.get_or_create(
code=row['BISAC Code'],
heading=row['Thema Literal 1'])[0]

bisac_model.save()

print("BISAC fixtures installed. At next Thoth sync, subject codes "
"will be linked to BISAC entries.")
42 changes: 42 additions & 0 deletions thothdjango/management/commands/install_thema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
(c) ΔQ Programming LLP, 2021
This program is free software; you may redistribute and/or modify
it under the terms of the Apache License v2.0.
"""
import os
import pathlib

from django.core.management.base import BaseCommand
import csv

from thoth.models import Thema


class Command(BaseCommand):
"""
A management command that installs Thema code support
"""

help = "Installs Thema code functionality into Thoth components"

def handle(self, *args, **options):
script_dir = pathlib.Path(__file__).parent.parent.parent.resolve()
path = os.path.join(script_dir, 'fixtures', 'thema.csv')

if not os.path.isfile(path):
print('Please place thema.csv, converted from '
'https://www.editeur.org/151/Thema/, '
'in the thoth/fixtures directory.')
return

with open(path, newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
thema_model = Thema.objects.get_or_create(
code=row['Code'],
heading=row['English Heading'])[0]

thema_model.save()

print("Thema fixtures installed. At next Thoth sync, subject codes "
"will be linked to THEMA entries.")
Loading

0 comments on commit 201a10b

Please sign in to comment.