Add import_json_file()

This commit is contained in:
RumovZ 2022-05-11 16:32:11 +02:00
parent 322e44fa3e
commit b2beab8f40
5 changed files with 41 additions and 11 deletions

View file

@ -18,7 +18,8 @@ service ImportExportService {
rpc ExportAnkiPackage(ExportAnkiPackageRequest) returns (generic.UInt32); rpc ExportAnkiPackage(ExportAnkiPackageRequest) returns (generic.UInt32);
rpc GetCsvMetadata(CsvMetadataRequest) returns (CsvMetadata); rpc GetCsvMetadata(CsvMetadataRequest) returns (CsvMetadata);
rpc ImportCsv(ImportCsvRequest) returns (ImportResponse); 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 { message ImportCollectionPackageRequest {

View file

@ -428,8 +428,11 @@ class Collection(DeprecatedNamesMixin):
is_html=is_html, is_html=is_html,
) )
def import_json(self, json: str) -> ImportLogWithChanges: def import_json_file(self, path: str) -> ImportLogWithChanges:
return self._backend.import_json(json) return self._backend.import_json_file(path)
def import_json_string(self, json: str) -> ImportLogWithChanges:
return self._backend.import_json_string(json)
# Object helpers # Object helpers
########################################################################## ##########################################################################

View file

@ -96,7 +96,7 @@ class MnemosyneImporter(Importer):
QueryOp( QueryOp(
parent=mw, parent=mw,
op=lambda _: mnemosyne.serialize(path), 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() ).with_progress().run_in_background()
@ -110,6 +110,19 @@ class CsvImporter(Importer):
aqt.import_export.import_dialog.ImportDialog(mw, path) 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]] = [ IMPORTERS: list[Type[Importer]] = [
ColpkgImporter, ColpkgImporter,
ApkgImporter, ApkgImporter,
@ -168,10 +181,12 @@ def import_collection_package_op(
) )
def import_json(mw: aqt.main.AnkiQt, json: str) -> None: def import_json_string(mw: aqt.main.AnkiQt, json: str) -> None:
CollectionOp(parent=mw, op=lambda col: col.import_json(json)).with_backend_progress( CollectionOp(
import_progress_update parent=mw, op=lambda col: col.import_json_string(json)
).success(show_import_log).run_in_background() ).with_backend_progress(import_progress_update).success(
show_import_log
).run_in_background()
def show_import_log(log_with_changes: ImportLogWithChanges) -> None: def show_import_log(log_with_changes: ImportLogWithChanges) -> None:

View file

@ -101,8 +101,13 @@ impl ImportExportService for Backend {
.map(Into::into) .map(Into::into)
} }
fn import_json(&self, input: pb::String) -> Result<pb::ImportResponse> { fn import_json_file(&self, input: pb::String) -> Result<pb::ImportResponse> {
self.with_col(|col| col.import_json(&input.val)) 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) .map(Into::into)
} }
} }

View file

@ -7,7 +7,13 @@ use crate::{
}; };
impl Collection { 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)?; let data: ForeignData = serde_json::from_str(json)?;
data.import(self) data.import(self)
} }