Skip to content

Commit

Permalink
Update the code for imp's removal with python 3.12 (#865)
Browse files Browse the repository at this point in the history
* Changed the `__init__.py` file to use importlib instead of imp.
  • Loading branch information
grazzolini authored and tyjak committed Aug 15, 2024
1 parent db55e27 commit e7b977b
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions i3pystatus/__init__.py
Original file line number Diff line number Diff line change
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()

0 comments on commit e7b977b

Please sign in to comment.