Changeset - 7e97c7f48a99
[Not reviewed]
0 3 2
Lance Edgar (lance) - 2 months ago 2024-08-15 20:28:03
lance@edbob.org
fix: move `get_class_hierarchy()` util function to wuttjamaican
5 files changed with 44 insertions and 41 deletions:
0 comments (0 inline, 0 general)
rattail/projects/base.py
Show inline comments
 
@@ -34,7 +34,8 @@ import sys
 
import colander
 
from mako.template import Template
 

	
 
from rattail.util import get_class_hierarchy, get_studly_prefix
 
from wuttjamaican.util import get_class_hierarchy
 
from rattail.util import get_studly_prefix
 
from rattail.mako import ResourceTemplateLookup
 

	
 

	
rattail/util.py
Show inline comments
 
@@ -37,7 +37,8 @@ import warnings
 
from wuttjamaican.util import (UNSPECIFIED,
 
                               load_entry_points as wutta_load_entry_points,
 
                               load_object as wutta_load_object,
 
                               make_title as wutta_make_title)
 
                               make_title as wutta_make_title,
 
                               get_class_hierarchy as wutta_get_class_hierarchy)
 

	
 

	
 
# TODO: deprecate / remove this
 
@@ -134,37 +135,14 @@ def data_diffs(local_data, host_data, fields=None,
 

	
 
def get_class_hierarchy(klass, topfirst=True):
 
    """
 
    Returns a list of all classes in the inheritance chain for the
 
    given class.
 

	
 
    For instance::
 

	
 
       class A(object):
 
          pass
 

	
 
       class B(A):
 
          pass
 

	
 
       class C(B):
 
          pass
 

	
 
       get_class_hierarchy(C)
 
       # -> [A, B, C]
 

	
 
    Specify ``topfirst=False`` to get ``[C, B, A]`` instead.
 
    DEPRECATED - use
 
    :func:`wuttjamaican:wuttjamaican.util.get_class_hierarchy()`
 
    instead.
 
    """
 
    hierarchy = []
 

	
 
    def traverse(cls):
 
        if cls is not object:
 
            hierarchy.append(cls)
 
            for parent in cls.__bases__:
 
                traverse(parent)
 

	
 
    traverse(klass)
 
    if topfirst:
 
        hierarchy.reverse()
 
    return hierarchy
 
    warnings.warn("rattail.util.get_class_hierarchy() is deprecated; "
 
                  "please use wuttjamaican.util.get_class_hierarchy() instead",
 
                  DeprecationWarning, stacklevel=2)
 
    return wutta_get_class_hierarchy(klass, topfirst=topfirst)
 

	
 

	
 
def get_package_name(name):
tests/projects/__init__.py
Show inline comments
 
new file 100644
tests/projects/test_base.py
Show inline comments
 
new file 100644
 
# -*- coding: utf-8; -*-
 

	
 
from rattail.projects import base as mod
tests/test_util.py
Show inline comments
 
@@ -4,7 +4,28 @@ import os
 
import shutil
 
from unittest import TestCase
 

	
 
from rattail import util
 
from rattail import util as mod
 

	
 

	
 
class A: pass
 
class B(A): pass
 
class C(B): pass
 

	
 
class TestGetClassHierarchy(TestCase):
 

	
 
    def test_basic(self):
 

	
 
        classes = mod.get_class_hierarchy(A)
 
        self.assertEqual(classes, [A])
 

	
 
        classes = mod.get_class_hierarchy(B)
 
        self.assertEqual(classes, [A, B])
 

	
 
        classes = mod.get_class_hierarchy(C)
 
        self.assertEqual(classes, [A, B, C])
 

	
 
        classes = mod.get_class_hierarchy(C, topfirst=False)
 
        self.assertEqual(classes, [C, B, A])
 

	
 

	
 
class ImportTests(TestCase):
 
@@ -24,21 +45,21 @@ class ImportTests(TestCase):
 
        shutil.rmtree(os.path.join(os.path.dirname(__file__), 'foo'))
 

	
 
    def test_module_already_imported(self):
 
        util_module = util.import_module_path('rattail.util')
 
        self.assertTrue(util_module is util)
 
        util_module = mod.import_module_path('rattail.util')
 
        self.assertTrue(util_module is mod)
 

	
 
    # def test_new_module(self):
 
    #     dirname = os.path.abspath(os.path.dirname(__file__))
 

	
 
    #     foo = util.import_module_path('tests.foo')
 
    #     foo = mod.import_module_path('tests.foo')
 
    #     self.assertEqual(foo.__file__, os.path.abspath(
 
    #             os.path.join(dirname, 'foo', '__init__.py')))
 

	
 
    #     bar = util.import_module_path('tests.foo.bar')
 
    #     bar = mod.import_module_path('tests.foo.bar')
 
    #     self.assertEqual(bar.__file__, os.path.abspath(
 
    #             os.path.join(dirname, 'foo', 'bar.py')))
 

	
 
    #     baz = util.import_module_path('tests.foo.baz')
 
    #     baz = mod.import_module_path('tests.foo.baz')
 
    #     self.assertEqual(baz.__file__, os.path.abspath(
 
    #             os.path.join(dirname, 'foo', 'baz', '__init__.py')))
 

	
 
@@ -53,15 +74,15 @@ class ImportTests(TestCase):
 

	
 
# """)
 

	
 
#         somevar = util.load_object('tests.foo.baz:somevar')
 
#         somevar = mod.load_object('tests.foo.baz:somevar')
 
#         self.assertEqual(somevar, 42)
 

	
 
#         somefunc = util.load_object('tests.foo.baz:somefunc')
 
#         somefunc = mod.load_object('tests.foo.baz:somefunc')
 
#         self.assertEqual(somefunc(), 420)
 

	
 

	
 
class TestPrettify(TestCase):
 

	
 
    def test_basic(self):
 
        text = util.prettify('foo_bar')
 
        text = mod.prettify('foo_bar')
 
        self.assertEqual(text, "Foo Bar")
0 comments (0 inline, 0 general)