From 645a8e2372660ff801db7f96dc2cb33a904df382 Mon Sep 17 00:00:00 2001 From: Pascal Vizeli Date: Thu, 27 Apr 2017 23:09:52 +0200 Subject: [PATCH] Add id to addons slug --- hassio/addons/data.py | 19 ++++++++++++++----- hassio/addons/util.py | 26 ++++++++++++++++++++++++++ hassio/api/supervisor.py | 2 +- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/hassio/addons/data.py b/hassio/addons/data.py index e42d6f630..2a1017198 100644 --- a/hassio/addons/data.py +++ b/hassio/addons/data.py @@ -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, diff --git a/hassio/addons/util.py b/hassio/addons/util.py index 688291246..bf9393f98 100644 --- a/hassio/addons/util.py +++ b/hassio/addons/util.py @@ -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)) diff --git a/hassio/api/supervisor.py b/hassio/api/supervisor.py index 938b7f685..3c09cc99b 100644 --- a/hassio/api/supervisor.py +++ b/hassio/api/supervisor.py @@ -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):