Export in JSON format.

This commit is contained in:
luxick
2017-08-09 11:06:25 +02:00
parent 6bc7d8ed57
commit 3e98935512
5 changed files with 75 additions and 8 deletions

View File

@@ -4,12 +4,12 @@ import copy
import re
import mtgsdk
import time
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GObject, Pango
from typing import Type, Dict, List
from cardvault import handlers
from cardvault import util
from cardvault import database
gi.require_version('Gtk', '3.0')
class Application:
@@ -219,7 +219,7 @@ class Application:
pass
def load_user_data(self):
util.log("Loading Data from database", util.LogLevel.Info)
util.log("Loading User Data from form '{}'".format(util.get_root_filename(util.DB_NAME)), util.LogLevel.Info)
start = time.time()
self.library = self.db.lib_get_all()
self.tags = self.db.tag_get_all()

View File

@@ -230,8 +230,38 @@
<object class="GtkBox" id="libTools">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">2</property>
<property name="margin_right">2</property>
<property name="vexpand">False</property>
<property name="spacing">2</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Showing:</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="searchTitle">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<attributes>
<attribute name="weight" value="bold"/>
</attributes>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkSearchEntry" id="searchLibEntry">
<property name="visible">True</property>
@@ -239,13 +269,13 @@
<property name="primary_icon_name">edit-find-symbolic</property>
<property name="primary_icon_activatable">False</property>
<property name="primary_icon_sensitive">False</property>
<property name="placeholder_text" translatable="yes">Search Library</property>
<property name="placeholder_text" translatable="yes">Search</property>
<signal name="changed" handler="do_refilter_library" object="libraryContainer" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
<property name="position">2</property>
</packing>
</child>
<child>
@@ -266,12 +296,12 @@
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
<property name="position">3</property>
</packing>
</child>
<child>
<object class="GtkButton" id="tagCardButton">
<property name="label" translatable="yes">Tag card</property>
<property name="label" translatable="yes">Tag card(s)</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
@@ -280,7 +310,7 @@
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
<property name="position">4</property>
</packing>
</child>
</object>

View File

@@ -51,6 +51,32 @@ class Handlers(SearchHandlers, LibraryHandlers, WantsHandlers):
dialog.destroy()
def do_export_json(self, item):
"""
Export user data to file
Called By: Export menu item
"""
response = self.app.show_dialog_yn("Temoprary Dialog", "[Choose data to export here]")
if response == Gtk.ResponseType.NO:
return
dialog = Gtk.FileChooserDialog("Export Library", self.app.ui.get_object("mainWindow"),
Gtk.FileChooserAction.SAVE,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
dialog.set_current_name("mtg_export-" + datetime.datetime.now().strftime("%Y-%m-%d") + ".json")
dialog.set_current_folder(os.path.expanduser("~"))
response = dialog.run()
if response == Gtk.ResponseType.OK:
# prepare export file
file = {"library": self.app.library, "tags": self.app.tags, "wants": self.app.wants}
util.export_json(dialog.get_filename(), file)
self.app.push_status("Library exported")
dialog.destroy()
def do_import_library(self, item):
"""Called by menu item import library"""
# Show file picker dialog for import

View File

@@ -1,6 +1,7 @@
import copy
import enum
import json
import jsonpickle
import os
import re
import sys
@@ -298,6 +299,16 @@ def export_library(path, file):
log(str(err), LogLevel.Error)
def export_json(path, file):
"""Write file in json format"""
try:
f = open(path, 'w')
s = jsonpickle.encode(file)
f.write(s)
except OSError as err:
log(str(err), LogLevel.Error)
def import_library(path: str) -> ():
try:
imported = pickle.load(open(path, 'rb'))

View File

@@ -37,5 +37,5 @@ setup(
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'License :: OSI Approved :: MIT License',
], install_requires=['gi', 'pillow', 'six', 'mtgsdk']
], install_requires=['gi', 'pillow', 'six', 'mtgsdk', 'jsonpickle']
)