def configure_option(self, current_value_install_dir, dest_value_install_dir): print("") #### Load current file as string with open(self.current_value_file_path, "r") as current_value_file: current_file_as_string = current_value_file.read() #### Extract current value, value_from_file_string also checks regex matches once and once only current_value = general.value_from_file_string( regex_match_string=self.regex_match_string, file_as_string=current_file_as_string, file_path=self.current_value_file_path, name=self.name, ) #### Explain to user and get updated value print(self.name + ": " + self.description) print("Current value: " + current_value) input_value = raw_input("Enter desired value, or leave blank to make no change: ") if input_value == "": output_value = current_value else: output_value = input_value if general.prompt_for_confirm('Value will be set to "' + output_value + '", is that correct? '): replacement_string = re.sub(r"\([^\)]*\)", output_value, self.regex_match_string) updated_file_as_str_count_tuple = re.subn( self.regex_match_string, replacement_string, current_file_as_string ) if updated_file_as_str_count_tuple[1] == 1: #### Write updated value to destination path with open(self.output_file_path, "w") as destination_value_file: # destination_value_file.truncate() destination_value_file.write(updated_file_as_str_count_tuple[0]) else: raise Exception( "Error:\n" + self.name + " was expecting just one substitution, but " + updated_file_as_str_count_tuple[1] + " " + "were made" ) else: print("Making no changes to " + self.output_file_path)
def read_and_return_value(self): ''' Loads current value as default and reads value from user, returning it to the caller - the object does not cache it ''' with open(self.current_val_filepath, 'r') as current_value_file: current_file_as_string = current_value_file.read() current_value = general.value_from_file_string( regex_match_string=self.current_val_regex, file_as_string=current_file_as_string, file_path=self.current_val_filepath, name=self.name ) print(self.name + ': ') print(self.desc) new_value = self.__get_value_input(current_value) while not re.match(self.validation_regex, new_value) or not general.prompt_for_confirm('Is this correct?', True): if not re.match(self.validation_regex, new_value): print('Error: ' + new_value + ' does not match validation regex: ' + self.validation_regex) new_value = self.__get_value_input(current_value) return new_value