def mnuSaveFileAs_button_release_event_cb( self, widget, signal_id, data=None): """ Save alias file using aother filename """ filename = dlg.dialog('Save file as...', dlg.saveas) if not filename: return False if os.path.isfile(filename): resp = dlg.msgbox_yesno( '\n'.join(( 'File exists:\n', filename, 'Would you like to overwrite the file?', )) ) if resp == gtk.RESPONSE_NO: return False # actually save the file if self.save_file(filename): self.stat_settext('Alias file saved as: {}'.format( amutil.filename_safe(filename) )) else: self.stat_settext('Unable to save alias file: {}'.format( amutil.filename_safe(filename) ))
def mnuSaveFile_button_release_event_cb( self, widget, signal_id, data=None): """ Save current alias file """ if self.save_file(): self.stat_settext( 'Alias file saved: {}'.format( amutil.filename_safe(settings.get('aliasfile')) ) ) else: self.printlog( 'Unable to save alias file: {}'.format( amutil.filename_safe(settings.get('aliasfile')) ) ) self.stat_settext('Unable to save alias file!')
def mnuSelFile_button_release_event_cb( self, widget, signal_id, data=None): """ Open alias file """ # Select new alias file sfile = dlg.dialog('Select an alias file to use:') if os.path.isfile(sfile): settings.setsave('aliasfile', sfile) self.stat_settext( 'Alias file: {}'.format(amutil.filename_safe(sfile)) ) self.printlog( 'Selected alias file: {}'.format(settings.get('aliasfile')) ) # reload aliases from this file self.load_aliases(True)
def mnuNewFile_button_release_event_cb( self, widget, signal_id, data=None): """ Creates a new, blank alias file to use/edit """ filename = dlg.dialog('Select a name for the new file...', dlg.saveas) if not filename: return False # Create a new blank file... if amutil.create_blank_file(filename, True): settings.setsave('aliasfile', filename) self.set_filename(filename) self.load_aliases(True) self.stat_settext( 'Created new alias file: {}'.format( amutil.filename_safe(filename) ) ) else: self.stat_settext('Unable to create new file!') dlg.msgbox( 'Unable to create new file at:\n{}'.format(filename), dlg.error )
def btnReload_clicked_cb(self, widget): self.load_aliases(True) aliasfile = settings.get("aliasfile") self.stat_settext("File reloaded: " + amutil.filename_safe(aliasfile)) self.printlog("Aliases reloaded from 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