Skip to content

Commit 5be97cf

Browse files
author
Gerit Wagner
committed
update cli-prep-man
1 parent c9678fd commit 5be97cf

1 file changed

Lines changed: 77 additions & 15 deletions

File tree

colrev/packages/cli_prep_man/src/cli_prep_man_prep_man.py

Lines changed: 77 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import logging
44
import typing
55

6-
import inquirer
6+
import questionary
77

88
import colrev.ops.prep_man
99
import colrev.review_manager
@@ -47,36 +47,41 @@ def _get_choices(
4747
)
4848
link_info = f"DOI: {r.data.get(Fields.DOI, 'No DOI')}"
4949

50-
label = f"[{i+1}]\n{title}\n{citation_info.strip()}\n{link_info}"
50+
label = f"[{i+1}] {title} - {citation_info.strip()} - {link_info}"
5151
choices.append(label)
5252
choices.append("Skip merging")
5353
return choices
5454

5555
def _get_selected_record(
56-
self, similar_records: typing.List[colrev.record.record_prep.PrepRecord]
56+
self,
57+
similar_records: typing.List[colrev.record.record_prep.PrepRecord],
5758
) -> typing.Optional[colrev.record.record_prep.PrepRecord]:
5859

5960
choices = self._get_choices(similar_records)
60-
# Prompt user to select a record
61-
answer = inquirer.list_input("Select a record to merge:", choices=choices)
62-
if answer == "Skip merging":
61+
answer = questionary.select(
62+
"Select a record to merge:",
63+
choices=choices,
64+
).ask()
65+
66+
# If user cancels or chooses to skip
67+
if answer is None or answer == "Skip merging":
6368
return None
6469

6570
selected_index = choices.index(answer)
66-
selected_record = similar_records[selected_index]
67-
return selected_record
71+
return similar_records[selected_index]
6872

6973
def _prep_record(self, record: dict) -> None:
7074
print(f"\n--- Record {record[Fields.ID]} ---")
71-
print(f"Title: {record[Fields.TITLE]}")
75+
# print(f"Title: {record[Fields.TITLE]}")
76+
print(colrev.record.record.Record(record))
7277

7378
# Use CrossrefAPI to find similar records
7479
try:
7580
# `url` gets overwritten internally
7681
api = crossref_api.CrossrefAPI(url="https://api.crossref.org/")
7782
similar_records = api.crossref_query(
78-
record_input=colrev.record.record_prep.PrepRecord(record)
79-
) # top_n=5
83+
record_input=colrev.record.record_prep.PrepRecord(record), top_n=5
84+
)
8085
except Exception as e: # pylint: disable=broad-exception-caught
8186
print(f"Crossref query failed: {e}")
8287
return
@@ -98,8 +103,8 @@ def prepare_manual(self, records: dict) -> dict:
98103
"""Run the prep-man operation."""
99104

100105
for record in records.values():
101-
if record[Fields.STATUS] != RecordState.md_needs_manual_preparation:
102-
continue
106+
# if record[Fields.STATUS] != RecordState.md_needs_manual_preparation:
107+
# continue
103108
if record[Fields.TITLE] in ["", "UNKNOWN"]:
104109
continue
105110

@@ -113,11 +118,68 @@ def main() -> None:
113118

114119
print("CLI initialized")
115120

116-
review_manager = colrev.review_manager.ReviewManager()
121+
def _select_target_ids(records: dict[str, dict]) -> list[str]:
122+
"""Ask user which records to process and return the selected record_ids.
123+
124+
Options:
125+
- All records where Fields.STATUS == RecordState.md_needs_man_prep
126+
- Manually entered, comma-separated record_ids
127+
"""
128+
mode = questionary.select(
129+
"Which records do you want to prepare manually?",
130+
choices=[
131+
"All records with status: md_needs_man_prep",
132+
"Selected records by ID (comma-separated)",
133+
],
134+
).ask()
135+
136+
if mode is None:
137+
# user cancelled
138+
return []
139+
140+
if mode.startswith("All"):
141+
target_ids = [
142+
rec_id
143+
for rec_id, rec in records.items()
144+
if rec.get(Fields.STATUS, None)
145+
== RecordState.md_needs_manual_preparation
146+
]
147+
else:
148+
raw_ids = (
149+
questionary.text("Enter record IDs (comma-separated):").ask() or ""
150+
)
151+
target_ids = [rid.strip() for rid in raw_ids.split(",") if rid.strip()]
152+
153+
# sanity-check: warn about unknown IDs
154+
unknown = [rid for rid in target_ids if rid not in records]
155+
if unknown:
156+
questionary.print(
157+
f"Warning: {len(unknown)} ID(s) not found and will be ignored: {', '.join(unknown)}",
158+
style="bold fg:yellow",
159+
)
160+
target_ids = [rid for rid in target_ids if rid in records]
161+
162+
return target_ids
163+
164+
review_manager = colrev.review_manager.ReviewManager(force_mode=True)
117165
prep_man_operation = review_manager.get_prep_man_operation()
118166
records = review_manager.dataset.load_records_dict()
119167

168+
target_ids = _select_target_ids(
169+
records
170+
) # or _select_target_ids(...) if not in a class
171+
172+
if not target_ids:
173+
questionary.print("No matching records selected.", style="bold fg:yellow")
174+
return
175+
176+
subset = {rid: records[rid] for rid in target_ids}
120177
cli_prep_man = CliPrepMan(prep_man_operation=prep_man_operation, settings={})
121-
records = cli_prep_man.prepare_manual(records=records)
178+
updated_subset = cli_prep_man.prepare_manual(records=subset)
122179

180+
# merge back into the full set and save
181+
records.update(updated_subset)
123182
review_manager.dataset.save_records_dict(records)
183+
questionary.print(
184+
f"Prepared {len(updated_subset)} record(s).", style="bold fg:green"
185+
)

0 commit comments

Comments
 (0)