Skip to content

Commit

Permalink
make channel_priority an enum with options
Browse files Browse the repository at this point in the history
  • Loading branch information
kalefranz committed Sep 3, 2018
1 parent d244111 commit 36bfd1c
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 9 deletions.
32 changes: 30 additions & 2 deletions conda/base/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
"""
from __future__ import absolute_import, division, print_function, unicode_literals

from enum import Enum
from enum import Enum, EnumMeta
from os.path import join

from ..common.compat import itervalues, on_win
from ..common.compat import itervalues, on_win, six_with_metaclass, string_types

PREFIX_PLACEHOLDER = ('/opt/anaconda1anaconda2'
# this is intentionally split into parts, such that running
Expand Down Expand Up @@ -169,6 +169,34 @@ def __str__(self):
return self.value


class ChannelPriorityMeta(EnumMeta):

def __call__(cls, value, *args, **kwargs):
try:
return super(ChannelPriorityMeta, cls).__call__(value, *args, **kwargs)
except ValueError:
if isinstance(value, string_types):
from .._vendor.auxlib.type_coercion import typify
value = typify(value)
if value is True:
value = 'flexible'
elif value is False:
value = cls.DISABLED
return super(ChannelPriorityMeta, cls).__call__(value, *args, **kwargs)


class ChannelPriority(six_with_metaclass(ChannelPriorityMeta, Enum)):
__name__ = "ChannelPriority"

STRICT = 'strict'
STRICT_OR_FLEXIBLE = 'strict_or_flexible'
FLEXIBLE = 'flexible'
DISABLED = 'disabled'

def __str__(self):
return self.value


# Magic files for permissions determination
PACKAGE_CACHE_MAGIC_FILE = 'urls.txt'
PREFIX_MAGIC_FILE = join('conda-meta', 'history')
Expand Down
11 changes: 6 additions & 5 deletions conda/base/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
import platform
import sys

from .constants import (APP_NAME, DEFAULTS_CHANNEL_NAME, DEFAULT_AGGRESSIVE_UPDATE_PACKAGES,
DEFAULT_CHANNELS, DEFAULT_CHANNEL_ALIAS, DEFAULT_CUSTOM_CHANNELS,
DepsModifier, ERROR_UPLOAD_URL, PLATFORM_DIRECTORIES, PREFIX_MAGIC_FILE,
PathConflict, ROOT_ENV_NAME, SEARCH_PATH, SafetyChecks, UpdateModifier)
from .constants import (APP_NAME, ChannelPriority, DEFAULTS_CHANNEL_NAME,
DEFAULT_AGGRESSIVE_UPDATE_PACKAGES, DEFAULT_CHANNELS,
DEFAULT_CHANNEL_ALIAS, DEFAULT_CUSTOM_CHANNELS, DepsModifier,
ERROR_UPLOAD_URL, PLATFORM_DIRECTORIES, PREFIX_MAGIC_FILE, PathConflict,
ROOT_ENV_NAME, SEARCH_PATH, SafetyChecks, UpdateModifier)
from .. import __version__ as CONDA_VERSION
from .._vendor.appdirs import user_data_dir
from .._vendor.auxlib.decorators import memoize, memoizedproperty
Expand Down Expand Up @@ -169,7 +170,7 @@ class Context(Configuration):
_channel_alias = PrimitiveParameter(DEFAULT_CHANNEL_ALIAS,
aliases=('channel_alias',),
validation=channel_alias_validation)
channel_priority = PrimitiveParameter(True)
channel_priority = PrimitiveParameter(ChannelPriority.FLEXIBLE)
_channels = SequenceParameter(string_types, default=(DEFAULTS_CHANNEL_NAME,),
aliases=('channels', 'channel',)) # channel for args.channel
_custom_channels = MapParameter(string_types, DEFAULT_CUSTOM_CHANNELS,
Expand Down
4 changes: 3 additions & 1 deletion conda/cli/main_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from .. import CondaError
from .._vendor.auxlib.entity import EntityEncoder
from .._vendor.toolz import concat, groupby
from ..base.constants import DepsModifier, PathConflict, SafetyChecks, UpdateModifier
from ..base.constants import (ChannelPriority, DepsModifier, PathConflict, SafetyChecks,
UpdateModifier)
from ..base.context import context, sys_rc_path, user_rc_path
from ..common.compat import Mapping, Sequence, isiterable, iteritems, itervalues, string_types
from ..common.configuration import pretty_list, pretty_map
Expand Down Expand Up @@ -346,6 +347,7 @@ def enum_representer(dumper, data):
yaml.representer.RoundTripRepresenter.add_representer(PathConflict, enum_representer)
yaml.representer.RoundTripRepresenter.add_representer(DepsModifier, enum_representer)
yaml.representer.RoundTripRepresenter.add_representer(UpdateModifier, enum_representer)
yaml.representer.RoundTripRepresenter.add_representer(ChannelPriority, enum_representer)

try:
with open(rc_path, 'w') as rc:
Expand Down
15 changes: 15 additions & 0 deletions conda/common/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,21 @@ def _clone_with_metaclass(Class):
return _clone_with_metaclass


def six_with_metaclass(meta, *bases):
"""Create a base class with a metaclass."""
# This requires a bit of explanation: the basic idea is to make a dummy
# metaclass for one level of class instantiation that replaces itself with
# the actual metaclass.
class metaclass(type):

def __new__(cls, name, this_bases, d):
return meta(name, bases, d)

@classmethod
def __prepare__(cls, name, this_bases):
return meta.__prepare__(name, bases)
return type.__new__(metaclass, str('temporary_class'), (), {})


NoneType = type(None)
primitive_types = tuple(chain(string_types, integer_types, (float, complex, bool, NoneType)))
Expand Down
8 changes: 8 additions & 0 deletions tests/base/test_constants.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals

from conda.base.constants import ChannelPriority
from conda.common.constants import NULL
from logging import getLogger

Expand All @@ -9,3 +10,10 @@

def test_null_is_falsey():
assert not NULL


def test_ChannelPriority():
assert ChannelPriority("strict") == ChannelPriority.STRICT
assert ChannelPriority["STRICT"] == ChannelPriority.STRICT
assert ChannelPriority(False) == ChannelPriority.DISABLED
assert ChannelPriority('false') == ChannelPriority.DISABLED
6 changes: 5 additions & 1 deletion tests/base/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from conda._vendor.auxlib.collection import AttrDict
from conda._vendor.auxlib.ish import dals
from conda._vendor.toolz.itertoolz import concat
from conda.base.constants import PathConflict
from conda.base.constants import PathConflict, ChannelPriority
from conda.base.context import context, reset_context
from conda.common.compat import odict, iteritems
from conda.common.configuration import ValidationError, YamlRawParameter
Expand Down Expand Up @@ -61,6 +61,7 @@ def setUp(self):
ftps: false
rsync: 'false'
aggressive_update_packages: []
channel_priority: false
""")
reset_context()
rd = odict(testdata=YamlRawParameter.make_raw_parameters('testdata', yaml_load(string)))
Expand Down Expand Up @@ -255,6 +256,9 @@ def test_aggressive_update_packages(self):
with env_var('CONDA_AGGRESSIVE_UPDATE_PACKAGES', ','.join(specs), reset_context):
assert context.aggressive_update_packages == tuple(MatchSpec(s) for s in specs)

def test_channel_priority(self):
assert context.channel_priority == ChannelPriority.DISABLED


class ContextDefaultRcTests(TestCase):

Expand Down

0 comments on commit 36bfd1c

Please sign in to comment.