Update the code for imp's removal with python 3.12 (#865)

* Changed the `__init__.py` file to use importlib instead of imp.
This commit is contained in:
Giancarlo Razzolini 2024-05-30 02:29:55 -03:00 committed by GitHub
parent fd96fc6d2e
commit 5cbb06292d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -6,7 +6,8 @@ from i3pystatus.core.settings import SettingsBase
from i3pystatus.core.util import formatp, get_module
import argparse
import imp
import importlib.util
import importlib.machinery
import logging
import os
@ -35,6 +36,19 @@ def clock_example():
status.run()
def load_source(modname, filename):
"""
From: https://docs.python.org/3/whatsnew/3.12.html?highlight=load_source#imp
"""
loader = importlib.machinery.SourceFileLoader(modname, filename)
spec = importlib.util.spec_from_file_location(modname, filename, loader=loader)
module = importlib.util.module_from_spec(spec)
# The module is always executed and not cached in sys.modules.
# Uncomment the following line to cache the module.
# sys.modules[module.__name__] = module
loader.exec_module(module)
return module
def main():
parser = argparse.ArgumentParser(description='''
run i3pystatus configuration file. Starts i3pystatus clock example if no arguments were provided
@ -44,6 +58,6 @@ def main():
if args.config:
module_name = "i3pystatus-config"
imp.load_source(module_name, args.config)
load_source(module_name, args.config)
else:
clock_example()