mirror of
https://github.com/ankitects/anki.git
synced 2025-09-25 09:16:38 -04:00
Add import_json_file()
This commit is contained in:
parent
322e44fa3e
commit
b2beab8f40
5 changed files with 41 additions and 11 deletions
|
@ -18,7 +18,8 @@ service ImportExportService {
|
|||
rpc ExportAnkiPackage(ExportAnkiPackageRequest) returns (generic.UInt32);
|
||||
rpc GetCsvMetadata(CsvMetadataRequest) returns (CsvMetadata);
|
||||
rpc ImportCsv(ImportCsvRequest) returns (ImportResponse);
|
||||
rpc ImportJson(generic.String) returns (ImportResponse);
|
||||
rpc ImportJsonFile(generic.String) returns (ImportResponse);
|
||||
rpc ImportJsonString(generic.String) returns (ImportResponse);
|
||||
}
|
||||
|
||||
message ImportCollectionPackageRequest {
|
||||
|
|
|
@ -428,8 +428,11 @@ class Collection(DeprecatedNamesMixin):
|
|||
is_html=is_html,
|
||||
)
|
||||
|
||||
def import_json(self, json: str) -> ImportLogWithChanges:
|
||||
return self._backend.import_json(json)
|
||||
def import_json_file(self, path: str) -> ImportLogWithChanges:
|
||||
return self._backend.import_json_file(path)
|
||||
|
||||
def import_json_string(self, json: str) -> ImportLogWithChanges:
|
||||
return self._backend.import_json_string(json)
|
||||
|
||||
# Object helpers
|
||||
##########################################################################
|
||||
|
|
|
@ -96,7 +96,7 @@ class MnemosyneImporter(Importer):
|
|||
QueryOp(
|
||||
parent=mw,
|
||||
op=lambda _: mnemosyne.serialize(path),
|
||||
success=lambda json: import_json(mw, json),
|
||||
success=lambda json: import_json_string(mw, json),
|
||||
).with_progress().run_in_background()
|
||||
|
||||
|
||||
|
@ -110,6 +110,19 @@ class CsvImporter(Importer):
|
|||
aqt.import_export.import_dialog.ImportDialog(mw, path)
|
||||
|
||||
|
||||
class JsonImporter(Importer):
|
||||
accepted_file_endings = [".json", ".anki-json"]
|
||||
|
||||
@staticmethod
|
||||
def do_import(mw: aqt.main.AnkiQt, path: str) -> None:
|
||||
CollectionOp(
|
||||
parent=mw,
|
||||
op=lambda col: col.import_json_file(path),
|
||||
).with_backend_progress(import_progress_update).success(
|
||||
show_import_log
|
||||
).run_in_background()
|
||||
|
||||
|
||||
IMPORTERS: list[Type[Importer]] = [
|
||||
ColpkgImporter,
|
||||
ApkgImporter,
|
||||
|
@ -168,10 +181,12 @@ def import_collection_package_op(
|
|||
)
|
||||
|
||||
|
||||
def import_json(mw: aqt.main.AnkiQt, json: str) -> None:
|
||||
CollectionOp(parent=mw, op=lambda col: col.import_json(json)).with_backend_progress(
|
||||
import_progress_update
|
||||
).success(show_import_log).run_in_background()
|
||||
def import_json_string(mw: aqt.main.AnkiQt, json: str) -> None:
|
||||
CollectionOp(
|
||||
parent=mw, op=lambda col: col.import_json_string(json)
|
||||
).with_backend_progress(import_progress_update).success(
|
||||
show_import_log
|
||||
).run_in_background()
|
||||
|
||||
|
||||
def show_import_log(log_with_changes: ImportLogWithChanges) -> None:
|
||||
|
|
|
@ -101,8 +101,13 @@ impl ImportExportService for Backend {
|
|||
.map(Into::into)
|
||||
}
|
||||
|
||||
fn import_json(&self, input: pb::String) -> Result<pb::ImportResponse> {
|
||||
self.with_col(|col| col.import_json(&input.val))
|
||||
fn import_json_file(&self, input: pb::String) -> Result<pb::ImportResponse> {
|
||||
self.with_col(|col| col.import_json_file(&input.val))
|
||||
.map(Into::into)
|
||||
}
|
||||
|
||||
fn import_json_string(&self, input: pb::String) -> Result<pb::ImportResponse> {
|
||||
self.with_col(|col| col.import_json_string(&input.val))
|
||||
.map(Into::into)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,13 @@ use crate::{
|
|||
};
|
||||
|
||||
impl Collection {
|
||||
pub fn import_json(&mut self, json: &str) -> Result<OpOutput<NoteLog>> {
|
||||
pub fn import_json_file(&mut self, path: &str) -> Result<OpOutput<NoteLog>> {
|
||||
let slice = std::fs::read(path)?;
|
||||
let data: ForeignData = serde_json::from_slice(&slice)?;
|
||||
data.import(self)
|
||||
}
|
||||
|
||||
pub fn import_json_string(&mut self, json: &str) -> Result<OpOutput<NoteLog>> {
|
||||
let data: ForeignData = serde_json::from_str(json)?;
|
||||
data.import(self)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue