Skip to content

Commit

Permalink
Use pre module scan to create classes table (#2824)
Browse files Browse the repository at this point in the history
* pre-scan the module to create classes table.

* reformat.

* changed the module pre-scan result to json file.

* removed the no use import.

* Improved create_classes_table_static() error message logging.
  • Loading branch information
yhwen authored Aug 22, 2024
1 parent 9785297 commit 81d004a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
20 changes: 11 additions & 9 deletions nvflare/fuel/utils/class_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import pkgutil
from typing import Dict, List, Optional

from nvflare.fuel.utils.components_utils import create_classes_table_static
from nvflare.security.logging import secure_format_exception

DEPRECATED_PACKAGES = ["nvflare.app_common.pt", "nvflare.app_common.homomorphic_encryption"]
Expand Down Expand Up @@ -85,10 +86,10 @@ def __init__(self, base_pkgs: List[str], module_names: List[str], exclude_libs=T
self.exclude_libs = exclude_libs

self._logger = logging.getLogger(self.__class__.__name__)
self._class_table: Dict[str, str] = {}
self._create_classes_table()
self._class_table = create_classes_table_static()

def _create_classes_table(self):
def create_classes_table(self):
class_table: Dict[str, str] = {}
scan_result_table = {}
for base in self.base_pkgs:
package = importlib.import_module(base)
Expand All @@ -111,20 +112,21 @@ def _create_classes_table(self):
# same class name exists in multiple modules
if name in scan_result_table:
scan_result = scan_result_table[name]
if name in self._class_table:
self._class_table.pop(name)
self._class_table[f"{scan_result.module_name}.{name}"] = module_name
self._class_table[f"{module_name}.{name}"] = module_name
if name in class_table:
class_table.pop(name)
class_table[f"{scan_result.module_name}.{name}"] = module_name
class_table[f"{module_name}.{name}"] = module_name
else:
scan_result = _ModuleScanResult(class_name=name, module_name=module_name)
scan_result_table[name] = scan_result
self._class_table[name] = module_name
except (ModuleNotFoundError, RuntimeError) as e:
class_table[name] = module_name
except (ModuleNotFoundError, RuntimeError, AttributeError) as e:
self._logger.debug(
f"Try to import module {module_name}, but failed: {secure_format_exception(e)}. "
f"Can't use name in config to refer to classes in module: {module_name}."
)
pass
return class_table

def get_module_name(self, class_name) -> Optional[str]:
"""Gets the name of the module that contains this class.
Expand Down
Loading

0 comments on commit 81d004a

Please sign in to comment.