Move mtgsdk from cardvault module.

This commit is contained in:
luxick
2017-04-17 17:48:44 +02:00
parent ba1ffa7864
commit c2856439c7
15 changed files with 29 additions and 17 deletions

View File

@@ -182,10 +182,19 @@ class Application:
self.push_status("Library saved")
def load_library(self):
all_existing = True
# Load library file
self.library = util.load_file(util.get_root_filename("library"))
if not self.library:
all_existing = False
self.library = {}
# Load tags file
self.tags = util.load_file(util.get_root_filename("tags"))
if not self.tags:
all_existing = False
self.tags = {}
if not all_existing:
self.save_library()
self.push_status("Library loaded")
def get_untagged_cards(self):

View File

@@ -31,7 +31,7 @@ class Handlers:
if response == Gtk.ResponseType.OK:
# prepare export file
file = {"library": self.app.library, "tags": self.app.tags}
util.export_library(dialog.get_filename, file)
util.export_library(dialog.get_filename(), file)
dialog.destroy()
@@ -54,6 +54,7 @@ class Handlers:
self.app.tags = tags
# Cause current page to reload with imported data
self.app.current_page.emit('show')
self.app.unsaved_changes = True
dialog.destroy()
def on_view_changed(self, item):

View File

@@ -11,7 +11,6 @@ def init_library_view(app):
card_list.list.connect("row-activated", app.handlers.on_library_card_selected)
container.add(card_list)
container.add_overlay(app.ui.get_object("noResults"))
container.add_overlay(app.ui.get_object("libEmpty"))
container.show_all()
app.ui.get_object("noResults").set_visible(False)
@@ -26,7 +25,6 @@ def reload_library(app, tag=None):
reload_tag_list(app, tag)
card_tree = app.ui.get_object("libraryContainer").get_child()
if lib:
app.ui.get_object("libEmpty").set_visible(False)
app.ui.get_object("noResults").set_visible(False)
card_tree.update(lib)
else:

View File

@@ -1,20 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This file is part of mtgsdk.
# https://github.com/MagicTheGathering/mtg-sdk-python
# Licensed under the MIT license:
# http://www.opensource.org/licenses/MIT-license
# Copyright (c) 2016, Andrew Backes <backes.andrew@gmail.com>
from mtgsdk.config import __version__, __pypi_packagename__, __github_username__, __github_reponame__, __endpoint__
from mtgsdk.card import Card
from mtgsdk.set import Set
from mtgsdk.supertype import Supertype
from mtgsdk.subtype import Subtype
from mtgsdk.type import Type
from mtgsdk.changelog import Changelog
from mtgsdk.restclient import RestClient
from mtgsdk.restclient import MtgException
from mtgsdk.querybuilder import QueryBuilder

View File

@@ -1,68 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This file is part of mtgsdk.
# https://github.com/MagicTheGathering/mtg-sdk-python
# Licensed under the MIT license:
# http://www.opensource.org/licenses/MIT-license
# Copyright (c) 2016, Andrew Backes <backes.andrew@gmail.com>
from mtgsdk.querybuilder import QueryBuilder
class Card(object):
RESOURCE = 'cards'
def __init__(self, response_dict={}):
self.name = response_dict.get('name')
self.layout = response_dict.get('layout')
self.mana_cost = response_dict.get('manaCost')
self.cmc = response_dict.get('cmc')
self.colors = response_dict.get('colors')
self.names = response_dict.get('names')
self.type = response_dict.get('type')
self.supertypes = response_dict.get('supertypes')
self.subtypes = response_dict.get('subtypes')
self.types = response_dict.get('types')
self.rarity = response_dict.get('rarity')
self.text = response_dict.get('text')
self.flavor = response_dict.get('flavor')
self.artist = response_dict.get('artist')
self.number = response_dict.get('number')
self.power = response_dict.get('power')
self.toughness = response_dict.get('toughness')
self.loyalty = response_dict.get('loyalty')
self.multiverse_id = response_dict.get('multiverseid')
self.variations = response_dict.get('variations')
self.watermark = response_dict.get('watermark')
self.border = response_dict.get('border')
self.timeshifted = response_dict.get('timeshifted')
self.hand = response_dict.get('hand')
self.life = response_dict.get('life')
self.release_date = response_dict.get('releaseDate')
self.starter = response_dict.get('starter')
self.printings = response_dict.get('printings')
self.original_text = response_dict.get('originalText')
self.original_type = response_dict.get('originalType')
self.source = response_dict.get('source')
self.image_url = response_dict.get('imageUrl')
self.set = response_dict.get('set')
self.set_name = response_dict.get('setName')
self.id = response_dict.get('id')
self.legalities = response_dict.get('legalities')
self.rulings = response_dict.get('rulings')
self.foreign_names = response_dict.get('foreignNames')
self.owned = None
@staticmethod
def find(id):
return QueryBuilder(Card).find(id)
@staticmethod
def where(**kwargs):
return QueryBuilder(Card).where(**kwargs)
@staticmethod
def all():
return QueryBuilder(Card).all()

View File

@@ -1,25 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This file is part of mtgsdk.
# https://github.com/MagicTheGathering/mtg-sdk-python
# Licensed under the MIT license:
# http://www.opensource.org/licenses/MIT-license
# Copyright (c) 2016, Andrew Backes <backes.andrew@gmail.com>
from mtgsdk.querybuilder import QueryBuilder
class Changelog(object):
RESOURCE = 'changelogs'
def __init__(self, response_dict={}):
self.id = response_dict.get('id')
self.version = response_dict.get('version')
self.details = response_dict.get('details')
self.release_date = response_dict.get('releaseDate')
@staticmethod
def all():
return QueryBuilder(Changelog).all()

View File

@@ -1,15 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This file is part of mtgsdk.
# https://github.com/MagicTheGathering/mtg-sdk-python
# Licensed under the MIT license:
# http://www.opensource.org/licenses/MIT-license
# Copyright (c) 2016, Andrew Backes <backes.andrew@gmail.com>
__version__ = "1.2.0"
__pypi_packagename__ = "mtgsdk"
__github_username__ = "MagicTheGathering"
__github_reponame__ = "mtg-sdk-python"
__endpoint__ = "https://api.magicthegathering.io/v1"

View File

@@ -1,100 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This file is part of mtgsdk.
# https://github.com/MagicTheGathering/mtg-sdk-python
# Licensed under the MIT license:
# http://www.opensource.org/licenses/MIT-license
# Copyright (c) 2016, Andrew Backes <backes.andrew@gmail.com>
from mtgsdk.restclient import RestClient
from mtgsdk.config import __endpoint__
class QueryBuilder(object):
def __init__(self, type):
self.params = {}
self.type = type
def find(self, id):
"""Get a resource by its id
Args:
id (string): Resource id
Returns:
object: Instance of the resource type
"""
url = "{}/{}/{}".format(__endpoint__, self.type.RESOURCE, id)
response = RestClient.get(url)[self.type.RESOURCE[:-1]]
return self.type(response)
def find_many(self, url, type, resource):
"""Get a list of resources
Args:
url (string): URL to invoke
type (class): Class type
resource (string): The REST Resource
Returns:
list of object: List of resource instances
"""
list = []
response = RestClient.get(url)[resource]
if len(response) > 0:
for item in response:
list.append(type(item))
return list
def where(self, **kwargs):
"""Adds a parameter to the dictionary of query parameters
Args:
**kwargs: Arbitrary keyword arguments.
Returns:
QueryBuilder: Instance of the QueryBuilder
"""
for key, value in kwargs.items():
self.params[key] = value
return self
def all(self):
"""Get all resources, automatically paging through data
Returns:
list of object: List of resource objects
"""
list = []
page = 1
fetch_all = True
url = "{}/{}".format(__endpoint__, self.type.RESOURCE)
if 'page' in self.params:
page = self.params['page']
fetch_all = False
while True:
response = RestClient.get(url, self.params)[self.type.RESOURCE]
if len(response) > 0:
for item in response:
list.append(self.type(item))
if not fetch_all:
break
else:
page += 1
self.where(page=page)
else:
break
return list
def array(self):
"""Get all resources and return the result as an array
Returns:
array of str: Array of resources
"""
url = "{}/{}".format(__endpoint__, self.type.RESOURCE)
return RestClient.get(url, self.params)[self.type.RESOURCE]

View File

@@ -1,47 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This file is part of mtgsdk.
# https://github.com/MagicTheGathering/mtg-sdk-python
# Licensed under the MIT license:
# http://www.opensource.org/licenses/MIT-license
# Copyright (c) 2016, Andrew Backes <backes.andrew@gmail.com>
import json
from urllib.request import Request, urlopen
from urllib.error import HTTPError
from urllib.parse import urlencode
class RestClient(object):
@staticmethod
def get(url, params={}):
"""Invoke an HTTP GET request on a url
Args:
url (string): URL endpoint to request
params (dict): Dictionary of url parameters
Returns:
dict: JSON response as a dictionary
"""
request_url = url
if len(params) > 0:
request_url = "{}?{}".format(url, urlencode(params))
try:
req = Request(request_url, headers={'User-Agent': 'Mozilla/5.0'})
response = json.loads(urlopen(req).read().decode("utf-8"))
return response
except HTTPError as err:
raise MtgException(err.read())
class MtgException(Exception):
def __init__(self, description):
self.description = description
def __str__(self):
return self.description

View File

@@ -1,49 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This file is part of mtgsdk.
# https://github.com/MagicTheGathering/mtg-sdk-python
# Licensed under the MIT license:
# http://www.opensource.org/licenses/MIT-license
# Copyright (c) 2016, Andrew Backes <backes.andrew@gmail.com>
from mtgsdk.querybuilder import QueryBuilder
from mtgsdk.config import __endpoint__
from mtgsdk.card import Card
class Set(object):
RESOURCE = 'sets'
def __init__(self, response_dict={}):
self.code = response_dict.get('code')
self.name = response_dict.get('name')
self.type = response_dict.get('type')
self.border = response_dict.get('border')
self.mkm_id = response_dict.get('mkm_id')
self.mkm_name = response_dict.get('mkm_name')
self.release_date = response_dict.get('releaseDate')
self.gatherer_code = response_dict.get('gathererCode')
self.magic_cards_info_code = response_dict.get('magicCardsInfoCode')
self.booster = response_dict.get('booster')
self.old_code = response_dict.get('oldCode')
self.block = response_dict.get('block')
self.online_only = response_dict.get('onlineOnly')
@staticmethod
def find(id):
return QueryBuilder(Set).find(id)
@staticmethod
def where(**kwargs):
return QueryBuilder(Set).where(**kwargs)
@staticmethod
def all():
return QueryBuilder(Set).all()
@staticmethod
def generate_booster(code):
url = "{}/{}/{}/booster".format(__endpoint__, Set.RESOURCE, code)
return QueryBuilder(Set).find_many(url, Card, Card.RESOURCE)

View File

@@ -1,19 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This file is part of mtgsdk.
# https://github.com/MagicTheGathering/mtg-sdk-python
# Licensed under the MIT license:
# http://www.opensource.org/licenses/MIT-license
# Copyright (c) 2016, Andrew Backes <backes.andrew@gmail.com>
from mtgsdk.querybuilder import QueryBuilder
class Subtype(object):
RESOURCE = 'subtypes'
@staticmethod
def all():
return QueryBuilder(Subtype).array()

View File

@@ -1,19 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This file is part of mtgsdk.
# https://github.com/MagicTheGathering/mtg-sdk-python
# Licensed under the MIT license:
# http://www.opensource.org/licenses/MIT-license
# Copyright (c) 2016, Andrew Backes <backes.andrew@gmail.com>
from mtgsdk.querybuilder import QueryBuilder
class Supertype(object):
RESOURCE = 'supertypes'
@staticmethod
def all():
return QueryBuilder(Supertype).array()

View File

@@ -1,19 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This file is part of mtgsdk.
# https://github.com/MagicTheGathering/mtg-sdk-python
# Licensed under the MIT license:
# http://www.opensource.org/licenses/MIT-license
# Copyright (c) 2016, Andrew Backes <backes.andrew@gmail.com>
from mtgsdk.querybuilder import QueryBuilder
class Type(object):
RESOURCE = 'types'
@staticmethod
def all():
return QueryBuilder(Type).array()

View File

@@ -1,9 +1,12 @@
import gi
from cardvault import util
from cardvault import cardlist
from gi.repository import Gtk, Gdk
from mtgsdk import Card
from urllib.error import URLError, HTTPError
import gi
from gi.repository import Gtk, Gdk
from cardvault import cardlist
from cardvault import util
from mtgsdk import Card
gi.require_version('Gtk', '3.0')

View File

@@ -1,13 +1,15 @@
import os
import gi
import re
import enum
import copy
import enum
import json
from gi.repository import GdkPixbuf, Gtk
from PIL import Image as PImage
import os
import re
from urllib import request
import gi
import six.moves.cPickle as pickle
from PIL import Image as PImage
from gi.repository import GdkPixbuf
gi.require_version('Gtk', '3.0')
from mtgsdk import Set
@@ -214,8 +216,6 @@ def import_library(path):
def save_file(path, file):
if not os.path.exists(path):
os.makedirs(path)
# Serialize using cPickle
try:
pickle.dump(file, open(path, 'wb'))
@@ -227,7 +227,8 @@ def save_file(path, file):
def load_file(path):
if not os.path.isfile(path):
log(path + " does not exist", LogLevel.Error)
log(path + " does not exist", LogLevel.Warning)
return
try:
loaded = pickle.load(open(path, 'rb'))
except OSError as err: