Skip to content

Commit

Permalink
copy config and directory and load dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
bitSheriff committed Sep 26, 2023
1 parent f657788 commit fe1960e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
27 changes: 24 additions & 3 deletions shelfslide.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import src.book as Book
import src.display as Display
import src.slideshow as Slideshow
import src.config as Config

SLIDESHOW_MIN_SLEEP = 300 # min 5min sleep

Expand Down Expand Up @@ -93,6 +94,9 @@ def config_args():
parser.add_argument('--show', '-s', default="", help='Show given picture')
parser.add_argument('--logo', '-l', action='store_true', help='Just show the logo and exit')

parser.add_argument('--copy-conf', action='store_true', help='Copy the configuration and books to the user config directory')



# return the parsed arguments
return parser.parse_args()
Expand Down Expand Up @@ -167,6 +171,17 @@ def logging_config(verbose):
# set the logging to a file
logging.basicConfig(filename='shelfslide.log', filemode='w', format='%(name)s - %(levelname)s - %(message)s')

def load_config_file():

# check if the file exists in the user config directory
if os.path.exists(os.path.expanduser('~') + '/.config/shelfslide/config.yaml'):
with open(os.path.expanduser('~') + '/.config/shelfslide/config.yaml', 'r') as file:
config_file = yaml.safe_load(file)
else:
# load the config file from the application directory
with open('config.yaml', 'r') as file:
config_file = yaml.safe_load(file)
return config_file

##
# @brief Main function
Expand All @@ -178,9 +193,15 @@ def main():
# configure the logging
logging_config(parser.verbose)

# load the config file
with open('config.yaml', 'r') as file:
config_file = yaml.safe_load(file)
# check if the copy-conf flag is set
if parser.copy_conf:
# copy the configuration file and the books directory to the user's home directory
Config.copy_conf()
Config.copy_books_dir()
sys.exit(0)

# load the configuration file
config_file = load_config_file()

# configure the display
display = Display.display( config_file['display']['type'],
Expand Down
39 changes: 39 additions & 0 deletions src/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@


def copy_conf():
"""
Copy the configuration file to the user's home directory.
"""
import os
import shutil
from . import __file__ as root

root = os.path.dirname(root)
conf = os.path.join(root, 'config.yaml')
home = os.path.expanduser('~')
dest = os.path.join(home, '.config', 'shelfslide', 'config.yaml')

if not os.path.exists(os.path.dirname(dest)):
os.makedirs(os.path.dirname(dest))

shutil.copy(conf, dest)
print("Configuration file copied to {}".format(dest))

def copy_books_dir():
"""
Copy the books directory to the user's home directory.
"""
import os
import shutil
from . import __file__ as root

root = os.path.dirname(root)
books = os.path.join(root, 'books')
home = os.path.expanduser('~')
dest = os.path.join(home, '.config', 'shelfslide', 'books')

if not os.path.exists(os.path.dirname(dest)):
os.makedirs(os.path.dirname(dest))

shutil.copytree(books, dest)
print("Books directory copied to {}".format(dest))

0 comments on commit fe1960e

Please sign in to comment.