mirror of
https://github.com/capnproto/pycapnp.git
synced 2025-03-04 08:24:43 +01:00
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:
parent
c77b5fa132
commit
db63fd1880
4 changed files with 96 additions and 0 deletions
|
@ -3116,6 +3116,12 @@ class _Importer:
|
||||||
|
|
||||||
module_name = module_name[:-len('_capnp')]
|
module_name = module_name[:-len('_capnp')]
|
||||||
capnp_module_name = module_name + self.extension
|
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:
|
if package_path:
|
||||||
paths = package_path
|
paths = package_path
|
||||||
|
@ -3134,8 +3140,14 @@ class _Importer:
|
||||||
path = _os.getcwd()
|
path = _os.getcwd()
|
||||||
elif not is_abs(path):
|
elif not is_abs(path):
|
||||||
path = abspath(path)
|
path = abspath(path)
|
||||||
|
|
||||||
if is_file(path+sep+capnp_module_name):
|
if is_file(path+sep+capnp_module_name):
|
||||||
return _Loader(fullname, join_path(path, capnp_module_name), self.additional_paths)
|
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
|
_importer = None
|
||||||
|
|
||||||
|
|
39
test/addressbook with spaces.capnp
Normal file
39
test/addressbook with spaces.capnp
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
@0xc39aee9191aedcf3;
|
||||||
|
|
||||||
|
const qux :UInt32 = 123;
|
||||||
|
|
||||||
|
struct Person {
|
||||||
|
id @0 :UInt32;
|
||||||
|
name @1 :Text;
|
||||||
|
email @2 :Text;
|
||||||
|
phones @3 :List(PhoneNumber);
|
||||||
|
|
||||||
|
struct PhoneNumber {
|
||||||
|
number @0 :Text;
|
||||||
|
type @1 :Type;
|
||||||
|
|
||||||
|
enum Type {
|
||||||
|
mobile @0;
|
||||||
|
home @1;
|
||||||
|
work @2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
employment :union {
|
||||||
|
unemployed @4 :Void;
|
||||||
|
employer @5 :Employer;
|
||||||
|
school @6 :Text;
|
||||||
|
selfEmployed @7 :Void;
|
||||||
|
# We assume that a person is only one of these.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Employer {
|
||||||
|
name @0 :Text;
|
||||||
|
boss @1 :Person;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct AddressBook {
|
||||||
|
people @0 :List(Person);
|
||||||
|
}
|
||||||
|
|
39
test/addressbook-with-dashes.capnp
Normal file
39
test/addressbook-with-dashes.capnp
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
@0xd33206731939e03b;
|
||||||
|
|
||||||
|
const qux :UInt32 = 123;
|
||||||
|
|
||||||
|
struct Person {
|
||||||
|
id @0 :UInt32;
|
||||||
|
name @1 :Text;
|
||||||
|
email @2 :Text;
|
||||||
|
phones @3 :List(PhoneNumber);
|
||||||
|
|
||||||
|
struct PhoneNumber {
|
||||||
|
number @0 :Text;
|
||||||
|
type @1 :Type;
|
||||||
|
|
||||||
|
enum Type {
|
||||||
|
mobile @0;
|
||||||
|
home @1;
|
||||||
|
work @2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
employment :union {
|
||||||
|
unemployed @4 :Void;
|
||||||
|
employer @5 :Employer;
|
||||||
|
school @6 :Text;
|
||||||
|
selfEmployed @7 :Void;
|
||||||
|
# We assume that a person is only one of these.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Employer {
|
||||||
|
name @0 :Text;
|
||||||
|
boss @1 :Person;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct AddressBook {
|
||||||
|
people @0 :List(Person);
|
||||||
|
}
|
||||||
|
|
|
@ -58,6 +58,12 @@ def test_failed_import():
|
||||||
def test_defualt_import_hook():
|
def test_defualt_import_hook():
|
||||||
import addressbook_capnp
|
import addressbook_capnp
|
||||||
|
|
||||||
|
def test_dash_import():
|
||||||
|
import addressbook_with_dashes_capnp
|
||||||
|
|
||||||
|
def test_spaces_import():
|
||||||
|
import addressbook_with_spaces_capnp
|
||||||
|
|
||||||
def test_add_import_hook():
|
def test_add_import_hook():
|
||||||
capnp.add_import_hook([this_dir])
|
capnp.add_import_hook([this_dir])
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue