Skip to content

Commit

Permalink
Remove logging when --log is not passed
Browse files Browse the repository at this point in the history
  • Loading branch information
orbi-tal committed Nov 26, 2024
1 parent e4a974b commit a3c0804
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 123 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ wheels/
*.egg
tests/
pytest.ini
profile.*

# Virtual Environment
venv/
Expand Down
2 changes: 1 addition & 1 deletion build.spec
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ exe = EXE(
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=False,
console=False, # Console disabled
disable_windowed_traceback=False,
target_arch=None,
codesign_identity=None,
Expand Down
166 changes: 44 additions & 122 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
import importlib.util
import json
import logging
from logging.handlers import RotatingFileHandler
import os
import signal
import sys
import threading
import time

import pystray
from PIL import Image, ImageDraw
from PIL import Image, ImageDraw, PngImagePlugin, IcoImagePlugin
from pystray import MenuItem as item

APP_NAME = "Glaze Autotiler"
Expand All @@ -27,11 +28,7 @@ class AutoTiler:
"""Main class for handling window tiling automation in Glaze WM."""

def __init__(self, log_enabled=False):
"""Initialize the AutoTiler.
Args:
log_enabled (bool): Whether to enable detailed logging
"""
"""Initialize the AutoTiler."""
user_profile = os.getenv("USERPROFILE")
if user_profile is None:
raise ValueError("USERPROFILE environment variable not found")
Expand Down Expand Up @@ -59,7 +56,7 @@ def __init__(self, log_enabled=False):
self.app_dir = os.path.dirname(os.path.abspath(__file__))
self.res_dir = os.path.join(self.app_dir, "res")
self.icon_path = os.path.join(self.res_dir, "icon.png")
self.icon = None # Add this to store the icon reference
self.icon = None
self.app_name = APP_NAME
self.version = APP_VERSION

Expand Down Expand Up @@ -167,26 +164,33 @@ def pre_package_default_scripts(self):

def setup_logging(self, log_enabled=False):
"""Configure logging settings."""
current_time = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
log_filename = f"autotiler_{current_time}.log"
log_file = os.path.join(self.log_dir, log_filename)
log_level = logging.DEBUG if log_enabled else logging.INFO

logging.basicConfig(
filename=log_file,
level=log_level,
format="%(asctime)s - %(levelname)s - %(message)s",
force=True,
)
# Create a logger
logger = logging.getLogger()
logger.setLevel(log_level)

if log_enabled:
console_handler = logging.StreamHandler()
# Create console handler and set level to debug
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(log_level)
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
console_handler.setFormatter(formatter)
logging.getLogger().addHandler(console_handler)
console_formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
console_handler.setFormatter(console_formatter)
logger.addHandler(console_handler)

current_time = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
log_filename = f"autotiler_{current_time}.log"
log_file = os.path.join(self.log_dir, log_filename)

logging.info("Log file created: %s", log_filename)
file_handler = RotatingFileHandler(
log_file, maxBytes=10*1024*1024, backupCount=5
)
file_handler.setLevel(log_level)
file_formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
file_handler.setFormatter(file_formatter)
logger.addHandler(file_handler)

logging.info("Log file created: %s", log_filename)

def load_layouts(self):
"""Load available layout scripts from configured paths."""
Expand All @@ -202,13 +206,7 @@ def load_layouts(self):
logging.info("Available layouts: %s", list(self.layouts.keys()))

def _load_single_layout(self, layout_name, layout_info, script_paths):
"""Load a single layout script.
Args:
layout_name (str): Name of the layout
layout_info (dict): Layout configuration
script_paths (list): Paths to search for scripts
"""
"""Load a single layout script."""
if not layout_info.get("enabled", True):
logging.debug("Skipping disabled layout: %s", layout_name)
return
Expand All @@ -228,15 +226,7 @@ def _load_single_layout(self, layout_name, layout_info, script_paths):
logging.error("Error loading layout %s from %s: %s", layout_name, script_path, e)

def _find_script_path(self, layout_name, script_paths):
"""Find the script path for a layout.
Args:
layout_name (str): Name of the layout
script_paths (list): Paths to search for scripts
Returns:
str: Full path to script if found, None otherwise
"""
"""Find the script path for a layout."""
script_filename = f"{layout_name}.py"
for path in script_paths:
if not os.path.exists(path):
Expand All @@ -247,15 +237,7 @@ def _find_script_path(self, layout_name, script_paths):
return None

def _load_layout_module(self, layout_name, script_path):
"""Load a Python module from a script path.
Args:
layout_name (str): Name of the layout
script_path (str): Path to the script file
Returns:
module: Loaded Python module or None if failed
"""
"""Load a Python module from a script path."""
spec = importlib.util.spec_from_file_location(layout_name, script_path)
if spec is None:
logging.error("Could not create spec for %s", layout_name)
Expand All @@ -270,14 +252,7 @@ def _load_layout_module(self, layout_name, script_path):
return module

def _register_layout(self, layout_name, layout_info, module, script_path):
"""Register a layout in the layouts dictionary.
Args:
layout_name (str): Name of the layout
layout_info (dict): Layout configuration
module: Loaded Python module
script_path (str): Path to the script file
"""
"""Register a layout in the layouts dictionary."""
self.layouts[layout_name] = {
"module": module,
"path": script_path,
Expand All @@ -287,11 +262,7 @@ def _register_layout(self, layout_name, layout_info, module, script_path):
logging.info("Successfully loaded layout: %s from %s", layout_name, script_path)

def get_script_paths(self):
"""Get list of paths to search for layout scripts.
Returns:
list: List of directory paths containing layout scripts
"""
"""Get list of paths to search for layout scripts."""
if os.path.exists(self.config_file):
with open(self.config_file, "r") as file:
config = json.load(file)
Expand Down Expand Up @@ -335,14 +306,7 @@ def _merge_configs(self, current, default):
return current

def _validate_default_layout(self, config):
"""Validate and return default layout.
Args:
config (dict): Configuration dictionary
Returns:
str: Name of the validated default layout
"""
"""Validate and return default layout."""
if config.get("default_layout", "dwindle") not in self.layouts:
logging.warning(
f"Default layout {config.get('default_layout')} not found. "
Expand All @@ -352,20 +316,14 @@ def _validate_default_layout(self, config):
return config.get("default_layout", "dwindle")

def get_layout_config(self):
"""Get layout-specific configuration from config file and creates default config if it doesn't exist.
Returns:
dict: Layout configuration dictionary
"""
# Default layout config
"""Get layout-specific configuration from config file and creates default config if it doesn't exist."""
default_config = {
"layouts": {
"dwindle": {"display_name": "Dwindle Layout", "enabled": True},
"master_stack": {"display_name": "Master Stack", "enabled": True},
}
}

# Create config file if it doesn't exist
if not os.path.exists(self.config_file):
try:
with open(self.config_file, "w") as file:
Expand All @@ -375,7 +333,6 @@ def get_layout_config(self):
logging.error(f"Error creating config file: {e}")
return default_config["layouts"]

# Read existing config
try:
with open(self.config_file, "r") as file:
config = json.load(file)
Expand All @@ -385,11 +342,7 @@ def get_layout_config(self):
return default_config["layouts"]

def update_config(self, new_layout):
"""Update configuration with new default layout.
Args:
new_layout (str): Name of the new default layout
"""
"""Update configuration with new default layout."""
config_data = {}
if os.path.exists(self.config_file):
with open(self.config_file, "r") as file:
Expand All @@ -400,14 +353,7 @@ def update_config(self, new_layout):
logging.info(f"Config updated with new layout: {new_layout}")

def start_layout(self, layout_name):
"""Start a specified layout.
Args:
layout_name (str): Name of the layout to start
Returns:
bool: True if layout started successfully, False otherwise
"""
"""Start a specified layout."""
if layout_name not in self.layouts:
logging.error(f"Layout {layout_name} not found")
return False
Expand Down Expand Up @@ -452,18 +398,16 @@ def check_config_changes(self):
if current_mtime > self.config_last_modified:
logging.info("Config file changed, reloading...")

# Validate JSON before proceeding
try:
with open(self.config_file, "r") as f:
json.load(f) # Test if JSON is valid
json.load(f)

self.config_last_modified = current_mtime
self.load_layouts()
self.default_layout = self.load_config()
self.refresh_menu()
except json.JSONDecodeError as e:
logging.error(f"Invalid JSON in config file: {e}")
# Don't update last_modified time so we'll retry on next check

except Exception as e:
logging.error(f"Error checking config changes: {e}")
Expand All @@ -475,7 +419,6 @@ def stop_script(self):
self.cancel_event.set()
if not self.running_task.done():
try:
# Cancel the future directly
self.loop.call_soon_threadsafe(self.running_task.cancel)
except Exception as e:
logging.error(f"Error cancelling task: {e}")
Expand All @@ -501,11 +444,7 @@ def refresh_menu(self):
logging.error("Error refreshing menu: %s", e)

def _create_layout_menu_items(self):
"""Create menu items for layouts.
Returns:
list: List of layout menu items
"""
"""Create menu items for layouts."""
menu_items = []
try:
layout_config = self.get_layout_config()
Expand All @@ -521,16 +460,7 @@ def _create_layout_menu_items(self):
return menu_items

def _create_single_layout_item(self, layout_name, layout_info, layout_config):
"""Create a menu item for a single layout.
Args:
layout_name (str): Name of the layout
layout_info (dict): Layout information
layout_config (dict): Layout configuration
Returns:
MenuItem: Created menu item or None if layout should be skipped
"""
"""Create a menu item for a single layout."""
try:
layout_config_info = layout_config.get(layout_name, {})
if not layout_config_info.get("enabled", True):
Expand All @@ -555,16 +485,12 @@ def make_callback(name):
return None

def _create_control_menu_items(self):
"""Create control menu items (Stop, Refresh, Quit).
Returns:
list: List of control menu items
"""
return [
item("Stop Script", self.stop_script),
item("Refresh", self.refresh_menu),
item("Quit", lambda: self.quit_app(self.icon)),
]
"""Create control menu items (Stop, Refresh, Quit)."""
return [
item("Stop Script", self.stop_script),
item("Refresh", self.refresh_menu),
item("Quit", lambda: self.quit_app(self.icon)),
]

def create_icon(self):
"""Create and configure the system tray icon."""
Expand Down Expand Up @@ -610,11 +536,7 @@ def monitor_config(self):
logging.error(f"Error in config monitor: {e}")

def quit_app(self, icon):
"""Quit the application cleanly.
Args:
icon (pystray.Icon): System tray icon to remove
"""
"""Quit the application cleanly."""
logging.info("Quitting the application...")
self.stop_script()
icon.stop()
Expand Down

0 comments on commit a3c0804

Please sign in to comment.