Example #1
0
 def _get_unused_from_module(self, module):
     is_test_module = any(
         part.startswith("test") for part in module.__name__.split("."))
     for attr, value in module.__dict__.items():
         usages = self.usages[module][attr]
         if self.print_all:
             message = "%d (%s)" % (len(usages), usages)
             yield UnusedObject(module, attr, value, message)
             continue
         # Ignore attributes injected by Python
         if attr.startswith("__") and attr.endswith("__"):
             continue
         # Ignore tests
         if is_test_module and attr.startswith(("test", "Test")):
             continue
         own_usage = _UsageKind.aggregate_modules(usages)
         star_usage = self._has_import_star_usage(module, attr)
         usage = _UsageKind.aggregate([own_usage, star_usage])
         if usage is _UsageKind.used:
             continue
         if not self._should_record_as_unused(module, attr, value):
             continue
         if any(
                 hasattr(import_starred, attr)
                 for import_starred in self.module_to_import_stars[module]):
             continue
         if usage is _UsageKind.used_in_test:
             if not is_test_module and not safe_in(value,
                                                   _test_helper_objects):
                 yield UnusedObject(module, attr, value,
                                    "used only in tests")
         else:
             yield UnusedObject(module, attr, value, "unused")
Example #2
0
 def publish(self):
     m2 = {
         attr: str(getattr(self, attr))
         for attr in dir(self)
         if not callable(getattr(self, attr)) and not attr.startswith("__")
     }
     logger.trace(f'publish dict: {m2}')
     del m2['PPK']
     return m2
Example #3
0
 def publish(self):
     if self.private_key_file == '':
         self.private_key_file = f'/etc/wireguard/{self.sitecfg.locus}_priv'
     m2 = {
         attr: str(getattr(self, attr))
         for attr in dir(self)
         if not callable(getattr(self, attr)) and not attr.startswith("__")
     }
     m2['local_ipv4'] = [str(x) for x in self.local_ipv4]
     m2['local_ipv6'] = [str(x) for x in self.local_ipv6]
     del m2['hostname']
     del m2['sitecfg']
     logger.trace(pprint.pformat(m2))
     return self.hostname, m2
Example #4
0
def __auto_repr__(obj):
    attr_names = tuple()
    if hasattr(obj, '__dict__'):
        attr_names += tuple(obj.__dict__.keys())
    if hasattr(obj, '__slots__'):
        attr_names += tuple(obj.__slots__)

    items = []
    for attr in sorted(set(attr_names)):
        if attr.startswith('_'):
            continue
        value = getattr(obj, attr)
        # TODO:  should we add this feature to minimize some talktative reprs
        # such as of URL?
        #if value is None:
        #    continue
        items.append("%s=%s" % (attr, shortened_repr(value)))

    return "%s(%s)" % (obj.__class__.__name__, ', '.join(items))
Example #5
0
 def _get_handlers(cls):
     return [
         getattr(cls, attr) for attr in dir(cls)
         if callable(getattr(cls, attr)) and attr.startswith('handle_')
     ]