remove whitespace from output

This commit is contained in:
Arvind Tiwari 2018-02-08 19:10:37 +05:30
parent b9c53717e3
commit 4145129974
4 changed files with 57 additions and 38 deletions

3
.gitignore vendored
View file

@ -37,3 +37,6 @@ secret-key
*.mo *.mo
*.log *.log
*.sql *.sql
# to keep empty dirs
!.gitkeep

View file

@ -71,7 +71,7 @@
</div> </div>
<div class="description select-configuration input form-group justify-center"> <div class="description select-configuration input form-group justify-center">
<label for="config">OS</label> <label for="config">OS</label>
<select name="config" id=""> <select name="config">
{% for template in templates %} {% for template in templates %}
<option value="{{template.opennebula_vm_template_id}}">{{template.name}}</option> <option value="{{template.opennebula_vm_template_id}}">{{template.name}}</option>
{% endfor %} {% endfor %}

View file

@ -22,7 +22,7 @@ Example:
""" """
# import csv # import csv
# import json import json
import logging import logging
import os import os
import re import re
@ -45,8 +45,8 @@ RE_PATTERNS = {
'^\s*\@media([^{]+)\{\s*([\s\S]*?})\s*}' '^\s*\@media([^{]+)\{\s*([\s\S]*?})\s*}'
), ),
'css_selector': ( 'css_selector': (
'^\s*([.#\[:_A-Za-z][^{]*)' '^\s*([.#\[:_A-Za-z][^{]*?)\s*'
'{([\s\S]*?)}' '\s*{([\s\S]*?)\s*}'
), ),
'html_class': 'class=[\'\"]([a-zA-Z0-9-_\s]*)', 'html_class': 'class=[\'\"]([a-zA-Z0-9-_\s]*)',
'html_id': 'id=[\'\"]([a-zA-Z0-9-_]*)' 'html_id': 'id=[\'\"]([a-zA-Z0-9-_]*)'
@ -81,11 +81,12 @@ class Command(BaseCommand):
def handle(self, *args, **options): def handle(self, *args, **options):
apps_list = options['apps'] apps_list = options['apps']
report = {}
for app in apps_list: for app in apps_list:
if options['css']: if options['css']:
self.optimize_css(app) report[app] = self.optimize_css(app)
# else: # write report
# optimize_all(app) write_report(report)
def optimize_css(self, app_name): def optimize_css(self, app_name):
"""Optimize declarations inside a css stylesheet """Optimize declarations inside a css stylesheet
@ -103,8 +104,7 @@ class Command(BaseCommand):
'css_dup': get_css_duplication(css_selectors), 'css_dup': get_css_duplication(css_selectors),
'css_unused': get_css_unused(css_selectors, html_selectors) 'css_unused': get_css_unused(css_selectors, html_selectors)
} }
# write report return report
write_report(report)
def get_files(app_name): def get_files(app_name):
@ -192,8 +192,11 @@ def get_selectors_css(files):
data = f.read() data = f.read()
media_selectors[file] = string_match_pattern(data, 'css_media') media_selectors[file] = string_match_pattern(data, 'css_media')
new_data = string_remove_pattern(data, 'css_media') new_data = string_remove_pattern(data, 'css_media')
default_match = string_match_pattern(new_data, 'css_selector')
selectors[file] = { selectors[file] = {
'default': string_match_pattern(new_data, 'css_selector') 'default': [
[' '.join(grp.split()) for grp in m] for m in default_match
]
} }
# get declarations from media queries # get declarations from media queries
for file, match_list in media_selectors.items(): for file, match_list in media_selectors.items():
@ -224,9 +227,10 @@ def get_selectors_html(files):
selectors = {} selectors = {}
for file in files: for file in files:
results = templates_match_pattern(file, ['html_class', 'html_id']) results = templates_match_pattern(file, ['html_class', 'html_id'])
class_dict = {c: 1 for match in results[0] for c in match.split()}
selectors[file] = { selectors[file] = {
'class': results[0], 'classes': list(class_dict.keys()),
'id': results[1], 'ids': results[1],
} }
return selectors return selectors
@ -284,8 +288,8 @@ def string_remove_pattern(data, patterns):
patterns (list or str): The pattern(s) to be removed from the file patterns (list or str): The pattern(s) to be removed from the file
Returns: Returns:
str: The new string with all instance of matching pattern removed str: The new string with all instance of matching pattern
from it removed from it
""" """
if not isinstance(patterns, str): if not isinstance(patterns, str):
for p in patterns: for p in patterns:
@ -353,23 +357,30 @@ def get_css_unused(css_selectors, html_selectors):
html_selectors (dict): A dictonary containing the 'class' and 'id' html_selectors (dict): A dictonary containing the 'class' and 'id'
declarations from all html files declarations from all html files
""" """
pass with open('utils/optimize/test.json', 'w') as f:
json.dump([html_selectors, css_selectors], f, indent=4)
# print(html_selectors, css_selectors)
def write_report(results, filename='frontend'): def write_report(all_reports, filename='frontend'):
"""Write the generated report to a file for re-use """Write the generated report to a file for re-use
Args; Args;
results (dict): A dictonary of results obtained from different tests all_reports (dict): A dictonary of report obtained from different tests
filename (str): An optional suffix for the output file filename (str): An optional suffix for the output file
""" """
full_filename = '../optimize_' + filename + '.html' full_filename = 'utils/optimize/optimize_' + filename + '.html'
output_file = os.path.join( output_file = os.path.join(
settings.PROJECT_DIR, full_filename settings.PROJECT_DIR, full_filename
) )
with open('utils/optimize/op_frontend.json', 'w') as f:
json.dump(all_reports, f, indent=4)
with open(output_file, 'w', newline='') as f: with open(output_file, 'w', newline='') as f:
data = template.loader.render_to_string('utils/report.html', results) f.write(
f.write(data) template.loader.render_to_string(
'utils/report.html', {'all_reports': all_reports}
)
)
# w = csv.writer(f) # w = csv.writer(f)
# print(zip_longest(*results)) # print(zip_longest(*results))
# for r in zip_longest(*results): # for r in zip_longest(*results):

View file

@ -22,25 +22,30 @@
<h3>Duplicate Rules in a Stylesheet</h3> <h3>Duplicate Rules in a Stylesheet</h3>
<hr> <hr>
</div> </div>
<div class="card-text"> {% for app, report in all_reports.items %}
{% for file, media_group in css_dup.items %} <div class="card-text">
<strong>{{file}}</strong> <h4 class="pb-2">{{app}}</h4>
<ul class="list-unstyled"> <div class="pl-2">
{% for media, rules in media_group.items %} {% for file, media_group in report.css_dup.items %}
<li> <strong>{{file}}</strong>
{{media}} : <ul class="list-unstyled">
<ul> {% for media, rules in media_group.items %}
{% for rule, count in rules.items %} <li>
<li><strong>{{rule}}</strong> <em>({{count}})</em></li> {{media}} :
{% endfor %} <ul>
</ul> {% for rule, count in rules.items %}
</li> <li><strong>{{rule}}</strong> <em>({{count}})</em></li>
{% empty %} {% endfor %}
<li class="text-success">No Duplicates!</li> </ul>
</li>
{% empty %}
<li class="text-success">No Duplicates!</li>
{% endfor %}
</ul>
{% endfor %} {% endfor %}
</ul> </div>
{% endfor %} </div>
</div> {% endfor %}
</div> </div>
</div> </div>
</div> </div>