Added sources files saving + misc

- source files can be saved to be replayed later
- gcp files are now in ~/.gcp (directory is created if needed)
- fixed an encoding error in a warning
This commit is contained in:
Goffi 2010-09-28 15:42:47 +08:00
parent 42efd0d775
commit fdea18077f
1 changed files with 57 additions and 2 deletions

59
gcp
View File

@ -72,7 +72,9 @@ const_DBUS_INTERFACE = "org.goffi.gcp"
const_DBUS_PATH = "/org/goffi/gcp"
const_BUFF_SIZE = 4096
const_PRESERVE = set(['mode','ownership','timestamps'])
const_JOURNAL_PATH = "~/.gcp_journal"
const_FILES_DIR = "~/.gcp"
const_JOURNAL_PATH = const_FILES_DIR + "/journal"
const_SAVED_LIST = const_FILES_DIR + "/saved_list"
class DbusObject(dbus.service.Object):
@ -140,6 +142,9 @@ class Journal():
class GCP():
def __init__(self):
files_dir = os.path.expanduser(const_FILES_DIR)
if not os.path.exists(files_dir):
os.makedirs(files_dir)
try:
sessions_bus = dbus.SessionBus()
db_object = sessions_bus.get_object(const_DBUS_INTERFACE,
@ -245,7 +250,7 @@ class GCP():
if os.path.isdir(abspath):
full_dest_path = dest_path if os.path.isabs(path) else os.path.normpath(os.path.join(dest_path, path))
if not options.recursive:
warning (_('omitting directory "%s"') % abspath)
warning (_('omitting directory "%s"') % abspath.decode('utf-8','replace'))
else:
self.__appendDirToList(abspath, full_dest_path, options)
else:
@ -437,6 +442,21 @@ class GCP():
parser.add_option("--preserve", action="store", default='mode,ownership,timestamps',
help=_("preserve the specified attributes"))
parser.add_option("--save", action="store",
help=_("Save source arguments"))
parser.add_option("--force-save", action="store",
help=_("Save source arguments and replace memory if it already exists"))
parser.add_option("--load", action="store",
help=_("Load source arguments"))
parser.add_option("--list", action="store_true", default=False,
help=_("List names of saved sources"))
parser.add_option("--full-list", action="store_true", default=False,
help=_("List names of saved sources and files in it"))
parser.add_option("--no-unicode-fix", action="store_false", dest='unicode_fix', default=True,
help=_("don't fixe name encoding errors")) #TODO
@ -469,6 +489,41 @@ class GCP():
exit(2)
else:
options.preserve = preserve
if options.save or options.load or options.list or options.full_list:
try:
with open(os.path.expanduser(const_SAVED_LIST),'r') as saved_fd:
saved_files = pickle.load(saved_fd)
except:
saved_files={}
if options.list or options.full_list:
info(_('Saved sources:'))
sources = saved_files.keys()
sources.sort()
for source in sources:
info("\t[%s]" % source)
if options.full_list:
for filename in saved_files[source]:
info("\t\t%s" % filename)
info("---\n")
if not args:
exit(0)
if options.save or options.force_save:
if saved_files.has_key(options.save) and not options.force_save:
error(_("There is already a saved sources with this name, skipping --save"))
else:
if len(args)>1:
saved_files[options.save] = map(os.path.abspath,args[:-1])
with open(os.path.expanduser(const_SAVED_LIST),'w') as saved_fd:
pickle.dump(saved_files,saved_fd)
if options.load:
if not saved_files.has_key(options.load):
error(_("No saved sources with this name, check existing names with --list"))
else:
args = saved_files[options.load] + args
#if there is an other instance of gcp, we send options to it
if not self._main_instance: