# coding: utf-8 # # Copyright © 2012-2014 Ejwa Software. All rights reserved. # # This file is part of gitinspector. # # gitinspector is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # gitinspector is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with gitinspector. If not, see . from __future__ import print_function from __future__ import unicode_literals from localization import N_ from outputable import Outputable import re import terminal import textwrap __filters__ = {"file": [[], set()], "author": [[], set()], "email": [[], set()], "revision": [[], set()]} class InvalidRegExpError(ValueError): def __init__(self, msg): super(InvalidRegExpError, self).__init__(msg) self.msg = msg def get(): return __filters__ def __add_one__(string): for i in __filters__: if (i + ":").lower() == string[0:len(i) + 1].lower(): __filters__[i][0].append(string[len(i) + 1:]) return __filters__["file"][0].append(string) def add(string): rules = string.split(",") for rule in rules: __add_one__(rule) def clear(): for i in __filters__: __filters__[i][0] = [] def get_filered(filter_type="file"): return __filters__[filter_type][1] def has_filtered(): for i in __filters__: if __filters__[i][1]: return True return False def set_filtered(string, filter_type="file"): string = string.strip() if len(string) > 0: for i in __filters__[filter_type][0]: try: if re.search(i, string) != None: __filters__[filter_type][1].add(string) return True except: raise InvalidRegExpError(_("invalid regular expression specified")) return False FILTERING_INFO_TEXT = N_("The following files were excluded from the statistics due to the specified exclusion patterns") FILTERING_AUTHOR_INFO_TEXT = N_("The following authors were excluded from the statistics due to the specified exclusion patterns") FILTERING_EMAIL_INFO_TEXT = N_("The authors with the following emails were excluded from the statistics due to the specified " \ "exclusion patterns") FILTERING_EMAIL_INFO_TEXT = N_("The following commit revisions were excluded from the statistics due to the specified " \ "exclusion patterns") class Filtering(Outputable): @staticmethod def __output_html_section__(info_string, filtered): filtering_xml = "" if filtered: filtering_xml += "

" + info_string + "."+ "

" for i in filtered: filtering_xml += "

" + i + "

" return filtering_xml def output_html(self): if has_filtered(): filtering_xml = "
" Filtering.__output_html_section__(_(FILTERING_INFO_TEXT), __filters__["file"][1]) Filtering.__output_html_section__(_(FILTERING_AUTHOR_INFO_TEXT), __filters__["author"][1]) Filtering.__output_html_section__(_(FILTERING_EMAIL_INFO_TEXT), __filters__["email"][1]) Filtering.__output_html_section__(_(FILTERING_EMAIL_INFO_TEXT), __filters__["revision"][1]) filtering_xml += "
" print(filtering_xml) @staticmethod def __output_text_section__(info_string, filtered): if filtered: print("\n" + textwrap.fill(info_string + ":", width=terminal.get_size()[0])) for i in filtered: (width, _unused) = terminal.get_size() print("...%s" % i[-width+3:] if len(i) > width else i) def output_text(self): Filtering.__output_text_section__(_(FILTERING_INFO_TEXT), __filters__["file"][1]) Filtering.__output_text_section__(_(FILTERING_AUTHOR_INFO_TEXT), __filters__["author"][1]) Filtering.__output_text_section__(_(FILTERING_EMAIL_INFO_TEXT), __filters__["email"][1]) Filtering.__output_text_section__(_(FILTERING_EMAIL_INFO_TEXT), __filters__["revision"][1]) @staticmethod def __output_xml_section__(info_string, filtered, container_tagname): if filtered: message_xml = "\t\t\t" +info_string + "\n" filtering_xml = "" for i in filtered: filtering_xml += "\t\t\t\t".format(container_tagname) + i + "\n".format(container_tagname) print("\t\t<{0}>".format(container_tagname)) print(message_xml + "\t\t\t\n" + filtering_xml + "\t\t\t\n") print("\t\t".format(container_tagname)) def output_xml(self): if has_filtered(): print("\t") Filtering.__output_xml_section__(_(FILTERING_INFO_TEXT), __filters__["file"][1], "files") Filtering.__output_xml_section__(_(FILTERING_AUTHOR_INFO_TEXT), __filters__["author"][1], "authors") Filtering.__output_xml_section__(_(FILTERING_EMAIL_INFO_TEXT), __filters__["email"][1], "emails") Filtering.__output_xml_section__(_(FILTERING_EMAIL_INFO_TEXT), __filters__["revision"][1].union(), "revisions") print("\t")