Skip to content

Commit

Permalink
Merge pull request #1726 from AppDaemon/create_app_toml
Browse files Browse the repository at this point in the history
App management TOML
  • Loading branch information
acockburn committed Mar 25, 2023
2 parents 134fc04 + 1b447db commit cca4311
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 20 deletions.
26 changes: 16 additions & 10 deletions appdaemon/app_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,8 @@ async def read_config(self): # noqa: C901
app_valid = True
if app == "global_modules":
self.logger.warning(
"global_modules directive has been deprecated and will be removed in a future release"
"global_modules directive has been deprecated and will be removed"
" in a future release"
)
#
# Check the parameter format for string or list
Expand All @@ -425,7 +426,10 @@ async def read_config(self): # noqa: C901
else:
if self.AD.invalid_config_warnings:
self.logger.warning(
"global_modules should be a list or a string in File '%s' - ignoring",
(
"global_modules should be a list or a string in File"
" '%s' - ignoring"
),
file,
)
elif app == "sequence":
Expand Down Expand Up @@ -688,7 +692,9 @@ async def check_config(self, silent=False, add_threads=True): # noqa: C901

# if silent is False:
await self.set_state(
self.total_apps_sensor, state=active_apps + inactive, attributes={"friendly_name": "Total Apps"}
self.total_apps_sensor,
state=active_apps + inactive,
attributes={"friendly_name": "Total Apps"},
)

self.logger.info("Found %s active apps", active_apps)
Expand Down Expand Up @@ -1207,7 +1213,7 @@ def get_app_dependencies(self, app):
return deps

def create_app(self, app=None, **kwargs): # @next-release create_app()
"""Used to create an app, which is written to a Yaml file"""
"""Used to create an app, which is written to a config file"""

executed = True
app_file = kwargs.pop("app_file", None)
Expand Down Expand Up @@ -1245,12 +1251,12 @@ def create_app(self, app=None, **kwargs): # @next-release create_app()
app_directory = os.path.join(self.AD.app_dir, app_directory)

if app_file is None:
app_file = os.path.join(app_directory, f"{app}.yaml")
app_file = os.path.join(app_directory, f"{app}{self.ext}")
self.logger.info("Creating app using filename %s", app_file)

else:
if app_file[-5:] != ".yaml":
app_file = f"{app_file}.yaml"
if app_file[-5:] != self.ext:
app_file = f"{app_file}{self.ext}"

app_file = os.path.join(app_directory, app_file)

Expand Down Expand Up @@ -1278,7 +1284,7 @@ def create_app(self, app=None, **kwargs): # @next-release create_app()

# at this point now to create write to file
try:
utils.write_to_file(app_file, **new_config)
utils.write_config_file(app_file, **new_config)

data = {
"event_type": "app_created",
Expand Down Expand Up @@ -1320,7 +1326,7 @@ def edit_app(self, app, **kwargs): # @next-release edit_app()

# now update the file with the new data
try:
utils.write_to_file(app_file, **new_config)
utils.write_config_file(app_file, **new_config)

data = {
"event_type": "app_edited",
Expand Down Expand Up @@ -1361,7 +1367,7 @@ def remove_app(self, app, **kwargs): # @next-release remove_app()
os.remove(app_file)

else:
utils.write_to_file(app_file, **file_config)
utils.write_config_file(app_file, **file_config)

data = {
"event_type": "app_removed",
Expand Down
28 changes: 21 additions & 7 deletions appdaemon/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from collections.abc import Iterable
import concurrent.futures
import tomli
import tomli_w
import re
from typing import Callable

Expand Down Expand Up @@ -279,13 +280,6 @@ def get_kwargs(kwargs):
return result


def write_to_file(yaml_file, **kwargs):
"""Used to write the app to Yaml file"""

with open(yaml_file, "w") as stream:
yaml.dump(kwargs, stream, Dumper=yaml.SafeDumper)


def rreplace(s, old, new, occurrence):
li = s.rsplit(old, occurrence)
return new.join(li)
Expand Down Expand Up @@ -566,6 +560,26 @@ def get_object_size(obj, seen=None):
return size


def write_config_file(path, **kwargs):
extension = os.path.splitext(path)[1]
if extension == ".yaml":
write_yaml_config(path, **kwargs)
elif extension == ".toml":
write_toml_config(path, **kwargs)
else:
raise ValueError(f"ERROR: unknown file extension: {extension}")


def write_yaml_config(path, **kwargs):
with open(path, "w") as stream:
yaml.dump(kwargs, stream, Dumper=yaml.SafeDumper)


def write_toml_config(path, **kwargs):
with open(path, "wb") as stream:
tomli_w.dump(kwargs, stream)


def read_config_file(path):
extension = os.path.splitext(path)[1]
if extension == ".yaml":
Expand Down
6 changes: 4 additions & 2 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ distlib==0.3.6
# via virtualenv
feedparser==6.0.10
# via appdaemon (pyproject.toml)
filelock==3.10.3
filelock==3.10.4
# via virtualenv
flake8==6.0.0
# via appdaemon (pyproject.toml)
frozenlist==1.3.3
# via
# aiohttp
# aiosignal
identify==2.5.21
identify==2.5.22
# via pre-commit
idna==3.4
# via
Expand Down Expand Up @@ -136,6 +136,8 @@ sockjs==0.11.0
# via appdaemon (pyproject.toml)
tomli==2.0.1
# via appdaemon (pyproject.toml)
tomli-w==1.0.0
# via appdaemon (pyproject.toml)
urllib3==1.26.15
# via requests
uvloop==0.17.0 ; sys_platform != "win32"
Expand Down
2 changes: 2 additions & 0 deletions doc-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ sphinxcontrib-serializinghtml==1.1.5
# via sphinx
tomli==2.0.1
# via appdaemon (pyproject.toml)
tomli-w==1.0.0
# via appdaemon (pyproject.toml)
tornado==6.2
# via livereload
urllib3==1.26.15
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ dependencies = [
'sockjs ~= 0.11.0',
'uvloop==0.17.0; sys_platform != "win32"',
'websocket-client ~= 1.5.1',
'tomli ~= 2.0.1'
'tomli ~= 2.0.1',
'tomli_w ~= 1.0.0'
]

classifiers= [
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ sockjs==0.11.0
# via appdaemon (pyproject.toml)
tomli==2.0.1
# via appdaemon (pyproject.toml)
tomli-w==1.0.0
# via appdaemon (pyproject.toml)
urllib3==1.26.15
# via requests
uvloop==0.17.0 ; sys_platform != "win32"
Expand Down

0 comments on commit cca4311

Please sign in to comment.