3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-27 21:19:31 +01:00

Move DeprecatedAttributesObject, CamelCaseToSnakeCase to structures (#476)

This commit is contained in:
James Lu 2017-08-28 19:36:55 -07:00
parent 0907f05296
commit b1159400f1
3 changed files with 47 additions and 46 deletions

View File

@ -45,7 +45,7 @@ class ChannelState(structures.IRCCaseInsensitiveDict):
return self._data[key] return self._data[key]
class PyLinkNetworkCore(utils.DeprecatedAttributesObject, utils.CamelCaseToSnakeCase): class PyLinkNetworkCore(structures.DeprecatedAttributesObject, structures.CamelCaseToSnakeCase):
"""Base IRC object for PyLink.""" """Base IRC object for PyLink."""
def __init__(self, netname): def __init__(self, netname):
@ -1568,7 +1568,7 @@ class Server():
IrcServer = Server IrcServer = Server
class Channel(utils.DeprecatedAttributesObject, utils.CamelCaseToSnakeCase, structures.CopyWrapper): class Channel(structures.DeprecatedAttributesObject, structures.CamelCaseToSnakeCase, structures.CopyWrapper):
"""PyLink IRC channel class.""" """PyLink IRC channel class."""
def __init__(self, irc, name=None): def __init__(self, irc, name=None):

View File

@ -11,6 +11,7 @@ import pickle
import os import os
import threading import threading
from copy import copy, deepcopy from copy import copy, deepcopy
import string
from .log import log from .log import log
from . import conf from . import conf
@ -151,6 +152,50 @@ class IRCCaseInsensitiveSet(CaseInsensitiveSet):
def __copy__(self): def __copy__(self):
return self.__class__(self._irc, data=self._data.copy()) return self.__class__(self._irc, data=self._data.copy())
class DeprecatedAttributesObject():
"""
Object implementing deprecated attributes and warnings on access.
"""
def __init__(self):
self.deprecated_attributes = {}
def __getattribute__(self, attr):
# Note: "self.deprecated_attributes" calls this too, so the != check is
# needed to prevent a recursive loop!
# Also ignore reserved names beginning with "__".
if attr != 'deprecated_attributes' and not attr.startswith('__') and attr in self.deprecated_attributes:
log.warning('Attribute %s.%s is deprecated: %s' % (self.__class__.__name__, attr,
self.deprecated_attributes.get(attr)))
return object.__getattribute__(self, attr)
class CamelCaseToSnakeCase():
"""
Class which automatically converts missing attributes from camel case to snake case.
"""
def __getattr__(self, attr):
"""
Attribute fetching fallback function which normalizes camel case attributes to snake case.
"""
assert isinstance(attr, str), "Requested attribute %r is not a string!" % attr
normalized_attr = '' # Start off with the first letter, which is ignored when processing
for char in attr:
if char in string.ascii_uppercase:
char = '_' + char.lower()
normalized_attr += char
classname = self.__class__.__name__
if normalized_attr == attr:
# __getattr__ only fires if normal attribute fetching fails, so we can assume that
# the attribute was tried already and failed.
raise AttributeError('%s object has no attribute with normalized name %r' % (classname, attr))
target = getattr(self, normalized_attr)
log.warning('%s.%s is deprecated, considering migrating to %s.%s!', classname, attr, classname, normalized_attr)
return target
class DataStore: class DataStore:
""" """
Generic database class. Plugins should use a subclass of this such as JSONDataStore or Generic database class. Plugins should use a subclass of this such as JSONDataStore or

View File

@ -669,47 +669,3 @@ class IRCParser(argparse.ArgumentParser):
def exit(self, *args): def exit(self, *args):
return return
class DeprecatedAttributesObject():
"""
Object implementing deprecated attributes and warnings on access.
"""
def __init__(self):
self.deprecated_attributes = {}
def __getattribute__(self, attr):
# Note: "self.deprecated_attributes" calls this too, so the != check is
# needed to prevent a recursive loop!
# Also ignore reserved names beginning with "__".
if attr != 'deprecated_attributes' and not attr.startswith('__') and attr in self.deprecated_attributes:
log.warning('Attribute %s.%s is deprecated: %s' % (self.__class__.__name__, attr,
self.deprecated_attributes.get(attr)))
return object.__getattribute__(self, attr)
class CamelCaseToSnakeCase():
"""
Class which automatically converts missing attributes from camel case to snake case.
"""
def __getattr__(self, attr):
"""
Attribute fetching fallback function which normalizes camel case attributes to snake case.
"""
assert isinstance(attr, str), "Requested attribute %r is not a string!" % attr
normalized_attr = '' # Start off with the first letter, which is ignored when processing
for char in attr:
if char in string.ascii_uppercase:
char = '_' + char.lower()
normalized_attr += char
classname = self.__class__.__name__
if normalized_attr == attr:
# __getattr__ only fires if normal attribute fetching fails, so we can assume that
# the attribute was tried already and failed.
raise AttributeError('%s object has no attribute with normalized name %r' % (classname, attr))
target = getattr(self, normalized_attr)
log.warning('%s.%s is deprecated, considering migrating to %s.%s!', classname, attr, classname, normalized_attr)
return target