Improve the backup logic and add log file
parent
fc374eab84
commit
c647d6f347
|
@ -68,7 +68,9 @@ Page {
|
||||||
anchors.topMargin: 10
|
anchors.topMargin: 10
|
||||||
text: "Save"
|
text: "Save"
|
||||||
onClicked: {
|
onClicked: {
|
||||||
iconEditor.attrs.old_icon = iconEditor.attrs.Icon.toString()
|
if(iconEditor.attrs.old_icon === undefined) {
|
||||||
|
iconEditor.attrs.old_icon = iconEditor.attrs.Icon.toString()
|
||||||
|
}
|
||||||
iconEditor.attrs.Icon = icon.source.toString()
|
iconEditor.attrs.Icon = icon.source.toString()
|
||||||
py.importModule("main", function(){
|
py.importModule("main", function(){
|
||||||
py.call("main.save_icon", [iconEditor.attrs], function(result){
|
py.call("main.save_icon", [iconEditor.attrs], function(result){
|
||||||
|
|
43
src/main.py
43
src/main.py
|
@ -1,13 +1,29 @@
|
||||||
import pyotherside
|
import pyotherside
|
||||||
from PIL import Image, ImageOps, ImageDraw, ImageChops
|
from PIL import Image, ImageDraw, ImageChops
|
||||||
|
from PIL.Image import Image as ImageType
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from glob import glob
|
from glob import glob
|
||||||
from collections import Counter
|
from collections import Counter
|
||||||
|
import requests
|
||||||
import base64
|
import base64
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
|
import logging
|
||||||
|
|
||||||
def base64_to_img(base64_bytes: bytes) -> Image:
|
logging_filename = os.path.join(os.getenv("HOME"),
|
||||||
|
".local",
|
||||||
|
"share",
|
||||||
|
"org.git.kirbylife.harbour-muchkin",
|
||||||
|
"muchkin.log")
|
||||||
|
logging.basicConfig(filename=logging_filename, filemode="w", level=logging.DEBUG)
|
||||||
|
|
||||||
|
def download_icon(url) -> ImageType:
|
||||||
|
response = requests.get(url)
|
||||||
|
img = Image.open(BytesIO(response.content))
|
||||||
|
img = img.convert("RGBA")
|
||||||
|
return img
|
||||||
|
|
||||||
|
def base64_to_img(base64_bytes: bytes) -> ImageType:
|
||||||
img_bytes = base64.b64decode(base64_bytes)
|
img_bytes = base64.b64decode(base64_bytes)
|
||||||
img_buffer = BytesIO(img_bytes)
|
img_buffer = BytesIO(img_bytes)
|
||||||
img = Image.open(img_buffer)
|
img = Image.open(img_buffer)
|
||||||
|
@ -24,7 +40,7 @@ def avg_colors(img):
|
||||||
counter[pixel] += 1
|
counter[pixel] += 1
|
||||||
return counter.most_common(1)[0][0]
|
return counter.most_common(1)[0][0]
|
||||||
|
|
||||||
def add_background(img: Image, bg_tuple: tuple) -> Image:
|
def add_background(img: ImageType, bg_tuple: tuple) -> ImageType:
|
||||||
bg = Image.new("RGBA", img.size, bg_tuple)
|
bg = Image.new("RGBA", img.size, bg_tuple)
|
||||||
bg.paste(img, (0, 0), img)
|
bg.paste(img, (0, 0), img)
|
||||||
return bg
|
return bg
|
||||||
|
@ -80,8 +96,18 @@ def get_web_icons():
|
||||||
return list(map(parse_file, apps))
|
return list(map(parse_file, apps))
|
||||||
|
|
||||||
def sailify(raw_str, pattern):
|
def sailify(raw_str, pattern):
|
||||||
base64_bytes = raw_str.replace("data:image/png;base64,", "", 1).encode()
|
try:
|
||||||
img = base64_to_img(base64_bytes)
|
# New versions of Sailfish has the url on the icons
|
||||||
|
img = download_icon(raw_str)
|
||||||
|
logging.info("Downloaded from url: " + raw_str)
|
||||||
|
except requests.exceptions.InvalidSchema:
|
||||||
|
# The old ones converted the icon to base64
|
||||||
|
base64_bytes = raw_str.replace("data:image/png;base64,", "", 1).encode()
|
||||||
|
img = base64_to_img(base64_bytes)
|
||||||
|
logging.info("Converted from base64")
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(e)
|
||||||
|
raise e
|
||||||
bg_tuple = avg_colors(img)
|
bg_tuple = avg_colors(img)
|
||||||
img = add_background(img, bg_tuple)
|
img = add_background(img, bg_tuple)
|
||||||
img = crop_to_circle(img, pattern)
|
img = crop_to_circle(img, pattern)
|
||||||
|
@ -93,12 +119,17 @@ def sailify(raw_str, pattern):
|
||||||
|
|
||||||
def backup_icon(app):
|
def backup_icon(app):
|
||||||
if not os.path.exists(app["path"] + "backup"):
|
if not os.path.exists(app["path"] + "backup"):
|
||||||
shutil.copyfile(app["path"], app["path"] + "_backup")
|
backup_path = app["path"] + "_backup"
|
||||||
|
shutil.copyfile(app["path"], backup_path)
|
||||||
|
logging.info("Icon backed up on: " + backup_path)
|
||||||
|
else:
|
||||||
|
logging.info("Backup already exists")
|
||||||
|
|
||||||
def save_icon(app):
|
def save_icon(app):
|
||||||
new_content = deparse_file(app)
|
new_content = deparse_file(app)
|
||||||
with open(app["path"], "w") as f:
|
with open(app["path"], "w") as f:
|
||||||
f.write(new_content)
|
f.write(new_content)
|
||||||
|
logging.info("Icon saved on: " + app["path"])
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
Loading…
Reference in New Issue