added gui directory check and creation, created module for referencing gui directories

This commit is contained in:
bharnden 2019-11-01 16:31:40 -07:00
parent 0f78acaa0c
commit f10acbc8d9
6 changed files with 46 additions and 14 deletions

View file

@ -1,6 +1,7 @@
import logging import logging
import tkinter as tk import tkinter as tk
from coretk import appdirs
from coretk.coreclient import CoreClient from coretk.coreclient import CoreClient
from coretk.coremenubar import CoreMenubar from coretk.coremenubar import CoreMenubar
from coretk.coretoolbar import CoreToolbar from coretk.coretoolbar import CoreToolbar
@ -86,5 +87,6 @@ class Application(tk.Frame):
if __name__ == "__main__": if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
appdirs.check_directory()
app = Application() app = Application()
app.mainloop() app.mainloop()

39
coretk/coretk/appdirs.py Normal file
View file

@ -0,0 +1,39 @@
import logging
import shutil
from pathlib import Path
# gui home paths
HOME_PATH = Path.home().joinpath(".coretk")
BACKGROUNDS_PATH = HOME_PATH.joinpath("backgrounds")
CUSTOM_EMANE_PATH = HOME_PATH.joinpath("custom_emane")
CUSTOM_SERVICE_PATH = HOME_PATH.joinpath("custom_services")
ICONS_PATH = HOME_PATH.joinpath("icons")
MOBILITY_PATH = HOME_PATH.joinpath("mobility")
XML_PATH = HOME_PATH.joinpath("xml")
CONFIG_PATH = HOME_PATH.joinpath("gui.yaml")
# local paths
LOCAL_ICONS_PATH = Path(__file__).parent.joinpath("icons").absolute()
LOCAL_BACKGROUND_PATH = Path(__file__).parent.joinpath("backgrounds").absolute()
def check_directory():
if HOME_PATH.exists():
logging.info("~/.coretk exists")
return
logging.info("creating ~/.coretk")
HOME_PATH.mkdir()
BACKGROUNDS_PATH.mkdir()
CUSTOM_EMANE_PATH.mkdir()
CUSTOM_SERVICE_PATH.mkdir()
ICONS_PATH.mkdir()
MOBILITY_PATH.mkdir()
XML_PATH.mkdir()
for image in LOCAL_ICONS_PATH.glob("*"):
new_image = ICONS_PATH.joinpath(image.name)
shutil.copy(image, new_image)
for background in LOCAL_BACKGROUND_PATH.glob("*"):
new_background = BACKGROUNDS_PATH.joinpath(background.name)
shutil.copy(background, new_background)
with CONFIG_PATH.open("w") as f:
f.write("# gui config")

View file

Before

Width:  |  Height:  |  Size: 312 KiB

After

Width:  |  Height:  |  Size: 312 KiB

View file

Before

Width:  |  Height:  |  Size: 196 KiB

After

Width:  |  Height:  |  Size: 196 KiB

View file

@ -1,13 +1,10 @@
import logging import logging
import os
from enum import Enum from enum import Enum
from PIL import Image, ImageTk from PIL import Image, ImageTk
from core.api.grpc import core_pb2 from core.api.grpc import core_pb2
from coretk.appdirs import LOCAL_ICONS_PATH
PATH = os.path.abspath(os.path.dirname(__file__))
ICONS_DIR = os.path.join(PATH, "icons")
class Images: class Images:
@ -15,14 +12,11 @@ class Images:
@classmethod @classmethod
def load_all(cls): def load_all(cls):
for file_name in os.listdir(ICONS_DIR): for image in LOCAL_ICONS_PATH.glob("*"):
file_path = os.path.join(ICONS_DIR, file_name) cls.load(image.stem, str(image))
name = file_name.split(".")[0]
cls.load(name, file_path)
@classmethod @classmethod
def load(cls, name, file_path): def load(cls, name, file_path):
# file_path = os.path.join(PATH, file_path)
image = Image.open(file_path) image = Image.open(file_path)
tk_image = ImageTk.PhotoImage(image) tk_image = ImageTk.PhotoImage(image)
cls.images[name] = tk_image cls.images[name] = tk_image

View file

@ -3,14 +3,12 @@ set wallpaper
""" """
import enum import enum
import logging import logging
import os
import tkinter as tk import tkinter as tk
from tkinter import filedialog from tkinter import filedialog
from PIL import Image, ImageTk from PIL import Image, ImageTk
PATH = os.path.abspath(os.path.dirname(__file__)) from coretk.appdirs import BACKGROUNDS_PATH
WALLPAPER_DIR = os.path.join(PATH, "wallpaper")
class ScaleOption(enum.Enum): class ScaleOption(enum.Enum):
@ -60,7 +58,7 @@ class CanvasWallpaper:
def open_image_link(self): def open_image_link(self):
filename = filedialog.askopenfilename( filename = filedialog.askopenfilename(
initialdir=WALLPAPER_DIR, initialdir=str(BACKGROUNDS_PATH),
title="Open", title="Open",
filetypes=( filetypes=(
("images", "*.gif *.jpg *.png *.bmp *pcx *.tga ..."), ("images", "*.gif *.jpg *.png *.bmp *pcx *.tga ..."),
@ -207,7 +205,6 @@ class CanvasWallpaper:
cropy = img_h = tk_img.height() cropy = img_h = tk_img.height()
if img_w > canvas_w: if img_w > canvas_w:
cropx -= img_w - canvas_w cropx -= img_w - canvas_w
if img_h > canvas_h: if img_h > canvas_h:
cropy -= img_h - canvas_h cropy -= img_h - canvas_h