public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-commits] proj/portage:master commit in: pym/portage/emaint/modules/logs/, pym/portage/emaint/
@ 2013-08-02 20:20 Zac Medico
  0 siblings, 0 replies; 2+ messages in thread
From: Zac Medico @ 2013-08-02 20:20 UTC (permalink / raw
  To: gentoo-commits

commit:     42b0a6e898172c375d4d47c3346b25dac2333bf0
Author:     Zac Medico <zmedico <AT> gentoo <DOT> org>
AuthorDate: Fri Aug  2 20:18:11 2013 +0000
Commit:     Zac Medico <zmedico <AT> gentoo <DOT> org>
CommitDate: Fri Aug  2 20:18:11 2013 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/portage.git;a=commit;h=42b0a6e8

emaint: remove callback usage

This will simplify conversion to argparse.

---
 pym/portage/emaint/defaults.py              |  4 +-
 pym/portage/emaint/main.py                  | 60 ++++++++++-------------------
 pym/portage/emaint/modules/logs/__init__.py |  8 +---
 3 files changed, 25 insertions(+), 47 deletions(-)

diff --git a/pym/portage/emaint/defaults.py b/pym/portage/emaint/defaults.py
index d9d83ff..ee0dc11 100644
--- a/pym/portage/emaint/defaults.py
+++ b/pym/portage/emaint/defaults.py
@@ -1,16 +1,18 @@
-# Copyright 2005-2012 Gentoo Foundation
+# Copyright 2005-2013 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
 # parser option data
 CHECK = {"short": "-c", "long": "--check",
 	"help": "Check for problems (a default option for most modules)",
 	'status': "Checking %s for problems",
+	'action': 'store_true',
 	'func': 'check'
 	}
 
 FIX = {"short": "-f", "long": "--fix",
 	"help": "Attempt to fix problems (a default option for most modules)",
 	'status': "Attempting to fix %s",
+	'action': 'store_true',
 	'func': 'fix'
 	}
 

diff --git a/pym/portage/emaint/main.py b/pym/portage/emaint/main.py
index 5775ce1..4b6d666 100644
--- a/pym/portage/emaint/main.py
+++ b/pym/portage/emaint/main.py
@@ -30,36 +30,16 @@ class OptionItem(object):
 		self.help = opt['help']
 		self.status = opt['status']
 		self.func = opt['func']
-		self.action = opt.get('action', "callback")
+		self.action = opt.get('action', 'store')
 		self.type = opt.get('type', None)
 		self.dest = opt.get('dest', None)
-		self.callback = opt.get('callback', self._exclusive)
-		self.callback_kwargs = opt.get('callback_kwargs', {"var":"action"})
-
 
 	def _exclusive(self, option, *args, **kw):
 		"""Generic check for the 2 default options
 		"""
-		var = kw.get("var", None)
-		if var is None:
-			raise ValueError("var not specified to exclusive()")
 		if getattr(self.parser, var, ""):
 			raise OptionValueError("%s and %s are exclusive options"
 				% (getattr(self.parser, var), option))
-		setattr(self.parser, var, str(option))
-
-	def check_action(self, action):
-		"""Checks if 'action' is the same as this option
-
-		@type action: string
-		@param action: the action to compare
-		@rtype: boolean
-		"""
-		if action == self.action:
-			return True
-		elif action == '/'.join([self.short, self.long]):
-			return True
-		return False
 
 
 def usage(module_controller):
@@ -172,24 +152,27 @@ def emaint_main(myargv):
 				parser_options.append(OptionItem(desc[opt], parser))
 	for opt in parser_options:
 		parser.add_option(opt.short, opt.long, help=opt.help, action=opt.action,
-		type=opt.type, dest=opt.dest,
-			callback=opt.callback, callback_kwargs=opt.callback_kwargs)
-
-	parser.action = None
+		type=opt.type, dest=opt.dest)
 
 	(options, args) = parser.parse_args(args=myargv)
-	#print('options', options, '\nargs', args, '\naction', parser.action)
 	if len(args) != 1:
 		parser.error("Incorrect number of arguments")
 	if args[0] not in module_names:
 		parser.error("%s target is not a known target" % args[0])
 
-	if parser.action:
-		action = parser.action
-	else:
-		action = "-c/--check"
-	long_action = action.split('/')[1].lstrip('-')
-	#print("DEBUG: action = ", action, long_action)
+
+	func = status = long_action = None
+	for opt in parser_options:
+		if opt.long == '--check':
+			# Default action
+			status = opt.status
+			func = opt.func
+			long_action = 'check'
+		elif opt.status and getattr(options, opt.long.lstrip("-"), False):
+			status = opt.status
+			func = opt.func
+			long_action = opt.long.lstrip('-')
+			break
 
 	if args[0] == "all":
 		tasks = []
@@ -200,15 +183,12 @@ def emaint_main(myargv):
 	elif long_action in module_controller.get_functions(args[0]):
 		tasks = [module_controller.get_class(args[0] )]
 	else:
-		print("\nERROR: module '%s' does not have option '%s'\n" %(args[0], action))
-		print(module_opts(module_controller, args[0]))
+		portage.util.writemsg(
+			"\nERROR: module '%s' does not have option '--%s'\n\n" %
+			(args[0], long_action), noiselevel=-1)
+		portage.util.writemsg(module_opts(module_controller, args[0]),
+			noiselevel=-1)
 		sys.exit(1)
-	func = status = None
-	for opt in parser_options:
-		if opt.check_action(action):
-			status = opt.status
-			func = opt.func
-			break
 
 	# need to pass the parser options dict to the modules
 	# so they are available if needed.

diff --git a/pym/portage/emaint/modules/logs/__init__.py b/pym/portage/emaint/modules/logs/__init__.py
index 6f62d03..c5204b7 100644
--- a/pym/portage/emaint/modules/logs/__init__.py
+++ b/pym/portage/emaint/modules/logs/__init__.py
@@ -20,17 +20,15 @@ module_spec = {
 					"help": "Cleans out logs more than 7 days old (cleanlogs only)" + \
 								 "   module-options: -t, -p",
 					'status': "Cleaning %s",
-					'func': 'clean'
+					'action': 'store_true',
+					'func': 'clean',
 					},
 				'time': {
 					"short": "-t", "long": "--time",
 					"help": "(cleanlogs only): -t, --time   Delete logs older than NUM of days",
 					'status': "",
-					'action': 'store',
 					'type': 'int',
 					'dest': 'NUM',
-					'callback': None,
-					'callback_kwargs': None,
 					'func': 'clean'
 					},
 				'pretend': {
@@ -39,8 +37,6 @@ module_spec = {
 					'status': "",
 					'action': 'store_true',
 					'dest': 'pretend',
-					'callback': None,
-					'callback_kwargs': None,
 					'func': 'clean'
 					}
 				}


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [gentoo-commits] proj/portage:master commit in: pym/portage/emaint/modules/logs/, pym/portage/emaint/
@ 2013-08-02 21:49 Zac Medico
  0 siblings, 0 replies; 2+ messages in thread
From: Zac Medico @ 2013-08-02 21:49 UTC (permalink / raw
  To: gentoo-commits

commit:     c9a8f7c11183f368c1d046f125c542a986f13357
Author:     Zac Medico <zmedico <AT> gentoo <DOT> org>
AuthorDate: Fri Aug  2 21:48:14 2013 +0000
Commit:     Zac Medico <zmedico <AT> gentoo <DOT> org>
CommitDate: Fri Aug  2 21:48:14 2013 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/portage.git;a=commit;h=c9a8f7c1

emaint: portage.util._argparse

---
 pym/portage/emaint/defaults.py              |  7 ++-
 pym/portage/emaint/main.py                  | 66 ++++++++++++++++++++---------
 pym/portage/emaint/modules/logs/__init__.py |  4 +-
 3 files changed, 54 insertions(+), 23 deletions(-)

diff --git a/pym/portage/emaint/defaults.py b/pym/portage/emaint/defaults.py
index ee0dc11..30f36af 100644
--- a/pym/portage/emaint/defaults.py
+++ b/pym/portage/emaint/defaults.py
@@ -16,5 +16,10 @@ FIX = {"short": "-f", "long": "--fix",
 	'func': 'fix'
 	}
 
+VERSION = {"long": "--version",
+	"help": "show program's version number and exit",
+	'action': 'store_true',
+	}
+
 # parser options
-DEFAULT_OPTIONS = {'check': CHECK, 'fix': FIX}
+DEFAULT_OPTIONS = {'check': CHECK, 'fix': FIX, 'version': VERSION}

diff --git a/pym/portage/emaint/main.py b/pym/portage/emaint/main.py
index 3476397..9f987fa 100644
--- a/pym/portage/emaint/main.py
+++ b/pym/portage/emaint/main.py
@@ -6,34 +6,56 @@ from __future__ import print_function
 
 import sys
 import textwrap
-from optparse import OptionParser
-
 
 import portage
 from portage import os
 from portage.emaint.module import Modules
 from portage.emaint.progress import ProgressBar
 from portage.emaint.defaults import DEFAULT_OPTIONS
+from portage.util._argparse import ArgumentParser
 
 class OptionItem(object):
-	"""class to hold module OptionParser options data
+	"""class to hold module ArgumentParser options data
 	"""
 
-	def __init__(self, opt, parser):
+	def __init__(self, opt):
 		"""
 		@type opt: dictionary
 		@param opt: options parser options
 		"""
-		self.parser = parser
-		self.short = opt['short']
-		self.long = opt['long']
-		self.help = opt['help']
-		self.status = opt['status']
-		self.func = opt['func']
-		self.action = opt.get('action', 'store')
-		self.type = opt.get('type', None)
-		self.dest = opt.get('dest', None)
-
+		self.short = opt.get('short')
+		self.long = opt.get('long')
+		self.help = opt.get('help')
+		self.status = opt.get('status')
+		self.func = opt.get('func')
+		self.action = opt.get('action')
+		self.type = opt.get('type')
+		self.dest = opt.get('dest')
+
+	@property
+	def pargs(self):
+		pargs = []
+		if self.short is not None:
+			pargs.append(self.short)
+		if self.long is not None:
+			pargs.append(self.long)
+		return pargs
+
+	@property
+	def kwargs(self):
+		# Support for keyword arguments varies depending on the action,
+		# so only pass in the keywords that are needed, in order
+		# to avoid a TypeError.
+		kwargs = {}
+		if self.help is not None:
+			kwargs['help'] = self.help
+		if self.action is not None:
+			kwargs['action'] = self.action
+		if self.type is not None:
+			kwargs['type'] = self.type
+		if self.dest is not None:
+			kwargs['dest'] = self.dest
+		return kwargs
 
 def usage(module_controller):
 		_usage = "usage: emaint [options] COMMAND"
@@ -133,21 +155,25 @@ def emaint_main(myargv):
 	module_names.insert(0, "all")
 
 
-	parser = OptionParser(usage=usage(module_controller), version=portage.VERSION)
+	parser = ArgumentParser(usage=usage(module_controller))
 	# add default options
 	parser_options = []
 	for opt in DEFAULT_OPTIONS:
-		parser_options.append(OptionItem(DEFAULT_OPTIONS[opt], parser))
+		parser_options.append(OptionItem(DEFAULT_OPTIONS[opt]))
 	for mod in module_names[1:]:
 		desc = module_controller.get_func_descriptions(mod)
 		if desc:
 			for opt in desc:
-				parser_options.append(OptionItem(desc[opt], parser))
+				parser_options.append(OptionItem(desc[opt]))
 	for opt in parser_options:
-		parser.add_option(opt.short, opt.long, help=opt.help, action=opt.action,
-		type=opt.type, dest=opt.dest)
+		parser.add_argument(*opt.pargs, **opt.kwargs)
+
+	options, args = parser.parse_known_args(args=myargv)
+
+	if options.version:
+		print(portage.VERSION)
+		return os.EX_OK
 
-	(options, args) = parser.parse_args(args=myargv)
 	if len(args) != 1:
 		parser.error("Incorrect number of arguments")
 	if args[0] not in module_names:

diff --git a/pym/portage/emaint/modules/logs/__init__.py b/pym/portage/emaint/modules/logs/__init__.py
index c5204b7..0407efe 100644
--- a/pym/portage/emaint/modules/logs/__init__.py
+++ b/pym/portage/emaint/modules/logs/__init__.py
@@ -1,4 +1,4 @@
-# Copyright 2005-2012 Gentoo Foundation
+# Copyright 2005-2013 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
 """Check and clean old logs in the PORT_LOGDIR.
@@ -27,7 +27,7 @@ module_spec = {
 					"short": "-t", "long": "--time",
 					"help": "(cleanlogs only): -t, --time   Delete logs older than NUM of days",
 					'status': "",
-					'type': 'int',
+					'type': int,
 					'dest': 'NUM',
 					'func': 'clean'
 					},


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2013-08-02 21:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-08-02 20:20 [gentoo-commits] proj/portage:master commit in: pym/portage/emaint/modules/logs/, pym/portage/emaint/ Zac Medico
  -- strict thread matches above, loose matches on Subject: below --
2013-08-02 21:49 Zac Medico

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox