comparison gcp @ 17:132042c7abb2

Improved sources files management - sources saving command are now shown in a group in --help and now start with --sources prefix - moving sources saving management in a separate method - added --sources-del
author Goffi <goffi@goffi.org>
date Tue, 28 Sep 2010 16:07:45 +0800
parents b03bd83c1661
children 9d015d756c0f
comparison
equal deleted inserted replaced
16:b03bd83c1661 17:132042c7abb2
29 import gettext 29 import gettext
30 gettext.install('gcp', "i18n", unicode=True) 30 gettext.install('gcp', "i18n", unicode=True)
31 31
32 import sys 32 import sys
33 import os,os.path 33 import os,os.path
34 from optparse import OptionParser #To be replaced by argparse ASAP 34 from optparse import OptionParser, OptionGroup #To be replaced by argparse ASAP
35 import cPickle as pickle 35 import cPickle as pickle
36 try: 36 try:
37 import gobject 37 import gobject
38 #DBus 38 #DBus
39 import dbus, dbus.glib 39 import dbus, dbus.glib
414 try: 414 try:
415 self.pbar.finish() 415 self.pbar.finish()
416 except AttributeError: 416 except AttributeError:
417 pass 417 pass
418 418
419 def __sourcesSaving(self,options,args):
420 """Manage saving/loading/deleting etc of sources files
421 @param options: options as parsed from command line
422 @param args: args parsed from command line"""
423 if options.sources_save or options.sources_load\
424 or options.sources_list or options.sources_full_list\
425 or options.sources_del:
426 try:
427 with open(os.path.expanduser(const_SAVED_LIST),'r') as saved_fd:
428 saved_files = pickle.load(saved_fd)
429 except:
430 saved_files={}
431
432 if options.sources_del:
433 if not saved_files.has_key(options.sources_del):
434 error(_("No saved sources with this name, check existing names with --sources-list"))
435 else:
436 del saved_files[options.sources_del]
437 with open(os.path.expanduser(const_SAVED_LIST),'w') as saved_fd:
438 pickle.dump(saved_files,saved_fd)
439 if not args:
440 exit(0)
441
442
443 if options.sources_list or options.sources_full_list:
444 info(_('Saved sources:'))
445 sources = saved_files.keys()
446 sources.sort()
447 for source in sources:
448 info("\t[%s]" % source)
449 if options.sources_full_list:
450 for filename in saved_files[source]:
451 info("\t\t%s" % filename)
452 info("---\n")
453 if not args:
454 exit(0)
455
456 if options.sources_save or options.sources_replace:
457 if saved_files.has_key(options.sources_save) and not options.sources_replace:
458 error(_("There is already a saved sources with this name, skipping --sources-save"))
459 else:
460 if len(args)>1:
461 saved_files[options.sources_save] = map(os.path.abspath,args[:-1])
462 with open(os.path.expanduser(const_SAVED_LIST),'w') as saved_fd:
463 pickle.dump(saved_files,saved_fd)
464
465 if options.sources_load:
466 if not saved_files.has_key(options.sources_load):
467 error(_("No saved sources with this name, check existing names with --sources-list"))
468 else:
469 saved_args = saved_files[options.sources_load]
470 saved_args.reverse()
471 for arg in saved_args:
472 args.insert(0,arg)
473
419 def parseArguments(self, full_args=sys.argv[1:], source_path = os.getcwd()): 474 def parseArguments(self, full_args=sys.argv[1:], source_path = os.getcwd()):
420 """Parse arguments and add files to queue 475 """Parse arguments and add files to queue
421 @param full_args: list of arguments strings (without program name) 476 @param full_args: list of arguments strings (without program name)
422 @param source_path: path from where the arguments come, ad given by os.getcwd() 477 @param source_path: path from where the arguments come, ad given by os.getcwd()
423 @return: a tuple (boolean, message) where the boolean is the success of the arguments 478 @return: a tuple (boolean, message) where the boolean is the success of the arguments
440 parser.add_option("-f", "--force", action="store_true", default=False, 495 parser.add_option("-f", "--force", action="store_true", default=False,
441 help=_("force overwriting of existing files")) 496 help=_("force overwriting of existing files"))
442 497
443 parser.add_option("--preserve", action="store", default='mode,ownership,timestamps', 498 parser.add_option("--preserve", action="store", default='mode,ownership,timestamps',
444 help=_("preserve the specified attributes")) 499 help=_("preserve the specified attributes"))
445
446 parser.add_option("--save", action="store",
447 help=_("Save source arguments"))
448
449 parser.add_option("--force-save", action="store",
450 help=_("Save source arguments and replace memory if it already exists"))
451
452 parser.add_option("--load", action="store",
453 help=_("Load source arguments"))
454
455 parser.add_option("--list", action="store_true", default=False,
456 help=_("List names of saved sources"))
457
458 parser.add_option("--full-list", action="store_true", default=False,
459 help=_("List names of saved sources and files in it"))
460 500
461 parser.add_option("--no-unicode-fix", action="store_false", dest='unicode_fix', default=True, 501 parser.add_option("--no-unicode-fix", action="store_false", dest='unicode_fix', default=True,
462 help=_("don't fixe name encoding errors")) #TODO 502 help=_("don't fixe name encoding errors")) #TODO
463 503
464 parser.add_option("--no-fs-fix", action="store_false", dest='fs_fix', default=True, 504 parser.add_option("--no-fs-fix", action="store_false", dest='fs_fix', default=True,
467 parser.add_option("--no-progress", action="store_false", dest="progress", default=True, 507 parser.add_option("--no-progress", action="store_false", dest="progress", default=True,
468 help=_("deactivate progress bar")) 508 help=_("deactivate progress bar"))
469 509
470 parser.add_option("-v", "--verbose", action="store_true", default=False, 510 parser.add_option("-v", "--verbose", action="store_true", default=False,
471 help=_("Show what is currently done")) 511 help=_("Show what is currently done"))
512
513 group_saving = OptionGroup(parser, "sources saving")
514
515 group_saving.add_option("--sources-save", action="store",
516 help=_("Save source arguments"))
517
518 group_saving.add_option("--sources-replace", action="store",
519 help=_("Save source arguments and replace memory if it already exists"))
520
521 group_saving.add_option("--sources-load", action="store",
522 help=_("Load source arguments"))
523
524 group_saving.add_option("--sources-del", action="store",
525 help=_("delete saved sources"))
526
527 group_saving.add_option("--sources-list", action="store_true", default=False,
528 help=_("List names of saved sources"))
529
530 group_saving.add_option("--sources-full-list", action="store_true", default=False,
531 help=_("List names of saved sources and files in it"))
532
533 parser.add_option_group(group_saving)
534
472 535
473 (options, args) = parser.parse_args(full_args) 536 (options, args) = parser.parse_args(full_args)
474 #options check 537 #options check
475 if options.progress and not pbar_available: 538 if options.progress and not pbar_available:
476 warning (_("Progress bar is not available, deactivating")) 539 warning (_("Progress bar is not available, deactivating"))
488 error('- %s' % value) 551 error('- %s' % value)
489 exit(2) 552 exit(2)
490 else: 553 else:
491 options.preserve = preserve 554 options.preserve = preserve
492 555
493 if options.save or options.load or options.list or options.full_list: 556 self.__sourcesSaving(options, args)
494 try:
495 with open(os.path.expanduser(const_SAVED_LIST),'r') as saved_fd:
496 saved_files = pickle.load(saved_fd)
497 except:
498 saved_files={}
499
500 if options.list or options.full_list:
501 info(_('Saved sources:'))
502 sources = saved_files.keys()
503 sources.sort()
504 for source in sources:
505 info("\t[%s]" % source)
506 if options.full_list:
507 for filename in saved_files[source]:
508 info("\t\t%s" % filename)
509 info("---\n")
510 if not args:
511 exit(0)
512
513 if options.save or options.force_save:
514 if saved_files.has_key(options.save) and not options.force_save:
515 error(_("There is already a saved sources with this name, skipping --save"))
516 else:
517 if len(args)>1:
518 saved_files[options.save] = map(os.path.abspath,args[:-1])
519 with open(os.path.expanduser(const_SAVED_LIST),'w') as saved_fd:
520 pickle.dump(saved_files,saved_fd)
521
522 if options.load:
523 if not saved_files.has_key(options.load):
524 error(_("No saved sources with this name, check existing names with --list"))
525 else:
526 args = saved_files[options.load] + args
527 557
528 #if there is an other instance of gcp, we send options to it 558 #if there is an other instance of gcp, we send options to it
529 if not self._main_instance: 559 if not self._main_instance:
530 info (_("There is already one instance of %s running, pluging to it") % NAME_SHORT) 560 info (_("There is already one instance of %s running, pluging to it") % NAME_SHORT)
531 #XXX: we have to serialize data as dbus only accept valid unicode, and filenames 561 #XXX: we have to serialize data as dbus only accept valid unicode, and filenames