Add ability to import modules with dashes or spaces

Any module with underscores in it will now attempt to look for 3 files,
first the original, then with dashes, then with spaces, ie:
import addressbook_v_2_capnp
will search for 'addressbook_v_2.capnp', 'addressbook-v-2.capnp', and
'addressbook v 2.capnp'.
This commit is contained in:
Jason Paryani
2014-06-12 16:37:06 -07:00
parent c77b5fa132
commit db63fd1880
4 changed files with 96 additions and 0 deletions

View File

@@ -3116,6 +3116,12 @@ class _Importer:
module_name = module_name[:-len('_capnp')]
capnp_module_name = module_name + self.extension
has_underscores = False
if '_' in capnp_module_name:
capnp_module_name_dashes = capnp_module_name.replace('_', '-')
capnp_module_name_spaces = capnp_module_name.replace('_', ' ')
has_underscores = True
if package_path:
paths = package_path
@@ -3134,8 +3140,14 @@ class _Importer:
path = _os.getcwd()
elif not is_abs(path):
path = abspath(path)
if is_file(path+sep+capnp_module_name):
return _Loader(fullname, join_path(path, capnp_module_name), self.additional_paths)
if has_underscores:
if is_file(path+sep+capnp_module_name_dashes):
return _Loader(fullname, join_path(path, capnp_module_name_dashes), self.additional_paths)
if is_file(path+sep+capnp_module_name_spaces):
return _Loader(fullname, join_path(path, capnp_module_name_spaces), self.additional_paths)
_importer = None