Fix loading when no package found

This commit is contained in:
Matthew Honnibal 2017-03-16 18:30:02 -05:00
parent 5c66cffafd
commit adb0b7e43b
2 changed files with 12 additions and 9 deletions

View File

@ -35,14 +35,16 @@ set_lang_class(bn.Bengali.lang, bn.Bengali)
def load(name, **overrides):
data_path = overrides.get('path', util.get_data_path())
meta = parse_package_meta(data_path, name)
lang = meta['lang'] if meta and 'lang' in meta else 'en'
meta = parse_package_meta(data_path, name, require=False)
lang = meta['lang'] if meta and 'lang' in meta else name
cls = get_lang_class(lang)
overrides['meta'] = meta
overrides['path'] = Path(data_path / name)
model_path = Path(data_path) / name
if model_path.exists():
overrides['path'] = model_path
return cls(**overrides)
def info(name):
meta = parse_package_meta(util.get_data_path(), name)
meta = parse_package_meta(util.get_data_path(), name, require=True)
print(json.dumps(meta, indent=2))

View File

@ -149,15 +149,16 @@ def check_renamed_kwargs(renamed, kwargs):
raise TypeError("Keyword argument %s now renamed to %s" % (old, new))
def parse_package_meta(package_path, package, on_error=False):
def parse_package_meta(package_path, package, require=True):
location = os.path.join(str(package_path), package, 'meta.json')
if not os.path.isfile(location) and on_error:
on_error()
else:
if os.path.isfile(location):
with io.open(location, encoding='utf8') as f:
meta = json.load(f)
return meta
return False
elif require:
raise IOError("Could not read meta.json from %s" % location)
else:
return None
def print_msg(*text, **kwargs):