-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Add Pseudonymization to CLI #13158
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
Merged
Merged
Add Pseudonymization to CLI #13158
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c41d8b8
Add Pseudonymization to CLI
paudelritij 5d5299f
Reformat Pseudonymize command
paudelritij 8ba58e2
Remove exclamation(!) sign
paudelritij 997d859
Rename BibTex to BibTeX
paudelritij f91743f
Refactor localization messages and update ADR annotation
paudelritij 17343f5
Add implementation dependency for ADR
paudelritij 2ddc7cd
Update JabRef_en.properties
paudelritij 48407f0
Merge branch 'main' into fix-for-issue-13109
koppor 4977296
Improve output file path handling and consolidate file existence check
paudelritij 87dccd0
Merge branch 'main' into fix-for-issue-13109
paudelritij 1bd0309
Add new class to JabKitLauncher
koppor 39a904e
Enhance CLI output messages
paudelritij 3aa2840
Refactor methods to display success message after successful save
paudelritij 60b4349
Merge branch 'main' into fix-for-issue-13109
paudelritij File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package org.jabref.cli; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Optional; | ||
|
||
import org.jabref.logic.importer.ParserResult; | ||
import org.jabref.logic.l10n.Localization; | ||
import org.jabref.logic.pseudonymization.Pseudonymization; | ||
import org.jabref.logic.pseudonymization.PseudonymizationResultCsvWriter; | ||
import org.jabref.logic.util.io.FileUtil; | ||
import org.jabref.model.database.BibDatabaseContext; | ||
|
||
import io.github.adr.linked.ADR; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import picocli.CommandLine.Command; | ||
import picocli.CommandLine.Mixin; | ||
import picocli.CommandLine.Option; | ||
import picocli.CommandLine.ParentCommand; | ||
|
||
@Command(name = "pseudonymize", description = "Perform pseudonymization of the library") | ||
public class Pseudonymize implements Runnable { | ||
private final static Logger LOGGER = LoggerFactory.getLogger(Pseudonymize.class); | ||
private static final String PSEUDO_SUFFIX = ".pseudo"; | ||
private static final String BIB_EXTENSION = ".bib"; | ||
private static final String CSV_EXTENSION = ".csv"; | ||
|
||
@ParentCommand | ||
private ArgumentProcessor argumentProcessor; | ||
|
||
@Mixin | ||
private ArgumentProcessor.SharedOptions sharedOptions = new ArgumentProcessor.SharedOptions(); | ||
|
||
@ADR(45) | ||
@Option(names = {"--input"}, description = "BibTeX file to be pseudonymized", required = true) | ||
private String inputFile; | ||
|
||
@Option(names = {"--output"}, description = "Output pseudo-bib file") | ||
private String outputFile; | ||
|
||
@Option(names = {"--key"}, description = "Output pseudo-keys file") | ||
private String keyFile; | ||
|
||
@Option(names = {"-f", "--force"}, description = "Overwrite output file(s) if any exist(s)") | ||
private boolean force; | ||
|
||
@Override | ||
public void run() { | ||
Path inputPath = Path.of(inputFile); | ||
String fileName = FileUtil.getBaseName(inputFile); | ||
Path pseudoBibPath = resolveOutputPath(outputFile, inputPath, fileName + PSEUDO_SUFFIX + BIB_EXTENSION); | ||
Path pseudoKeyPath = resolveOutputPath(keyFile, inputPath, fileName + PSEUDO_SUFFIX + CSV_EXTENSION); | ||
|
||
Optional<ParserResult> parserResult = ArgumentProcessor.importFile( | ||
inputFile, | ||
"bibtex", | ||
argumentProcessor.cliPreferences, | ||
sharedOptions.porcelain); | ||
|
||
if (parserResult.isEmpty()) { | ||
System.out.println(Localization.lang("Unable to open file '%0'.", inputFile)); | ||
return; | ||
} | ||
|
||
if (parserResult.get().isInvalid()) { | ||
System.out.println(Localization.lang("Input file '%0' is invalid and could not be parsed.", inputFile)); | ||
return; | ||
} | ||
|
||
System.out.println(Localization.lang("Pseudonymizing library '%0'...", fileName)); | ||
Pseudonymization pseudonymization = new Pseudonymization(); | ||
BibDatabaseContext databaseContext = parserResult.get().getDatabaseContext(); | ||
Pseudonymization.Result result = pseudonymization.pseudonymizeLibrary(databaseContext); | ||
|
||
if (!fileOverwriteCheck(pseudoBibPath)) { | ||
return; | ||
} | ||
|
||
ArgumentProcessor.saveDatabaseContext( | ||
argumentProcessor.cliPreferences, | ||
argumentProcessor.entryTypesManager, | ||
result.bibDatabaseContext(), | ||
pseudoBibPath); | ||
|
||
if (!fileOverwriteCheck(pseudoKeyPath)) { | ||
return; | ||
} | ||
|
||
try { | ||
PseudonymizationResultCsvWriter.writeValuesMappingAsCsv(pseudoKeyPath, result); | ||
System.out.println(Localization.lang("Saved %0.", pseudoKeyPath)); | ||
} catch (IOException ex) { | ||
LOGGER.error("Unable to save keys for pseudonymized library", ex); | ||
} | ||
} | ||
|
||
private Path resolveOutputPath(String customPath, Path inputPath, String defaultFileName) { | ||
return customPath != null ? Path.of(customPath) : inputPath.getParent().resolve(defaultFileName); | ||
} | ||
|
||
private boolean fileOverwriteCheck(Path filePath) { | ||
if (!Files.exists(filePath)) { | ||
return true; | ||
} | ||
|
||
String fileName = filePath.getFileName().toString(); | ||
|
||
if (!force) { | ||
System.out.println(Localization.lang("File '%0' already exists. Use -f or --force to overwrite.", fileName)); | ||
return false; | ||
} | ||
|
||
System.out.println(Localization.lang("File '%0' already exists. Overwriting.", fileName)); | ||
return true; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.