diff options
Diffstat (limited to 'portato/backend/flags.py')
-rw-r--r-- | portato/backend/flags.py | 55 |
1 files changed, 28 insertions, 27 deletions
diff --git a/portato/backend/flags.py b/portato/backend/flags.py index 56df231..baa1f37 100644 --- a/portato/backend/flags.py +++ b/portato/backend/flags.py @@ -3,7 +3,7 @@ # File: portato/backend/flags.py # This file is part of the Portato-Project, a graphical portage-frontend. # -# Copyright (C) 2006 René 'Necoro' Neumann +# Copyright (C) 2006-2009 René 'Necoro' Neumann # This is free software. You may redistribute copies of it under the terms of # the GNU General Public License version 2. # There is NO WARRANTY, to the extent permitted by law. @@ -203,10 +203,10 @@ def sort_use_flag_list (flaglist): """ def flag_key (flag): - if flag[0] in "+-": - return flag[1:] - else: - return flag + if flag[0] in "+-": + return flag[1:] + else: + return flag flaglist.sort(key = flag_key) return flaglist @@ -262,7 +262,7 @@ def set_use_flag (pkg, flag): if pkg.matches(crit): # we have the inverted flag in the uselist/newuselist --> delete it if invFlag in flags or (file, line, invFlag, False) in newUseFlags[cpv] or (file, line, flag, True) in newUseFlags[cpv]: - if added: del newUseFlags[-1] # we currently added it as an extra option - delete it + if added: del newUseFlags[cpv][-1] # we currently added it as an extra option - delete it added = True jumpOut = False for t in ((file, line, invFlag, False),(file, line, flag, True)): @@ -343,22 +343,22 @@ def write_use_flags (): """This writes our changed useflags into the file.""" global newUseFlags, useFlags + def combine (list): + """Shortcut for reverting the list into a string.""" + return " ".join(list)+"\n" + def insert (flag, list): """Shortcut for inserting a new flag right after the package-name.""" list.insert(1,flag) def remove (flag, list): """Removes a flag.""" - try: - list.remove(flag) - except ValueError: # flag is given as flag\n - list.remove(flag+"\n") - list.append("\n") #re-insert the newline + list.remove(flag) # no more flags there - comment it out - if len(list) == 1 or list[1][0] in ("#","\n"): + if len(list) == 1 or list[1][0] == "#": list[0] = "#"+list[0] - insert("#removed by portato#",list) + list.append("#removed by portato#") file_cache = {} # cache for having to read the file only once: name->[lines] for cpv in newUseFlags: @@ -381,14 +381,14 @@ def write_use_flags (): while i < line: # stop at the given line lines.append(f.readline()) i += 1 - l = f.readline().split(" ") + l = f.readline().split() # delete or insert if delete: remove(flag,l) else: insert(flag,l) - lines.append(" ".join(l)) + lines.append(combine(l)) # read the rest lines.extend(f.readlines()) @@ -396,33 +396,34 @@ def write_use_flags (): file_cache[file] = lines else: # in cache - l = file_cache[file][line-1].split(" ") + l = file_cache[file][line-1].split() if delete: remove(flag, l) else: - insert(flag,l) - file_cache[file][line-1] = " ".join(l) + insert(flag, l) + file_cache[file][line-1] = combine(l) if flagsToAdd: # write new lines msg = "\n#portato update#\n" + comb = combine(flagsToAdd) if CONFIG["usePerVersion"]: # add on a per-version-base - msg += "=%s %s\n" % (cpv, ' '.join(flagsToAdd)) + msg += "=%s %s" % (cpv, comb) else: # add on a per-package-base list = system.split_cpv(cpv) - msg += "%s/%s %s\n" % (list[0], list[1], ' '.join(flagsToAdd)) + msg += "%s/%s %s" % (list[0], list[1], combine) + if not file in file_cache: - f = open(file, "a") - f.write(msg) - f.close() + with open(file, "a") as f: + f.write(msg) else: file_cache[file].append(msg) # write to disk - for file in file_cache.keys(): - f = open(file, "w") - f.writelines(file_cache[file]) - f.close() + for file in file_cache: + with open(file, "w") as f: + f.writelines(file_cache[file]) + # reset useFlags = {} newUseFlags = {} |