Skip to content

Commit

Permalink
- Added ability to display the matching end-of-paragraph lines. Specify
Browse files Browse the repository at this point in the history
  the "-P" option. (Issue #1, #1)
- Fixed some minor issues with the "-i" (case-blind) option
- Removed some dead code.
  • Loading branch information
bmc committed Mar 24, 2012
1 parent c1273fc commit bad8bee
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 23 deletions.
27 changes: 19 additions & 8 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,38 +1,48 @@
Change log for paragrep
---------------------------------------------------------------------------

Version 3.1.0 (24 March, 2012)

- Added ability to display the matching end-of-paragraph lines. Specify
the "-P" option. (Issue #1, https://github.com/bmc/paragrep/issues/1)
- "-a" and "-o" options not working correctly with multiple regexs. Problem
was a Python statement indentation issue. (Issue #2,
https://github.com/bmc/paragrep/issues/2)
- Fixed some minor issues with the "-i" (case-blind) option
- Removed some dead code.


Version 3.0.7 (6 March, 2012)

- Removed unused "-t" option from option summary in usage message.

---------------------------------------------------------------------------

Version 3.0.6 (14 March, 2011)

- Removed setup.py dependency on ez_setup.
- Changed to refer to 'grizzled-python', instead of 'grizzled'

---------------------------------------------------------------------------

Version 3.0.5 (28 March, 2010)

- Updated to latest version of ez_setup.py
- Adjusted license to new BSD license.
- Changed URL to new GitHub URL.

---------------------------------------------------------------------------

Version 3.0.4 (16 January, 2009)

- Now reads lines from the files in a more memory-efficient manner.
- Fixed problem with compilation of regexps.

---------------------------------------------------------------------------

Version 3.0.3 (9 November, 2008)

- Version (--version) option is now handled by the option parser.
- Removed left-over camel case variable names.
- Fixed -i (case-blind) matching, which wasn't working.

---------------------------------------------------------------------------

Version 3.0.2 (3 September, 2008)

- Changed epydoc markup to use reStructuredText.
Expand All @@ -41,12 +51,13 @@ Version 3.0.2 (3 September, 2008)
- Added --version option.
- Now properly bundles ez_setup.py

---------------------------------------------------------------------------

Version 3.0.1 (20 May, 2008)

- Updated to correspond to changes in the dependent Grizzled API.

---------------------------------------------------------------------------

Version 3.0 (3 April, 2008)

- Initial version posted to the web.
- Python version posted to the web. (Previous versions were implemented
in Perl and C.)
47 changes: 32 additions & 15 deletions paragrep/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@
__docformat__ = 'restructuredtext'

# Info about the module
__version__ = '3.0.8'
__version__ = '3.1.0'
__author__ = 'Brian M. Clapper'
__email__ = 'bmc@clapper.org'
__url__ = 'http://software.clapper.org/paragrep/'
Expand Down Expand Up @@ -234,6 +234,7 @@ def __init__(self):
self.case_blind = False
self.negate = False
self.show_version = False
self.print_eop = False

self.__print_file_name = False
self.__print_file_header = False
Expand Down Expand Up @@ -266,6 +267,17 @@ def _search(self, f, filename=None):
paragraph = []
last_empty = False
found = False
eop_line = None

def print_paragraph(paragraph):
if self.__print_file_header:
print '::::::::::\n%s\n::::::::::\n' % filename
self.__print_file_header = False
print '\n'.join(paragraph)
if self.print_eop and (eop_line is not None):
print eop_line
else:
print ""

for line in f:
if self.eop_regexp.match(line):
Expand All @@ -274,14 +286,20 @@ def _search(self, f, filename=None):
# the end of the paragraph, search the accumulated lines of
# the paragraph.

if line[-1] == '\n':
eop_line = line[:-1]
else:
eop_line = line

if not last_empty:
last_empty = True
found = self._search_paragraph(paragraph, filename)
found = self._search_paragraph(paragraph)
if found:
print_paragraph(paragraph)
paragraph = []

else:
# Save this line in the current paragraph buffer

if line[-1] == '\n':
line = line[:-1]
paragraph += [line]
Expand All @@ -290,35 +308,29 @@ def _search(self, f, filename=None):
# We might have a paragraph left in the buffer. If so, search it.

if not last_empty:
if self._search_paragraph(paragraph, filename):
if self._search_paragraph(paragraph):
found = True
print_paragraph(paragraph)

return found

def _search_paragraph(self, paragraph, filename):
def _search_paragraph(self, paragraph):
found_count_must_be = 1
if self.anding:
# If ANDing, must match ALL the regular expressions.
found_count_must_be = len(self.regexps)

paragraph_as_one_string = ' '.join(paragraph)
if self.case_blind:
paragraph_as_one_string = paragraph_as_one_string.lower()

total_found = 0
for re in self.regexps:
for line in paragraph:
if re.search(line):
total_found += 1
break

if ((total_found == found_count_must_be) and (not self.negate)) or \
((total_found != found_count_must_be) and self.negate):
if (not self.negate) and (total_found >= found_count_must_be):
found = True
elif self.negate and (total_found != found_count_must_be):
found = True
if self.__print_file_header:
print '::::::::::\n%s\n::::::::::\n' % filename
self.__print_file_header = False
print '\n'.join(paragraph) + '\n'
else:
found = False

Expand Down Expand Up @@ -370,6 +382,10 @@ def _parse_params(paragrepper, argv):
dest='eop_regexp', default=r'^\s*$', metavar='eop_regexp',
help=r'Specify an alternate regular expression ' \
'to match end-of-paragraph. Default: %default')
parser.add_option('-P', '--print-eop', action='store_true',
dest='print_eop', default=False, metavar='print_eop',
help=r'Print the line that marks the end of each. ' \
'paragraph. Default: %default')
parser.add_option('-v', '--negate', action='store_true', dest='negate',
help='Negate the sense of the match.')

Expand All @@ -380,6 +396,7 @@ def _parse_params(paragrepper, argv):
paragrepper.anding = options.anding
paragrepper.case_blind = options.caseblind
paragrepper.negate = options.negate
paragrepper.print_eop = options.print_eop

# Figure out where to get the regular expressions to find.

Expand Down

0 comments on commit bad8bee

Please sign in to comment.