npbackup/i18n/loaders/python_loader.py
2023-03-22 00:17:16 +01:00

27 lines
774 B
Python

import os.path
import sys
from importlib import util
from .. import config
from . import Loader, I18nFileLoadError
class PythonLoader(Loader):
"""class to load python files"""
def __init__(self):
super(PythonLoader, self).__init__()
def load_file(self, filename):
_, name = os.path.split(filename)
module_name, _ = os.path.splitext(name)
try:
spec = util.spec_from_file_location(module_name, filename)
module = util.module_from_spec(spec)
spec.loader.exec_module(module)
return vars(module)
except Exception as e:
raise I18nFileLoadError("error loading file {0}".format(filename)) from e
def parse_file(self, file_content):
return file_content