Allow to build a manifest parser from directory

This commit is contained in:
Ivan Kravets
2019-10-01 00:11:31 +03:00
parent 703912fdc9
commit af1a0f3587
3 changed files with 37 additions and 16 deletions

View File

@ -26,7 +26,7 @@ from platformio.commands import PlatformioCLI
from platformio.compat import dump_json_to_unicode from platformio.compat import dump_json_to_unicode
from platformio.managers.lib import LibraryManager, get_builtin_libs, is_builtin_lib from platformio.managers.lib import LibraryManager, get_builtin_libs, is_builtin_lib
from platformio.package.manifest.model import StrictManifestModel 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.proc import is_ci
from platformio.project.config import ProjectConfig from platformio.project.config import ProjectConfig
from platformio.project.helpers import get_project_dir, is_platformio_project 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) raise exception.InvalidLibConfURL(config_url)
# Validate manifest # 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)) result = util.get_api_result("/lib/register", data=dict(config_url=config_url))
if "message" in result and result["message"]: if "message" in result and result["message"]:

View File

@ -56,33 +56,52 @@ class ManifestFileType(object):
return ManifestFileType.LIBRARY_JSON return ManifestFileType.LIBRARY_JSON
class ManifestFactory(object): class ManifestParserFactory(object):
@staticmethod @staticmethod
def type_to_clsname(type_): def type_to_clsname(t):
type_ = type_.replace(".", " ") t = t.replace(".", " ")
type_ = type_.title() t = t.title()
return "%sManifestParser" % type_.replace(" ", "") return "%sManifestParser" % t.replace(" ", "")
@staticmethod @staticmethod
def new_from_file(path): def new_from_file(path, remote_url=False):
if not path or not os.path.isfile(path): if not path or not os.path.isfile(path):
raise ManifestException("Manifest file does not exist %s" % path) raise ManifestException("Manifest file does not exist %s" % path)
for type_ in get_class_attributes(ManifestFileType).values(): for t in get_class_attributes(ManifestFileType).values():
if path.endswith(type_): if path.endswith(t):
return ManifestFactory.new(get_file_contents(path), type_) return ManifestParserFactory.new(get_file_contents(path), t, remote_url)
raise ManifestException("Unknown manifest file type %s" % path) 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 @staticmethod
def new_from_url(remote_url): def new_from_url(remote_url):
r = requests.get(remote_url) r = requests.get(remote_url)
r.raise_for_status() r.raise_for_status()
return ManifestFactory.new( return ManifestParserFactory.new(
r.text, ManifestFileType.from_uri(remote_url), remote_url r.text, ManifestFileType.from_uri(remote_url), remote_url
) )
@staticmethod @staticmethod
def new(contents, type_, remote_url=None): def new(contents, type, remote_url=None):
clsname = ManifestFactory.type_to_clsname(type_) # pylint: disable=redefined-builtin
clsname = ManifestParserFactory.type_to_clsname(type)
if clsname not in globals(): if clsname not in globals():
raise ManifestException("Unknown manifest file type %s" % clsname) raise ManifestException("Unknown manifest file type %s" % clsname)
return globals()[clsname](contents, remote_url) return globals()[clsname](contents, remote_url)

View File

@ -176,7 +176,9 @@ def test_library_json_valid_model():
"license": "MIT" "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()) model = ManifestModel(**data.as_dict())
assert sorted(model.as_dict().items()) == sorted( assert sorted(model.as_dict().items()) == sorted(
{ {
@ -221,7 +223,7 @@ category=Display
url=https://github.com/olikraus/u8glib url=https://github.com/olikraus/u8glib
architectures=avr,sam architectures=avr,sam
""" """
data = parser.ManifestFactory.new( data = parser.ManifestParserFactory.new(
contents, parser.ManifestFileType.LIBRARY_PROPERTIES contents, parser.ManifestFileType.LIBRARY_PROPERTIES
) )
model = ManifestModel(**data.as_dict()) model = ManifestModel(**data.as_dict())