From af1a0f35870050633aed53700c4cf96a1e329bae Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Tue, 1 Oct 2019 00:11:31 +0300 Subject: [PATCH] Allow to build a manifest parser from directory --- platformio/commands/lib.py | 4 +-- platformio/package/manifest/parser.py | 43 +++++++++++++++++++-------- tests/test_pkgmanifest.py | 6 ++-- 3 files changed, 37 insertions(+), 16 deletions(-) diff --git a/platformio/commands/lib.py b/platformio/commands/lib.py index 18ec9da2..3f52cb8b 100644 --- a/platformio/commands/lib.py +++ b/platformio/commands/lib.py @@ -26,7 +26,7 @@ from platformio.commands import PlatformioCLI from platformio.compat import dump_json_to_unicode from platformio.managers.lib import LibraryManager, get_builtin_libs, is_builtin_lib from platformio.package.manifest.model import StrictManifestModel -from platformio.package.manifest.parser import ManifestFactory +from platformio.package.manifest.parser import ManifestParserFactory from platformio.proc import is_ci from platformio.project.config import ProjectConfig from platformio.project.helpers import get_project_dir, is_platformio_project @@ -495,7 +495,7 @@ def lib_register(config_url): raise exception.InvalidLibConfURL(config_url) # Validate manifest - StrictManifestModel(**ManifestFactory.new_from_url(config_url).as_dict()) + StrictManifestModel(**ManifestParserFactory.new_from_url(config_url).as_dict()) result = util.get_api_result("/lib/register", data=dict(config_url=config_url)) if "message" in result and result["message"]: diff --git a/platformio/package/manifest/parser.py b/platformio/package/manifest/parser.py index 90be3a9b..ec2e713c 100644 --- a/platformio/package/manifest/parser.py +++ b/platformio/package/manifest/parser.py @@ -56,33 +56,52 @@ class ManifestFileType(object): return ManifestFileType.LIBRARY_JSON -class ManifestFactory(object): +class ManifestParserFactory(object): @staticmethod - def type_to_clsname(type_): - type_ = type_.replace(".", " ") - type_ = type_.title() - return "%sManifestParser" % type_.replace(" ", "") + def type_to_clsname(t): + t = t.replace(".", " ") + t = t.title() + return "%sManifestParser" % t.replace(" ", "") @staticmethod - def new_from_file(path): + def new_from_file(path, remote_url=False): if not path or not os.path.isfile(path): raise ManifestException("Manifest file does not exist %s" % path) - for type_ in get_class_attributes(ManifestFileType).values(): - if path.endswith(type_): - return ManifestFactory.new(get_file_contents(path), type_) + for t in get_class_attributes(ManifestFileType).values(): + if path.endswith(t): + return ManifestParserFactory.new(get_file_contents(path), t, remote_url) raise ManifestException("Unknown manifest file type %s" % path) + @staticmethod + def new_from_dir(path, remote_url=None): + assert os.path.isdir(path), "Invalid directory %s" % path + file_order = [ + ManifestFileType.PLATFORM_JSON, + ManifestFileType.LIBRARY_JSON, + ManifestFileType.LIBRARY_PROPERTIES, + ManifestFileType.MODULE_JSON, + ManifestFileType.PACKAGE_JSON, + ] + for t in file_order: + if not os.path.isfile(os.path.join(path, t)): + continue + return ManifestParserFactory.new( + get_file_contents(os.path.join(path, t)), t, remote_url + ) + raise ManifestException("Unknown manifest file type in %s directory" % path) + @staticmethod def new_from_url(remote_url): r = requests.get(remote_url) r.raise_for_status() - return ManifestFactory.new( + return ManifestParserFactory.new( r.text, ManifestFileType.from_uri(remote_url), remote_url ) @staticmethod - def new(contents, type_, remote_url=None): - clsname = ManifestFactory.type_to_clsname(type_) + def new(contents, type, remote_url=None): + # pylint: disable=redefined-builtin + clsname = ManifestParserFactory.type_to_clsname(type) if clsname not in globals(): raise ManifestException("Unknown manifest file type %s" % clsname) return globals()[clsname](contents, remote_url) diff --git a/tests/test_pkgmanifest.py b/tests/test_pkgmanifest.py index e631af94..9a71d384 100644 --- a/tests/test_pkgmanifest.py +++ b/tests/test_pkgmanifest.py @@ -176,7 +176,9 @@ def test_library_json_valid_model(): "license": "MIT" } """ - data = parser.ManifestFactory.new(contents, parser.ManifestFileType.LIBRARY_JSON) + data = parser.ManifestParserFactory.new( + contents, parser.ManifestFileType.LIBRARY_JSON + ) model = ManifestModel(**data.as_dict()) assert sorted(model.as_dict().items()) == sorted( { @@ -221,7 +223,7 @@ category=Display url=https://github.com/olikraus/u8glib architectures=avr,sam """ - data = parser.ManifestFactory.new( + data = parser.ManifestParserFactory.new( contents, parser.ManifestFileType.LIBRARY_PROPERTIES ) model = ManifestModel(**data.as_dict())