Skip to content

Commit

Permalink
Reformats code
Browse files Browse the repository at this point in the history
  • Loading branch information
derphilipp committed Feb 3, 2016
1 parent 212b018 commit 59bad3e
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 6 deletions.
60 changes: 60 additions & 0 deletions pwgrep/file_helper.py
Original file line number Diff line number Diff line change
@@ -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)
10 changes: 4 additions & 6 deletions pwgrep/result_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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))

41 changes: 41 additions & 0 deletions pwgrep/search_result.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 59bad3e

Please sign in to comment.