CMS 3D CMS Logo

List of all members | Public Member Functions | Public Attributes | Private Member Functions | Private Attributes
argparse._ActionsContainer Class Reference
Inheritance diagram for argparse._ActionsContainer:
argparse._ArgumentGroup argparse.ArgumentParser argparse._MutuallyExclusiveGroup

Public Member Functions

def __init__ (self, description, prefix_chars, argument_default, conflict_handler)
 
def add_argument (self, args, kwargs)
 
def add_argument_group (self, args, kwargs)
 
def add_mutually_exclusive_group (self, kwargs)
 
def register (self, registry_name, value, object)
 
def set_defaults (self, kwargs)
 

Public Attributes

 argument_default
 
 conflict_handler
 
 description
 
 prefix_chars
 

Private Member Functions

def _add_action (self, action)
 
def _add_container_actions (self, container)
 
def _check_conflict (self, action)
 
def _get_handler (self)
 
def _get_optional_kwargs (self, args, kwargs)
 
def _get_positional_kwargs (self, dest, kwargs)
 
def _handle_conflict_error (self, action, conflicting_actions)
 
def _handle_conflict_resolve (self, action, conflicting_actions)
 
def _pop_action_class (self, kwargs, default=None)
 
def _registry_get (self, registry_name, value, default=None)
 
def _remove_action (self, action)
 

Private Attributes

 _action_groups
 
 _actions
 
 _defaults
 
 _has_negative_number_optionals
 
 _mutually_exclusive_groups
 
 _negative_number_matcher
 
 _option_string_actions
 
 _registries
 

Detailed Description

Definition at line 1161 of file argparse.py.

Constructor & Destructor Documentation

def argparse._ActionsContainer.__init__ (   self,
  description,
  prefix_chars,
  argument_default,
  conflict_handler 
)

Definition at line 1167 of file argparse.py.

1167  conflict_handler):
1168  super(_ActionsContainer, self).__init__()
1169 
1170  self.description = description
1171  self.argument_default = argument_default
1172  self.prefix_chars = prefix_chars
1173  self.conflict_handler = conflict_handler
1174 
1175  # set up registries
1176  self._registries = {}
1177 
1178  # register actions
1179  self.register('action', None, _StoreAction)
1180  self.register('action', 'store', _StoreAction)
1181  self.register('action', 'store_const', _StoreConstAction)
1182  self.register('action', 'store_true', _StoreTrueAction)
1183  self.register('action', 'store_false', _StoreFalseAction)
1184  self.register('action', 'append', _AppendAction)
1185  self.register('action', 'append_const', _AppendConstAction)
1186  self.register('action', 'count', _CountAction)
1187  self.register('action', 'help', _HelpAction)
1188  self.register('action', 'version', _VersionAction)
1189  self.register('action', 'parsers', _SubParsersAction)
1190 
1191  # raise an exception if the conflict handler is invalid
1192  self._get_handler()
1193 
1194  # action storage
1195  self._actions = []
1197 
1198  # groups
1199  self._action_groups = []
1201 
1202  # defaults storage
1203  self._defaults = {}
1204 
1205  # determines whether an "option" looks like a negative number
1206  self._negative_number_matcher = _re.compile(r'^-\d+|-\d*.\d+$')
1207 
1208  # whether or not there are any optionals that look like negative
1209  # numbers -- uses a list so it can be shared and edited
1211 
def __init__(self, description, prefix_chars, argument_default, conflict_handler)
Definition: argparse.py:1167
def register(self, registry_name, value, object)
Definition: argparse.py:1215

Member Function Documentation

def argparse._ActionsContainer._add_action (   self,
  action 
)
private

Definition at line 1277 of file argparse.py.

References argparse._ActionsContainer._check_conflict(), python.rootplot.argparse._ActionsContainer._check_conflict(), argparse._ActionsContainer._has_negative_number_optionals, python.rootplot.argparse._ActionsContainer._has_negative_number_optionals, python.rootplot.argparse._ArgumentGroup._has_negative_number_optionals, argparse._ActionsContainer._option_string_actions, python.rootplot.argparse._ActionsContainer._option_string_actions, and python.rootplot.argparse._ArgumentGroup._option_string_actions.

Referenced by argparse._ActionsContainer._add_container_actions(), and argparse._ActionsContainer.add_argument().

1277  def _add_action(self, action):
1278  # resolve any conflicts
1279  self._check_conflict(action)
1280 
1281  # add to actions list
1282  self._actions.append(action)
1283  action.container = self
1284 
1285  # index the action by any option strings it has
1286  for option_string in action.option_strings:
1287  self._option_string_actions[option_string] = action
1288 
1289  # set the flag if any option strings look like negative numbers
1290  for option_string in action.option_strings:
1291  if self._negative_number_matcher.match(option_string):
1292  if not self._has_negative_number_optionals:
1293  self._has_negative_number_optionals.append(True)
1294 
1295  # return the created action
1296  return action
1297 
def _check_conflict(self, action)
Definition: argparse.py:1414
def _add_action(self, action)
Definition: argparse.py:1277
def argparse._ActionsContainer._add_container_actions (   self,
  container 
)
private

Definition at line 1301 of file argparse.py.

References crabWrapper._, argparse._ActionsContainer._action_groups, python.rootplot.argparse._ActionsContainer._action_groups, argparse._ActionsContainer._add_action(), argparse._ActionsContainer.add_argument_group(), python.rootplot.argparse._ActionsContainer.add_argument_group(), argparse._ActionsContainer.add_mutually_exclusive_group(), and python.rootplot.argparse._ActionsContainer.add_mutually_exclusive_group().

1301  def _add_container_actions(self, container):
1302  # collect groups by titles
1303  title_group_map = {}
1304  for group in self._action_groups:
1305  if group.title in title_group_map:
1306  msg = _('cannot merge actions - two groups are named %r')
1307  raise ValueError(msg % (group.title))
1308  title_group_map[group.title] = group
1309 
1310  # map each action to its group
1311  group_map = {}
1312  for group in container._action_groups:
1313 
1314  # if a group with the title exists, use that, otherwise
1315  # create a new group matching the container's group
1316  if group.title not in title_group_map:
1317  title_group_map[group.title] = self.add_argument_group(
1318  title=group.title,
1319  description=group.description,
1320  conflict_handler=group.conflict_handler)
1321 
1322  # map the actions to their new group
1323  for action in group._group_actions:
1324  group_map[action] = title_group_map[group.title]
1325 
1326  # add container's mutually exclusive groups
1327  # NOTE: if add_mutually_exclusive_group ever gains title= and
1328  # description= then this code will need to be expanded as above
1329  for group in container._mutually_exclusive_groups:
1330  mutex_group = self.add_mutually_exclusive_group(
1331  required=group.required)
1332 
1333  # map the actions to their new mutex group
1334  for action in group._group_actions:
1335  group_map[action] = mutex_group
1336 
1337  # add all actions to this container or their group
1338  for action in container._actions:
1339  group_map.get(action, self)._add_action(action)
1340 
def _add_container_actions(self, container)
Definition: argparse.py:1301
def add_argument_group(self, args, kwargs)
Definition: argparse.py:1267
def add_mutually_exclusive_group(self, kwargs)
Definition: argparse.py:1272
def _add_action(self, action)
Definition: argparse.py:1277
def argparse._ActionsContainer._check_conflict (   self,
  action 
)
private

Definition at line 1414 of file argparse.py.

References argparse._ActionsContainer._get_handler(), python.rootplot.argparse._ActionsContainer._get_handler(), argparse._ActionsContainer._option_string_actions, python.rootplot.argparse._ActionsContainer._option_string_actions, python.rootplot.argparse._ArgumentGroup._option_string_actions, and argparse._ActionsContainer.conflict_handler.

Referenced by argparse._ActionsContainer._add_action().

1414  def _check_conflict(self, action):
1415 
1416  # find all options that conflict with this option
1417  confl_optionals = []
1418  for option_string in action.option_strings:
1419  if option_string in self._option_string_actions:
1420  confl_optional = self._option_string_actions[option_string]
1421  confl_optionals.append((option_string, confl_optional))
1422 
1423  # resolve any conflicts
1424  if confl_optionals:
1425  conflict_handler = self._get_handler()
1426  conflict_handler(action, confl_optionals)
1427 
def _check_conflict(self, action)
Definition: argparse.py:1414
def argparse._ActionsContainer._get_handler (   self)
private

Definition at line 1405 of file argparse.py.

References crabWrapper._, argparse._ActionsContainer.conflict_handler, and python.rootplot.argparse._ActionsContainer.conflict_handler.

Referenced by argparse._ActionsContainer._check_conflict().

1405  def _get_handler(self):
1406  # determine function from conflict handler string
1407  handler_func_name = '_handle_conflict_%s' % self.conflict_handler
1408  try:
1409  return getattr(self, handler_func_name)
1410  except AttributeError:
1411  msg = _('invalid conflict_resolution value: %r')
1412  raise ValueError(msg % self.conflict_handler)
1413 
def argparse._ActionsContainer._get_optional_kwargs (   self,
  args,
  kwargs 
)
private

Definition at line 1357 of file argparse.py.

References crabWrapper._, argparse._set, cmsPerfStripChart.dict, argparse._ActionsContainer.prefix_chars, and python.rootplot.argparse._ActionsContainer.prefix_chars.

Referenced by argparse._ActionsContainer.add_argument().

1357  def _get_optional_kwargs(self, *args, **kwargs):
1358  # determine short and long option strings
1359  option_strings = []
1360  long_option_strings = []
1361  for option_string in args:
1362  # error on one-or-fewer-character option strings
1363  if len(option_string) < 2:
1364  msg = _('invalid option string %r: '
1365  'must be at least two characters long')
1366  raise ValueError(msg % option_string)
1367 
1368  # error on strings that don't start with an appropriate prefix
1369  if not option_string[0] in self.prefix_chars:
1370  msg = _('invalid option string %r: '
1371  'must start with a character %r')
1372  tup = option_string, self.prefix_chars
1373  raise ValueError(msg % tup)
1374 
1375  # error on strings that are all prefix characters
1376  if not (_set(option_string) - _set(self.prefix_chars)):
1377  msg = _('invalid option string %r: '
1378  'must contain characters other than %r')
1379  tup = option_string, self.prefix_chars
1380  raise ValueError(msg % tup)
1381 
1382  # strings starting with two prefix characters are long options
1383  option_strings.append(option_string)
1384  if option_string[0] in self.prefix_chars:
1385  if option_string[1] in self.prefix_chars:
1386  long_option_strings.append(option_string)
1387 
1388  # infer destination, '--foo-bar' -> 'foo_bar' and '-x' -> 'x'
1389  dest = kwargs.pop('dest', None)
1390  if dest is None:
1391  if long_option_strings:
1392  dest_option_string = long_option_strings[0]
1393  else:
1394  dest_option_string = option_strings[0]
1395  dest = dest_option_string.lstrip(self.prefix_chars)
1396  dest = dest.replace('-', '_')
1397 
1398  # return the updated keyword arguments
1399  return dict(kwargs, dest=dest, option_strings=option_strings)
1400 
def _get_optional_kwargs(self, args, kwargs)
Definition: argparse.py:1357
def argparse._ActionsContainer._get_positional_kwargs (   self,
  dest,
  kwargs 
)
private

Definition at line 1341 of file argparse.py.

References crabWrapper._, and cmsPerfStripChart.dict.

Referenced by argparse._ActionsContainer.add_argument().

1341  def _get_positional_kwargs(self, dest, **kwargs):
1342  # make sure required is not specified
1343  if 'required' in kwargs:
1344  msg = _("'required' is an invalid argument for positionals")
1345  raise TypeError(msg)
1346 
1347  # mark positional arguments as required if at least one is
1348  # always required
1349  if kwargs.get('nargs') not in [OPTIONAL, ZERO_OR_MORE]:
1350  kwargs['required'] = True
1351  if kwargs.get('nargs') == ZERO_OR_MORE and 'default' not in kwargs:
1352  kwargs['required'] = True
1353 
1354  # return the keyword arguments with no option strings
1355  return dict(kwargs, dest=dest, option_strings=[])
1356 
def _get_positional_kwargs(self, dest, kwargs)
Definition: argparse.py:1341
def argparse._ActionsContainer._handle_conflict_error (   self,
  action,
  conflicting_actions 
)
private

Definition at line 1428 of file argparse.py.

References crabWrapper._, and join().

1428  def _handle_conflict_error(self, action, conflicting_actions):
1429  message = _('conflicting option string(s): %s')
1430  conflict_string = ', '.join([option_string
1431  for option_string, action
1432  in conflicting_actions])
1433  raise ArgumentError(action, message % conflict_string)
1434 
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
def _handle_conflict_error(self, action, conflicting_actions)
Definition: argparse.py:1428
def argparse._ActionsContainer._handle_conflict_resolve (   self,
  action,
  conflicting_actions 
)
private

Definition at line 1435 of file argparse.py.

1435  def _handle_conflict_resolve(self, action, conflicting_actions):
1436 
1437  # remove all conflicting options
1438  for option_string, action in conflicting_actions:
1439 
1440  # remove the conflicting option
1441  action.option_strings.remove(option_string)
1442  self._option_string_actions.pop(option_string, None)
1443 
1444  # if the option now has no option string, remove it from the
1445  # container holding it
1446  if not action.option_strings:
1447  action.container._remove_action(action)
1448 
1449 
def _handle_conflict_resolve(self, action, conflicting_actions)
Definition: argparse.py:1435
def argparse._ActionsContainer._pop_action_class (   self,
  kwargs,
  default = None 
)
private

Definition at line 1401 of file argparse.py.

References argparse._ActionsContainer._registry_get(), and python.rootplot.argparse._ActionsContainer._registry_get().

Referenced by argparse._ActionsContainer.add_argument(), and argparse.ArgumentParser.add_subparsers().

1401  def _pop_action_class(self, kwargs, default=None):
1402  action = kwargs.pop('action', default)
1403  return self._registry_get('action', action, action)
1404 
def _registry_get(self, registry_name, value, default=None)
Definition: argparse.py:1219
def _pop_action_class(self, kwargs, default=None)
Definition: argparse.py:1401
def argparse._ActionsContainer._registry_get (   self,
  registry_name,
  value,
  default = None 
)
private

Definition at line 1219 of file argparse.py.

References argparse._ActionsContainer._registries, python.rootplot.argparse._ActionsContainer._registries, python.rootplot.argparse._ArgumentGroup._registries, and reco.get().

Referenced by argparse.ArgumentParser._get_value(), and argparse._ActionsContainer._pop_action_class().

1219  def _registry_get(self, registry_name, value, default=None):
1220  return self._registries[registry_name].get(value, default)
1221 
def _registry_get(self, registry_name, value, default=None)
Definition: argparse.py:1219
T get(const Candidate &c)
Definition: component.h:55
def argparse._ActionsContainer._remove_action (   self,
  action 
)
private

Definition at line 1298 of file argparse.py.

1298  def _remove_action(self, action):
1299  self._actions.remove(action)
1300 
def _remove_action(self, action)
Definition: argparse.py:1298
def argparse._ActionsContainer.add_argument (   self,
  args,
  kwargs 
)
add_argument(dest, ..., name=value, ...)
add_argument(option_string, option_string, ..., name=value, ...)

Definition at line 1237 of file argparse.py.

References argparse._ActionsContainer._add_action(), python.rootplot.argparse._ActionsContainer._add_action(), python.rootplot.argparse._ArgumentGroup._add_action(), python.rootplot.argparse._MutuallyExclusiveGroup._add_action(), python.rootplot.argparse.ArgumentParser._add_action(), argparse._ActionsContainer._defaults, python.rootplot.argparse._ActionsContainer._defaults, python.rootplot.argparse._ArgumentGroup._defaults, argparse._ActionsContainer._get_optional_kwargs(), python.rootplot.argparse._ActionsContainer._get_optional_kwargs(), argparse._ActionsContainer._get_positional_kwargs(), python.rootplot.argparse._ActionsContainer._get_positional_kwargs(), argparse._ActionsContainer._pop_action_class(), python.rootplot.argparse._ActionsContainer._pop_action_class(), argparse._ActionsContainer.argument_default, python.rootplot.argparse._ActionsContainer.argument_default, argparse._ActionsContainer.prefix_chars, and python.rootplot.argparse._ActionsContainer.prefix_chars.

1237  def add_argument(self, *args, **kwargs):
1238  """
1239  add_argument(dest, ..., name=value, ...)
1240  add_argument(option_string, option_string, ..., name=value, ...)
1241  """
1242 
1243  # if no positional args are supplied or only one is supplied and
1244  # it doesn't look like an option string, parse a positional
1245  # argument
1246  chars = self.prefix_chars
1247  if not args or len(args) == 1 and args[0][0] not in chars:
1248  kwargs = self._get_positional_kwargs(*args, **kwargs)
1249 
1250  # otherwise, we're adding an optional argument
1251  else:
1252  kwargs = self._get_optional_kwargs(*args, **kwargs)
1253 
1254  # if no default was supplied, use the parser-level default
1255  if 'default' not in kwargs:
1256  dest = kwargs['dest']
1257  if dest in self._defaults:
1258  kwargs['default'] = self._defaults[dest]
1259  elif self.argument_default is not None:
1260  kwargs['default'] = self.argument_default
1261 
1262  # create the action object, and add it to the parser
1263  action_class = self._pop_action_class(kwargs)
1264  action = action_class(**kwargs)
1265  return self._add_action(action)
1266 
def _get_optional_kwargs(self, args, kwargs)
Definition: argparse.py:1357
def add_argument(self, args, kwargs)
Definition: argparse.py:1237
def _pop_action_class(self, kwargs, default=None)
Definition: argparse.py:1401
def _get_positional_kwargs(self, dest, kwargs)
Definition: argparse.py:1341
def _add_action(self, action)
Definition: argparse.py:1277
def argparse._ActionsContainer.add_argument_group (   self,
  args,
  kwargs 
)

Definition at line 1267 of file argparse.py.

Referenced by argparse._ActionsContainer._add_container_actions(), and argparse.ArgumentParser.add_subparsers().

1267  def add_argument_group(self, *args, **kwargs):
1268  group = _ArgumentGroup(self, *args, **kwargs)
1269  self._action_groups.append(group)
1270  return group
1271 
def add_argument_group(self, args, kwargs)
Definition: argparse.py:1267
def argparse._ActionsContainer.add_mutually_exclusive_group (   self,
  kwargs 
)

Definition at line 1272 of file argparse.py.

Referenced by argparse._ActionsContainer._add_container_actions().

1272  def add_mutually_exclusive_group(self, **kwargs):
1273  group = _MutuallyExclusiveGroup(self, **kwargs)
1274  self._mutually_exclusive_groups.append(group)
1275  return group
1276 
def add_mutually_exclusive_group(self, kwargs)
Definition: argparse.py:1272
def argparse._ActionsContainer.register (   self,
  registry_name,
  value,
  object 
)

Definition at line 1215 of file argparse.py.

1215  def register(self, registry_name, value, object):
1216  registry = self._registries.setdefault(registry_name, {})
1217  registry[value] = object
1218 
def register(self, registry_name, value, object)
Definition: argparse.py:1215
def argparse._ActionsContainer.set_defaults (   self,
  kwargs 
)

Definition at line 1225 of file argparse.py.

References argparse._ActionsContainer._actions, python.rootplot.argparse._ActionsContainer._actions, and python.rootplot.argparse._ArgumentGroup._actions.

1225  def set_defaults(self, **kwargs):
1226  self._defaults.update(kwargs)
1227 
1228  # if these defaults match any existing arguments, replace
1229  # the previous default on the object with the new one
1230  for action in self._actions:
1231  if action.dest in kwargs:
1232  action.default = kwargs[action.dest]
1233 
def set_defaults(self, kwargs)
Definition: argparse.py:1225

Member Data Documentation

argparse._ActionsContainer._action_groups
private
argparse._ActionsContainer._actions
private
argparse._ActionsContainer._defaults
private
argparse._ActionsContainer._has_negative_number_optionals
private
argparse._ActionsContainer._mutually_exclusive_groups
private
argparse._ActionsContainer._negative_number_matcher
private

Definition at line 1206 of file argparse.py.

argparse._ActionsContainer._option_string_actions
private
argparse._ActionsContainer._registries
private

Definition at line 1176 of file argparse.py.

Referenced by argparse._ActionsContainer._registry_get().

argparse._ActionsContainer.argument_default

Definition at line 1171 of file argparse.py.

Referenced by argparse._ActionsContainer.add_argument().

argparse._ActionsContainer.conflict_handler
argparse._ActionsContainer.description

Definition at line 1170 of file argparse.py.

Referenced by argparse.ArgumentParser.format_help().

argparse._ActionsContainer.prefix_chars