Skip to content

Commit

Permalink
Update: Make single file modules PEP 561 compliant (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
nutti committed Oct 17, 2023
1 parent 5f3610e commit 8a2f0b6
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 24 deletions.
38 changes: 26 additions & 12 deletions src/fake_bpy_module/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,27 +648,33 @@ def _build_generation_info_internal(
self, analyze_result: AnalysisResult,
module_structure: 'ModuleStructure') -> 'GenerationInfoByRule':
def find_target_file(
name: str, structure: 'ModuleStructure', target: str) -> str:
name: str, structure: 'ModuleStructure', target: str,
module_level: int) -> str:
for m in structure.children():
mod_name = name + m.name
if mod_name == target:
if len(m.children()) == 0:
if len(m.children()) == 0 and module_level != 0:
return mod_name + ".py"
return mod_name + "/__init__.py"

if len(m.children()) > 0:
ret = find_target_file(mod_name + "/", m, target)
ret = find_target_file(
mod_name + "/", m, target, module_level+1)
if ret:
return ret
return None

def build_child_modules(
gen_info: 'GenerationInfoByRule', name: str,
structure: 'ModuleStructure'):
structure: 'ModuleStructure', module_level: int):
for m in structure.children():
mod_name = name + m.name
if len(m.children()) == 0:
filename = re.sub(r"\.", "/", mod_name) + ".py"
if module_level != 0:
filename = re.sub(r"\.", "/", mod_name) + ".py"
else:
filename = \
re.sub(r"\.", "/", mod_name) + "/__init__.py"
info = gen_info.create_target(filename)
info.data = []
info.child_modules = []
Expand All @@ -679,17 +685,18 @@ def build_child_modules(
info.data = []
info.child_modules = [child.name for child in m.children()]
info.name = mod_name
build_child_modules(gen_info, mod_name + ".", m)
build_child_modules(
gen_info, mod_name + ".", m, module_level+1)

# build child modules
generator_info = GenerationInfoByRule()
build_child_modules(generator_info, "", module_structure)
build_child_modules(generator_info, "", module_structure, 0)

# build data
for section in analyze_result.section_info:
for info in section.info_list:
target = find_target_file("", module_structure,
re.sub(r"\.", "/", info.module()))
re.sub(r"\.", "/", info.module()), 0)
if target is None:
raise RuntimeError("Could not find target file to "
f"generate (target: {info.module()})")
Expand Down Expand Up @@ -1218,18 +1225,25 @@ def __init__(self, config: 'PackageGeneratorConfig'):
# create module directories/files
def _create_empty_modules(self, package_structure: 'ModuleStructure'):
def make_module_dirs(base_path: str, structure: 'ModuleStructure'):
def make_dir(path, structure_: 'ModuleStructure'):
def make_dir(path, structure_: 'ModuleStructure',
module_level: int):
for item in structure_.children():
if len(item.children()) >= 1:
if len(item.children()) == 0:
if module_level == 0:
dir_path = path + "/" + item.name
pathlib.Path(dir_path).mkdir(
parents=True, exist_ok=True)
self._create_py_typed_file(dir_path)
elif len(item.children()) >= 1:
dir_path = path + "/" + item.name
pathlib.Path(dir_path).mkdir(
parents=True, exist_ok=True)
self._create_py_typed_file(dir_path)
if dir_path == base_path:
continue
make_dir(dir_path, item)
make_dir(dir_path, item, module_level+1)

make_dir(base_path, structure)
make_dir(base_path, structure, 0)

make_module_dirs(self._config.output_dir, package_structure)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -634,9 +634,11 @@ def test_single_rule(self):
self.assertIsNotNone(actual_rule)
self.assertIsNotNone(actual_gen_info)

self.assertEqual(set(actual_gen_info.targets()), {"module_abc.py"})
self.assertEqual(
set(actual_gen_info.targets()), {"module_abc/__init__.py"})

target_module_abc = actual_gen_info.get_target("module_abc.py")
target_module_abc = actual_gen_info.get_target(
"module_abc/__init__.py")
self.assertEqual(len(target_module_abc.data), 1)
self.assertEqual(target_module_abc.data[0].type(), "class")
self.assertEqual(target_module_abc.data[0].name(), "Class123")
Expand Down Expand Up @@ -726,7 +728,8 @@ def test_multiple_rules(self):
self.assertEqual(
set(actual_gen_info_1.targets()),
{"module_1/__init__.py", "module_1/submodule_1.py"})
self.assertEqual(set(actual_gen_info_2.targets()), {"module_2.py"})
self.assertEqual(
set(actual_gen_info_2.targets()), {"module_2/__init__.py"})

target_module_1 = actual_gen_info_1.get_target("module_1/__init__.py")
self.assertEqual(len(target_module_1.data), 1)
Expand Down Expand Up @@ -755,7 +758,7 @@ def test_multiple_rules(self):
self.assertEqual(len(target_module_1_submodule_1.child_modules), 0)
self.assertEqual(len(target_module_1_submodule_1.dependencies), 0)

target_module_2 = actual_gen_info_2.get_target("module_2.py")
target_module_2 = actual_gen_info_2.get_target("module_2/__init__.py")
self.assertEqual(len(target_module_2.data), 1)
self.assertEqual(target_module_2.data[0].type(), "function")
self.assertEqual(target_module_2.data[0].name(), "function_1")
Expand Down Expand Up @@ -822,9 +825,10 @@ def test_exceptional_rule(self):
self.assertIsNotNone(actual_gen_info)

self.assertEqual(
set(actual_gen_info.targets()), {"module_exceptional.py"})
set(actual_gen_info.targets()), {"module_exceptional/__init__.py"})

target_module_abc = actual_gen_info.get_target("module_exceptional.py")
target_module_abc = actual_gen_info.get_target(
"module_exceptional/__init__.py")
self.assertEqual(len(target_module_abc.data), 2)
self.assertEqual(target_module_abc.data[0].type(), "class")
self.assertEqual(target_module_abc.data[0].name(), "ClassExp")
Expand Down Expand Up @@ -888,7 +892,7 @@ def test_single_rules(self):
actual_files_dir = self.output_dir

py_files = [
"module_abc.py",
"module_abc/__init__.py",
]
for file_ in py_files:
expect_file_path = f"{expect_files_dir}/{file_}"
Expand All @@ -899,7 +903,7 @@ def test_single_rules(self):
self.assertTrue(filecmp.cmp(expect_file_path, actual_file_path))

json_files = [
"module_abc.py-dump.json",
"module_abc/__init__.py-dump.json",
]
for file_ in json_files:
expect_file_path = f"{expect_files_dir}/{file_}"
Expand Down Expand Up @@ -957,7 +961,7 @@ def test_multiple_rules(self):
py_files = [
"module_1/__init__.py",
"module_1/submodule_1.py",
"module_2.py",
"module_2/__init__.py",
]
for file_ in py_files:
expect_file_path = f"{expect_files_dir}/{file_}"
Expand All @@ -970,7 +974,7 @@ def test_multiple_rules(self):
json_files = [
"module_1/__init__.py-dump.json",
"module_1/submodule_1.py-dump.json",
"module_2.py-dump.json",
"module_2/__init__.py-dump.json",
]
for file_ in json_files:
expect_file_path = f"{expect_files_dir}/{file_}"
Expand Down Expand Up @@ -1019,7 +1023,7 @@ def test_exceptional_rules(self):
actual_files_dir = self.output_dir

py_files = [
"module_exceptional.py",
"module_exceptional/__init__.py",
]
for file_ in py_files:
expect_file_path = f"{expect_files_dir}/{file_}"
Expand All @@ -1030,7 +1034,7 @@ def test_exceptional_rules(self):
self.assertTrue(filecmp.cmp(expect_file_path, actual_file_path))

json_files = [
"module_exceptional.py-dump.json",
"module_exceptional/__init__.py-dump.json",
]
for file_ in json_files:
expect_file_path = f"{expect_files_dir}/{file_}"
Expand Down

0 comments on commit 8a2f0b6

Please sign in to comment.