From 5bb7c74147732f938152deec31a70c7f99301572 Mon Sep 17 00:00:00 2001 From: Raymond Li Date: Sun, 3 Oct 2021 16:43:06 -0400 Subject: [PATCH] Dynamically import --- main.py | 33 ++++----------------------------- projects/__init__.py | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 29 deletions(-) create mode 100644 projects/__init__.py diff --git a/main.py b/main.py index e6f650c..4f37e26 100644 --- a/main.py +++ b/main.py @@ -7,34 +7,9 @@ This mirror status checker determines whether CSC mirror is up-to-date with upst import time import sys import requests -from projects.almalinux import AlmaLinux -from projects.alpine import Alpine -from projects.apache import Apache -from projects.arch import Arch -from projects.centos import CentOS -from projects.ceph import Ceph -from projects.cpan import CPAN -from projects.cygwin import Cygwin -from projects.debian import Debian -from projects.debiancd import DebianCD -from projects.debianmultimedia import DebianMultimedia -from projects.debianports import DebianPorts -from projects.debiansecurity import DebianSecurity -from projects.eclipse import Eclipse -from projects.fedora import Fedora -from projects.freebsd import FreeBSD -from projects.gentoodistfiles import GentooDistfiles -from projects.gentooportage import GentooPortage -from projects.gnome import GNOME -from projects.gnu import GNU -from projects.gutenberg import Gutenberg -from projects.ipfire import IPFire -from projects.kde import KDE -from projects.kdeapplicationdata import KDEApplicationData -from projects.kernel import Kernel -from projects.openbsd import OpenBSD -from shared import CSC_MIRROR -from dateparser.search import search_dates # this library seems to be super slow but the other library: dateutil.parser gets some errors + +from projects import * # noqa +# from dateparser.search import search_dates # this library seems to be super slow but the other library: dateutil.parser gets some errors # http://theautomatic.net/2018/12/18/2-packages-for-extracting-dates-from-a-string-of-text-in-python/ import re # import regular expressions to remove stray numbers in string that might interfere with date finding import json # import json to read project info stored in json file @@ -111,4 +86,4 @@ if __name__ == "__main__": except requests.exceptions.RequestException as err: print(f"Error: {project}\n{err}") with open("data.json", "w", encoding="utf-8") as file: - json.dump(data, file, indent=4) + json.dump(data, file, indent='\t') diff --git a/projects/__init__.py b/projects/__init__.py new file mode 100644 index 0000000..4bcd514 --- /dev/null +++ b/projects/__init__.py @@ -0,0 +1,17 @@ +from inspect import isclass +from pkgutil import iter_modules +from pathlib import Path +from importlib import import_module + +# iterate through the modules in the current package +package_dir = Path(__file__).resolve().parent +for (_, module_name, _) in iter_modules([package_dir]): + + # import the module and iterate through its attributes + module = import_module(f"{__name__}.{module_name}") + for attribute_name in dir(module): + attribute = getattr(module, attribute_name) + + if isclass(attribute): + # Add the class to this package's variables + globals()[attribute_name] = attribute