-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.py
More file actions
118 lines (97 loc) · 2.94 KB
/
cli.py
File metadata and controls
118 lines (97 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import logging
from enum import Enum
from typing import Annotated
import networkx as nx
import typer
from rich import print as rprint
from bibx import (
query_openalex,
read_any,
read_scopus_bib,
read_scopus_csv,
read_scopus_ris,
read_wos,
)
from bibx.algorithms.sap import Sap
from bibx.sources.openalex import EnrichReferences
app = typer.Typer()
@app.callback()
def set_verbose(
verbose: Annotated[ # noqa: FBT002
bool, typer.Option("--verbose", "-v", help="Enable verbose logging.")
] = False,
) -> None:
"""BibX is a command-line tool for parsing bibliographic data."""
if verbose:
logging.basicConfig(level=logging.DEBUG)
class Format(Enum):
"""Supported formats."""
WOS = "wos"
RIS = "ris"
BIB = "bib"
CSV = "csv"
@app.command()
def describe(format: Format, filename: str) -> None:
"""Parse a file and provides a short description."""
if format == Format.WOS:
with open(filename) as f:
c = read_wos(f)
rprint(":boom: the file satisfies the ISI WOS format")
rprint(f"There are {len(c.articles)} records parsed")
if format == Format.RIS:
with open(filename) as f:
c = read_scopus_ris(f)
rprint(":boom: the file satisfies the scopus RIS format")
rprint(f"There are {len(c.articles)} records parsed")
if format == Format.BIB:
with open(filename) as f:
c = read_scopus_bib(f)
rprint(":boom: the file satisfies the scopus BIB format")
rprint(f"There are {len(c.articles)} records parsed")
if format == Format.CSV:
with open(filename) as f:
c = read_scopus_csv(f)
rprint(":boom: the file satisfies the scopus CSV format")
rprint(f"There are {len(c.articles)} records parsed")
@app.command()
def toy_sap() -> None:
"""Run the sap algorithm on a toy graph."""
graph = nx.DiGraph()
for node in "abcde":
graph.add_node(node, year=2000)
graph.add_edge("a", "b")
for node in "cde":
graph.add_edge("b", node)
s = Sap()
graph = s.tree(graph)
rprint(graph)
@app.command()
def sap(filename: str) -> None:
"""Run the sap algorithm on a seed file of any supported format."""
with open(filename) as f:
collection = read_any(f)
s = Sap()
graph = s.create_graph(collection)
graph = s.clean_graph(graph)
graph = s.tree(graph)
rprint(graph)
@app.command()
def openalex(
query: list[str],
enrich: EnrichReferences = typer.Option(
help="how to handle references",
default=EnrichReferences.BASIC,
),
) -> None:
"""Run the sap algorithm on a seed file of any supported format."""
c = query_openalex(" ".join(query), enrich=enrich)
s = Sap()
graph = s.create_graph(c)
graph = s.clean_graph(graph)
graph = s.tree(graph)
rprint(graph)
def main() -> None:
"""Entry point for the CLI."""
app()
if __name__ == "__main__":
app()