ManifestParser: init from dir using name of file in remote url if provided

This commit is contained in:
Ivan Kravets
2019-10-03 16:14:51 +03:00
parent 76865a1730
commit a7855ae664
2 changed files with 28 additions and 2 deletions

View File

@ -54,7 +54,7 @@ class ManifestFileType(object):
return ManifestFileType.MODULE_JSON
if uri.endswith("package.json"):
return ManifestFileType.PACKAGE_JSON
return ManifestFileType.LIBRARY_JSON
return None
class ManifestParserFactory(object):
@ -76,6 +76,16 @@ class ManifestParserFactory(object):
@staticmethod
def new_from_dir(path, remote_url=None):
assert os.path.isdir(path), "Invalid directory %s" % path
type_from_uri = ManifestFileType.from_uri(remote_url) if remote_url else None
if type_from_uri and os.path.isfile(os.path.join(path, type_from_uri)):
return ManifestParserFactory.new(
get_file_contents(os.path.join(path, type_from_uri)),
type_from_uri,
remote_url=remote_url,
package_dir=path,
)
file_order = [
ManifestFileType.PLATFORM_JSON,
ManifestFileType.LIBRARY_JSON,
@ -99,7 +109,9 @@ class ManifestParserFactory(object):
r = requests.get(remote_url)
r.raise_for_status()
return ManifestParserFactory.new(
r.text, ManifestFileType.from_uri(remote_url), remote_url
r.text,
ManifestFileType.from_uri(remote_url) or ManifestFileType.LIBRARY_JSON,
remote_url,
)
@staticmethod

View File

@ -425,6 +425,20 @@ def test_package_json_model():
assert mp.as_dict()["system"] == ["darwin_x86_64"]
def test_parser_from_dir(tmpdir_factory):
pkg_dir = tmpdir_factory.mktemp("package")
pkg_dir.join("library.json").write('{"name": "library.json"}')
pkg_dir.join("library.properties").write("name=library.properties")
data = parser.ManifestParserFactory.new_from_dir(str(pkg_dir)).as_dict()
assert data["name"] == "library.json"
data = parser.ManifestParserFactory.new_from_dir(
str(pkg_dir), remote_url="http://localhost/library.properties"
).as_dict()
assert data["name"] == "library.properties"
def test_examples_from_dir(tmpdir_factory):
package_dir = tmpdir_factory.mktemp("project")
package_dir.join("library.json").write(