def mnuIntegrate_button_release_event_cb( self, widget, signal_id, data=None): if not self.ensure_bashrc(): return None aliasfile = settings.get('aliasfile') if integrator.helper_checkfile(aliasfile): dlg.msgbox('File is already integrated.') return None if self.file_integrate_msg( integrator.helper_addfile(aliasfile), successmsg='File was integrated.', failuremsg='Failed to integrate file into bashrc!'): # go ahead and check file mode, chmod +x if needed. # (not everyone remembers they have to do this, # they will want 'Integrate' to 'just work') amutil.chmod_file(aliasfile)
def save_file(self, sfilename=None): # noqa if sfilename is None: sfilename = settings.get("aliasfile") self.printlog("save_file: saving to: " + sfilename) # Setup shell script header = """#!/bin/bash # Generated by {settings.name} {settings.version} # -Christopher Welborn # Note to user: # If you must edit this file manually please stick to this style: # Use tabs, not spaces. # No tabs before definitons. # Seperate lines for curly braces. # Use 1 tab depth for start of code block in functions. # Function description is first comment in code block. # Alias description is comment right side of alias definition. # # ...if you use a different style it may or may not # break the program and I can't help you. """.format(settings=settings) # Write Definitions, Cycle thru main data list # lst_lines.append("\n# Definitions:\n") # List for aliases (put'em at the top of the file) lst_aliases = [] # List for functions (put'em after aliases) lst_functions = [] # List for exports (put at bottom of file) lst_exports = [] # Cycle thru items in treeAliases (alphabetical already) # This extra step makes the functions print in alphbetical order # ...aliases and exports don't need this method, a simple sorted(set()) # works because they are single lines. multiline functions can't be # sorted like that. treeitm = self.listAliases.get_iter_first() while (treeitm is not None): # Get value for this item treeval = amutil.trim_markup( self.listAliases.get_value(treeitm, 0)) # try to get next item treeitm = self.listAliases.iter_next(treeitm) # Search data list for this item, and save data to appropriate # list. for itm in self.lst_data: # Name for alias/function sname = itm.name # Match tree name with data name, and retrieve it's info... if treeval == sname: # Set type based on command length if len(itm.cmd) > 1: bfunction = True else: bfunction = False # Comments? scomment = itm.comment # Append comment char if needed if scomment and (not scomment.startswith('#')): scomment = ' '.join(('#', scomment)) # WRITE FUNCTION ITEM if bfunction: lst_functions.append('\nfunction {}()\n'.format(sname)) lst_functions.append('{\n') # comment given? if scomment: lst_functions.append('\t{}\n'.format(scomment)) # Write command section for scmdline in itm.cmd: lst_functions.append('\t{}\n'.format(scmdline)) lst_functions.append('}\n') else: # WRITE ALIAS ITEM # Fix quotes around command if '"' in itm.cmd[0]: scmd = "'" + itm.cmd[0] + "'" else: scmd = '"' + itm.cmd[0] + '"' # Comment given? if len(scomment) > 0: scmd = ' '.join((scmd, scomment)) lst_aliases.append('alias {}={}\n'.format(sname, scmd)) # Save Exports, cycle thru main data list # lst_lines.append("\n# Exports\n") for itm in self.lst_data: if (itm.exported.lower() == 'yes'): lst_exports.append('export {}\n'.format(itm.name)) # Sort lines lst_aliases = sorted(set(lst_aliases)) lst_exports = sorted(set(lst_exports)) # Write file # save temp file to home dir aliasfiletmp = os.path.join(integrator.home, '.aliasmanager.tmp') btmp = False # @UnusedVariable with open(aliasfiletmp, 'w') as fwrite: fwrite.write(header) fwrite.write("\n\n# Aliases:\n") fwrite.writelines(lst_aliases) fwrite.write("\n# Functions:") fwrite.writelines(lst_functions) fwrite.write("\n# Exports:\n") fwrite.writelines(lst_exports) # add new line because its a shell script, needs to end with \n fwrite.write('\n') # self.printlog("Temporary file written: " + aliasfiletmp) btmp = True if not btmp: self.stat_text('Unable to write temp file: {}'.format( amutil.filename_safe(aliasfiletmp))) self.printlog('Unable to write temp file: {}'.format( aliasfiletmp)) # Temp file didn't write, there is no reason to continue. return False try: # Backup destination file if it doesn't exist backupfile = '{}~'.format(sfilename) if (os.path.isfile(sfilename) and (not os.path.isfile(backupfile))): backupcmd = 'cp {} {}'.format(sfilename, backupfile) os.system(backupcmd) self.printlog('Backup created.') # Copy temp file to destination,,, copycmd = 'cp {} {}'.format(aliasfiletmp, sfilename) os.system(copycmd) except Exception as ex: self.stat_settext('Unable to copy to destination: {}'.format( amutil.filename_safe(sfilename))) self.printlog('Unable to copy to destination!') self.printlog('Error: {}'.format(ex)) return False dlg.msgbox( 'Unable to copy to destination: {}'.format( amutil.filename_safe(sfilename)), dlg.error) self.printlog('Temp file copied to destination: {}'.format(sfilename)) # chmod +x if needed schmod_result = amutil.chmod_file(sfilename) self.printlog(schmod_result) # Success return True