CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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__
 
def add_argument
 
def add_argument_group
 
def add_mutually_exclusive_group
 
def register
 
def set_defaults
 

Public Attributes

 argument_default
 
 conflict_handler
 
 description
 
 prefix_chars
 

Private Member Functions

def _add_action
 
def _add_container_actions
 
def _check_conflict
 
def _get_handler
 
def _get_optional_kwargs
 
def _get_positional_kwargs
 
def _handle_conflict_error
 
def _handle_conflict_resolve
 
def _pop_action_class
 
def _registry_get
 
def _remove_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.

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

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().

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

Definition at line 1301 of file argparse.py.

References 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().

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

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

Definition at line 1405 of file argparse.py.

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

Referenced by argparse._ActionsContainer._check_conflict().

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

Definition at line 1357 of file argparse.py.

References argparse._set, python.multivaluedict.dict, argparse._ActionsContainer.prefix_chars, and python.rootplot.argparse._ActionsContainer.prefix_chars.

Referenced by argparse._ActionsContainer.add_argument().

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

Definition at line 1341 of file argparse.py.

References python.multivaluedict.dict.

Referenced by argparse._ActionsContainer.add_argument().

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

Definition at line 1428 of file argparse.py.

References join().

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

Definition at line 1435 of file argparse.py.

1436  def _handle_conflict_resolve(self, action, conflicting_actions):
1437 
1438  # remove all conflicting options
1439  for option_string, action in conflicting_actions:
1440 
1441  # remove the conflicting option
1442  action.option_strings.remove(option_string)
1443  self._option_string_actions.pop(option_string, None)
1444 
1445  # if the option now has no option string, remove it from the
1446  # container holding it
1447  if not action.option_strings:
1448  action.container._remove_action(action)
1449 
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().

1402  def _pop_action_class(self, kwargs, default=None):
1403  action = kwargs.pop('action', default)
1404  return self._registry_get('action', action, action)
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().

1220  def _registry_get(self, registry_name, value, default=None):
1221  return self._registries[registry_name].get(value, default)
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.

1299  def _remove_action(self, action):
1300  self._actions.remove(action)
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.

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

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

Definition at line 1272 of file argparse.py.

Referenced by argparse._ActionsContainer._add_container_actions().

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

Definition at line 1215 of file argparse.py.

1216  def register(self, registry_name, value, object):
1217  registry = self._registries.setdefault(registry_name, {})
1218  registry[value] = object
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.

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

Member Data Documentation

argparse._ActionsContainer._action_groups
private

Definition at line 1199 of file argparse.py.

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

argparse._ActionsContainer._actions
private

Definition at line 1195 of file argparse.py.

Referenced by argparse.ArgumentParser._get_optional_actions(), argparse.ArgumentParser._get_positional_actions(), argparse.ArgumentParser._parse_known_args(), argparse.ArgumentParser.format_help(), argparse.ArgumentParser.format_usage(), argparse.ArgumentParser.parse_known_args(), and argparse._ActionsContainer.set_defaults().

argparse._ActionsContainer._defaults
private

Definition at line 1203 of file argparse.py.

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

argparse._ActionsContainer._has_negative_number_optionals
private

Definition at line 1210 of file argparse.py.

Referenced by argparse._ActionsContainer._add_action(), and argparse.ArgumentParser._parse_optional().

argparse._ActionsContainer._mutually_exclusive_groups
private

Definition at line 1200 of file argparse.py.

Referenced by argparse.ArgumentParser._parse_known_args(), argparse.ArgumentParser.add_subparsers(), argparse.ArgumentParser.format_help(), and argparse.ArgumentParser.format_usage().

argparse._ActionsContainer._negative_number_matcher
private

Definition at line 1206 of file argparse.py.

argparse._ActionsContainer._option_string_actions
private

Definition at line 1196 of file argparse.py.

Referenced by argparse._ActionsContainer._add_action(), argparse._ActionsContainer._check_conflict(), argparse.ArgumentParser._get_option_tuples(), argparse.ArgumentParser._parse_known_args(), and argparse.ArgumentParser._parse_optional().

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

Definition at line 1173 of file argparse.py.

Referenced by argparse._ActionsContainer._check_conflict(), and argparse._ActionsContainer._get_handler().

argparse._ActionsContainer.description

Definition at line 1170 of file argparse.py.

Referenced by argparse.ArgumentParser.format_help().

argparse._ActionsContainer.prefix_chars

Definition at line 1172 of file argparse.py.

Referenced by argparse.ArgumentParser._get_option_tuples(), argparse._ActionsContainer._get_optional_kwargs(), argparse.ArgumentParser._parse_known_args(), argparse.ArgumentParser._parse_optional(), and argparse._ActionsContainer.add_argument().