Add id to addons slug

This commit is contained in:
Pascal Vizeli 2017-04-27 23:09:52 +02:00
parent c6cc8adbb7
commit 645a8e2372
3 changed files with 41 additions and 6 deletions

View File

@ -6,6 +6,7 @@ import glob
import voluptuous as vol
from voluptuous.humanize import humanize_error
from .util import extract_hash_from_path
from .validate import validate_options, SCHEMA_ADDON_CONFIG
from ..const import (
FILE_HASSIO_ADDONS, ATTR_NAME, ATTR_VERSION, ATTR_SLUG, ATTR_DESCRIPTON,
@ -47,9 +48,9 @@ class AddonsData(Config):
self._current_data = {}
self._read_addons_folder(self.config.path_addons_repo)
self._read_addons_folder(self.config.path_addons_custom)
self._read_addons_folder(self.config.path_addons_custom, custom=True)
def _read_addons_folder(self, folder):
def _read_addons_folder(self, folder, custom=False):
"""Read data from addons folder."""
pattern = ADDONS_REPO_PATTERN.format(folder)
@ -58,9 +59,17 @@ class AddonsData(Config):
addon_config = read_json_file(addon)
addon_config = SCHEMA_ADDON_CONFIG(addon_config)
self._current_data[addon_config[ATTR_SLUG]] = addon_config
if custom:
addon_slug = "{}_{}".format(
extract_hash_from_path(folder, addon),
addon_config[ATTR_SLUG],
)
else:
addon_slug = addon_config[ATTR_SLUG]
except (OSError, KeyError):
self._current_data[addon_slug] = addon_config
except OSError:
_LOGGER.warning("Can't read %s", addon)
except vol.Invalid as ex:
@ -105,7 +114,7 @@ class AddonsData(Config):
data.append({
ATTR_NAME: values[ATTR_NAME],
ATTR_SLUG: values[ATTR_SLUG],
ATTR_SLUG: addon,
ATTR_DESCRIPTON: values[ATTR_DESCRIPTON],
ATTR_VERSION: values[ATTR_VERSION],
ATTR_INSTALLED: i_version,

View File

@ -1,8 +1,34 @@
"""Util addons functions."""
import hashlib
import pathlib
import re
import unicodedata
RE_SLUGIFY = re.compile(r'[^a-z0-9_]+')
def slugify(text):
"""Slugify a given text."""
text = unicodedata.normalize('NFKD', text)
text = text.lower()
text = text.replace(" ", "_")
text = RE_SLUGIFY.sub("", text)
return text
def get_hash_from_repository(repo):
"""Generate a hash from repository."""
key = repo.lower().encode()
return hashlib.sha1(key).hexdigest()
def extract_hash_from_path(base_path, options_path):
"""Extract repo id from path."""
base_dir = pathlib.PurePosixPath(base_path).parts[-1]
dirlist = iter(pathlib.PurePosixPath(options_path).parts)
for obj in dirlist:
if obj != base_dir:
continue
return slugify(next(dirlist))

View File

@ -59,7 +59,7 @@ class APISupervisor(object):
if ATTR_ADDONS_REPOSITORIES in body:
new = set(body[ATTR_ADDONS_REPOSITORIES])
old = set(self.config.addons_repositories.keys())
old = set(self.config.addons_repositories)
# add new repositories
for url in set(new - old):