def _parse_and_set_args(self, arguments): # the first index of arguments MUST be either activate, deactivate, or reactivate if arguments is None: from .exceptions import ArgumentError raise ArgumentError( "'activate', 'deactivate', or 'reactivate' command must be given" ) command = arguments[0] arguments = tuple(drop(self.shift_args + 1, arguments)) help_flags = ('-h', '--help', '/?') non_help_args = tuple(arg for arg in arguments if arg not in help_flags) help_requested = len(arguments) != len(non_help_args) remainder_args = list(arg for arg in non_help_args if arg and arg != command) if not command: from .exceptions import ArgumentError raise ArgumentError( "'activate', 'deactivate', 'hook', or 'reactivate' " "command must be given") elif help_requested: from .exceptions import ActivateHelp, DeactivateHelp, GenericHelp help_classes = { 'activate': ActivateHelp(), 'deactivate': DeactivateHelp(), 'hook': GenericHelp('hook'), 'reactivate': GenericHelp('reactivate'), } raise help_classes[command] elif command not in ('activate', 'deactivate', 'reactivate', 'hook'): from .exceptions import ArgumentError raise ArgumentError("invalid command '%s'" % command) if command == 'activate': try: stack_idx = remainder_args.index('--stack') except ValueError: self.stack = False else: del remainder_args[stack_idx] self.stack = True if len(remainder_args) > 1: from .exceptions import ArgumentError raise ArgumentError( command + ' does not accept more than one argument:\n' + str(remainder_args) + '\n') self.env_name_or_prefix = remainder_args and remainder_args[ 0] or 'base' else: if remainder_args: from .exceptions import ArgumentError raise ArgumentError( '%s does not accept arguments\nremainder_args: %s\n' % (command, remainder_args)) self.command = command
def _parse_and_set_args(self, arguments): # the first index of arguments MUST be either activate, deactivate, or reactivate if arguments is None: from .exceptions import ArgumentError raise ArgumentError("'activate', 'deactivate', or 'reactivate' command must be given") command = arguments[0] arguments = tuple(drop(self.shift_args + 1, arguments)) help_flags = ('-h', '--help', '/?') non_help_args = tuple(arg for arg in arguments if arg not in help_flags) help_requested = len(arguments) != len(non_help_args) remainder_args = list(arg for arg in non_help_args if arg and arg != command) if not command: from .exceptions import ArgumentError raise ArgumentError("'activate', 'deactivate', 'hook', or 'reactivate' " "command must be given") elif help_requested: from .exceptions import ActivateHelp, DeactivateHelp, GenericHelp help_classes = { 'activate': ActivateHelp(), 'deactivate': DeactivateHelp(), 'hook': GenericHelp('hook'), 'reactivate': GenericHelp('reactivate'), } raise help_classes[command] elif command not in ('activate', 'deactivate', 'reactivate', 'hook'): from .exceptions import ArgumentError raise ArgumentError("invalid command '%s'" % command) if command == 'activate': try: stack_idx = remainder_args.index('--stack') except ValueError: self.stack = False else: del remainder_args[stack_idx] self.stack = True if len(remainder_args) > 1: from .exceptions import ArgumentError raise ArgumentError(command + ' does not accept more than one argument:\n' + str(remainder_args) + '\n') self.env_name_or_prefix = remainder_args and remainder_args[0] or 'base' else: if remainder_args: from .exceptions import ArgumentError raise ArgumentError('%s does not accept arguments\nremainder_args: %s\n' % (command, remainder_args)) self.command = command
def _parse_and_set_args(self, arguments): # the first index of arguments MUST be either activate, deactivate, or reactivate if arguments is None: from .exceptions import ArgumentError raise ArgumentError( "'activate', 'deactivate', or 'reactivate' command must be given" ) command = arguments[0] arguments = tuple(drop(self.shift_args + 1, arguments)) help_flags = ('-h', '--help', '/?') non_help_args = tuple(arg for arg in arguments if arg not in help_flags) help_requested = len(arguments) != len(non_help_args) remainder_args = tuple(arg for arg in non_help_args if arg and arg != command) if not command: from .exceptions import ArgumentError raise ArgumentError( "'activate', 'deactivate', or 'reactivate' command must be given" ) elif help_requested: from . import CondaError class Help(CondaError): # NOQA pass raise Help("help requested for %s" % command) elif command not in ('activate', 'deactivate', 'reactivate'): from .exceptions import ArgumentError raise ArgumentError("invalid command '%s'" % command) elif command == 'activate' and len(remainder_args) > 1: from .exceptions import ArgumentError raise ArgumentError( 'activate does not accept more than one argument:\n' + str(remainder_args) + '\n') elif command != 'activate' and remainder_args: from .exceptions import ArgumentError raise ArgumentError( '%s does not accept arguments\nremainder_args: %s\n' % (command, remainder_args)) if command == 'activate': self.env_name_or_prefix = remainder_args and remainder_args[ 0] or 'root' self.command = command
def head_tail(it: Iterable[E]) -> Tuple[E, Iterable[E]]: """ Split provided iterable into head element and tail iterable. >>> head, tail = head_tail(iter([1, 2, 3])) >>> head, list(tail) (1, [2, 3]) >>> head, tail = head_tail(iter([42])) >>> head, list(tail) (42, []) Raises :class:`StopIteration` if the original iterable is empty. >>> head_tail(iter([])) Traceback (most recent call last): ... StopIteration """ head, seq = peek(it) tail = drop(1, seq) return head, tail
def _parse_and_set_args(self, arguments): # the first index of arguments MUST be either activate, deactivate, or reactivate if arguments is None: from .exceptions import ArgumentError raise ArgumentError("'activate', 'deactivate', or 'reactivate' command must be given") command = arguments[0] arguments = tuple(drop(self.shift_args + 1, arguments)) help_flags = ('-h', '--help', '/?') non_help_args = tuple(arg for arg in arguments if arg not in help_flags) help_requested = len(arguments) != len(non_help_args) remainder_args = tuple(arg for arg in non_help_args if arg and arg != command) if not command: from .exceptions import ArgumentError raise ArgumentError("'activate', 'deactivate', or 'reactivate' command must be given") elif help_requested: from . import CondaError class Help(CondaError): # NOQA pass raise Help("help requested for %s" % command) elif command not in ('activate', 'deactivate', 'reactivate'): from .exceptions import ArgumentError raise ArgumentError("invalid command '%s'" % command) elif command == 'activate' and len(remainder_args) > 1: from .exceptions import ArgumentError raise ArgumentError('activate does not accept more than one argument:\n' + str(remainder_args) + '\n') elif command != 'activate' and remainder_args: from .exceptions import ArgumentError raise ArgumentError('%s does not accept arguments\nremainder_args: %s\n' % (command, remainder_args)) if command == 'activate': self.env_name_or_prefix = remainder_args and remainder_args[0] or 'root' self.command = command
def test_drop(): assert list(drop(3, 'ABCDE')) == list('DE') assert list(drop(1, (3, 2, 1))) == list((2, 1))
def _read_channel_configuration(scheme, host, port, path): # return location, name, scheme, auth, token path = path and path.rstrip('/') test_url = Url(host=host, port=port, path=path).url # Step 1. No path given; channel name is None if not path: return Url(host=host, port=port).url.rstrip('/'), None, scheme or None, None, None # Step 2. migrated_custom_channels matches for name, location in sorted(context.migrated_custom_channels.items(), reverse=True, key=lambda x: len(x[0])): location, _scheme, _auth, _token = split_scheme_auth_token(location) if tokenized_conda_url_startswith(test_url, join_url(location, name)): # translate location to new location, with new credentials subname = test_url.replace(join_url(location, name), '', 1).strip('/') channel_name = join_url(name, subname) channel = _get_channel_for_name(channel_name) return channel.location, channel_name, channel.scheme, channel.auth, channel.token # Step 3. migrated_channel_aliases matches for migrated_alias in context.migrated_channel_aliases: if test_url.startswith(migrated_alias.location): name = test_url.replace(migrated_alias.location, '', 1).strip('/') ca = context.channel_alias return ca.location, name, ca.scheme, ca.auth, ca.token # Step 4. custom_channels matches for name, channel in sorted(context.custom_channels.items(), reverse=True, key=lambda x: len(x[0])): that_test_url = join_url(channel.location, channel.name) if tokenized_startswith(test_url.split('/'), that_test_url.split('/')): subname = test_url.replace(that_test_url, '', 1).strip('/') return (channel.location, join_url(channel.name, subname), scheme, channel.auth, channel.token) # Step 5. channel_alias match ca = context.channel_alias if ca.location and tokenized_startswith(test_url.split('/'), ca.location.split('/')): name = test_url.replace(ca.location, '', 1).strip('/') or None return ca.location, name, scheme, ca.auth, ca.token # Step 6. not-otherwise-specified file://-type urls if host is None: # this should probably only happen with a file:// type url assert port is None location, name = test_url.rsplit('/', 1) if not location: location = '/' _scheme, _auth, _token = 'file', None, None return location, name, _scheme, _auth, _token # Step 7. fall through to host:port as channel_location and path as channel_name # but bump the first token of paths starting with /conda for compatibility with # Anaconda Enterprise Repository software. bump = None path_parts = path.strip('/').split('/') if path_parts and path_parts[0] == 'conda': bump, path = 'conda', '/'.join(drop(1, path_parts)) return (Url(host=host, port=port, path=bump).url.rstrip('/'), path.strip('/') or None, scheme or None, None, None)
def test_drop(): assert list(drop(3, "ABCDE")) == list("DE") assert list(drop(1, (3, 2, 1))) == list((2, 1))