Back to index

python-biopython  1.60
__init__.py
Go to the documentation of this file.
00001 # Copyright 2007 by Tiago Antao <tiagoantao@gmail.com>.  All rights reserved.
00002 # This code is part of the Biopython distribution and governed by its
00003 # license.  Please see the LICENSE file that should have been included
00004 # as part of this package.
00005 
00006 
00007 '''
00008 Support for asynchronous execution.
00009 
00010 '''
00011 
00012 import os
00013 import thread
00014 
00015 
00016 class Async(object):
00017     '''Abstract Asynchronous execution class.
00018 
00019        This is the top abstract class.
00020        Concrete classes must implement the _run_program method.
00021     '''
00022 
00023     def __init__(self):
00024         '''Async constructor.
00025 
00026            Initializes the queues, among other things.
00027            Of notice, is the access_ds lock for controlling exclusive
00028                access to this object.
00029         '''
00030         self.running = {}
00031         self.waiting = []
00032         self.done = {}
00033         self.id = 0
00034         self.hooks = {}
00035         self.access_ds = thread.allocate_lock()
00036 
00037     def run_program(self, program, parameters, input_files):
00038         '''Runs a program.
00039         
00040            Real _run_program to be implemented by concrete classes.
00041 
00042            parameters:
00043            program String identifying program.
00044            parameters List of String parameters.
00045            input_files Hash of Input file descriptors.
00046 
00047            returns:
00048            Task Id.
00049 
00050            The input_files hash key is the path that is passed
00051            to the program. It should always be relative.
00052            Value is a stream.
00053         '''
00054         if program in self.hooks:
00055             self.access_ds.acquire()
00056             self.id += 1
00057             id = self.id
00058             self.access_ds.release()
00059             self._run_program(id, self.hooks[program], parameters, input_files)
00060             return id
00061 
00062     def _run_program(self, id, program, parameters, input_files):
00063         """Actually run the program, handled by a subclass (PRIVATE).
00064 
00065         This method should be replaced by any derived class to do
00066         something useful. It will be called by the run_program method.
00067         """
00068         raise NotImplementedError("This object should be subclassed")
00069 
00070     def get_result(self, id):
00071         ''' Returns the results for a certain Id, the info for that Id is
00072             forgotten.
00073 
00074             parameters:
00075             id Id of the task.
00076 
00077             returns:
00078             (return_code, output_files) return code and file access
00079             object.
00080 
00081             The output_files hash key is a relative file name, and the value a
00082             output stream.
00083         '''
00084         self.access_ds.acquire()
00085         if id in self.done:
00086             returnCode, fileObject = self.done[id]
00087             del self.done[id]
00088             self.access_ds.release()
00089         else:
00090             self.access_ds.release()
00091             return None
00092 
00093 class FileRetriever(object):
00094     '''An Abstract Support class to retrieve files.
00095     '''
00096 
00097     def __init__(self):
00098         self.file_list=[]
00099 
00100     def get_File_list(self):
00101         '''Returns the list of available files.
00102         '''
00103         return self.file_list
00104 
00105     def get_file(self, name):
00106         raise NotImplementedError('Abstract method')
00107 
00108 class DirectoryRetriever(FileRetriever):
00109     '''Retrieves a directory content.
00110     '''
00111 
00112     def __init__(self, directory):
00113         FileRetriever.__init__(self)
00114         self.directory = directory
00115         walk_list = os.walk(directory)
00116         for dir, dir_list, file_list in walk_list:
00117             for file in file_list:
00118                 self.file_list.append(file[len(directory)+1:])
00119 
00120     def get_file(self, name):
00121         return open(self.directory + os.sep + name)