def process_files (self) :
     """
     if possible given the current state of the model, process the files
     and save the resulting outputs to disk
     
     in order to process files:
     
         there must be at least one file in the list for processing
         a gender list must be loaded
         an output directory must be set
             the output directory must not be the same as the directory that
             holds any of the files in the list for processing
     
     FUTURE, this method handles errors with printing, move to throwing
     an exception for the gui to handle.
     """
     
     # check the minimum prerequisites for file processing
     if self.check_processing_prereqs() is None:
         return
     
     # check that we have files to process
     if len(self.files_to_process_list) <= 0 :
         print ("No files to process.")
         return
     
     # check if we have an output directory set at all
     if self.output_path is None :
         print ("No output directory is selected.")
         return
     
     # check if the output directory contains any of the input files
     # if so, we don't want to process them!
     for file_path in list(self.files_to_process_list) :
         file_directory = os.path.dirname(file_path)
         if os.path.samefile(self.output_path, file_directory) :
             print ("Unable to output to directory containing input files.")
             return
     
     # at this point we've minimally validated that we can process the files
     for file_path in list(self.files_to_process_list) :
         
         swap_util.process_one_file(file_path,
                                    str(self.output_path),
                                    self.gender_defs,
                                    self.gender_orders,
                                    process_file_names=self.do_process_file_names)
예제 #2
0
 def swap ( ) :
     """swap in specific genders for a set of documents
     
     given information in the form of commandline options:
     
         1. load a gender list document that will define the genders
            of the characters for this run
         2. load the files in the input directory and process genders
            in any sheets that have file types we know how to process
         3. save the resulting gendered documents to the output directory
     """
     
     # make sure we aren't going to overwrite our input files
     if options.outDirectory == options.inputDirectory :
         print ("Input and output directories cannot be the same. "
                + "Please select a different output directory to avoid "
                + "destroying your original sheets.")
         return 1
     
     # make sure we have a gender list to work with
     if options.genderList is None :
         print ("Unable to process files without a gender list document "
                + "defining the character's genders.")
         return 1
     
     print ("Opening and parsing gender list: " + options.genderList)
     genderListFile = open(options.genderList, "r")
     genderDefinitions, genderOrdering = swap_util.parse_genderlist_file(genderListFile.readlines())
     genderListFile.close()
     
     # create the output directory if needed
     if not os.path.exists(options.outDirectory):
         print "Making output directory: " + options.outDirectory
         os.makedirs(options.outDirectory)
     
     # get a list of files in the input directory
     print ("Examining all input character sheets in: " + options.inputDirectory)
     possibleSheets = os.listdir(options.inputDirectory)
     
     # for each file in the input directory...
     for possibleSheet in possibleSheets :
         
         processThisSheet = False
         
         # check to see if it looks like the kind of sheet we're expecting
         print ("----------------------------------")
         print ("Examining file: " + possibleSheet)
         number      = possibleSheet.split('.')[0]
         _, fileType = os.path.splitext(possibleSheet)
         
         # if this really is a number and it represents a character that's
         # in the gender definitions we were given, the name looks good so far
         if number.isdigit() and int(number) in genderDefinitions :
             processThisSheet = True
         # double check that this is a type of file we can process
         processThisSheet = (processThisSheet and
                             (fileType == ".txt" or fileType == ".rtf"))
         #print "fileType: " + fileType
         
         if processThisSheet :
             
             #process_one_file (file_path, output_path, gender_defs, process_file_names=False)
             swap_util.process_one_file(os.path.join(options.inputDirectory, possibleSheet),
                                        options.outDirectory,
                                        genderDefinitions,
                                        genderOrdering,
                                        process_file_names=options.processName)
             
         else :
             print ("File " + possibleSheet
                    + " does not match character sheet name patterns. "
                    + "This file will not be processed.")
     
     return 0