Xqt has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/311349

Change subject: [IMPR] Make LogEntryFactory logtypes public
......................................................................

[IMPR] Make LogEntryFactory logtypes public

- rename LogEntryFactory._logtypes to LogEntryFactory.logtypes
  in order to make them public
- add a _logtypes class method to deprecate the old class attribute
- use a new decorator 'classproperty' to get property access to the
  _logtypes method
- create a new classproperty class to realize the classproperty decorator
  and test it
- rename all old attributes to the new one

Change-Id: I5467a59beb93372a89426b6bfaaca4cd38e71e57
---
M pywikibot/logentries.py
M pywikibot/tools/__init__.py
M tests/logentry_tests.py
M tests/tools_tests.py
4 files changed, 68 insertions(+), 7 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/49/311349/1

diff --git a/pywikibot/logentries.py b/pywikibot/logentries.py
index 05607b6..6df2123 100644
--- a/pywikibot/logentries.py
+++ b/pywikibot/logentries.py
@@ -14,7 +14,7 @@
 
 import pywikibot
 from pywikibot.exceptions import Error
-from pywikibot.tools import deprecated
+from pywikibot.tools import deprecated, classproperty
 
 if sys.version_info[0] > 2:
     basestring = (str, )
@@ -371,7 +371,7 @@
     Only available method is create()
     """
 
-    _logtypes = {
+    logtypes = {
         'block': BlockEntry,
         'protect': ProtectEntry,
         'rights': RightsEntry,
@@ -403,6 +403,12 @@
             logclass = LogEntryFactory._getEntryClass(logtype)
             self._creator = lambda data: logclass(data, self._site)
 
+    @classproperty
+    @deprecated('LogEntryFactory.logtypes')
+    def _logtypes(cls):
+        """DEPRECATED LogEntryFactory class attribute of log types."""
+        return cls.logtypes
+
     def create(self, logdata):
         """
         Instantiate the LogEntry object representing logdata.
@@ -423,7 +429,7 @@
         @rtype: class
         """
         try:
-            return cls._logtypes[logtype]
+            return cls.logtypes[logtype]
         except KeyError:
             return LogEntry
 
diff --git a/pywikibot/tools/__init__.py b/pywikibot/tools/__init__.py
index ffafbdf..78d3c5e 100644
--- a/pywikibot/tools/__init__.py
+++ b/pywikibot/tools/__init__.py
@@ -169,6 +169,36 @@
         return func
 
 
+class classproperty(object):
+
+    """
+    Metaclass to accesss a class method as a property.
+
+    This class may be used as a decorator:
+
+    class Foo(object):
+
+        _bar = 'baz'  # a class property
+
+        @classproperty
+        def bar(cls):  # a class property method
+            return cls._bar
+
+    >>> Foo._bar
+    'baz'
+    >>> Foo.bar
+    'baz'
+    """
+
+    def __init__(self, cls_method):
+        """Hold the class method."""
+        self.method = cls_method
+
+    def __get__(self, instance, owner):
+        """Get the attribute of the owner class by its method."""
+        return self.method(owner)
+
+
 class UnicodeMixin(object):
 
     """Mixin class to add __str__ method in Python 2 or 3."""
diff --git a/tests/logentry_tests.py b/tests/logentry_tests.py
index 20d8935..48646b6 100644
--- a/tests/logentry_tests.py
+++ b/tests/logentry_tests.py
@@ -1,7 +1,7 @@
 # -*- coding: utf-8  -*-
 """Test logentries module."""
 #
-# (C) Pywikibot team, 2015
+# (C) Pywikibot team, 2015-2016
 #
 # Distributed under the terms of the MIT license.
 #
@@ -68,7 +68,7 @@
     def _test_logevent(self, logtype):
         """Test a single logtype entry."""
         logentry = self._get_logentry(logtype)
-        if logtype in LogEntryFactory._logtypes:
+        if logtype in LogEntryFactory.logtypes:
             self.assertEqual(logentry._expectedType, logtype)
         else:
             self.assertIsNone(logentry._expectedType)
@@ -110,7 +110,7 @@
             return test_logevent
 
         # create test methods for the support logtype classes
-        for logtype in LogEntryFactory._logtypes:
+        for logtype in LogEntryFactory.logtypes:
             cls.add_method(dct, 'test_%sEntry' % logtype.title(),
                            test_method(logtype))
 
@@ -136,7 +136,7 @@
         # initialized yet.
         available_types = set(self.site._paraminfo.parameter(
             'query+logevents', 'type')['type'])
-        for simple_type in available_types - set(LogEntryFactory._logtypes):
+        for simple_type in available_types - set(LogEntryFactory.logtypes):
             if not simple_type:
                 # paraminfo also reports an empty string as a type
                 continue
diff --git a/tests/tools_tests.py b/tests/tools_tests.py
index c6525e9..194a30b 100644
--- a/tests/tools_tests.py
+++ b/tests/tools_tests.py
@@ -23,6 +23,7 @@
     mock = e
 
 from pywikibot import tools
+from pywikibot.tools import classproperty
 
 from tests import join_xml_data_path
 
@@ -724,6 +725,30 @@
         self.chmod.assert_called_once_with(self.file, 0o600)
 
 
+class Foo(object):
+
+    """Test class to verify classproperty decorator."""
+
+    _bar = 'baz'
+
+    @classproperty
+    def bar(cls):
+        """Class property method."""
+        return cls._bar
+
+    
+class TestClassProperty(TestCase):
+
+    """Test classproperty decorator."""
+
+    net = False
+
+    def test_classproperty(self):
+        """Test for classproperty decorator."""
+        self.assertEqual(Foo.bar, 'baz')
+        self.assertEqual(Foo.bar, Foo._bar)
+
+
 if __name__ == '__main__':  # pragma: no cover
     try:
         unittest.main()

-- 
To view, visit https://gerrit.wikimedia.org/r/311349
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I5467a59beb93372a89426b6bfaaca4cd38e71e57
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Xqt <i...@gno.de>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to