Skip to content

Commit

Permalink
fixes #28 and #29
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfgangFahl committed Dec 29, 2023
1 parent 16c71fa commit cd52438
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 4 deletions.
2 changes: 1 addition & 1 deletion ceurspt/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__="0.0.6"
__version__="0.0.7"
55 changes: 53 additions & 2 deletions ceurspt/ceurws.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
'''
import ceurspt.ceurws_base
import ceurspt.models.dblp

import json
from bs4 import BeautifulSoup
from ceurspt.profiler import Profiler
from ceurspt.version import Version
Expand Down Expand Up @@ -165,7 +165,40 @@ def as_wb_dict(self)->dict:
"P1545": f"{index+1}"
}})
return wb


def as_wbi_cli_text(self, qid: str) -> str:
"""
Generates a series of Wikibase CLI command strings to add claims to the entity
represented by this paper, based on the provided QID.
Args:
qid (str): The QID of the Wikibase item to which the claims will be added.
Returns:
str: A string containing all the 'wb add-claim' commands separated by newlines.
"""
# Get the dictionary representation of the paper
wb_dict = self.as_wb_dict()

# Initialize an empty list to hold all commands
cli_commands = []

# Iterate through each claim to create a separate wb add-claim command
for prop, value in wb_dict['claims'].items():
# Handle different structures in claims (e.g., simple vs. complex with qualifiers)
if isinstance(value, list): # Expecting a list of values (or complex value structures)
for val in value:
# Convert value to a JSON string and escape quotes for command line
val_json = json.dumps(val).replace('"', '\\"')
cli_commands.append(f'wb add-claim {qid} {prop} "{val_json}"')
else: # A single value or simple structure
# Convert value to a JSON string and escape quotes for command line
value_json = json.dumps(value).replace('"', '\\"')
cli_commands.append(f'wb add-claim {qid} {prop} "{value_json}"')

# Combine all commands into a single string separated by newlines
cli = "\n".join(cli_commands)
return cli

def as_quickstatements(self)->str:
"""
Expand Down Expand Up @@ -438,11 +471,23 @@ def getIconBar(self,soup):
"link":f"/{pdf_name}.wbjson",
"valid":True
},
{
"src": "/static/icons/32px-Wikibase_logo.svg.png",
"title": "wikibase CLI",
"link":f"/{pdf_name}.wbcli",
"valid":True
},
{
"src": "/static/icons/32px-JSON_vector_logo.svg.png",
"title": "JSON metadata",
"link":f"/{pdf_name}.json",
"valid":True
},
{
"src": "/static/icons/32px-YAML_Logo.svg.png",
"title": "YAML metadata",
"link":f"/{pdf_name}.yaml",
"valid":True
}
]
icon_tag=Volume.create_icon_bar(soup, icon_list=icon_list)
Expand Down Expand Up @@ -734,6 +779,12 @@ def getIconBar(self,soup):
"link":f"/Vol-{self.number}.json",
"valid":True
},
{
"src": "/static/icons/32px-YAML_Logo.svg.png",
"title": "YML metadata",
"link":f"/Vol-{self.number}.yaml",
"valid":True
}

]
icon_tag=Volume.create_icon_bar(soup, icon_list=icon_list)
Expand Down
2 changes: 1 addition & 1 deletion ceurspt/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Version(object):
name = ""
version = ceurspt.__version__
date = '2023-03-17'
updated = '2023-07-12'
updated = '2023-12-29'
description = 'CEUR-WS Single Point of Truth RestFUL server',

authors = 'Tim Holzheim, Wolfgang Fahl'
Expand Down
48 changes: 48 additions & 0 deletions ceurspt/webserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
@author: wf
"""
from typing import Optional
import yaml

from fastapi import FastAPI, HTTPException
from fastapi.responses import FileResponse, HTMLResponse, PlainTextResponse, Response, RedirectResponse
Expand Down Expand Up @@ -69,6 +70,15 @@ async def paperWikibaseCliJson(number: int, pdf_name: str):
paper_dict = paper.as_wb_dict()
return paper_dict

@self.app.get("/Vol-{number:int}/{pdf_name}/{qid}.wbcli")
async def paperWikibaseCli(number: int, pdf_name,qid: str):
"""
get the json response to the wikibase-cli for the given paper
"""
paper = self.getPaper(number, pdf_name)
paper_cli_text = paper.as_wbi_cli_text(qid)
return PlainTextResponse(paper_cli_text)

@self.app.get("/Vol-{number:int}/{pdf_name}.html")
async def paperHtml(number: int, pdf_name: str):
"""
Expand Down Expand Up @@ -237,6 +247,44 @@ async def volume_paper_citation(number: int, pdf_name: str):
return PlainTextResponse(content=citation)
else:
return {"error": f"unknown volume number {number} or paper {pdf_name}"}

@self.app.get("/Vol-{number:int}/{pdf_name}.yaml")
async def paperYaml(number: int, pdf_name: str):
paper = self.getPaper(number, pdf_name)
paper_dict = paper.getMergedDict()
yaml_content = yaml.dump(paper_dict)
return Response(content=yaml_content, media_type="application/x-yaml")

@self.app.get("/Vol-{number:int}.yaml")
async def volumeYaml(number: int):
vol = self.getVolume(number)
if vol:
volume_dict = vol.getMergedDict()
else:
volume_dict = {"error": f"unknown volume number {number}"}
yaml_content = yaml.dump(volume_dict)
return Response(content=yaml_content, media_type="application/x-yaml")

@self.app.get("/volume/{number:int}/paper.yaml", tags=["yaml"])
async def volume_papers_yaml(number: int):
vol = self.getVolume(number)
if vol:
paper_records = [paper.getMergedDict() for paper in vol.papers]
else:
paper_records = {"error": f"unknown volume number {number}"}
yaml_content = yaml.dump(paper_records)
return Response(content=yaml_content, media_type="application/x-yaml")

@self.app.get("/volume/{number:int}/paper/{pdf_name:str}.yaml", tags=["yaml"])
async def volume_paper_yaml(number: int, pdf_name: str):
paper = self.getPaper(number, pdf_name)
if paper:
paper_dict = paper.getMergedDict()
else:
paper_dict = {"error": f"unknown volume number {number} or paper {pdf_name}"}
yaml_content = yaml.dump(paper_dict)
return Response(content=yaml_content, media_type="application/x-yaml")


def volumeHtml(self, number: int, ext: str = ".pdf") -> HTMLResponse:
"""
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ dependencies = [
# https://github.com/tqdm/tqdm
# optional?
"tqdm",
# https://pypi.org/project/PyYAML/
"PyYAML",
# https://github.com/ijl/orjson
"orjson>=3.8.9",
"bibtexparser",
Expand Down
Binary file added static/icons/32px-Wikibase_logo.svg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/icons/32px-YAML_Logo.svg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit cd52438

Please sign in to comment.