Skip to content

Commit

Permalink
feat: support matching wildcard slugs for include and exclude
Browse files Browse the repository at this point in the history
You can use operators like '*' to select groups of addons such as 'core_*' to include all core addons.

closes: #66
  • Loading branch information
jcwillox committed Aug 9, 2022
1 parent ba1668a commit f684b66
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions custom_components/auto_backup/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Component to create and automatically remove Home Assistant backups."""
import logging
from datetime import datetime, timedelta, timezone
from fnmatch import fnmatchcase
from os import getenv
from os.path import join, isfile
from typing import List, Dict, Tuple, Optional
Expand Down Expand Up @@ -244,27 +245,29 @@ def ensure_slugs(cls, inclusion, installed_addons) -> Tuple[List, List]:
addons = inclusion[ATTR_ADDONS]
folders = inclusion[ATTR_FOLDERS]
return (
cls.ensure_addon_slugs(addons, installed_addons),
list(cls.ensure_addon_slugs(addons, installed_addons)),
cls.ensure_folder_slugs(folders),
)

@staticmethod
def ensure_addon_slugs(addons, installed_addons) -> List[str]:
"""Replace addon names with their appropriate slugs."""
"""Expand wildcards and replace addon names with their appropriate slugs."""
if not addons:
return []

def match_addon(addon):
for addon in addons:
matched = False
for installed_addon in installed_addons:
# perform case-insensitive match.
if addon.casefold() == installed_addon["name"].casefold():
return installed_addon["slug"]
if addon == installed_addon["slug"]:
return addon
_LOGGER.warning("Addon '%s' does not exist", addon)
return addon

return [match_addon(addon) for addon in addons]
yield installed_addon["slug"]
matched = True
if fnmatchcase(installed_addon["slug"], addon):
yield installed_addon["slug"]
matched = True
if not matched:
_LOGGER.warning("Addon '%s' does not exist", addon)
yield addon

@staticmethod
def ensure_folder_slugs(folders) -> List[str]:
Expand Down Expand Up @@ -332,6 +335,8 @@ async def async_create_backup(self, data: Dict):
if include:
addons, folders = self.ensure_slugs(include, installed_addons)

_LOGGER.debug("Including; addons: %s, folders: %s", addons, folders)

if exclude:
excluded_addons, excluded_folders = self.ensure_slugs(
exclude, installed_addons
Expand All @@ -345,6 +350,15 @@ async def async_create_backup(self, data: Dict):
folder for folder in folders if folder not in excluded_folders
]

_LOGGER.debug(
"Excluding; addons: %s, folders: %s",
excluded_addons,
excluded_folders,
)
_LOGGER.debug(
"Including (excluded); addons: %s, folders: %s", addons, folders
)

data[ATTR_ADDONS] = addons
data[ATTR_FOLDERS] = folders
await self._async_create_backup(data, partial=True)
Expand Down

0 comments on commit f684b66

Please sign in to comment.