Dynamically import

This commit is contained in:
Raymond Li 2021-10-03 16:43:06 -04:00
parent fd073aa0d5
commit 5bb7c74147
Signed by untrusted user: r389li
GPG Key ID: A014EA89B62BBB1B
2 changed files with 21 additions and 29 deletions

View File

@ -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')

projects/__init__.py Normal file
View File

@ -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