Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: current
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 16 additions & 2 deletions i3pystatus/__init__.py
Expand Up @@ -6,7 +6,8 @@
from i3pystatus.core.util import formatp, get_module

import argparse
import imp
import importlib.util
import importlib.machinery
import logging
import os

Expand Down Expand Up @@ -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
Expand All @@ -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()