Beispiel #1
0
 def __str__(self):
     """Convert object to string for printing"""
     return (("{label:%s,regex:%s,git_project_location:%s,case_sensitive:%s,"
              "exclude_these_file_extensions:%s,github_link:%s") %
             (self.label, self.regex, self.git_project_location,
              string2bool(self.case_sensitive),
              str(self.exclude_these_file_extensions), self.github_link))
Beispiel #2
0
def get_search_criteria(config, section_name):
    """Read in new search criteria from configuration file.

    Returns:
        `SearchCriteria`
    """
    label = section_name[7:]
    regex = ''
    git_project_location = ''
    exclude_these_file_extensions_list = ''
    case_sensitive = False
    github_link = ''

    name_value_pairs = config.items(section_name)
    for name, value in name_value_pairs:
        if name == 'regex':
            regex = value
        elif name == 'git_project_location':
            git_project_location = value
        elif name == 'exclude_these_file_extensions':
            exclude_these_file_extensions_list = value
        elif name == 'case_sensitive':
            case_sensitive = string2bool(value)
        elif name == 'github_link':
            github_link = value
        else:
            sys.exit(("Invalid configuration syntax '%s' under section '%s' in "
                      "'%s'. Please fix.") %
                     (name, section_name, const.config_filename))

    search_crit = SearchCriteria(label)
    search_crit.regex = regex
    search_crit.git_project_location = git_project_location
    if case_sensitive:
        search_crit.case_sensitive = True # False by default in class

    #split list as string into actual list
    if is_not_blank_or_whitespace(exclude_these_file_extensions_list):
        search_crit.exclude_these_file_extensions = exclude_these_file_extensions_list.split(',')

    search_crit.github_link = github_link

    return search_crit
Beispiel #3
0
def set_globals_from_config():
    """Set global consts from config file values and returns search parameters.

    Defaults:
        print_debug: False (can be set to True)
        output_format: plaintext (can be set to 'HTML')
        detect_pattern: all (can be set to 'added_only')

    Returns:
        A tuple of (searches, file_extension_exclusions, commit_hash_exclusions)
            searches: A list of SearchCriteria objects to be processed.
            file_extension_exclusions: A list of file extensions to exclude from
                search.
            commit_hash_exclusions: A list of commit hashes to exclude from
                search.
    """
    config = DictConfigParser()

    try:
        config.readfp(open(const.config_filename))
    except ConfigParser.Error:
        sys.exit("Could not read or parse '%s'" % const.config_filename)

    config.read(const.config_filename)

    searches = [] #list of SearchCriteria objs
    file_extension_exclusions = [] #list of strings
    commit_hash_exclusions = [] #list of strings

    for section_name in config.sections():
        if section_name == 'Global':
            const.print_debug = string2bool(config.get('Global', 'PRINT_DEBUG'))
            if config.get('Global', 'OUTPUT_FORMAT') == 'HTML':
                const.output_format = 'HTML'
            if config.get('Global', 'DETECT_PATTERN') == 'added_only':
                const.detect_pattern = 'added_only'
        elif section_name[0:7] == 'search:':
            search_crit = get_search_criteria(config, section_name)
            dprint("Appending this search criteria: %s" % search_crit)
            searches.append(search_crit)
        elif section_name == 'Excluded Filetypes':
            #read in file types to exclude from search results always
            name_value_pairs = config.items(section_name)
            for name in name_value_pairs:
                file_extension_exclusions.append(name)
            dprint("Added %d file extension(s) to ignore    always: %s" %
                   (len(file_extension_exclusions), file_extension_exclusions))
        elif section_name == 'Excluded Commits':
            name_value_pairs = config.items(section_name)
            for name in name_value_pairs:
                commit_hash_exclusions.append(name)
            dprint("Added %d commit(s) to ignore always: %s" %
                   (len(commit_hash_exclusions), commit_hash_exclusions))
        else:
            sys.exit(("Found invalid section name '%s' in configuration file "
                      "'%s'. Please fix the configuration file.") %
                     (section_name, const.config_filename))

    #Set default constants if not set in config file
    try:
        const.print_debug = False
    except:
        pass
    try:
        const.output_format = 'plaintext'
    except:
        pass
    try:
        const.detect_pattern = 'all'
    except:
        pass

    return (searches, file_extension_exclusions, commit_hash_exclusions)