# Path: Pyblio/Selection.py
# class Selection:
#     ''' This class provides iterators that implement constraints (like
#     sorting and searching) on another iterator '''
#     
#     def __init__ (self, search = None, sort = None):
#         self.search = search
#         self.sort   = sort
#         return
#     
#     def iterator (self, iterator):
#         ''' returns an iterator that will provide the entries of the
#         database according to the properties of the current Selection
#         '''
# 
#         # no filtering ? then pass through
#         if not self.search and not self.sort:
#             return iterator
# 
#         if not self.sort:
#             return SearchSelection (self.search, iterator)
# 
#         if not self.search:
#             return SortSelection (self.sort, iterator)
# 
#         return SortSelection (self.sort,
#                               SearchSelection (self.search, iterator))
# 
# Path: Pyblio/Sort.py
# class Sort:
#     ''' This class defines the methods used to sort a database '''
# 
#     def __init__ (self, fields = None):
#         ''' Create a Sort class with a given set of SortFields '''
# 
#         self.fields = fields or []
#         return
# 
#     def sort (self, iterator):
#         ''' Returns a list of keys sorted according to the current
#         sort settings '''
# 
#         self.base = iterator.base
# 
#         S = []
#         extractors = [f.get_extractor() for f in self.fields]
#         for e in iterator:
#             s = []
#             for f in extractors:
#                 s.extend (f (e))
# 	    s.append (e)
#             S.append (s)
#         S.sort ()
#         result = [x [-1] for x in S]
#         return result
# 
#     def __repr__ (self):
#         return 'Sort (%s)' % str (self.fields)
# 
# Path: Pyblio/Open.py
# def bibopen (entity, how = None):
#     ''' Generic function to open a bibliographic database '''
# 
#     def simple_try (url, how):
# 	# url is Fields.URL instance, only to be passed to opener
# 	base = None
# 
# 	if how == None:
# 	    listedmethods = Autoload.available ("format")
# 
# 	    for method in listedmethods:
# 		opener = get_by_name (method, 'open')
# 		if opener:
# 		    base = opener (url, 1)
# 		    if base is not None:
# 			return base
# 	    return None
# 
# 	opener = get_by_name (how, 'open')
# 
# 	if opener:
# 	    base = opener (url, 0)
# 	else:
# 	    raise Exceptions.FormatError (_(u"method “%s” provides no opener") % how)
# 
# 	return base
# 
#     # Consider the reference as an URL: url is an Fields.URL instance
#     url = Fields.URL (entity)
# 
#     if url.url [0] == 'file' and not os.path.exists (url.url [2]):
# 	raise Exceptions.FileError (_(u"File “%s” does not exist") % url.get_url ())
# 
#     # eventually load a new module
#     if how is None:
# 	handler = Autoload.get_by_regexp ("format", url.get_url ())
# 	if handler:
# 	    how = handler.name
# 
#     base = simple_try (url, how)
# 
#     if base is None:
# 	raise Exceptions.FormatError (_(u"don’t know how to open “%s”") % entity)
# 
#     return base
# 
# def bibwrite (iter, out = None, how = None, database=None):
#     ''' writes a descriptions of a list of entries '''
# 
#     if database and Config.get ('bibtex/keep-preamble').data:
# 	preamble = database.get_metadata ('bibtex-preamble', [])
#     else:
# 	preamble = []
# 	
#     # default output
#     out = out or sys.stdout
# 
#     if how == None:
# 	entry = iter.first ()
# 	while entry:
# 	    out.write (str (entry) + "\n")
# 	    entry = iter.next ()
# 
# 	return
# 
#     writer = get_by_name (how, 'write')
# 
#     if writer is None:
# 	raise IOError, "type `%s' does not specify write method" % how
#     writer (iter, out, preamble=preamble)
#     return


import sys
from Pyblio import Selection, Sort
from Pyblio.Open import bibopen, bibwrite

how = sys.argv [2]

if how == '': how = None

a = bibopen (sys.argv [3], how)

f = open (sys.argv [4], 'w')

# write out in the key order
sel = Selection.Selection (sort = Sort.Sort ([Sort.KeySort ()]))
it  = sel.iterator (a.iterator ())


# Path: Pyblio/Selection.py
# class Selection:
#     ''' This class provides iterators that implement constraints (like
#     sorting and searching) on another iterator '''
#     
#     def __init__ (self, search = None, sort = None):
#         self.search = search
#         self.sort   = sort
#         return
#     
#     def iterator (self, iterator):
#         ''' returns an iterator that will provide the entries of the
#         database according to the properties of the current Selection
#         '''
# 
#         # no filtering ? then pass through
#         if not self.search and not self.sort:
#             return iterator
# 
#         if not self.sort:
#             return SearchSelection (self.search, iterator)
# 
#         if not self.search:
#             return SortSelection (self.sort, iterator)
# 
#         return SortSelection (self.sort,
#                               SearchSelection (self.search, iterator))
# 
# Path: Pyblio/Sort.py
# class Sort:
#     ''' This class defines the methods used to sort a database '''
# 
#     def __init__ (self, fields = None):
#         ''' Create a Sort class with a given set of SortFields '''
# 
#         self.fields = fields or []
#         return
# 
#     def sort (self, iterator):
#         ''' Returns a list of keys sorted according to the current
#         sort settings '''
# 
#         self.base = iterator.base
# 
#         S = []
#         extractors = [f.get_extractor() for f in self.fields]
#         for e in iterator:
#             s = []
#             for f in extractors:
#                 s.extend (f (e))
# 	    s.append (e)
#             S.append (s)
#         S.sort ()
#         result = [x [-1] for x in S]
#         return result
# 
#     def __repr__ (self):
#         return 'Sort (%s)' % str (self.fields)
# 
# Path: Pyblio/Open.py
# def bibopen (entity, how = None):
#     ''' Generic function to open a bibliographic database '''
# 
#     def simple_try (url, how):
# 	# url is Fields.URL instance, only to be passed to opener
# 	base = None
# 
# 	if how == None:
# 	    listedmethods = Autoload.available ("format")
# 
# 	    for method in listedmethods:
# 		opener = get_by_name (method, 'open')
# 		if opener:
# 		    base = opener (url, 1)
# 		    if base is not None:
# 			return base
# 	    return None
# 
# 	opener = get_by_name (how, 'open')
# 
# 	if opener:
# 	    base = opener (url, 0)
# 	else:
# 	    raise Exceptions.FormatError (_(u"method “%s” provides no opener") % how)
# 
# 	return base
# 
#     # Consider the reference as an URL: url is an Fields.URL instance
#     url = Fields.URL (entity)
# 
#     if url.url [0] == 'file' and not os.path.exists (url.url [2]):
# 	raise Exceptions.FileError (_(u"File “%s” does not exist") % url.get_url ())
# 
#     # eventually load a new module
#     if how is None:
# 	handler = Autoload.get_by_regexp ("format", url.get_url ())
# 	if handler:
# 	    how = handler.name
# 
#     base = simple_try (url, how)
# 
#     if base is None:
# 	raise Exceptions.FormatError (_(u"don’t know how to open “%s”") % entity)
# 
#     return base
# 
# def bibwrite (iter, out = None, how = None, database=None):
#     ''' writes a descriptions of a list of entries '''
# 
#     if database and Config.get ('bibtex/keep-preamble').data:
# 	preamble = database.get_metadata ('bibtex-preamble', [])
#     else:
# 	preamble = []
# 	
#     # default output
#     out = out or sys.stdout
# 
#     if how == None:
# 	entry = iter.first ()
# 	while entry:
# 	    out.write (str (entry) + "\n")
# 	    entry = iter.next ()
# 
# 	return
# 
#     writer = get_by_name (how, 'write')
# 
#     if writer is None:
# 	raise IOError, "type `%s' does not specify write method" % how
#     writer (iter, out, preamble=preamble)
#     return


# Path: tests/import-export.py
import sys
from Pyblio import Selection, Sort
from Pyblio.Open import bibopen, bibwrite


how = sys.argv [2]

if how == '': how = None

a = bibopen (sys.argv [3], how)

f = open (sys.argv [4], 'w')

# write out in the key order
sel = Selection.Selection (sort = Sort.Sort ([Sort.KeySort ()]))
it  = sel.iterator (a.iterator ())


bibwrite (it, out = f, how = a.id)