def treeSel_changed_cb(self, widget, user_data=None): # Find info for this item # Get Selected Item (modelNames, iterNames) = self.treeSel.get_selected() # Item Selected? if iterNames is None: self.selname = '' self.selindex = -1 self.selitem = None return None # Get Value of Selected Item itmValue = modelNames.get_value(iterNames, 0) # Set selected name/item (makes some code shorter later) self.selname = amutil.trim_markup(itmValue) self.selitem = self.get_item(self.selname) # get index for this item for itmindex in range(0, len(self.lst_data)): # Found value in main data list? if amutil.trim_markup(itmValue) == self.lst_data[itmindex].name: # Set selected index self.selindex = itmindex # Retrieve command cmdlst = self.selitem.cmd self.txtCommand_clear() # Build command string scmd = '\n'.join(cmdlst) # Set to textbox self.txtCommand_settext(scmd) # Is item exported? sexport = self.selitem.exported.lower() self.chkExport.set_active(sexport in ('yes', 'new')) # Set Export sensitive if its a function (if command is more than 1 # line) self.chkExport.set_sensitive(len(cmdlst) > 1) # Get comment # scomment = self.lst_data[itmindex].comment scomment = self.selitem.comment self.txtComment_clear() if len(scomment.replace(' ', '')) > 0: self.txtComment_settext(scomment) # Fix status if self.lblStat.get_text() != settings.name: self.stat_settext(settings.name)
def tree_get_index_search(self, parttext, listStore=None): """ retrieves the index of the first item starting with parttext """ if listStore is None: listStore = self.listAliases for i in range(0, len(listStore)): name = amutil.trim_markup(listStore[i][0]) if name.startswith(parttext): return i return -1
def tree_get_index(self, sname, listStore=None): """ retrieve an items index by name """ if listStore is None: listStore = self.listAliases for i in range(0, len(listStore)): # get row i, column 0's text name = listStore[i][0] # probably has pango markup, we need to trim it, if sname == amutil.trim_markup(name): return i # not found return -1
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