From 59bad3e678fc807d5ca9ea2b3cb503c0735ce08f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Wei=C3=9Fmann?= Date: Wed, 3 Feb 2016 18:21:55 +0100 Subject: [PATCH] Reformats code --- pwgrep/file_helper.py | 60 ++++++++++++++++++++++++++++++++++++++++ pwgrep/result_printer.py | 10 +++---- pwgrep/search_result.py | 41 +++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 6 deletions(-) create mode 100644 pwgrep/file_helper.py create mode 100644 pwgrep/search_result.py diff --git a/pwgrep/file_helper.py b/pwgrep/file_helper.py new file mode 100644 index 0000000..6f7f184 --- /dev/null +++ b/pwgrep/file_helper.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import mimetypes +import os + +from pwgrep import printer_helper + + +def file_is_binary(filename): + file_type, _ = mimetypes.guess_type(filename) + return file_type is None or not file_type.startswith('text') + + +def file_is_directory(filename): + return os.path.isdir(filename) + + +def files_from_directory_recursive(directory, deference_recursive=False): + files = symlink_walker(directory, deference_recursive=deference_recursive) + for file_name in files: + yield file_name + + +def symlink_walker(directory, deference_recursive=False, onerror=None): + """ + Wrapper around os.walk that recognizes symlink loops. + + Heavily inspired by Nick Coghlans walking algorithm + https://code.activestate.com/recipes/577913-selective-directory-walking/ + + :param directory: Directory to start in + :param deference_recursive: If symlinks shall be followed + :param onerror: Error handler of os.walk + :return: yields full filename of traversed files + """ + if deference_recursive: + absolute_root_path = os.path.abspath(os.path.realpath(directory)) + + for path, walk_subdirs, files in os.walk(directory, topdown=True, + onerror=onerror, + followlinks=deference_recursive): + # Recognize infinite symlink loops + if deference_recursive and os.path.islink(path): + # After following a symbolic link, we check if we refer + # to a parent directory. If yes: We have an infinite loop + relative_path = os.path.relpath(path, directory) + nominal_path = os.path.join(absolute_root_path, relative_path) + real_path = os.path.abspath(os.path.realpath(path)) + path_fragments = zip(nominal_path.split(os.path.sep), + real_path.split(os.path.sep)) + for nominal, real in path_fragments: + if nominal != real: + break + else: + printer_helper.print_loop_warning(path) + walk_subdirs[:] = [] + continue + for file_name in files: + yield os.path.join(path, file_name) diff --git a/pwgrep/result_printer.py b/pwgrep/result_printer.py index e08fdff..f4b4b1a 100644 --- a/pwgrep/result_printer.py +++ b/pwgrep/result_printer.py @@ -2,10 +2,9 @@ # -*- coding: utf-8 -*- from __future__ import print_function -import sys from pwgrep.colors import ConsoleColors -from pwgrep.search_result import TextSearchResult, BinarySearchResult, StdinSearchResult +from pwgrep import search_result class ResultPrinter(object): @@ -58,12 +57,11 @@ def print_stdin_match(self, search_result): self.print_any_match(search_result) def print_result(self, result): - if isinstance(result, TextSearchResult): + if isinstance(result, search_result.TextSearchResult): self.print_single_match(result) - elif isinstance(result, BinarySearchResult): + elif isinstance(result, search_result.BinarySearchResult): self.print_binary_match(result) - elif isinstance(result, StdinSearchResult): + elif isinstance(result, search_result.StdinSearchResult): self.print_stdin_match(result) else: assert False, "Invalid type to print: {}".format(type(result)) - diff --git a/pwgrep/search_result.py b/pwgrep/search_result.py new file mode 100644 index 0000000..baaf1e2 --- /dev/null +++ b/pwgrep/search_result.py @@ -0,0 +1,41 @@ +class SearchResult(object): + + def __init__(self): + pass + + +class FileSearchResult(object): + + def __init__(self, filename=None): + super(FileSearchResult, self).__init__() + self.filename = filename + assert(self.filename is not None) + + +class BinarySearchResult(FileSearchResult): + + def __init__(self, filename=None): + super(BinarySearchResult, self).__init__(filename) + self.match = True + + +class TextSearchResult(FileSearchResult): + + def __init__(self, line_number=None, line=None, match=None, filename=None): + super(TextSearchResult, self).__init__(filename) + self.line_number = line_number + self.match = match + self.line = line + assert(self.line_number is not None) + assert(self.line is not None) + + +class StdinSearchResult(SearchResult): + + def __init__(self, line_number=None, line=None, match=None): + super(StdinSearchResult, self).__init__() + self.line_number = line_number + self.match = match + self.line = line + assert(self.line is not None) + assert(self.line_number is not None)