<s> '''<STR_LIT>''' <EOL> import os <EOL> import cellprofiler . cpmodule as cpm <EOL> import cellprofiler . settings as cps <EOL> from cellprofiler . settings import YES , NO <EOL> A_RENUMBER = "<STR_LIT>" <EOL> A_DELETE = "<STR_LIT>" <EOL> class RenameOrRenumberFiles ( cpm . CPModule ) : <EOL> module_name = "<STR_LIT>" <EOL> category = "<STR_LIT>" <EOL> variable_revision_number = <NUM_LIT:2> <EOL> def create_settings ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . warning = cps . Divider ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . image_name = cps . FileImageNameSubscriber ( <EOL> '<STR_LIT>' , cps . NONE , doc = """<STR_LIT>""" ) <EOL> self . number_characters_prefix = cps . Integer ( <EOL> "<STR_LIT>" , <NUM_LIT:6> , <EOL> minval = <NUM_LIT:0> , doc = """<STR_LIT>""" ) <EOL> self . number_characters_suffix = cps . Integer ( <EOL> "<STR_LIT>" , <NUM_LIT:4> , <EOL> minval = <NUM_LIT:0> , doc = """<STR_LIT>""" ) <EOL> self . action = cps . Choice ( <EOL> "<STR_LIT>" , <EOL> [ A_RENUMBER , A_DELETE ] , doc = """<STR_LIT>""" ) <EOL> self . number_digits = cps . Integer ( <EOL> "<STR_LIT>" , <EOL> <NUM_LIT:4> , minval = <NUM_LIT:0> , doc = """<STR_LIT>""" % globals ( ) ) <EOL> self . wants_text = cps . Binary ( <EOL> "<STR_LIT>" , False , doc = """<STR_LIT>""" % globals ( ) ) <EOL> self . text_to_add = cps . Text ( <EOL> "<STR_LIT>" , "<STR_LIT>" , doc = """<STR_LIT>""" ) <EOL> self . wants_to_replace_spaces = cps . Binary ( <EOL> "<STR_LIT>" , False , doc = """<STR_LIT>""" % globals ( ) ) <EOL> self . space_replacement = cps . Text ( <EOL> "<STR_LIT>" , "<STR_LIT:_>" , doc = """<STR_LIT>""" ) <EOL> def settings ( self ) : <EOL> '''<STR_LIT>''' <EOL> return [ self . image_name , self . number_characters_prefix , <EOL> self . number_characters_suffix , self . action , <EOL> self . number_digits , self . wants_text , self . text_to_add , <EOL> self . wants_to_replace_spaces , self . space_replacement ] <EOL> def visible_settings ( self ) : <EOL> '''<STR_LIT>''' <EOL> result = [ self . warning , self . image_name , self . number_characters_prefix , <EOL> self . number_characters_suffix , self . action ] <EOL> if self . action == A_RENUMBER : <EOL> result += [ self . number_digits ] <EOL> result += [ self . wants_text ] <EOL> if self . wants_text : <EOL> result += [ self . text_to_add ] <EOL> result += [ self . wants_to_replace_spaces ] <EOL> if self . wants_to_replace_spaces : <EOL> result += [ self . space_replacement ] <EOL> return result <EOL> def run ( self , workspace ) : <EOL> '''<STR_LIT>''' <EOL> image_name = self . image_name . value <EOL> m = workspace . measurements <EOL> path = m . get_current_image_measurement ( '<STR_LIT>' % image_name ) <EOL> file_name = m . get_current_image_measurement ( '<STR_LIT>' % image_name ) <EOL> prefix = file_name [ : self . number_characters_prefix . value ] <EOL> if self . number_characters_suffix . value == <NUM_LIT:0> : <EOL> suffix = "<STR_LIT>" <EOL> middle = file_name [ self . number_characters_prefix . value : ] <EOL> else : <EOL> suffix = file_name [ - self . number_characters_suffix . value : ] <EOL> middle = file_name [ self . number_characters_prefix . value : <EOL> - self . number_characters_suffix . value ] <EOL> if self . action == A_RENUMBER : <EOL> if not middle . isdigit ( ) : <EOL> raise ValueError ( <EOL> ( '<STR_LIT>' <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) % <EOL> ( file_name , middle ) ) <EOL> format = '<STR_LIT>' + str ( self . number_digits . value ) + '<STR_LIT:d>' <EOL> middle = format % int ( middle ) <EOL> elif self . action == A_DELETE : <EOL> middle = "<STR_LIT>" <EOL> else : <EOL> raise NotImplementedError ( "<STR_LIT>" % self . action . value ) <EOL> if self . wants_text : <EOL> middle += self . text_to_add . value <EOL> new_file_name = prefix + middle + suffix <EOL> if self . wants_to_replace_spaces : <EOL> new_file_name = new_file_name . replace ( <EOL> "<STR_LIT:U+0020>" , self . space_replacement . value ) <EOL> if self . show_window : <EOL> workspace . display_data . old_file_name = file_name <EOL> workspace . display_data . new_file_name = new_file_name <EOL> if workspace . pipeline . test_mode : <EOL> return <EOL> from bioformats . formatreader import clear_image_reader_cache <EOL> clear_image_reader_cache ( ) <EOL> os . rename ( os . path . join ( path , file_name ) , <EOL> os . path . join ( path , new_file_name ) ) <EOL> def display ( self , workspace , figure ) : <EOL> '''<STR_LIT>''' <EOL> figure . set_subplots ( ( <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> if workspace . pipeline . test_mode : <EOL> figure . subplot_table ( <EOL> <NUM_LIT:0> , <NUM_LIT:0> , [ [ "<STR_LIT>" ] ] ) <EOL> else : <EOL> statistics = [ ( workspace . display_data . old_file_name , <EOL> workspace . display_data . new_file_name ) ] <EOL> figure . subplot_table ( <EOL> <NUM_LIT:0> , <NUM_LIT:0> , statistics , col_labels = ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def validate_module_warnings ( self , pipeline ) : <EOL> '''<STR_LIT>''' <EOL> if pipeline . test_mode : <EOL> raise cps . ValidationError ( <EOL> "<STR_LIT>" , <EOL> self . image_name ) <EOL> def upgrade_settings ( self , setting_values , variable_revision_number , <EOL> module_name , from_matlab ) : <EOL> '''<STR_LIT>''' <EOL> if from_matlab and variable_revision_number == <NUM_LIT:1> : <EOL> image_name , number_characters_prefix , number_characters_suffix , text_to_add , number_digits = setting_values <EOL> wants_text = text_to_add . lower ( ) != cps . DO_NOT_USE . lower ( ) <EOL> wants_text = cps . YES if wants_text else cps . NO <EOL> if number_digits . lower ( ) == cps . DO_NOT_USE . lower ( ) : <EOL> number_digits = <NUM_LIT:4> <EOL> action = A_DELETE <EOL> else : <EOL> action = A_RENUMBER <EOL> setting_values = [ image_name , number_characters_prefix , <EOL> number_characters_suffix , action , <EOL> number_digits , wants_text , text_to_add ] <EOL> variable_revision_number = <NUM_LIT:1> <EOL> from_matlab = False <EOL> if ( not from_matlab ) and variable_revision_number == <NUM_LIT:1> : <EOL> setting_values = setting_values + [ cps . NO , "<STR_LIT:_>" ] <EOL> variable_revision_number = <NUM_LIT:2> <EOL> return setting_values , variable_revision_number , from_matlab </s>
