craftbeerpi4-pione/cbpi/cli.py

100 lines
3 KiB
Python
Raw Normal View History

2019-01-21 22:33:29 +01:00
import argparse
2019-01-05 20:43:48 +01:00
import logging
2019-07-27 21:08:19 +02:00
import requests
import yaml
2019-01-05 20:43:48 +01:00
from cbpi.craftbeerpi import CraftBeerPi
import os
import pathlib
import shutil
def create_plugin_file():
import os.path
if os.path.exists(os.path.join(".", 'config', "plugin_list.txt")) is False:
srcfile = os.path.join(os.path.dirname(__file__), "config", "plugin_list.txt")
destfile = os.path.join(".", 'config')
shutil.copy(srcfile, destfile)
2019-07-27 21:08:19 +02:00
print("Plugin Folder created")
2019-01-05 20:43:48 +01:00
def create_config_file():
import os.path
if os.path.exists(os.path.join(".", 'config', "config.yaml")) is False:
srcfile = os.path.join(os.path.dirname(__file__), "config", "config.yaml")
destfile = os.path.join(".", 'config')
shutil.copy(srcfile, destfile)
2019-07-27 21:08:19 +02:00
print("Config Folder created")
2019-01-05 20:43:48 +01:00
def create_home_folder_structure():
pathlib.Path(os.path.join(".", 'logs/sensors')).mkdir(parents=True, exist_ok=True)
pathlib.Path(os.path.join(".", 'config')).mkdir(parents=True, exist_ok=True)
2019-07-27 21:08:19 +02:00
print("Log Folder created")
2019-01-05 20:43:48 +01:00
2019-01-21 22:33:29 +01:00
def copy_splash():
srcfile = os.path.join(os.path.dirname(__file__), "config", "splash.png")
destfile = os.path.join(".", 'config')
shutil.copy(srcfile, destfile)
2019-07-27 21:08:19 +02:00
print("Splash Srceen created")
def check_for_setup():
if os.path.exists(os.path.join(".", "config", "config.yaml")) is False:
print("***************************************************")
print("CraftBeerPi Config File not found: %s" % os.path.join(".", "config", "config.yaml"))
print("Please run 'cbpi setup' before starting the server ")
print("***************************************************")
return False
else:
return True
def list_plugins():
print("***************************************************")
print("CraftBeerPi 4.x Plugin List")
print("***************************************************")
plugins_yaml = "https://raw.githubusercontent.com/Manuel83/craftbeerpi-plugins/master/plugins_v4.yaml"
r = requests.get(plugins_yaml)
data = yaml.load(r.content, Loader=yaml.FullLoader)
for name, value in data.items():
print(name)
2019-01-05 20:43:48 +01:00
def main():
2019-01-21 22:33:29 +01:00
parser = argparse.ArgumentParser(description='Welcome to CraftBeerPi 4')
2019-07-27 21:08:19 +02:00
parser.add_argument("action", type=str, help="start,stop,restart,setup,plugins")
2019-01-05 22:44:54 +01:00
2019-01-21 22:33:29 +01:00
args = parser.parse_args()
2019-01-05 20:43:48 +01:00
#logging.basicConfig(level=logging.INFO, filename='./logs/app.log', filemode='a', format='%(asctime)s - %(levelname)s - %(name)s - %(message)s')
2019-01-05 22:44:54 +01:00
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(name)s - %(message)s')
2019-01-05 20:43:48 +01:00
2019-01-21 22:33:29 +01:00
if args.action == "setup":
2019-07-27 21:08:19 +02:00
print("Setting up CBPi")
2019-01-21 22:33:29 +01:00
create_home_folder_structure()
create_plugin_file()
create_config_file()
copy_splash()
2019-07-27 21:08:19 +02:00
return
if args.action == "plugins":
list_plugins()
return
2019-01-21 22:33:29 +01:00
if args.action == "start":
2019-07-27 21:08:19 +02:00
if check_for_setup() is False:
return
2019-01-21 22:33:29 +01:00
cbpi = CraftBeerPi()
cbpi.start()
2019-07-27 21:08:19 +02:00
return
2019-01-21 22:33:29 +01:00
parser.print_help()
2019-01-05 20:43:48 +01:00