Skip to content

Commit

Permalink
Merge pull request #12 from Styria-Digital/feature/test_base_special_…
Browse files Browse the repository at this point in the history
…cases

Feature/test base special cases
  • Loading branch information
mislavcimpersak committed Sep 8, 2016
2 parents 9f63009 + e5aa6f5 commit b5bbf37
Show file tree
Hide file tree
Showing 10 changed files with 277 additions and 59 deletions.
7 changes: 7 additions & 0 deletions demo/app/templates/app/templates/double_member_load.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% extends "master.html" %}
{% load example_simple_tag plus from app_tags %}
{% load example_simple_tag plus from app_tags %}

{% block body %}
<h1>App template</h1>
{% endblock body %}
6 changes: 6 additions & 0 deletions demo/app/templates/app/templates/only_filter.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% extends "master.html" %}
{% load plus from app_tags %}

{% block body %}
<p id="filter">Testing: {{ 2|plus:5 }}</p>
{% endblock body %}
2 changes: 1 addition & 1 deletion demo/app/templates/app/templates/with_tags.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ <h1>App template</h1>
{% example_inclusion_tag %}
<p id="simple_tag">Testing: {% example_simple_tag %}</p>
<p id="assignment_tag">Testing: {% example_assignment_tag as example%}</p>
<p id="filter">Testing: {{ 2|plus:5 }}</p>
<p id="filter">Testing: {% example_simple_tag 2|plus:5 %}</p>
{% endblock body %}
2 changes: 1 addition & 1 deletion demo/app/templatetags/app_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def example_inclusion_tag():


@register.simple_tag
def example_simple_tag():
def example_simple_tag(*args):
"""
An example of a simple tag.
"""
Expand Down
9 changes: 9 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[flake8]
exclude = build,.git,.tox
max-line-length = 100

[metadata]
license-file = LICENSE

[wheel]
universal = 1
23 changes: 12 additions & 11 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#!/usr/bin/env python

import os
from setuptools import setup, find_packages
from setuptools import find_packages, setup

setup(
name='django-unload',
version='0.1',
version='0.2',
url="https://github.com/Styria-Digital/django-unload",
description='Remove unused template tags',
author='Styria Digital Services',
description='Remove unused custom Django template tags and filters',
long_description=open(
os.path.join(os.path.dirname(__file__), 'README.rst')
).read(),
Expand All @@ -16,23 +17,23 @@
include_package_data=True,
zip_safe=False,
install_requires=[
'django>=1.8',
'django>=1.8, <1.9',
'tabulate==0.7.5',
],
classifiers=[
'Development Status :: 2 - Pre-Alpha',
'Development Status :: 3 - Alpha',
'Environment :: Web Environment',
'Framework :: Django',
'Framework :: Django :: 1.8',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Environment :: Web Environment',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
'Framework :: Django',
],
)
71 changes: 31 additions & 40 deletions unload/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,30 @@

from __future__ import unicode_literals

import sys
from django.template.base import Lexer, Template as BaseTemplate

from distutils.version import StrictVersion

from django.template.base import (
Lexer, Template as BaseTemplate, InvalidTemplateLibrary)

from .settings import (DJANGO_VERSION, BUILT_IN_TAGS, BUILT_IN_TAG_VALUES,
BUILT_IN_FILTERS)
from .utils import get_filters, update_dictionary

if StrictVersion(DJANGO_VERSION) > StrictVersion('1.8'):
from django.template.base import get_library
from .settings import BUILT_IN_TAGS, BUILT_IN_TAG_VALUES, BUILT_IN_FILTERS
from .utils import get_filters, get_templatetag_members, update_dictionary


class Template(BaseTemplate):
"""
An override of Django's Template class.
After calling the parent class, the template is analyzed for duplicates
and unnecessary loads.
Additional attributes:
:tokens: a list of tokens found in the template
:loaded_modules: a dictionary of loaded modules
:loaded_members: a dictionary of loaded tags/filters
:used_tags: a list of custom tags used in the template
:used_filters: a list of custom filters used in the template
:tags: a dictionary of custom tags loaded into the template
:filters: a dictionary of custom filters loaded into the template
:utilized_modules: a dictionary of utilization statuses
:utilized_members: a dictionary of utilization statuses
"""

def __init__(self, template_string, origin=None, name=None, engine=None):
super(Template, self).__init__(template_string, origin, name, engine)
Expand All @@ -32,7 +40,8 @@ def __init__(self, template_string, origin=None, name=None, engine=None):
self.used_tags = self._get_used_tags()
self.used_filters = self._get_used_filters()
# Get the tags and filters available to this template
self.tags, self.filters = self._get_templatetags_members()
self.tags, self.filters = get_templatetag_members(
self.name, self.loaded_modules)
# Find utilized modules, tags and filters
self.utilized_modules = self._get_utilized_modules()
self.utilized_members = self._get_utilized_members()
Expand All @@ -51,17 +60,21 @@ def list_duplicates(self):
lines = self.loaded_modules[module]
lines_str = ', '.join(map(str, lines))
if len(lines) > 1 and lines_str not in temp_table.keys():
temp_table[lines_str] = [module, None]
temp_table[lines_str] = [module, []]

# Find duplicate member loads
for member in self.loaded_members:
lines = self.loaded_members[member]
lines_str = ', '.join(map(str, lines))

if len(lines) > 1:
if lines_str not in temp_table.keys():
temp_table[lines_str] = [None, member]
else:
temp_table[lines_str][1] = member
temp_table[lines_str][1].append(member)

for key in temp_table:
if temp_table[key][1] == []:
temp_table[key][1] = None
else:
temp_table[key][1] = '; '.join(temp_table[key][1])

# Prepare output format
headers = ['Duplicate module', 'Duplicate tag/filter', 'Line number']
Expand Down Expand Up @@ -146,28 +159,6 @@ def _get_utilized_modules(self):

return utilized_modules

def _get_templatetags_members(self):
"""
Get the names of tags and filters from available templatetags modules.
:returns: {'somelib': [tags]}, {'somelib': [filters]}
"""
tags = {}
filters = {}
for module in self.loaded_modules:
try:
lib = get_library(module)
except InvalidTemplateLibrary:
msg = ('Unable to locate the loaded library! Library: {}; '
'Template: {}\n').format(module, self.name)
sys.stdout.write(msg)
tags[module] = []
continue
tags[module] = lib.tags.keys()
filters[module] = lib.filters.keys()

return tags, filters

def _get_tokens(self):
"""
Get the list of tokens from the template source.
Expand Down
Loading

0 comments on commit b5bbf37

Please sign in to comment.