Separate cache path for files and images
This commit is contained in:
@@ -1,10 +1,11 @@
|
|||||||
# Title of the Program Window
|
# Title of the Program Window
|
||||||
import os
|
import os
|
||||||
|
|
||||||
applicationtitle="MTG Collector (working title) v0.1"
|
application_title= "MTG Collector (working title) v0.1"
|
||||||
|
|
||||||
# Path of image cache
|
# Path of image cache
|
||||||
cachepath= os.path.dirname(__file__) + "/.cache/"
|
cache_path= os.path.dirname(__file__) + "/.cache/"
|
||||||
|
image_cache_path=os.path.dirname(__file__) + "/.cache/images/"
|
||||||
|
|
||||||
# When True Search view will list a card multiple times for each set they appear in
|
# When True Search view will list a card multiple times for each set they appear in
|
||||||
show_from_all_sets=False
|
show_from_all_sets=False
|
||||||
@@ -9,7 +9,7 @@ from gi.repository import Gtk, GObject
|
|||||||
|
|
||||||
class MainWindow(Gtk.Window):
|
class MainWindow(Gtk.Window):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
Gtk.Window.__init__(self, title=config.applicationtitle)
|
Gtk.Window.__init__(self, title=config.application_title)
|
||||||
self.set_border_width(2)
|
self.set_border_width(2)
|
||||||
self.set_size_request(1000, 700)
|
self.set_size_request(1000, 700)
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,14 @@
|
|||||||
from urllib.error import URLError, HTTPError
|
|
||||||
|
|
||||||
import gi
|
|
||||||
from gi.repository import Pango
|
|
||||||
import util
|
import util
|
||||||
import details
|
import details
|
||||||
import config
|
import config
|
||||||
|
|
||||||
gi.require_version('Gtk', '3.0')
|
|
||||||
from gi.repository import Gtk, GdkPixbuf, GObject
|
|
||||||
from mtgsdk import Card
|
|
||||||
import threading
|
import threading
|
||||||
|
import gi
|
||||||
|
from urllib.error import URLError, HTTPError
|
||||||
|
from mtgsdk import Card
|
||||||
|
from gi.repository import Gtk, GdkPixbuf, GObject, Pango
|
||||||
|
gi.require_version('Gtk', '3.0')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class SearchView(Gtk.Grid):
|
class SearchView(Gtk.Grid):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@@ -98,7 +97,7 @@ class SearchView(Gtk.Grid):
|
|||||||
# completer.connect("match-selected", self.match_selected)
|
# completer.connect("match-selected", self.match_selected)
|
||||||
# self.set_combo.get_child().set_completion(completer)
|
# self.set_combo.get_child().set_completion(completer)
|
||||||
|
|
||||||
#Type
|
# Type
|
||||||
type_label = Gtk.Label("Type", xalign=0)
|
type_label = Gtk.Label("Type", xalign=0)
|
||||||
self.type_store = Gtk.ListStore(str)
|
self.type_store = Gtk.ListStore(str)
|
||||||
types = ["Any", "Creature", "Artifact", "Instant"
|
types = ["Any", "Creature", "Artifact", "Instant"
|
||||||
@@ -152,23 +151,23 @@ class SearchView(Gtk.Grid):
|
|||||||
info.set_padding(5, 5)
|
info.set_padding(5, 5)
|
||||||
|
|
||||||
index = Gtk.CellRendererText()
|
index = Gtk.CellRendererText()
|
||||||
self.indexcolumn = Gtk.TreeViewColumn(title=index, cell_renderer=index, text=0)
|
col_id = Gtk.TreeViewColumn(title=index, cell_renderer=index, text=0)
|
||||||
self.indexcolumn.set_visible(False)
|
col_id.set_visible(False)
|
||||||
self.column1 = Gtk.TreeViewColumn(title="Image", cell_renderer=image, pixbuf=1)
|
col_image = Gtk.TreeViewColumn(title="Image", cell_renderer=image, pixbuf=1)
|
||||||
self.column1.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE)
|
col_image.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE)
|
||||||
self.column2 = Gtk.TreeViewColumn(title="Name", cell_renderer=title, text=2)
|
col_name = Gtk.TreeViewColumn(title="Name", cell_renderer=title, text=2)
|
||||||
self.column2.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE)
|
col_name.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE)
|
||||||
self.column3 = Gtk.TreeViewColumn(title="Card Text", cell_renderer=info, text=3)
|
col_text = Gtk.TreeViewColumn(title="Card Text", cell_renderer=info, text=3)
|
||||||
self.column3.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE)
|
col_text.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE)
|
||||||
self.column3.set_resizable(True)
|
col_text.set_resizable(True)
|
||||||
self.column3.set_expand(True)
|
col_text.set_expand(True)
|
||||||
self.column4 = Gtk.TreeViewColumn(title="Mana Cost", cell_renderer=image, pixbuf=4)
|
col_mana = Gtk.TreeViewColumn(title="Mana Cost", cell_renderer=image, pixbuf=4)
|
||||||
self.column4.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE)
|
col_mana.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE)
|
||||||
|
|
||||||
self.list.append_column(self.column1)
|
self.list.append_column(col_image)
|
||||||
self.list.append_column(self.column2)
|
self.list.append_column(col_name)
|
||||||
self.list.append_column(self.column3)
|
self.list.append_column(col_text)
|
||||||
self.list.append_column(self.column4)
|
self.list.append_column(col_mana)
|
||||||
|
|
||||||
# Detail View for selected Card
|
# Detail View for selected Card
|
||||||
self.details = details.DetailBar()
|
self.details = details.DetailBar()
|
||||||
@@ -188,9 +187,6 @@ class SearchView(Gtk.Grid):
|
|||||||
# Details
|
# Details
|
||||||
self.attach(self.details, 4, 0, 1, 1)
|
self.attach(self.details, 4, 0, 1, 1)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
self.selection = self.list.get_selection()
|
self.selection = self.list.get_selection()
|
||||||
self.selection.connect("changed", self.on_card_selected)
|
self.selection.connect("changed", self.on_card_selected)
|
||||||
|
|
||||||
@@ -241,12 +237,12 @@ class SearchView(Gtk.Grid):
|
|||||||
print("\nStart online search")
|
print("\nStart online search")
|
||||||
GObject.idle_add(util.push_status, "Searching for cards", priorty=GObject.PRIORITY_DEFAULT)
|
GObject.idle_add(util.push_status, "Searching for cards", priorty=GObject.PRIORITY_DEFAULT)
|
||||||
try:
|
try:
|
||||||
self.cards = Card.where(name=term)\
|
self.cards = Card.where(name=term) \
|
||||||
.where(colorIdentity=",".join(colorlist)) \
|
.where(colorIdentity=",".join(colorlist)) \
|
||||||
.where(types=typefilter)\
|
.where(types=typefilter) \
|
||||||
.where(set=setfilter)\
|
.where(set=setfilter) \
|
||||||
.where(rarity=rarityfilter) \
|
.where(rarity=rarityfilter) \
|
||||||
.where(pageSize=50)\
|
.where(pageSize=50) \
|
||||||
.where(page=1).all()
|
.where(page=1).all()
|
||||||
except (URLError, HTTPError) as err:
|
except (URLError, HTTPError) as err:
|
||||||
print("Error connecting to the internet")
|
print("Error connecting to the internet")
|
||||||
|
|||||||
@@ -1,20 +1,16 @@
|
|||||||
import json
|
|
||||||
import os
|
import os
|
||||||
import pickle
|
import pickle
|
||||||
|
|
||||||
import gi
|
import gi
|
||||||
import re
|
import re
|
||||||
|
|
||||||
import config
|
import config
|
||||||
import network
|
import network
|
||||||
gi.require_version('Gtk', '3.0')
|
|
||||||
from gi.repository import GdkPixbuf, Gtk
|
from gi.repository import GdkPixbuf, Gtk
|
||||||
from PIL import Image as PImage
|
from PIL import Image as PImage
|
||||||
from urllib import request
|
from urllib import request
|
||||||
from mtgsdk import Set
|
gi.require_version('Gtk', '3.0')
|
||||||
from urllib.error import URLError, HTTPError
|
|
||||||
|
|
||||||
# Loacally stored images for faster loading times
|
# Locally stored images for faster loading times
|
||||||
imagecache = []
|
imagecache = []
|
||||||
manaicons = {}
|
manaicons = {}
|
||||||
|
|
||||||
@@ -25,7 +21,7 @@ status_bar = None
|
|||||||
|
|
||||||
|
|
||||||
def load_sets():
|
def load_sets():
|
||||||
path = config.cachepath + "sets"
|
path = config.cache_path + "sets"
|
||||||
if not os.path.isfile(path):
|
if not os.path.isfile(path):
|
||||||
# use mtgsdk api to retrieve al list of all sets
|
# use mtgsdk api to retrieve al list of all sets
|
||||||
new_sets = network.net_load_sets()
|
new_sets = network.net_load_sets()
|
||||||
@@ -33,9 +29,9 @@ def load_sets():
|
|||||||
show_message("API Error", "Could not retrieve Set infos")
|
show_message("API Error", "Could not retrieve Set infos")
|
||||||
return
|
return
|
||||||
# Serialize the loaded data to a file
|
# Serialize the loaded data to a file
|
||||||
pickle.dump(new_sets, open(config.cachepath + "sets", 'wb'))
|
pickle.dump(new_sets, open(config.cache_path + "sets", 'wb'))
|
||||||
# Deserialize set data from local file
|
# Deserialize set data from local file
|
||||||
sets = pickle.load(open(config.cachepath + "sets", 'rb'))
|
sets = pickle.load(open(config.cache_path + "sets", 'rb'))
|
||||||
# Sort the loaded sets based on the sets name
|
# Sort the loaded sets based on the sets name
|
||||||
for set in sorted(sets, key=lambda x: x.name):
|
for set in sorted(sets, key=lambda x: x.name):
|
||||||
set_list.append(set)
|
set_list.append(set)
|
||||||
@@ -67,23 +63,23 @@ def load_mana_icons():
|
|||||||
|
|
||||||
|
|
||||||
def reload_image_cache():
|
def reload_image_cache():
|
||||||
if not os.path.exists(config.cachepath):
|
if not os.path.exists(config.image_cache_path):
|
||||||
os.makedirs(config.cachepath)
|
os.makedirs(config.image_cache_path)
|
||||||
|
|
||||||
# return array of images
|
# return array of images
|
||||||
imageslist = os.listdir(config.cachepath)
|
imageslist = os.listdir(config.image_cache_path)
|
||||||
imagecache.clear()
|
imagecache.clear()
|
||||||
for image in imageslist:
|
for image in imageslist:
|
||||||
try:
|
try:
|
||||||
img = PImage.open(config.cachepath + image)
|
img = PImage.open(config.image_cache_path + image)
|
||||||
imagecache.append(img)
|
imagecache.append(img)
|
||||||
except OSError as err:
|
except OSError as err:
|
||||||
print("Error loading image: " + str(err))
|
print("Error loading image: " + str(err))
|
||||||
|
|
||||||
|
|
||||||
def load_dummy_image(sizex, sizey):
|
def load_dummy_image(sizex, sizey):
|
||||||
return GdkPixbuf.Pixbuf.new_from_file_at_size(os.path.dirname(__file__) +
|
return GdkPixbuf.Pixbuf.new_from_file_at_size(os.path.dirname(__file__)
|
||||||
'/resources/images/dummy.jpg', sizex, sizey)
|
+ '/resources/images/dummy.jpg', sizex, sizey)
|
||||||
|
|
||||||
|
|
||||||
def load_card_image_online(card, sizex, sizey):
|
def load_card_image_online(card, sizex, sizey):
|
||||||
@@ -91,7 +87,7 @@ def load_card_image_online(card, sizex, sizey):
|
|||||||
if url is None:
|
if url is None:
|
||||||
print("No Image URL provided")
|
print("No Image URL provided")
|
||||||
return load_dummy_image(sizex, sizey)
|
return load_dummy_image(sizex, sizey)
|
||||||
filename = config.cachepath + card.multiverse_id.__str__() + ".PNG"
|
filename = config.image_cache_path + card.multiverse_id.__str__() + ".PNG"
|
||||||
print("Loading image for " + card.name + "from: " + url)
|
print("Loading image for " + card.name + "from: " + url)
|
||||||
response = request.urlretrieve(url, filename)
|
response = request.urlretrieve(url, filename)
|
||||||
return GdkPixbuf.Pixbuf.new_from_file_at_size(filename, sizex, sizey)
|
return GdkPixbuf.Pixbuf.new_from_file_at_size(filename, sizex, sizey)
|
||||||
@@ -128,8 +124,8 @@ def create_mana_icons(mana_string):
|
|||||||
image.paste(loaded, (xpos, 0))
|
image.paste(loaded, (xpos, 0))
|
||||||
poscounter += 1
|
poscounter += 1
|
||||||
|
|
||||||
image.save(config.cachepath + "manaicon.png", "PNG")
|
image.save(config.cache_path + "manaicon.png", "PNG")
|
||||||
pixbuf = GdkPixbuf.Pixbuf.new_from_file(config.cachepath + "manaicon.png")
|
pixbuf = GdkPixbuf.Pixbuf.new_from_file(config.cache_path + "manaicon.png")
|
||||||
pixbuf = pixbuf.scale_simple(image.width / 5, image.height / 5, GdkPixbuf.InterpType.HYPER)
|
pixbuf = pixbuf.scale_simple(image.width / 5, image.height / 5, GdkPixbuf.InterpType.HYPER)
|
||||||
os.remove(config.cachepath + "manaicon.png")
|
os.remove(config.cache_path + "manaicon.png")
|
||||||
return pixbuf
|
return pixbuf
|
||||||
|
|||||||
Reference in New Issue
Block a user