# Path: bibstuff/bibgrammar.py
# def Parse(src, processor=None) :
# 
# Path: bibstuff/bibstyles/shared.py
# def reformat_para(para='', left=0, right=72, just='LEFT'):
# 	"""Simple paragraph reformatter.  Allows specification
# 	of left and right margins, and of justification style
# 	(using constants defined in module).
# 	:note: Adopted by Schwilk from David Mertz's example in TPiP
# 	:see:  Mertz, David,  *Text Processing in Python* (TPiP)
# 	"""
# 	LEFT, RIGHT, CENTER = 'LEFT', 'RIGHT', 'CENTER'
# 	words = para.split()
# 	lines = []
# 	line  = ''
# 	word = 0
# 	end_words = 0
# 	while not end_words:
# 		if len(words[word]) > right-left: # Handle very long words
# 			line = words[word]
# 			word +=1
# 			if word >= len(words):
# 				end_words = 1
# 		else:							 # Compose line of words
# 			while len(line)+len(words[word]) <= right-left:
# 				line += words[word]+' '
# 				word += 1
# 				if word >= len(words):
# 					end_words = 1
# 					break
# 		lines.append(line)
# 		line = ''
# 	if just.upper() == CENTER:
# 		r, l = right, left
# 		return '\n'.join([' '*left+ln.center(r-l) for ln in lines])
# 	elif just.upper() == RIGHT:
# 		return '\n'.join([line.rjust(right) for line in lines])
# 	else: # left justify
# 		return '\n'.join([' '*left+line for line in lines])


import re
import sys
import logging
	import sys
from simpleparse.dispatchprocessor import dispatch, DispatchProcessor, getString, lines
from bibstuff import bibgrammar
from bibstuff.bibstyles.shared import reformat_para
		from bibstuff import bibname #ai: shd move all bibname into here? possibly
		  `string_or_compiled` : string to compile or compiled regex
		    pattern for searching
		  `field` : string
		    field to search in self (default: search all fields)
		"""
		if isinstance(string_or_compiled, str):
			if ignore_case:
				reo = re.compile(string_or_compiled, re.MULTILINE | re.IGNORECASE)
			else:
				reo = re.compile(string_or_compiled, re.MULTILINE)
		else: #->must have a compiled regular expression
			reo = string_or_compiled
		"""
		Find regex in bib_entry.
		If field is omitted, search is through all fields.
		
		:note: used by bibsearch.py
		"""
		ls = [entry for entry in self.entries
			if entry.search_fields(string_or_compiled=reo, field=field, ignore_case=ignore_case)]
		return ls


# self test
# -------------------------
# usage: bibfile.py DATABASE_FILE
if __name__ == "__main__":
	if len(sys.argv) > 1 :
		src = open(sys.argv[1]).read()
		bfile = BibFile()

# Path: bibstuff/bibgrammar.py
# def Parse(src, processor=None) :
# 
# Path: bibstuff/bibstyles/shared.py
# def reformat_para(para='', left=0, right=72, just='LEFT'):
# 	"""Simple paragraph reformatter.  Allows specification
# 	of left and right margins, and of justification style
# 	(using constants defined in module).
# 	:note: Adopted by Schwilk from David Mertz's example in TPiP
# 	:see:  Mertz, David,  *Text Processing in Python* (TPiP)
# 	"""
# 	LEFT, RIGHT, CENTER = 'LEFT', 'RIGHT', 'CENTER'
# 	words = para.split()
# 	lines = []
# 	line  = ''
# 	word = 0
# 	end_words = 0
# 	while not end_words:
# 		if len(words[word]) > right-left: # Handle very long words
# 			line = words[word]
# 			word +=1
# 			if word >= len(words):
# 				end_words = 1
# 		else:							 # Compose line of words
# 			while len(line)+len(words[word]) <= right-left:
# 				line += words[word]+' '
# 				word += 1
# 				if word >= len(words):
# 					end_words = 1
# 					break
# 		lines.append(line)
# 		line = ''
# 	if just.upper() == CENTER:
# 		r, l = right, left
# 		return '\n'.join([' '*left+ln.center(r-l) for ln in lines])
# 	elif just.upper() == RIGHT:
# 		return '\n'.join([line.rjust(right) for line in lines])
# 	else: # left justify
# 		return '\n'.join([' '*left+line for line in lines])


# Path: bibstuff/bibfile.py
import re
import sys
import logging
	import sys
from simpleparse.dispatchprocessor import dispatch, DispatchProcessor, getString, lines
from bibstuff import bibgrammar
from bibstuff.bibstyles.shared import reformat_para
		from bibstuff import bibname #ai: shd move all bibname into here? possibly

		  `string_or_compiled` : string to compile or compiled regex
		    pattern for searching
		  `field` : string
		    field to search in self (default: search all fields)
		"""
		if isinstance(string_or_compiled, str):
			if ignore_case:
				reo = re.compile(string_or_compiled, re.MULTILINE | re.IGNORECASE)
			else:
				reo = re.compile(string_or_compiled, re.MULTILINE)
		else: #->must have a compiled regular expression
			reo = string_or_compiled
		"""
		Find regex in bib_entry.
		If field is omitted, search is through all fields.
		
		:note: used by bibsearch.py
		"""
		ls = [entry for entry in self.entries
			if entry.search_fields(string_or_compiled=reo, field=field, ignore_case=ignore_case)]
		return ls


# self test
# -------------------------
# usage: bibfile.py DATABASE_FILE
if __name__ == "__main__":
	if len(sys.argv) > 1 :
		src = open(sys.argv[1]).read()
		bfile = BibFile()

		bibgrammar.Parse(src, bfile)