Skip to content

Commit

Permalink
put most all parser setup in single module
Browse files Browse the repository at this point in the history
  • Loading branch information
kalefranz committed Aug 17, 2017
1 parent 84cc316 commit 16c8878
Show file tree
Hide file tree
Showing 19 changed files with 877 additions and 895 deletions.
27 changes: 27 additions & 0 deletions conda/_vendor/auxlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,30 @@ def emit(self, record):
__license__ = "ISC"
__copyright__ = "(c) 2015 Kale Franz. All rights reserved."
__summary__ = """auxiliary library to the python standard library"""


class _Null(object):
"""
Examples:
>>> len(_Null())
0
>>> bool(_Null())
False
>>> _Null().__nonzero__()
False
"""
def __nonzero__(self):
return self.__bool__()

def __bool__(self):
return False

def __len__(self):
return 0


# Use this NULL object when needing to distinguish a value from None
# For example, when parsing json, you may need to determine if a json key was given and set
# to null, or the key didn't exist at all. There could be a bit of potential confusion here,
# because in python null == None, while here I'm defining NULL to mean 'not defined'.
NULL = _Null()
27 changes: 0 additions & 27 deletions conda/_vendor/auxlib/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,6 @@
from .compat import isiterable, iteritems, odict, text_type


class _Null(object):
"""
Examples:
>>> len(_Null())
0
>>> bool(_Null())
False
>>> _Null().__nonzero__()
False
"""
def __nonzero__(self):
return self.__bool__()

def __bool__(self):
return False

def __len__(self):
return 0


# Use this NULL object when needing to distinguish a value from None
# For example, when parsing json, you may need to determine if a json key was given and set
# to null, or the key didn't exist at all. There could be a bit of potential confusion here,
# because in python null == None, while here I'm defining NULL to mean 'not defined'.
NULL = _Null()


def make_immutable(value):
# this function is recursive, and if nested data structures fold back on themselves,
# there will likely be recursion errors
Expand Down
4 changes: 0 additions & 4 deletions conda/base/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,3 @@
All other ``conda`` modules may import from ``conda.base``.
"""
from __future__ import absolute_import, division, print_function
from logging import getLogger

log = getLogger(__name__)
22 changes: 10 additions & 12 deletions conda/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
conda <command> -h
"""
from __future__ import absolute_import, division, print_function, unicode_literals

import sys

PARSER = None
Expand All @@ -49,17 +50,10 @@ def generate_parser():

from .. import __version__
from .conda_argparse import ArgumentParser
from .main_clean import configure_parser as configure_parser_clean
from .main_config import configure_parser as configure_parser_config
from .main_create import configure_parser as configure_parser_create
from .main_help import configure_parser as configure_parser_help
from .main_info import configure_parser as configure_parser_info
from .main_install import configure_parser as configure_parser_install
from .main_list import configure_parser as configure_parser_list
from .main_package import configure_parser as configure_parser_package
from .main_remove import configure_parser as configure_parser_remove
from .main_search import configure_parser as configure_parser_search
from .main_update import configure_parser as configure_parser_update
from .parsers import (configure_parser_clean, configure_parser_config, configure_parser_create,
configure_parser_help, configure_parser_info, configure_parser_install,
configure_parser_list, configure_parser_package, configure_parser_remove,
configure_parser_search, configure_parser_update)

p = ArgumentParser(
description='conda is a tool for managing and deploying applications,'
Expand Down Expand Up @@ -133,7 +127,11 @@ def _main(*args):
context.__init__(SEARCH_PATH, 'conda', args)
init_loggers(context)

exit_code = args.func(args, p)
relative_mod, func_name = args.func.rsplit('.', 1)
# func_name should always be 'execute'
from importlib import import_module
module = import_module(relative_mod, __name__.rsplit('.', 1)[0])
exit_code = getattr(module, func_name)(args, p)
if isinstance(exit_code, int):
return exit_code

Expand Down
55 changes: 0 additions & 55 deletions conda/cli/main_clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,66 +12,11 @@
from os.path import getsize, isdir, join
import sys

from .conda_argparse import add_parser_json, add_parser_quiet, add_parser_yes
from ..base.constants import CONDA_TARBALL_EXTENSION
from ..base.context import context

log = getLogger(__name__)

descr = """
Remove unused packages and caches.
"""

example = """
Examples:
conda clean --tarballs
"""

def configure_parser(sub_parsers):
p = sub_parsers.add_parser(
'clean',
description=descr,
help=descr,
epilog=example,
)
add_parser_yes(p)
add_parser_json(p)
add_parser_quiet(p)
p.add_argument(
"-a", "--all",
action="store_true",
help="Remove index cache, lock files, tarballs, "
"unused cache packages, and source cache.",
)
p.add_argument(
"-i", "--index-cache",
action="store_true",
help="Remove index cache.",
)
p.add_argument(
"-l", "--lock",
action="store_true",
help="Remove all conda lock files.",
)
p.add_argument(
"-t", "--tarballs",
action="store_true",
help="Remove cached package tarballs.",
)
p.add_argument(
'-p', '--packages',
action='store_true',
help="""Remove unused cached packages. Warning: this does not check
for symlinked packages.""",
)
p.add_argument(
'-s', '--source-cache',
action='store_true',
help="""Remove files from the source cache of conda build.""",
)
p.set_defaults(func=execute)


def find_tarballs():
from ..core.package_cache import PackageCache
Expand Down
190 changes: 0 additions & 190 deletions conda/cli/main_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,208 +5,18 @@
# Consult LICENSE.txt or http://opensource.org/licenses/BSD-3-Clause.
from __future__ import absolute_import, division, print_function, unicode_literals

from argparse import SUPPRESS
import collections
import json
import os
from os.path import isfile, join
import sys
from textwrap import wrap

from .conda_argparse import add_parser_json
from .. import CondaError
from ..base.constants import CONDA_HOMEPAGE_URL
from ..base.context import context, sys_rc_path, user_rc_path
from ..common.compat import isiterable, iteritems, string_types, text_type
from ..common.constants import NULL
from ..common.io import timeout

descr = """
Modify configuration values in .condarc. This is modeled after the git
config command. Writes to the user .condarc file (%s) by default.
""" % user_rc_path

# Note, the extra whitespace in the list keys is on purpose. It's so the
# formatting from help2man is still valid YAML (otherwise it line wraps the
# keys like "- conda - defaults"). Technically the parser here still won't
# recognize it because it removes the indentation, but at least it will be
# valid.
additional_descr = """
See `conda config --describe` or %s/docs/config.html
for details on all the options that can go in .condarc.
Examples:
Display all configuration values as calculated and compiled:
conda config --show
Display all identified configuration sources:
conda config --show-sources
Describe all available configuration options:
conda config --describe
Add the conda-canary channel:
conda config --add channels conda-canary
Set the output verbosity to level 3 (highest):
conda config --set verbosity 3
""" % CONDA_HOMEPAGE_URL


# Note, the formatting of this is designed to work well with help2man
example = """
Examples:
Get the channels defined in the system .condarc:
conda config --get channels --system
Add the 'foo' Binstar channel:
conda config --add channels foo
Disable the 'show_channel_urls' option:
conda config --set show_channel_urls no
"""


def configure_parser(sub_parsers):
p = sub_parsers.add_parser(
'config',
description=descr,
help=descr,
epilog=additional_descr,
)
add_parser_json(p)

# TODO: use argparse.FileType
location = p.add_mutually_exclusive_group()
location.add_argument(
"--system",
action="store_true",
help="""Write to the system .condarc file ({system}). Otherwise writes to the user
config file ({user}).""".format(system=sys_rc_path,
user=user_rc_path),
)
location.add_argument(
"--env",
action="store_true",
help="Write to the active conda environment .condarc file (%s). "
"If no environment is active, write to the user config file (%s)."
"" % (os.getenv('CONDA_PREFIX', "<no active environment>"), user_rc_path),
)
location.add_argument(
"--file",
action="store",
help="""Write to the given file. Otherwise writes to the user config file ({user})
or the file path given by the 'CONDARC' environment variable, if it is set
(default: %(default)s).""".format(user=user_rc_path),
default=os.environ.get('CONDARC', user_rc_path)
)

# XXX: Does this really have to be mutually exclusive. I think the below
# code will work even if it is a regular group (although combination of
# --add and --remove with the same keys will not be well-defined).
action = p.add_mutually_exclusive_group(required=True)
action.add_argument(
"--show",
action="store_true",
help="Display all configuration values as calculated and compiled.",
)
action.add_argument(
"--show-sources",
action="store_true",
help="Display all identified configuration sources.",
)
action.add_argument(
"--validate",
action="store_true",
help="Validate all configuration sources.",
)
action.add_argument(
"--describe",
action="store_true",
help="Describe available configuration parameters.",
)
action.add_argument(
"--write-default",
action="store_true",
help="Write the default configuration to a file. "
"Equivalent to `conda config --describe > ~/.condarc` "
"when no --env, --system, or --file flags are given.",
)
action.add_argument(
"--get",
nargs='*',
action="store",
help="Get a configuration value.",
default=None,
metavar='KEY',
)
action.add_argument(
"--append",
nargs=2,
action="append",
help="""Add one configuration value to the end of a list key.""",
default=[],
metavar=('KEY', 'VALUE'),
)
action.add_argument(
"--prepend", "--add",
nargs=2,
action="append",
help="""Add one configuration value to the beginning of a list key.""",
default=[],
metavar=('KEY', 'VALUE'),
)
action.add_argument(
"--set",
nargs=2,
action="append",
help="""Set a boolean or string key""",
default=[],
metavar=('KEY', 'VALUE'),
)
action.add_argument(
"--remove",
nargs=2,
action="append",
help="""Remove a configuration value from a list key. This removes
all instances of the value.""",
default=[],
metavar=('KEY', 'VALUE'),
)
action.add_argument(
"--remove-key",
nargs=1,
action="append",
help="""Remove a configuration key (and all its values).""",
default=[],
metavar="KEY",
)
action.add_argument(
"--stdin",
action="store_true",
help="Apply configuration information given in yaml format piped through stdin.",
)

p.add_argument(
"-f", "--force",
action="store_true",
default=NULL,
help=SUPPRESS, # TODO: No longer used. Remove in a future release.
)

p.set_defaults(func=execute)


def execute(args, parser):
from ..exceptions import CouldntParseError
Expand Down
Loading

0 comments on commit 16c8878

Please sign in to comment.