Commit d33180d7 authored by ashish's avatar ashish

Merge pull request #2 from ashish-gore/modelcache

Modelcache
parents 15d53271 b2d0c8c1
......@@ -2,10 +2,47 @@ import importlib
import inspect
import itertools
import os
import six
from django.conf import settings
def module_walk(root_module, include_self=True):
if isinstance(root_module, six.string_types):
root_module = importlib.import_module(root_module)
root_path = os.path.abspath(os.path.dirname(root_module.__file__))
if include_self:
yield root_module
for path, dirs, files in os.walk(root_path):
path = os.path.abspath(path)
for file in files:
if not file.rsplit('.', 1)[1] == 'py':
continue
if file == '__init__.py':
file = ''
file = os.path.join(path, file)
file = file.replace(root_path + os.sep, '')
module_path = '.'.join(filter(
None,
root_module.__name__.split('.')
+ file.rsplit('.', 1)[0].split(os.sep)
))
module = importlib.import_module(module_path)
yield module
class ModelCache(object):
"""Store for all the models in the project
"""
Store for all the models in the project
Based on Django's AppCache, this collects all the
models in the application for introspection.
......@@ -16,43 +53,38 @@ class ModelCache(object):
"""
__shared_state = dict(
modules={},
models=[]
)
def __init__(self):
self.__dict__ = self.__shared_state
def get_module_names_and_path(self, directory_path="."):
all_paths = []
@property
def models(self):
if hasattr(self, '_models'):
return self._models
for root, dirs, files in os.walk(directory_path):
all_paths.extend(
[(file[:-3], os.path.join(root, file))
for file in files if
file.endswith(".py") and file.startswith("models")]
)
self._models = list(set(itertools.chain(
*self.modules.values()
)))
return all_paths
return self._models
def get_models_from_modules(self, module_name):
module = importlib.import_module(module_name)
def find_models_in_module(self, module_path):
for module in module_walk(module_path, include_self=True):
models = self._get_models_from_module(module)
if models:
self.modules[module.__name__] = models
def _get_models_from_module(self, module):
models = [
obj for name, obj in vars(module).items()
if (inspect.isclass(obj)
and hasattr(obj, '__tablename__'))
and hasattr(obj, '_sa_class_manager'))
]
return models
model_cache = ModelCache()
model_cache.modules = {
module[0]: module[1]
for module in model_cache.get_module_names_and_path()
}
model_cache.models = list(itertools.chain(*[
model_cache.get_models_from_modules(module)
for module in model_cache.modules.keys()
]))
for path in getattr(settings, 'SA_MODEL_LOADER', []):
model_cache.find_models_in_module(path)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment