Skip to content
Snippets Groups Projects
Commit 14dd8d1c authored by Chris Lamb's avatar Chris Lamb :eyes:
Browse files

Split out formatting of class names into a common method that supports stripping.

parent 7b814f8c
No related branches found
No related tags found
No related merge requests found
Pipeline #150063 passed
......@@ -30,7 +30,7 @@ from diffoscope.exc import (
ContainerExtractionError,
)
from diffoscope.tools import tool_required
from diffoscope.utils import format_cmdline
from diffoscope.utils import format_cmdline, format_class
from diffoscope.config import Config
from diffoscope.profiling import profile
from diffoscope.difference import Difference
......@@ -237,9 +237,6 @@ class File(metaclass=abc.ABCMeta):
return self._other_file.__class__.CONTAINER_CLASSES[0](self)
return None
def type_name(klass):
return "{}.{}".format(klass.__module__, klass.__name__)
if hasattr(self, "_as_container"):
return self._as_container
......@@ -248,20 +245,24 @@ class File(metaclass=abc.ABCMeta):
# Try each container class in turn, returning the first one that
# instantiates without error.
for klass in klasses:
formatted_class = format_class(
klass, strip="diffoscope.comparators."
)
logger.debug(
"Instantiating a %s for %s", type_name(klass), self.name,
"Instantiating a %s for %s", formatted_class, self.name,
)
try:
self._as_container = klass(self)
logger.debug(
"Returning a %s for %s", type_name(klass), self.name,
"Returning a %s for %s", formatted_class, self.name,
)
return self._as_container
except RequiredToolNotFound as exc:
logger.debug(
"Cannot instantiate a %s; missing tool %s",
type_name(klass),
formatted_class,
exc.command,
)
try:
......
......@@ -21,6 +21,8 @@ import logging
from diffoscope.profiling import profile
from ...utils import format_class
from .. import ComparatorManager
logger = logging.getLogger(__name__)
......@@ -37,7 +39,11 @@ def try_recognize(file, cls, recognizes):
return False
# Found a match; perform type magic
logger.debug("Using %s.%s for %s", cls.__module__, cls.__name__, file.name)
logger.debug(
"Using %s for %s",
format_class(cls, strip="diffoscope.comparators."),
file.name,
)
new_cls = type(cls.__name__, (cls, type(file)), {})
file.__class__ = new_cls
......
......@@ -22,6 +22,8 @@ import time
import contextlib
import collections
from .utils import format_class
_ENABLED = False
......@@ -53,9 +55,7 @@ class ProfileManager:
def increment(self, start, namespace, key):
if not isinstance(key, str):
key = "{}.{}".format(
key.__class__.__module__, key.__class__.__name__
)
key = format_class(key.__class__)
self.data[namespace][key]["time"] += time.time() - start
self.data[namespace][key]["count"] += 1
......
......@@ -63,3 +63,12 @@ def bail_if_non_existing(*paths):
"%s: %s: No such file or directory\n" % (sys.argv[0], path)
)
sys.exit(2)
def format_class(klass, strip=""):
val = "{}.{}".format(klass.__module__, klass.__name__)
if val.startswith(strip):
val = val[len(strip) :]
return val
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment