Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tag to imported document #46

Merged
merged 3 commits into from
Mar 30, 2024
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
14 changes: 10 additions & 4 deletions papis_zotero/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from functools import partial
import os
import http.server
from typing import Optional
from typing import List, Optional, Tuple

import click

Expand All @@ -24,17 +25,22 @@ def main() -> None:
default=papis_zotero.server.ZOTERO_PORT,
type=int)
@click.option("--address", help="Address to bind", default="localhost")
def serve(address: str, port: int) -> None:
@click.option(
"-s", "--set", "set_list",
help="Set imported document metadata as <key> <value>. Can be used multiple times.",
multiple=True,
type=(str, str))
def serve(address: str, port: int, set_list: List[Tuple[str, str]],) -> None:
"""Start a ``zotero-connector`` server."""

logger.warning("The 'zotero-connector' server is experimental. "
"Please report bugs and improvements at "
"https://github.com/papis/papis-zotero/issues.")

server_address = (address, port)
request_handler = partial(papis_zotero.server.PapisRequestHandler, set_list)
try:
httpd = http.server.HTTPServer(server_address,
papis_zotero.server.PapisRequestHandler)
httpd = http.server.HTTPServer(server_address, request_handler)
except OSError:
logger.error(
"Address '%s:%s' is already in use. This may be because you "
Expand Down
12 changes: 10 additions & 2 deletions papis_zotero/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import json
import http.server
from typing import Any, Dict, List
from typing import Any, Dict, List, Tuple

import papis.api
import papis.crossref
Expand Down Expand Up @@ -70,7 +70,7 @@ def zotero_data_to_papis_data(item: Dict[str, Any]) -> Dict[str, Any]:
item.pop("uri", None)
item.pop("sessionID", None)

if item["referrer"] == "":
if item.get("referrer") == "":
item.pop("referrer", None)

for foreign_key, key in papis_zotero.utils.ZOTERO_TO_PAPIS_FIELDS.items():
Expand Down Expand Up @@ -122,6 +122,11 @@ def download_zotero_attachments(attachments: List[Dict[str, str]]) -> List[str]:


class PapisRequestHandler(http.server.BaseHTTPRequestHandler):
def __init__(self, set_list: List[Tuple[str, str]], request: Any,
client_address: Any, server: Any) -> None:
self.set_list = set_list
super().__init__(request, client_address, server)

def log_message(self, fmt: str, *args: Any) -> None:
logger.info(fmt, *args)

Expand Down Expand Up @@ -209,6 +214,9 @@ def handle_post_add(self) -> None:
files = []

papis_item = zotero_data_to_papis_data(item)
if self.set_list:
papis_item.update(self.set_list)

logger.info("Adding paper to papis.")
papis.commands.add.run(files, data=papis_item)

Expand Down
Loading