def set_new_gender_list (self, file_path) :
     """
     a new gender list file has been selected; load that list
     and send an appropriate update to the UI
     """
     
     # if we have a valid file path with a non-directory at the end of it
     if ( (file_path is not None)     and (file_path != '') and
          (os.path.exists(file_path)) and (not os.path.isdir(file_path)) ) :
         
         # open the file and set our internal gender data accordingly
         genderFile = open(file_path, 'r')
         self.loaded_list_path = file_path
         self.gender_defs, self.gender_orders = swap_util.parse_genderlist_file(genderFile.readlines())
         
         # let the view know that the gender information changed
         self.gui_for_updates.recieveUpdate ( genderListFilePath=self.loaded_list_path,
                                              genderDefinitions=self.gender_defs,
                                              genderOrdering=self.gender_orders,)
         
     else :
         print ("Unable to open file path: " + str(file_path))
예제 #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