def uncomment(self, string_of_line): if isarg(self.args, "-f") and system() == "Darwin": subprocess.call(["sudo", "chmod", "666", self.path]) try: myfile = self.open_file(self.path, "r") data = myfile.readlines() for n, i in enumerate(data): if string_of_line in i: if "#" in i: self.edited = True data[n] = i.replace("#", "") myprint("Uncommenting:", bcolors.BOLD, False) print ("'" + string_of_line[0:10] + "...'\n") else: myprint("String:", bcolors.BOLD, False) print ("'" + string_of_line[0:10] + "...'"), myprint("already uncommented\n", bcolors.BOLD, True) self.edited = False myfile.close() with open(self.path, "w") as f: f.writelines(data) f.close() except IOError: myprint("Failed to uncomment lines", bcolors.FAIL, True) myprint("Program Stopped \n", bcolors.FAIL, True) sys.exit()
def edit_file_with_file(self, find_string, replace_file_path, args, template={}): if isarg(self.args, "-f") and system() == "Darwin": subprocess.call(["sudo", "chmod", "666", self.path]) try: if self.__line_in_file(find_string): myprint("The text from File:", bcolors.BOLD, False) print (replace_file_path), myprint("was already added to: ", bcolors.BOLD, False) print self.path + "\n" self.edited = False else: with self.open_file(replace_file_path, "r") as read_file: with self.open_file(self.path, args) as append_file: append_file.write("\n" + "#The lines below were added by the environment setup script:\n") for line in read_file: if template: line = self.template(line, template) append_file.write(line) append_file.close() read_file.close() myprint("The text from File:", bcolors.BOLD, False) print (replace_file_path), myprint("added to: ", bcolors.BOLD, False) print self.path + "\n" self.edited = True except IOError: myprint("Failed to edit file", bcolors.FAIL, True) myprint("Program Stopped \n", bcolors.FAIL, True) sys.exit()
def append_file_with_string(self, find_string, replace_string): if isarg(self.args, "-f") and system() == "Darwin": subprocess.call(["sudo", "chmod", "666", self.path]) try: if self.__line_in_file(find_string): myprint("String:", bcolors.BOLD, False) print ("'" + replace_string[0:10] + "...'"), myprint("was already added to: ", bcolors.BOLD, False) print self.path + "\n" self.edited = False else: myfile = self.open_file(self.path, "a") myfile.write("\n" + "#The lines below were added by the environment setup script:\n" + replace_string) myfile.close() myprint("String:", bcolors.BOLD, False) print ("'" + replace_string[:10] + "...'"), myprint("added to: ", bcolors.BOLD, False) print self.path + "\n" self.edited = True except IOError: return