From bfd2b7585e64f4766e4eec315f94d187e2d4f976 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Sat, 1 May 2021 17:53:27 -0400 Subject: refactor: move the remaining backward.py code, no more backward.py --- coverage/misc.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'coverage/misc.py') diff --git a/coverage/misc.py b/coverage/misc.py index 7182d385..6f104ac0 100644 --- a/coverage/misc.py +++ b/coverage/misc.py @@ -5,6 +5,7 @@ import errno import hashlib +import importlib.util import inspect import locale import os @@ -315,6 +316,30 @@ def substitute_variables(text, variables): return text +def format_local_datetime(dt): + """Return a string with local timezone representing the date. + """ + return dt.astimezone().strftime('%Y-%m-%d %H:%M %z') + + +def import_local_file(modname, modfile=None): + """Import a local file as a module. + + Opens a file in the current directory named `modname`.py, imports it + as `modname`, and returns the module object. `modfile` is the file to + import if it isn't in the current directory. + + """ + if modfile is None: + modfile = modname + '.py' + spec = importlib.util.spec_from_file_location(modname, modfile) + mod = importlib.util.module_from_spec(spec) + sys.modules[modname] = mod + spec.loader.exec_module(mod) + + return mod + + class BaseCoverageException(Exception): """The base of all Coverage exceptions.""" pass -- cgit v1.2.1