[go: up one dir, main page]

Skip to content

Commit

Permalink
implement setting themes & user scripts, and other misc code rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
C2N14 committed Jan 5, 2019
1 parent f150c18 commit 47d4704
Show file tree
Hide file tree
Showing 19 changed files with 579 additions and 345 deletions.
25 changes: 17 additions & 8 deletions automathemely/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import os.path
_ROOT = os.path.abspath(os.path.dirname(__file__))
__version__ = 1.25

from pathlib import Path
from sys import stdout, stderr
import logging

_ROOT = str(Path(__file__).resolve().parent)
__version__ = 1.25

from automathemely.autoth_tools.utils import get_local, notify

# Create user config dir
if not os.path.isdir(get_local()):
os.makedirs(get_local())
# Move/rename older local config directory name to the new lowercase one
if Path.home().joinpath('.config', 'AutomaThemely').is_dir():
import shutil
shutil.move(str(Path.home().joinpath('.config', 'AutomaThemely')), get_local())

# Create user config dir if it doesn't exist already
Path(get_local()).mkdir(parents=True, exist_ok=True)


# Custom Handler to pass logs as notifications
Expand All @@ -19,8 +23,10 @@ def emit(self, record):
notify(message)


# noinspection SpellCheckingInspection
default_simple_format = '%(levelname)s: %(message)s'
timed_simple_format = '(%(asctime)s) %(levelname)s: %(message)s'
# noinspection SpellCheckingInspection
timed_details_format = '(%(asctime)s) (%(filename)s:%(funcName)s:%(lineno)s) %(levelname)s: %(message)s'

# Setup logging levels/handlers
main_file_handler = logging.FileHandler(get_local('automathemely.log'), mode='w')
Expand All @@ -32,10 +38,13 @@ def emit(self, record):
warning_or_higher_handler = logging.StreamHandler(stderr)
warning_or_higher_handler.setLevel(logging.WARNING)
# This will be added in run.py if notifications are enabled
# TODO: Figure out a better way to handle notifications that is as flexible as this that doesn't spam the user in case
# one of the imported libraries malfunctions and decide to also use this root logger
notifier_handler = NotifyHandler()
notifier_handler.setLevel(logging.INFO)

# Setup root logger
# noinspection SpellCheckingInspection
logging.basicConfig(
# filename=get_local('automathemely.log'),
# filemode='w',
Expand Down
9 changes: 0 additions & 9 deletions automathemely/autoth_tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +0,0 @@
import glob
from os.path import dirname, basename, isfile

modules = glob.glob(dirname(__file__)+"/*.py")
__all__ = [basename(f)[:-3] for f in modules if isfile(f) and not f.endswith('__init__.py')]

# Dynamically import all modules
# noinspection PyPep8
from . import *
44 changes: 8 additions & 36 deletions automathemely/autoth_tools/argmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json
import pickle as pkl

from automathemely.autoth_tools.utils import get_local
from automathemely.autoth_tools.utils import get_local, read_dict, write_dic

import logging
logger = logging.getLogger(__name__)
Expand All @@ -21,24 +21,6 @@
action='store_true', default=False)


# For --setting arg
def lookup_dic(d, k):
val = d
for key in k:
try:
val = val[key]
except KeyError:
return False
return True


# For --setting arg
def write_dic(dic, keys, value):
for key in keys[:-1]:
dic = dic.setdefault(key, {})
dic[keys[-1]] = value


# For --list arg
def print_list(d, indent=0):
for key, value in d.items():
Expand Down Expand Up @@ -95,7 +77,7 @@ def main(us_se):
else:
key_list = [to_set_key]

if lookup_dic(us_se, key_list):
if read_dict(us_se, key_list):
write_dic(us_se, key_list, to_set_val)

with open(get_local('user_settings.json'), 'w') as file:
Expand Down Expand Up @@ -131,19 +113,9 @@ def main(us_se):

# RESTART
elif args.restart:
logger.debug('Not implemented yet!')
return
# from automathemely.autoth_tools.utils import pgrep
# import subprocess
# import os
# if pgrep('autothscheduler.py', True):
# subprocess.Popen(['pkill', '-f', 'autothscheduler.py']).wait()
# try:
# err_out = open(get_local('automathemely.log'), 'w')
# except (FileNotFoundError, PermissionError):
# err_out = subprocess.DEVNULL
# subprocess.Popen(['/usr/bin/env', 'python3', get_bin('autothscheduler.py')],
# stdout=subprocess.DEVNULL,
# stderr=err_out,
# preexec_fn=os.setpgrp)
# return 'Restarted the scheduler', False
from automathemely.autoth_tools.utils import pgrep, get_bin
from subprocess import Popen, DEVNULL
if pgrep(['autothscheduler.py'], use_full=True):
Popen(['pkill', '-f', 'autothscheduler.py']).wait()
Popen(['python3', get_bin('autothscheduler.py')], start_new_session=True, stdout=DEVNULL, stderr=DEVNULL)
logger.info('Restarted the scheduler')
Loading

0 comments on commit 47d4704

Please sign in to comment.