<commit_before>import os
import sys

from spec import eq_, skip, Spec, ok_, trap
from mock import patch

from invoke.cli import parse, dispatch
from invoke.context import Context
from invoke.runner import run
from invoke.parser import Parser
from invoke.collection import Collection
from invoke.tasks import task
from invoke.exceptions import Failure
import invoke

from _utils import support

@trap
def _output_eq(args, stdout=None, stderr=None):
    """
    dispatch() 'args', matching output to 'std(out|err)'.

    Must give either or both of the output-expecting args.
    """
    args = ['inv'] + args
    dispatch(args)
    if stdout:
        eq_(sys.stdout.getvalue(), stdout)
    if stderr:
        eq_(sys.stderr.getvalue(), stderr)

class CLI(Spec):
    "Command-line behavior"
    def setup(self):
        os.chdir(support)
        self.sys_exit = patch('sys.exit').start()

    def teardown(self):
        patch.stopall()

    class basic_invocation:
        @trap
        def vanilla(self):
            os.chdir('implicit')
            dispatch(['inv', 'foo'])
            eq_(sys.stdout.getvalue(), "Hm\n")

        @trap
        def vanilla_with_explicit_collection(self):
            # Duplicates _output_eq above, but this way that can change w/o
            # breaking our expectations.
            dispatch(['inv', '-c', 'integration', 'print_foo'])
            eq_(sys.stdout.getvalue(), "foo\n")

        def args(self):
            _output_eq(
                ['-c', 'integration', 'print_name', '--name', 'inigo'],
                "inigo\n",
            )

        def underscored_args(self):
            _output_eq(
                ['-c', 'integration',
                    'print_underscored_arg', '--my-option', 'whatevs'],
                "whatevs\n",
            )

    def contextualized_tasks_are_given_parser_context_arg(self):
        # go() in contextualized.py just returns its initial arg
        retval = dispatch(['invoke', '-c', 'contextualized', 'go'])[0]
        assert isinstance(retval, Context)

    def core_help_option_prints_core_help(self):
        # TODO: change dynamically based on parser contents?
        # e.g. no core args == no [--core-opts],
        # no tasks == no task stuff?
        # NOTE: test will trigger default pty size of 80x24, so the below
        # string is formatted appropriately.
        # TODO: add more unit-y tests for specific behaviors:
        # * fill terminal w/ columns + spacing
        # * line-wrap help text in its own column
        expected = """
Usage: inv[oke] [--core-opts] task1 [--task1-opts] ... taskN [--taskN-opts]

Core options:
  --no-dedupe                      Disable task deduplication.
  -c STRING, --collection=STRING   Specify collection name to load. May be
                                   given >1 time.
  -d, --debug                      Enable debug output.
  -e, --echo                       Echo executed commands before running.
  -h [STRING], --help[=STRING]     Show core or per-task help and exit.
  -H STRING, --hide=STRING         Set default value of run()'s 'hide' kwarg.
  -l, --list                       List available tasks.
  -p, --pty                        Use a pty when executing shell commands.
  -r STRING, --root=STRING         Change root directory used for finding task
                                   modules.
  -V, --version                    Show version and exit.
  -w, --warn-only                  Warn, instead of failing, when shell
                                   commands fail.

""".lstrip()
        for flag in ['-h', '--help']:
            _output_eq([flag], expected)

    def per_task_help_prints_help_for_task_only(self):
        expected = """
Usage: inv[oke] [--core-opts] punch [--options] [other tasks here ...]

Docstring:
  none

Options:
  -h STRING, --why=STRING   Motive
  -w STRING, --who=STRING   Who to punch

""".lstrip()
        for flag in ['-h', '--help']:
            _output_eq(['-c', 'decorator', flag, 'punch'], expected)

    def per_task_help_works_for_unparameterized_tasks(self):
        expected = """
Usage: inv[oke] [--core-opts] biz [other tasks here ...]

Docstring:
  none

Options:
  none

""".lstrip()
        _output_eq(['-c', 'decorator', '-h', 'biz'], expected)

    def per_task_help_displays_docstrings_if_given(self):
        expected = """
Usage: inv[oke] [--core-opts] foo [other tasks here ...]

Docstring:
  Foo the bar.

Options:
  none

""".lstrip()
        _output_eq(['-c', 'decorator', '-h', 'foo'], expected)

    def per_task_help_dedents_correctly(self):
        expected = """
Usage: inv[oke] [--core-opts] foo2 [other tasks here ...]

Docstring:
  Foo the bar:

    example code

  Added in 1.0

Options:
  none

""".lstrip()
        _output_eq(['-c', 'decorator', '-h', 'foo2'], expected)

    def version_info(self):
        _output_eq(['-V'], "Invoke %s\n" % invoke.__version__)

    @trap
    def version_override(self):
        dispatch(['notinvoke', '-V'], version="nope 1.0")
        eq_(sys.stdout.getvalue(), "nope 1.0\n")

    class task_list:
        "--list"

        def _listing(self, lines):
            return ("""
Available tasks:

%s

""" % '\n'.join("  " + x for x in lines)).lstrip()

        def _list_eq(self, collection, listing):
            _output_eq(['-c', collection, '--list'], self._listing(listing))

        def simple_output(self):
            expected = self._listing((
                'bar',
                'foo',
                'print_foo',
                'print_name',
                'print_underscored_arg',
            ))
            for flag in ('-l', '--list'):
                _output_eq(['-c', 'integration', flag], expected)

        def namespacing(self):
            self._list_eq('namespacing', (
                'toplevel',
                'module.mytask',
            ))

        def top_level_tasks_listed_first(self):
            self._list_eq('simple_ns_list', (
                'z_toplevel',
                'a.subtask'
            ))

        def subcollections_sorted_in_depth_order(self):
            self._list_eq('deeper_ns_list', (
                'toplevel',
                'a.subtask',
                'a.nother.subtask',
            ))

        def aliases_sorted_alphabetically(self):
            self._list_eq('alias_sorting', (
                'toplevel (a, z)',
            ))

        def default_tasks(self):
            # sub-ns default task display as "real.name (collection name)"
            self._list_eq('explicit_root', (
                'top_level (othertop)',
                'sub.sub_task (sub, sub.othersub)',
            ))

        def docstrings_shown_alongside(self):
            self._list_eq('docstrings', (
                'leading_whitespace    foo',
                'no_docstring',
                'one_line              foo',
                'two_lines             foo',
                'with_aliases (a, b)   foo',
            ))

    def no_deduping(self):
        expected = """
foo
foo
bar
""".lstrip()
        _output_eq(
            ['-c', 'integration', '--no-dedupe', 'foo', 'bar'],
            expected
        )

    def debug_flag_activates_logging(self):
        # Have to patch our logger to get in before Nose logcapture kicks in.
        with patch('invoke.util.debug') as debug:
            dispatch(['inv', '-d', '-c', 'debugging', 'foo'])
            debug.assert_called_with('my-sentinel')

    class run_options:
        "run() related CLI flags"
        def _test_flag(self, flag, kwarg, value):
            with patch('invoke.context.run') as run:
                dispatch(['invoke'] + flag + ['-c', 'contextualized', 'run'])
                run.assert_called_with('x', **{kwarg: value})

        def warn_only(self):
            self._test_flag(['-w'], 'warn', True)

        def pty(self):
            self._test_flag(['-p'], 'pty', True)

        def hide(self):
            self._test_flag(['--hide', 'both'], 'hide', 'both')

        def echo(self):
            self._test_flag(['-e'], 'echo', True)

TB_SENTINEL = 'Traceback (most recent call last)'

class HighLevelFailures(Spec):

    def command_failure(self):
        "Command failure doesn't show tracebacks"
        result = run("inv -c fail simple", warn=True, hide='both')
        assert TB_SENTINEL not in result.stderr
        assert result.exited != 0

    class parsing:
        def should_not_show_tracebacks(self):
            result = run("inv -c fail missing_pos", warn=True, hide='both')
            assert TB_SENTINEL not in result.stderr

        def should_show_core_usage_on_core_failures(self):
            skip()

        def should_show_context_usage_on_context_failures(self):
            skip()

    def load_failure(self):
        skip()

class CLIParsing(Spec):
    """
    High level parsing tests
    """
    def setup(self):
        @task(positional=[])
        def mytask(mystring, s, boolean=False, b=False, v=False,
            long_name=False, true_bool=True):
            pass
        @task(aliases=['mytask27'])
        def mytask2():
            pass
        @task
        def mytask3(mystring):
            pass
        @task
        def mytask4(clean=False, browse=False):
            pass
        @task(aliases=['other'], default=True)
        def subtask():
            pass
        subcoll = Collection('sub', subtask)
        self.c = Collection(mytask, mytask2, mytask3, mytask4, subcoll)

    def _parser(self):
        return Parser(self.c.to_contexts())

    def _parse(self, argstr):
        return self._parser().parse_argv(argstr.split())

    def _compare(self, invoke, flagname, value):
        invoke = "mytask " + invoke
        result = self._parse(invoke)
        eq_(result[0].args[flagname].value, value)

    def _compare_names(self, given, real):
        eq_(self._parse(given)[0].name, real)

    def underscored_flags_can_be_given_as_dashed(self):
        self._compare('--long-name', 'long_name', True)

    def inverse_boolean_flags(self):
        self._compare('--no-true-bool', 'true_bool', False)

    def namespaced_task(self):
        self._compare_names("sub.subtask", "sub.subtask")

    def aliases(self):
        self._compare_names("mytask27", "mytask2")

    def subcollection_aliases(self):
        self._compare_names("sub.other", "sub.subtask")

    def subcollection_default_tasks(self):
        self._compare_names("sub", "sub.subtask")

    def boolean_args(self):
        "mytask --boolean"
        self._compare("--boolean", 'boolean', True)

    def flag_then_space_then_value(self):
        "mytask --mystring foo"
        self._compare("--mystring foo", 'mystring', 'foo')

    def flag_then_equals_sign_then_value(self):
        "mytask --mystring=foo"
        self._compare("--mystring=foo", 'mystring', 'foo')

    def short_boolean_flag(self):
        "mytask -b"
        self._compare("-b", 'b', True)

    def short_flag_then_space_then_value(self):
        "mytask -s value"
        self._compare("-s value", 's', 'value')

    def short_flag_then_equals_sign_then_value(self):
        "mytask -s=value"
        self._compare("-s=value", 's', 'value')

    def short_flag_with_adjacent_value(self):
        "mytask -svalue"
        r = self._parse("mytask -svalue")
        eq_(r[0].args.s.value, 'value')

    def _flag_value_task(self, value):
        r = self._parse("mytask -s %s mytask2" % value)
        eq_(len(r), 2)
        eq_(r[0].name, 'mytask')
        eq_(r[0].args.s.value, value)
        eq_(r[1].name, 'mytask2')

    def flag_value_then_task(self):
        "mytask -s value mytask2"
        self._flag_value_task('value')

    def flag_value_same_as_task_name(self):
        "mytask -s mytask2 mytask2"
        self._flag_value_task('mytask2')

    def three_tasks_with_args(self):
        "mytask --boolean mytask3 --mystring foo mytask2"
        r = self._parse("mytask --boolean mytask3 --mystring foo mytask2")
        eq_(len(r), 3)
        eq_([x.name for x in r], ['mytask', 'mytask3', 'mytask2'])
        eq_(r[0].args.boolean.value, True)
        eq_(r[1].args.mystring.value, 'foo')

    def tasks_with_duplicately_named_kwargs(self):
        "mytask --mystring foo mytask3 --mystring bar"
        r = self._parse("mytask --mystring foo mytask3 --mystring bar")
        eq_(r[0].name, 'mytask')
        eq_(r[0].args.mystring.value, 'foo')
        eq_(r[1].name, 'mytask3')
        eq_(r[1].args.mystring.value, 'bar')

    def multiple_short_flags_adjacent(self):
        "mytask -bv (and inverse)"
        for args in ('-bv', '-vb'):
            r = self._parse("mytask %s" % args)
            a = r[0].args
            eq_(a.b.value, True)
            eq_(a.v.value, True)

    def globbed_shortflags_with_multipass_parsing(self):
        "mytask -cb and -bc"
        for args in ('-bc', '-cb'):
            _, _, r = parse(['invoke', 'mytask4', args], self.c)
            a = r[0].args
            eq_(a.clean.value, True)
            eq_(a.browse.value, True)
<commit_msg>whitespace<commit_after>import os
import sys

from spec import eq_, skip, Spec, ok_, trap
from mock import patch

from invoke.cli import parse, dispatch
from invoke.context import Context
from invoke.runner import run
from invoke.parser import Parser
from invoke.collection import Collection
from invoke.tasks import task
from invoke.exceptions import Failure
import invoke

from _utils import support

@trap
def _output_eq(args, stdout=None, stderr=None):
    """
    dispatch() 'args', matching output to 'std(out|err)'.

    Must give either or both of the output-expecting args.
    """
    args = ['inv'] + args
    dispatch(args)
    if stdout:
        eq_(sys.stdout.getvalue(), stdout)
    if stderr:
        eq_(sys.stderr.getvalue(), stderr)

class CLI(Spec):
    "Command-line behavior"
    def setup(self):
        os.chdir(support)
        self.sys_exit = patch('sys.exit').start()

    def teardown(self):
        patch.stopall()

    class basic_invocation:
        @trap
        def vanilla(self):
            os.chdir('implicit')
            dispatch(['inv', 'foo'])
            eq_(sys.stdout.getvalue(), "Hm\n")

        @trap
        def vanilla_with_explicit_collection(self):
            # Duplicates _output_eq above, but this way that can change w/o
            # breaking our expectations.
            dispatch(['inv', '-c', 'integration', 'print_foo'])
            eq_(sys.stdout.getvalue(), "foo\n")

        def args(self):
            _output_eq(
                ['-c', 'integration', 'print_name', '--name', 'inigo'],
                "inigo\n",
            )

        def underscored_args(self):
            _output_eq(
                ['-c', 'integration',
                    'print_underscored_arg', '--my-option', 'whatevs'],
                "whatevs\n",
            )

    def contextualized_tasks_are_given_parser_context_arg(self):
        # go() in contextualized.py just returns its initial arg
        retval = dispatch(['invoke', '-c', 'contextualized', 'go'])[0]
        assert isinstance(retval, Context)

    def core_help_option_prints_core_help(self):
        # TODO: change dynamically based on parser contents?
        # e.g. no core args == no [--core-opts],
        # no tasks == no task stuff?
        # NOTE: test will trigger default pty size of 80x24, so the below
        # string is formatted appropriately.
        # TODO: add more unit-y tests for specific behaviors:
        # * fill terminal w/ columns + spacing
        # * line-wrap help text in its own column
        expected = """
Usage: inv[oke] [--core-opts] task1 [--task1-opts] ... taskN [--taskN-opts]

Core options:
  --no-dedupe                      Disable task deduplication.
  -c STRING, --collection=STRING   Specify collection name to load. May be
                                   given >1 time.
  -d, --debug                      Enable debug output.
  -e, --echo                       Echo executed commands before running.
  -h [STRING], --help[=STRING]     Show core or per-task help and exit.
  -H STRING, --hide=STRING         Set default value of run()'s 'hide' kwarg.
  -l, --list                       List available tasks.
  -p, --pty                        Use a pty when executing shell commands.
  -r STRING, --root=STRING         Change root directory used for finding task
                                   modules.
  -V, --version                    Show version and exit.
  -w, --warn-only                  Warn, instead of failing, when shell
                                   commands fail.

""".lstrip()
        for flag in ['-h', '--help']:
            _output_eq([flag], expected)

    def per_task_help_prints_help_for_task_only(self):
        expected = """
Usage: inv[oke] [--core-opts] punch [--options] [other tasks here ...]

Docstring:
  none

Options:
  -h STRING, --why=STRING   Motive
  -w STRING, --who=STRING   Who to punch

""".lstrip()
        for flag in ['-h', '--help']:
            _output_eq(['-c', 'decorator', flag, 'punch'], expected)

    def per_task_help_works_for_unparameterized_tasks(self):
        expected = """
Usage: inv[oke] [--core-opts] biz [other tasks here ...]

Docstring:
  none

Options:
  none

""".lstrip()
        _output_eq(['-c', 'decorator', '-h', 'biz'], expected)

    def per_task_help_displays_docstrings_if_given(self):
        expected = """
Usage: inv[oke] [--core-opts] foo [other tasks here ...]

Docstring:
  Foo the bar.

Options:
  none

""".lstrip()
        _output_eq(['-c', 'decorator', '-h', 'foo'], expected)

    def per_task_help_dedents_correctly(self):
        expected = """
Usage: inv[oke] [--core-opts] foo2 [other tasks here ...]

Docstring:
  Foo the bar:

    example code

  Added in 1.0

Options:
  none

""".lstrip()
        _output_eq(['-c', 'decorator', '-h', 'foo2'], expected)

    def version_info(self):
        _output_eq(['-V'], "Invoke %s\n" % invoke.__version__)

    @trap
    def version_override(self):
        dispatch(['notinvoke', '-V'], version="nope 1.0")
        eq_(sys.stdout.getvalue(), "nope 1.0\n")

    class task_list:
        "--list"

        def _listing(self, lines):
            return ("""
Available tasks:

%s

""" % '\n'.join("  " + x for x in lines)).lstrip()

        def _list_eq(self, collection, listing):
            _output_eq(['-c', collection, '--list'], self._listing(listing))

        def simple_output(self):
            expected = self._listing((
                'bar',
                'foo',
                'print_foo',
                'print_name',
                'print_underscored_arg',
            ))
            for flag in ('-l', '--list'):
                _output_eq(['-c', 'integration', flag], expected)

        def namespacing(self):
            self._list_eq('namespacing', (
                'toplevel',
                'module.mytask',
            ))

        def top_level_tasks_listed_first(self):
            self._list_eq('simple_ns_list', (
                'z_toplevel',
                'a.subtask'
            ))

        def subcollections_sorted_in_depth_order(self):
            self._list_eq('deeper_ns_list', (
                'toplevel',
                'a.subtask',
                'a.nother.subtask',
            ))

        def aliases_sorted_alphabetically(self):
            self._list_eq('alias_sorting', (
                'toplevel (a, z)',
            ))

        def default_tasks(self):
            # sub-ns default task display as "real.name (collection name)"
            self._list_eq('explicit_root', (
                'top_level (othertop)',
                'sub.sub_task (sub, sub.othersub)',
            ))

        def docstrings_shown_alongside(self):
            self._list_eq('docstrings', (
                'leading_whitespace    foo',
                'no_docstring',
                'one_line              foo',
                'two_lines             foo',
                'with_aliases (a, b)   foo',
            ))

    def no_deduping(self):
        expected = """
foo
foo
bar
""".lstrip()
        _output_eq(
            ['-c', 'integration', '--no-dedupe', 'foo', 'bar'],
            expected
        )

    def debug_flag_activates_logging(self):
        # Have to patch our logger to get in before Nose logcapture kicks in.
        with patch('invoke.util.debug') as debug:
            dispatch(['inv', '-d', '-c', 'debugging', 'foo'])
            debug.assert_called_with('my-sentinel')

    class run_options:
        "run() related CLI flags"
        def _test_flag(self, flag, kwarg, value):
            with patch('invoke.context.run') as run:
                dispatch(['invoke'] + flag + ['-c', 'contextualized', 'run'])
                run.assert_called_with('x', **{kwarg: value})

        def warn_only(self):
            self._test_flag(['-w'], 'warn', True)

        def pty(self):
            self._test_flag(['-p'], 'pty', True)

        def hide(self):
            self._test_flag(['--hide', 'both'], 'hide', 'both')

        def echo(self):
            self._test_flag(['-e'], 'echo', True)

TB_SENTINEL = 'Traceback (most recent call last)'

class HighLevelFailures(Spec):
    def command_failure(self):
        "Command failure doesn't show tracebacks"
        result = run("inv -c fail simple", warn=True, hide='both')
        assert TB_SENTINEL not in result.stderr
        assert result.exited != 0

    class parsing:
        def should_not_show_tracebacks(self):
            result = run("inv -c fail missing_pos", warn=True, hide='both')
            assert TB_SENTINEL not in result.stderr

        def should_show_core_usage_on_core_failures(self):
            skip()

        def should_show_context_usage_on_context_failures(self):
            skip()

    def load_failure(self):
        skip()

class CLIParsing(Spec):
    """
    High level parsing tests
    """
    def setup(self):
        @task(positional=[])
        def mytask(mystring, s, boolean=False, b=False, v=False,
            long_name=False, true_bool=True):
            pass
        @task(aliases=['mytask27'])
        def mytask2():
            pass
        @task
        def mytask3(mystring):
            pass
        @task
        def mytask4(clean=False, browse=False):
            pass
        @task(aliases=['other'], default=True)
        def subtask():
            pass
        subcoll = Collection('sub', subtask)
        self.c = Collection(mytask, mytask2, mytask3, mytask4, subcoll)

    def _parser(self):
        return Parser(self.c.to_contexts())

    def _parse(self, argstr):
        return self._parser().parse_argv(argstr.split())

    def _compare(self, invoke, flagname, value):
        invoke = "mytask " + invoke
        result = self._parse(invoke)
        eq_(result[0].args[flagname].value, value)

    def _compare_names(self, given, real):
        eq_(self._parse(given)[0].name, real)

    def underscored_flags_can_be_given_as_dashed(self):
        self._compare('--long-name', 'long_name', True)

    def inverse_boolean_flags(self):
        self._compare('--no-true-bool', 'true_bool', False)

    def namespaced_task(self):
        self._compare_names("sub.subtask", "sub.subtask")

    def aliases(self):
        self._compare_names("mytask27", "mytask2")

    def subcollection_aliases(self):
        self._compare_names("sub.other", "sub.subtask")

    def subcollection_default_tasks(self):
        self._compare_names("sub", "sub.subtask")

    def boolean_args(self):
        "mytask --boolean"
        self._compare("--boolean", 'boolean', True)

    def flag_then_space_then_value(self):
        "mytask --mystring foo"
        self._compare("--mystring foo", 'mystring', 'foo')

    def flag_then_equals_sign_then_value(self):
        "mytask --mystring=foo"
        self._compare("--mystring=foo", 'mystring', 'foo')

    def short_boolean_flag(self):
        "mytask -b"
        self._compare("-b", 'b', True)

    def short_flag_then_space_then_value(self):
        "mytask -s value"
        self._compare("-s value", 's', 'value')

    def short_flag_then_equals_sign_then_value(self):
        "mytask -s=value"
        self._compare("-s=value", 's', 'value')

    def short_flag_with_adjacent_value(self):
        "mytask -svalue"
        r = self._parse("mytask -svalue")
        eq_(r[0].args.s.value, 'value')

    def _flag_value_task(self, value):
        r = self._parse("mytask -s %s mytask2" % value)
        eq_(len(r), 2)
        eq_(r[0].name, 'mytask')
        eq_(r[0].args.s.value, value)
        eq_(r[1].name, 'mytask2')

    def flag_value_then_task(self):
        "mytask -s value mytask2"
        self._flag_value_task('value')

    def flag_value_same_as_task_name(self):
        "mytask -s mytask2 mytask2"
        self._flag_value_task('mytask2')

    def three_tasks_with_args(self):
        "mytask --boolean mytask3 --mystring foo mytask2"
        r = self._parse("mytask --boolean mytask3 --mystring foo mytask2")
        eq_(len(r), 3)
        eq_([x.name for x in r], ['mytask', 'mytask3', 'mytask2'])
        eq_(r[0].args.boolean.value, True)
        eq_(r[1].args.mystring.value, 'foo')

    def tasks_with_duplicately_named_kwargs(self):
        "mytask --mystring foo mytask3 --mystring bar"
        r = self._parse("mytask --mystring foo mytask3 --mystring bar")
        eq_(r[0].name, 'mytask')
        eq_(r[0].args.mystring.value, 'foo')
        eq_(r[1].name, 'mytask3')
        eq_(r[1].args.mystring.value, 'bar')

    def multiple_short_flags_adjacent(self):
        "mytask -bv (and inverse)"
        for args in ('-bv', '-vb'):
            r = self._parse("mytask %s" % args)
            a = r[0].args
            eq_(a.b.value, True)
            eq_(a.v.value, True)

    def globbed_shortflags_with_multipass_parsing(self):
        "mytask -cb and -bc"
        for args in ('-bc', '-cb'):
            _, _, r = parse(['invoke', 'mytask4', args], self.c)
            a = r[0].args
            eq_(a.clean.value, True)
            eq_(a.browse.value, True)
<|endoftext|>


<commit_before>"""
salt-ssh testing
"""

import pathlib
import shutil

import pytest

import salt.utils.files
import salt.utils.yaml

pytestmark = [
    pytest.mark.slow_test,
    pytest.mark.skip_on_windows(reason="salt-ssh not available on Windows"),
]

@pytest.fixture(autouse=True)
def thin_dir(salt_ssh_cli):
    try:
        yield
    finally:
        ret = salt_ssh_cli.run("config.get", "thin_dir")
        assert ret.returncode == 0
        thin_dir_path = ret.data
        shutil.rmtree(thin_dir_path, ignore_errors=True)

def test_ping(salt_ssh_cli):
    """
    Test a simple ping
    """
    ret = salt_ssh_cli.run("test.ping")
    assert ret.returncode == 0
    assert ret.data is True

def test_thin_dir(salt_ssh_cli):
    """
    test to make sure thin_dir is created
    and salt-call file is included
    """
    ret = salt_ssh_cli.run("config.get", "thin_dir")
    assert ret.returncode == 0
    thin_dir = pathlib.Path(ret.data)
    assert thin_dir.is_dir()
    assert thin_dir.joinpath("salt-call").exists()
    assert thin_dir.joinpath("running_data").exists()

def test_set_path(salt_ssh_cli, tmp_path, salt_ssh_roster_file):
    """
    test setting the path env variable
    """
    path = "/pathdoesnotexist/"
    roster_file = tmp_path / "roster-set-path"
    with salt.utils.files.fopen(salt_ssh_roster_file) as rfh:
        roster_data = salt.utils.yaml.safe_load(rfh)
        roster_data["localhost"].update(
            {
                "set_path": "$PATH:/usr/local/bin/:{}".format(path),
            }
        )
    with salt.utils.files.fopen(roster_file, "w") as wfh:
        salt.utils.yaml.safe_dump(roster_data, wfh)

    ret = salt_ssh_cli.run(
        "--roster-file={}".format(roster_file), "environ.get", "PATH"
    )
    assert ret.returncode == 0
    assert path in ret.data

def test_tty(salt_ssh_cli, tmp_path, salt_ssh_roster_file):
    """
    test using tty
    """
    roster_file = tmp_path / "roster-tty"
    with salt.utils.files.fopen(salt_ssh_roster_file) as rfh:
        roster_data = salt.utils.yaml.safe_load(rfh)
        roster_data["localhost"].update({"tty": True})
    with salt.utils.files.fopen(roster_file, "w") as wfh:
        salt.utils.yaml.safe_dump(roster_data, wfh)
    ret = salt_ssh_cli.run("--roster-file={}".format(roster_file), "test.ping")
    assert ret.returncode == 0
    assert ret.data is True
<commit_msg>Add test for issue 61083<commit_after>"""
salt-ssh testing
"""

import pathlib
import shutil

import pytest

import salt.utils.files
import salt.utils.yaml

pytestmark = [
    pytest.mark.slow_test,
    pytest.mark.skip_on_windows(reason="salt-ssh not available on Windows"),
]

@pytest.fixture(autouse=True)
def thin_dir(salt_ssh_cli):
    try:
        yield
    finally:
        ret = salt_ssh_cli.run("config.get", "thin_dir")
        assert ret.returncode == 0
        thin_dir_path = ret.data
        shutil.rmtree(thin_dir_path, ignore_errors=True)

def test_ping(salt_ssh_cli):
    """
    Test a simple ping
    """
    ret = salt_ssh_cli.run("test.ping")
    assert ret.returncode == 0
    assert ret.data is True

def test_thin_dir(salt_ssh_cli):
    """
    test to make sure thin_dir is created
    and salt-call file is included
    """
    ret = salt_ssh_cli.run("config.get", "thin_dir")
    assert ret.returncode == 0
    thin_dir = pathlib.Path(ret.data)
    assert thin_dir.is_dir()
    assert thin_dir.joinpath("salt-call").exists()
    assert thin_dir.joinpath("running_data").exists()

def test_wipe(salt_ssh_cli):
    """
    Ensure --wipe is respected by the state module wrapper
    issue #61083
    """
    ret = salt_ssh_cli.run("config.get", "thin_dir")
    assert ret.returncode == 0
    thin_dir = pathlib.Path(ret.data)
    assert thin_dir.exists()
    # only few modules (state and cp) will actually respect --wipe
    # (see commit #8a414d53284ec04940540ebd823306ab5119e105)
    salt_ssh_cli.run("--wipe", "state.apply")
    assert not thin_dir.exists()

def test_set_path(salt_ssh_cli, tmp_path, salt_ssh_roster_file):
    """
    test setting the path env variable
    """
    path = "/pathdoesnotexist/"
    roster_file = tmp_path / "roster-set-path"
    with salt.utils.files.fopen(salt_ssh_roster_file) as rfh:
        roster_data = salt.utils.yaml.safe_load(rfh)
        roster_data["localhost"].update(
            {
                "set_path": "$PATH:/usr/local/bin/:{}".format(path),
            }
        )
    with salt.utils.files.fopen(roster_file, "w") as wfh:
        salt.utils.yaml.safe_dump(roster_data, wfh)

    ret = salt_ssh_cli.run(
        "--roster-file={}".format(roster_file), "environ.get", "PATH"
    )
    assert ret.returncode == 0
    assert path in ret.data

def test_tty(salt_ssh_cli, tmp_path, salt_ssh_roster_file):
    """
    test using tty
    """
    roster_file = tmp_path / "roster-tty"
    with salt.utils.files.fopen(salt_ssh_roster_file) as rfh:
        roster_data = salt.utils.yaml.safe_load(rfh)
        roster_data["localhost"].update({"tty": True})
    with salt.utils.files.fopen(roster_file, "w") as wfh:
        salt.utils.yaml.safe_dump(roster_data, wfh)
    ret = salt_ssh_cli.run("--roster-file={}".format(roster_file), "test.ping")
    assert ret.returncode == 0
    assert ret.data is True
<|endoftext|>


<commit_before>from django.contrib import admin

from us_ignite.hubs.models import Hub, HubActivity

class HubAdmin(admin.ModelAdmin):
    list_display = ('name', 'slug', 'status', 'created')
    search_fields = ('name', 'slug', 'description', 'features')
    date_hierarchy = 'created'
    list_filter = ('status', 'created')

class HubActivityAdmin(admin.ModelAdmin):
    list_display = ('name', 'created')
    search_fields = ('name', 'description')
    date_hierarchy = 'created'
    list_filter = ('created', )

admin.site.register(Hub, HubAdmin)
admin.site.register(HubActivity, HubActivityAdmin)
<commit_msg>List ``HubRequest`` in the admin section.<commit_after>from django.contrib import admin

from us_ignite.hubs.models import Hub, HubActivity, HubRequest

class HubRequestAdmin(admin.ModelAdmin):
    list_display = ('name', 'status', 'created')
    search_fields = ('name', 'description')
    date_hierarchy = 'created'
    list_filter = ('status', 'created')

class HubAdmin(admin.ModelAdmin):
    list_display = ('name', 'slug', 'status', 'created')
    search_fields = ('name', 'slug', 'description', 'summary')
    date_hierarchy = 'created'
    list_filter = ('status', 'created')

class HubActivityAdmin(admin.ModelAdmin):
    list_display = ('name', 'created')
    search_fields = ('name', 'description')
    date_hierarchy = 'created'
    list_filter = ('created', )

admin.site.register(Hub, HubAdmin)
admin.site.register(HubActivity, HubActivityAdmin)
admin.site.register(HubRequest, HubRequestAdmin)
<|endoftext|>


<commit_before>from __future__ import with_statement

import os

from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import reverse, clear_url_caches
from django.test import TestCase
from django.test.utils import override_settings
from django.template import Template, Context
from django.utils import translation

class URLTestCaseBase(TestCase):
    """
    TestCase base-class for the URL tests.
    """
    urls = 'regressiontests.i18n.patterns.urls.default'

    def setUp(self):
        # Make sure the cache is empty before we are doing our tests.
        clear_url_caches()

    def tearDown(self):
        # Make sure we will leave an empty cache for other testcases.
        clear_url_caches()

URLTestCaseBase = override_settings(
    USE_I18N=True,
    LOCALE_PATHS=(
        os.path.join(os.path.dirname(__file__), 'locale'),
    ),
    TEMPLATE_DIRS=(
        os.path.join(os.path.dirname(__file__), 'templates'),
    ),
    LANGUAGE_CODE='en',
    LANGUAGES=(
        ('nl', 'Dutch'),
        ('en', 'English'),
        ('pt-br', 'Brazilian Portuguese'),
    ),
    MIDDLEWARE_CLASSES=(
        'django.middleware.locale.LocaleMiddleware',
        'django.middleware.common.CommonMiddleware',
    ),
)(URLTestCaseBase)

class URLPrefixTests(URLTestCaseBase):
    """
    Tests if the `i18n_patterns` is adding the prefix correctly.
    """
    def test_not_prefixed(self):
        with translation.override('en'):
            self.assertEqual(reverse('not-prefixed'), '/not-prefixed/')
        with translation.override('nl'):
            self.assertEqual(reverse('not-prefixed'), '/not-prefixed/')

    def test_prefixed(self):
        with translation.override('en'):
            self.assertEqual(reverse('prefixed'), '/en/prefixed/')
        with translation.override('nl'):
            self.assertEqual(reverse('prefixed'), '/nl/prefixed/')

    @override_settings(ROOT_URLCONF='regressiontests.i18n.patterns.urls.wrong')
    def test_invalid_prefix_use(self):
        self.assertRaises(ImproperlyConfigured, lambda: reverse('account:register'))

class URLDisabledTests(URLTestCaseBase):
    urls = 'regressiontests.i18n.patterns.urls.disabled'

    @override_settings(USE_I18N=False)
    def test_prefixed_i18n_disabled(self):
        with translation.override('en'):
            self.assertEqual(reverse('prefixed'), '/prefixed/')
        with translation.override('nl'):
            self.assertEqual(reverse('prefixed'), '/prefixed/')

class URLTranslationTests(URLTestCaseBase):
    """
    Tests if the pattern-strings are translated correctly (within the
    `i18n_patterns` and the normal `patterns` function).
    """
    def test_no_prefix_translated(self):
        with translation.override('en'):
            self.assertEqual(reverse('no-prefix-translated'), '/translated/')
            self.assertEqual(reverse('no-prefix-translated-slug', kwargs={'slug': 'yeah'}), '/translated/yeah/')

        with translation.override('nl'):
            self.assertEqual(reverse('no-prefix-translated'), '/vertaald/')
            self.assertEqual(reverse('no-prefix-translated-slug', kwargs={'slug': 'yeah'}), '/vertaald/yeah/')

        with translation.override('pt-br'):
            self.assertEqual(reverse('no-prefix-translated'), '/traduzidos/')
            self.assertEqual(reverse('no-prefix-translated-slug', kwargs={'slug': 'yeah'}), '/traduzidos/yeah/')

    def test_users_url(self):
        with translation.override('en'):
            self.assertEqual(reverse('users'), '/en/users/')

        with translation.override('nl'):
            self.assertEqual(reverse('users'), '/nl/gebruikers/')

        with translation.override('pt-br'):
            self.assertEqual(reverse('users'), '/pt-br/usuarios/')

class URLNamespaceTests(URLTestCaseBase):
    """
    Tests if the translations are still working within namespaces.
    """
    def test_account_register(self):
        with translation.override('en'):
            self.assertEqual(reverse('account:register'), '/en/account/register/')

        with translation.override('nl'):
            self.assertEqual(reverse('account:register'), '/nl/profiel/registeren/')

class URLRedirectTests(URLTestCaseBase):
    """
    Tests if the user gets redirected to the right URL when there is no
    language-prefix in the request URL.
    """
    def test_no_prefix_response(self):
        response = self.client.get('/not-prefixed/')
        self.assertEqual(response.status_code, 200)

    def test_en_redirect(self):
        response = self.client.get('/account/register/', HTTP_ACCEPT_LANGUAGE='en')
        self.assertRedirects(response, 'http://testserver/en/account/register/')

        response = self.client.get(response['location'])
        self.assertEqual(response.status_code, 200)

    def test_en_redirect_wrong_url(self):
        response = self.client.get('/profiel/registeren/', HTTP_ACCEPT_LANGUAGE='en')
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response['location'], 'http://testserver/en/profiel/registeren/')

        response = self.client.get(response['location'])
        self.assertEqual(response.status_code, 404)

    def test_nl_redirect(self):
        response = self.client.get('/profiel/registeren/', HTTP_ACCEPT_LANGUAGE='nl')
        self.assertRedirects(response, 'http://testserver/nl/profiel/registeren/')

        response = self.client.get(response['location'])
        self.assertEqual(response.status_code, 200)

    def test_nl_redirect_wrong_url(self):
        response = self.client.get('/account/register/', HTTP_ACCEPT_LANGUAGE='nl')
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response['location'], 'http://testserver/nl/account/register/')

        response = self.client.get(response['location'])
        self.assertEqual(response.status_code, 404)

    def test_pt_br_redirect(self):
        response = self.client.get('/conta/registre-se/', HTTP_ACCEPT_LANGUAGE='pt-br')
        self.assertRedirects(response, 'http://testserver/pt-br/conta/registre-se/')

        response = self.client.get(response['location'])
        self.assertEqual(response.status_code, 200)

class URLRedirectWithoutTrailingSlashTests(URLTestCaseBase):
    """
    Tests the redirect when the requested URL doesn't end with a slash
    (`settings.APPEND_SLASH=True`).
    """
    def test_not_prefixed_redirect(self):
        response = self.client.get('/not-prefixed', HTTP_ACCEPT_LANGUAGE='en')
        self.assertEqual(response.status_code, 301)
        self.assertEqual(response['location'], 'http://testserver/not-prefixed/')

    def test_en_redirect(self):
        response = self.client.get('/account/register', HTTP_ACCEPT_LANGUAGE='en')
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response['location'], 'http://testserver/en/account/register')

        response = self.client.get(response['location'])
        self.assertEqual(response.status_code, 301)
        self.assertEqual(response['location'], 'http://testserver/en/account/register/')

class URLRedirectWithoutTrailingSlashSettingTests(URLTestCaseBase):
    """
    Tests the redirect when the requested URL doesn't end with a slash
    (`settings.APPEND_SLASH=False`).
    """
    @override_settings(APPEND_SLASH=False)
    def test_not_prefixed_redirect(self):
        response = self.client.get('/not-prefixed', HTTP_ACCEPT_LANGUAGE='en')
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response['location'], 'http://testserver/en/not-prefixed')

        response = self.client.get(response['location'])
        self.assertEqual(response.status_code, 404)

    @override_settings(APPEND_SLASH=False)
    def test_en_redirect(self):
        response = self.client.get('/account/register', HTTP_ACCEPT_LANGUAGE='en')
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response['location'], 'http://testserver/en/account/register')

        response = self.client.get(response['location'])
        self.assertEqual(response.status_code, 404)

class URLResponseTests(URLTestCaseBase):
    """
    Tests if the response has the right language-code.
    """
    def test_not_prefixed_with_prefix(self):
        response = self.client.get('/en/not-prefixed/')
        self.assertEqual(response.status_code, 404)

    def test_en_url(self):
        response = self.client.get('/en/account/register/')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response['content-language'], 'en')
        self.assertEqual(response.context['LANGUAGE_CODE'], 'en')

    def test_nl_url(self):
        response = self.client.get('/nl/profiel/registeren/')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response['content-language'], 'nl')
        self.assertEqual(response.context['LANGUAGE_CODE'], 'nl')

    def test_wrong_en_prefix(self):
        response = self.client.get('/en/profiel/registeren/')
        self.assertEqual(response.status_code, 404)

    def test_wrong_nl_prefix(self):
        response = self.client.get('/nl/account/register/')
        self.assertEqual(response.status_code, 404)

    def test_pt_br_url(self):
        response = self.client.get('/pt-br/conta/registre-se/')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response['content-language'], 'pt-br')
        self.assertEqual(response.context['LANGUAGE_CODE'], 'pt-br')

class URLTagTests(URLTestCaseBase):
    """
    Test if the language tag works.
    """
    def test_strings_only(self):
        t = Template("""{% load i18n %}
            {% language 'nl' %}{% url no-prefix-translated %}{% endlanguage %}
            {% language 'pt-br' %}{% url no-prefix-translated %}{% endlanguage %}""")
        self.assertEqual(t.render(Context({})).strip().split(),
                         [u'/vertaald/', u'/traduzidos/'])

    def test_context(self):
        ctx = Context({'lang1':'nl', 'lang2':'pt-br'})
        tpl = Template("""{% load i18n %}
            {% language lang1 %}{% url no-prefix-translated %}{% endlanguage %}
            {% language lang2 %}{% url no-prefix-translated %}{% endlanguage %}""")
        self.assertEqual(tpl.render(ctx).strip().split(),
                         [u'/vertaald/', u'/traduzidos/'])

    def test_args(self):
        tpl = Template("""{% load i18n %}
            {% language 'nl' %}{% url no-prefix-translated-slug 'apo' %}{% endlanguage %}
            {% language 'pt-br' %}{% url no-prefix-translated-slug 'apo' %}{% endlanguage %}""")
        self.assertEqual(tpl.render(Context({})).strip().split(),
                         [u'/vertaald/apo/', u'/traduzidos/apo/'])

    def test_kwargs(self):
        tpl = Template("""{% load i18n %}
            {% language 'nl'  %}{% url no-prefix-translated-slug slug='apo' %}{% endlanguage %}
            {% language 'pt-br' %}{% url no-prefix-translated-slug slug='apo' %}{% endlanguage %}""")
        self.assertEqual(tpl.render(Context({})).strip().split(),
                         [u'/vertaald/apo/', u'/traduzidos/apo/'])

    def test_future_kwargs(self):
        tpl = Template("""{% load i18n %}{% load url from future %}
            {% language 'nl'  %}{% url 'no-prefix-translated-slug' slug='apo' %}{% endlanguage %}
            {% language 'pt-br' %}{% url 'no-prefix-translated-slug' slug='apo' %}{% endlanguage %}""")
        self.assertEqual(tpl.render(Context({})).strip().split(),
                         [u'/vertaald/apo/', u'/traduzidos/apo/'])
<commit_msg>FIlter warnings about deprecated syntax of the `url` template tag in recently added tests.<commit_after>from __future__ import with_statement

import os
import warnings

from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import reverse, clear_url_caches
from django.test import TestCase
from django.test.utils import override_settings
from django.template import Template, Context
from django.utils import translation

class URLTestCaseBase(TestCase):
    """
    TestCase base-class for the URL tests.
    """
    urls = 'regressiontests.i18n.patterns.urls.default'

    def setUp(self):
        # Make sure the cache is empty before we are doing our tests.
        clear_url_caches()

    def tearDown(self):
        # Make sure we will leave an empty cache for other testcases.
        clear_url_caches()

URLTestCaseBase = override_settings(
    USE_I18N=True,
    LOCALE_PATHS=(
        os.path.join(os.path.dirname(__file__), 'locale'),
    ),
    TEMPLATE_DIRS=(
        os.path.join(os.path.dirname(__file__), 'templates'),
    ),
    LANGUAGE_CODE='en',
    LANGUAGES=(
        ('nl', 'Dutch'),
        ('en', 'English'),
        ('pt-br', 'Brazilian Portuguese'),
    ),
    MIDDLEWARE_CLASSES=(
        'django.middleware.locale.LocaleMiddleware',
        'django.middleware.common.CommonMiddleware',
    ),
)(URLTestCaseBase)

class URLPrefixTests(URLTestCaseBase):
    """
    Tests if the `i18n_patterns` is adding the prefix correctly.
    """
    def test_not_prefixed(self):
        with translation.override('en'):
            self.assertEqual(reverse('not-prefixed'), '/not-prefixed/')
        with translation.override('nl'):
            self.assertEqual(reverse('not-prefixed'), '/not-prefixed/')

    def test_prefixed(self):
        with translation.override('en'):
            self.assertEqual(reverse('prefixed'), '/en/prefixed/')
        with translation.override('nl'):
            self.assertEqual(reverse('prefixed'), '/nl/prefixed/')

    @override_settings(ROOT_URLCONF='regressiontests.i18n.patterns.urls.wrong')
    def test_invalid_prefix_use(self):
        self.assertRaises(ImproperlyConfigured, lambda: reverse('account:register'))

class URLDisabledTests(URLTestCaseBase):
    urls = 'regressiontests.i18n.patterns.urls.disabled'

    @override_settings(USE_I18N=False)
    def test_prefixed_i18n_disabled(self):
        with translation.override('en'):
            self.assertEqual(reverse('prefixed'), '/prefixed/')
        with translation.override('nl'):
            self.assertEqual(reverse('prefixed'), '/prefixed/')

class URLTranslationTests(URLTestCaseBase):
    """
    Tests if the pattern-strings are translated correctly (within the
    `i18n_patterns` and the normal `patterns` function).
    """
    def test_no_prefix_translated(self):
        with translation.override('en'):
            self.assertEqual(reverse('no-prefix-translated'), '/translated/')
            self.assertEqual(reverse('no-prefix-translated-slug', kwargs={'slug': 'yeah'}), '/translated/yeah/')

        with translation.override('nl'):
            self.assertEqual(reverse('no-prefix-translated'), '/vertaald/')
            self.assertEqual(reverse('no-prefix-translated-slug', kwargs={'slug': 'yeah'}), '/vertaald/yeah/')

        with translation.override('pt-br'):
            self.assertEqual(reverse('no-prefix-translated'), '/traduzidos/')
            self.assertEqual(reverse('no-prefix-translated-slug', kwargs={'slug': 'yeah'}), '/traduzidos/yeah/')

    def test_users_url(self):
        with translation.override('en'):
            self.assertEqual(reverse('users'), '/en/users/')

        with translation.override('nl'):
            self.assertEqual(reverse('users'), '/nl/gebruikers/')

        with translation.override('pt-br'):
            self.assertEqual(reverse('users'), '/pt-br/usuarios/')

class URLNamespaceTests(URLTestCaseBase):
    """
    Tests if the translations are still working within namespaces.
    """
    def test_account_register(self):
        with translation.override('en'):
            self.assertEqual(reverse('account:register'), '/en/account/register/')

        with translation.override('nl'):
            self.assertEqual(reverse('account:register'), '/nl/profiel/registeren/')

class URLRedirectTests(URLTestCaseBase):
    """
    Tests if the user gets redirected to the right URL when there is no
    language-prefix in the request URL.
    """
    def test_no_prefix_response(self):
        response = self.client.get('/not-prefixed/')
        self.assertEqual(response.status_code, 200)

    def test_en_redirect(self):
        response = self.client.get('/account/register/', HTTP_ACCEPT_LANGUAGE='en')
        self.assertRedirects(response, 'http://testserver/en/account/register/')

        response = self.client.get(response['location'])
        self.assertEqual(response.status_code, 200)

    def test_en_redirect_wrong_url(self):
        response = self.client.get('/profiel/registeren/', HTTP_ACCEPT_LANGUAGE='en')
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response['location'], 'http://testserver/en/profiel/registeren/')

        response = self.client.get(response['location'])
        self.assertEqual(response.status_code, 404)

    def test_nl_redirect(self):
        response = self.client.get('/profiel/registeren/', HTTP_ACCEPT_LANGUAGE='nl')
        self.assertRedirects(response, 'http://testserver/nl/profiel/registeren/')

        response = self.client.get(response['location'])
        self.assertEqual(response.status_code, 200)

    def test_nl_redirect_wrong_url(self):
        response = self.client.get('/account/register/', HTTP_ACCEPT_LANGUAGE='nl')
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response['location'], 'http://testserver/nl/account/register/')

        response = self.client.get(response['location'])
        self.assertEqual(response.status_code, 404)

    def test_pt_br_redirect(self):
        response = self.client.get('/conta/registre-se/', HTTP_ACCEPT_LANGUAGE='pt-br')
        self.assertRedirects(response, 'http://testserver/pt-br/conta/registre-se/')

        response = self.client.get(response['location'])
        self.assertEqual(response.status_code, 200)

class URLRedirectWithoutTrailingSlashTests(URLTestCaseBase):
    """
    Tests the redirect when the requested URL doesn't end with a slash
    (`settings.APPEND_SLASH=True`).
    """
    def test_not_prefixed_redirect(self):
        response = self.client.get('/not-prefixed', HTTP_ACCEPT_LANGUAGE='en')
        self.assertEqual(response.status_code, 301)
        self.assertEqual(response['location'], 'http://testserver/not-prefixed/')

    def test_en_redirect(self):
        response = self.client.get('/account/register', HTTP_ACCEPT_LANGUAGE='en')
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response['location'], 'http://testserver/en/account/register')

        response = self.client.get(response['location'])
        self.assertEqual(response.status_code, 301)
        self.assertEqual(response['location'], 'http://testserver/en/account/register/')

class URLRedirectWithoutTrailingSlashSettingTests(URLTestCaseBase):
    """
    Tests the redirect when the requested URL doesn't end with a slash
    (`settings.APPEND_SLASH=False`).
    """
    @override_settings(APPEND_SLASH=False)
    def test_not_prefixed_redirect(self):
        response = self.client.get('/not-prefixed', HTTP_ACCEPT_LANGUAGE='en')
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response['location'], 'http://testserver/en/not-prefixed')

        response = self.client.get(response['location'])
        self.assertEqual(response.status_code, 404)

    @override_settings(APPEND_SLASH=False)
    def test_en_redirect(self):
        response = self.client.get('/account/register', HTTP_ACCEPT_LANGUAGE='en')
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response['location'], 'http://testserver/en/account/register')

        response = self.client.get(response['location'])
        self.assertEqual(response.status_code, 404)

class URLResponseTests(URLTestCaseBase):
    """
    Tests if the response has the right language-code.
    """
    def test_not_prefixed_with_prefix(self):
        response = self.client.get('/en/not-prefixed/')
        self.assertEqual(response.status_code, 404)

    def test_en_url(self):
        response = self.client.get('/en/account/register/')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response['content-language'], 'en')
        self.assertEqual(response.context['LANGUAGE_CODE'], 'en')

    def test_nl_url(self):
        response = self.client.get('/nl/profiel/registeren/')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response['content-language'], 'nl')
        self.assertEqual(response.context['LANGUAGE_CODE'], 'nl')

    def test_wrong_en_prefix(self):
        response = self.client.get('/en/profiel/registeren/')
        self.assertEqual(response.status_code, 404)

    def test_wrong_nl_prefix(self):
        response = self.client.get('/nl/account/register/')
        self.assertEqual(response.status_code, 404)

    def test_pt_br_url(self):
        response = self.client.get('/pt-br/conta/registre-se/')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response['content-language'], 'pt-br')
        self.assertEqual(response.context['LANGUAGE_CODE'], 'pt-br')

class URLTagTests(URLTestCaseBase):
    """
    Test if the language tag works.
    """
    def setUp(self):
        self.save_warnings_state()
        warnings.filterwarnings('ignore', category=DeprecationWarning,
                                module='django.template.defaulttags')

    def tearDown(self):
        self.restore_warnings_state()

    def test_strings_only(self):
        t = Template("""{% load i18n %}
            {% language 'nl' %}{% url no-prefix-translated %}{% endlanguage %}
            {% language 'pt-br' %}{% url no-prefix-translated %}{% endlanguage %}""")
        self.assertEqual(t.render(Context({})).strip().split(),
                         [u'/vertaald/', u'/traduzidos/'])

    def test_context(self):
        ctx = Context({'lang1':'nl', 'lang2':'pt-br'})
        tpl = Template("""{% load i18n %}
            {% language lang1 %}{% url no-prefix-translated %}{% endlanguage %}
            {% language lang2 %}{% url no-prefix-translated %}{% endlanguage %}""")
        self.assertEqual(tpl.render(ctx).strip().split(),
                         [u'/vertaald/', u'/traduzidos/'])

    def test_args(self):
        tpl = Template("""{% load i18n %}
            {% language 'nl' %}{% url no-prefix-translated-slug 'apo' %}{% endlanguage %}
            {% language 'pt-br' %}{% url no-prefix-translated-slug 'apo' %}{% endlanguage %}""")
        self.assertEqual(tpl.render(Context({})).strip().split(),
                         [u'/vertaald/apo/', u'/traduzidos/apo/'])

    def test_kwargs(self):
        tpl = Template("""{% load i18n %}
            {% language 'nl'  %}{% url no-prefix-translated-slug slug='apo' %}{% endlanguage %}
            {% language 'pt-br' %}{% url no-prefix-translated-slug slug='apo' %}{% endlanguage %}""")
        self.assertEqual(tpl.render(Context({})).strip().split(),
                         [u'/vertaald/apo/', u'/traduzidos/apo/'])

    def test_future_kwargs(self):
        tpl = Template("""{% load i18n %}{% load url from future %}
            {% language 'nl'  %}{% url 'no-prefix-translated-slug' slug='apo' %}{% endlanguage %}
            {% language 'pt-br' %}{% url 'no-prefix-translated-slug' slug='apo' %}{% endlanguage %}""")
        self.assertEqual(tpl.render(Context({})).strip().split(),
                         [u'/vertaald/apo/', u'/traduzidos/apo/'])
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

"""
Copyright [2009-2018] EMBL-European Bioinformatics Institute
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import pytest

from rnacentral_pipeline.rnacentral.precompute.rna_type import rna_type_of

from .helpers import load_data

@pytest.mark.parametrize('rna_id,rna_type', [  # pylint: disable=no-member
    ('URS0000000DBF_6239', 'piRNA'),
    ('URS00000101E5_9606', 'lncRNA'),
    ('URS0000016972_6239', 'miRNA'),
    ('URS000001E7BA_559292', 'tRNA'),
    ('URS00000478B7_9606', 'SRP_RNA'),
    ('URS000006F31F_4932', 'snoRNA'),
    ('URS0000086133_9606', 'misc_RNA'),
    ('URS00000AEE53_380749', 'tmRNA'),
    ('URS00000B3045_7227', 'guide_RNA'),
    ('URS00000DA486_3702', 'other'),
    ('URS00000F9D45_9606', 'rRNA'),
    ('URS000012DE89_9606', 'autocatalytically_spliced_intron'),
    ('URS0000130A6B_3702', 'precursor_RNA'),
    ('URS000013DDAE_4932', 'snoRNA'),
    ('URS000013F331_9606', 'RNase_P_RNA'),
    ('URS0000157BA2_4896', 'antisense_RNA'),
    ('URS000015995E_4615', 'miRNA'),
    ('URS000018EB2E_3702', 'lncRNA'),
    ('URS0000193C7E_9606', 'scRNA'),
    ('URS000019E0CD_9606', 'lncRNA'),
    ('URS00001DEEBE_562', 'tRNA'),
    ('URS00001E2C22_3702', 'rRNA'),
    ('URS000021515D_322710', 'autocatalytically_spliced_intron'),
    ('URS000024083D_9606', 'SRP_RNA'),
    ('URS000024F0F7_4932', 'snoRNA'),
    ('URS000025C52E_9606', 'other'),
    ('URS00002963C4_4565', 'SRP_RNA'),
    ('URS00002AE808_10090', 'miRNA'),  # ENA has it as piRNA
    ('URS00002F216C_36329', 'antisense_RNA'),
    ('URS00002F21DA_7227', 'precursor_RNA'),
    ('URS00003054F4_6239', 'piRNA'),
    ('URS000032B6B6_9606', 'snRNA'),
    ('URS000034C5CB_7227', 'SRP_RNA'),
    ('URS000037602E_9606', 'tmRNA'),
    ('URS00003AC4AA_3702', 'other'),
    ('URS00003BECAC_9606', 'lncRNA'),
    ('URS00003CE153_9606', 'lncRNA'),
    ('URS00003EBD9A_9913', 'telomerase_RNA'),
    ('URS00003EE18C_9544', 'vault_RNA'),
    ('URS0000409697_3702', 'tRNA'),
    ('URS000040F7EF_4577', 'siRNA'),
    ('URS000045EBF2_9606', 'lncRNA'),
    ('URS0000466DE6_6239', 'miRNA'),
    ('URS000048B30C_3702', 'tRNA'),
    ('URS000049E122_9606', 'misc_RNA'),
    ('URS00004A2461_9606', 'Y_RNA'),
    ('URS00004B11CA_223283', 'scRNA'),
    ('URS00004E52D3_10090', 'lncRNA'),
    ('URS00004E9E38_7227', 'miRNA'),
    ('URS00004FB44B_6239', 'rRNA'),
    ('URS000051DCEC_10090', 'snoRNA'),
    ('URS00005511ED_6239', 'lncRNA'),
    ('URS000055786A_7227', 'miRNA'),
    ('URS0000563A36_7227', 'snoRNA'),
    ('URS0000564CC6_224308', 'tmRNA'),
    ('URS0000569A4A_9606', 'snoRNA'),
    ('URS000059A8B2_7227', 'rasiRNA'),
    ('URS000059EA49_32644', 'tmRNA'),
    ('URS00005A245E_10090', 'tRNA'),
    ('URS00005CDD41_352472', 'RNase_P_RNA'),
    ('URS00005CF03F_9606', 'Y_RNA'),
    ('URS00005D0BAB_9606', 'piRNA'),
    ('URS00005EF0FF_4577', 'siRNA'),
    ('URS00005F2C2D_4932', 'rRNA'),
    ('URS00005F4CAF_3702', 'tRNA'),
    ('URS000060B496_10090', 'snoRNA'),
    ('URS000061DECF_1235461', 'autocatalytically_spliced_intron'),
    ('URS000061F377_559292', 'rRNA'),
    ('URS00006233F9_9606', 'ribozyme'),
    ('URS000064A09E_13616', 'vault_RNA'),
    ('URS00006550DA_10090', 'snoRNA'),
    ('URS000065BB41_7955', 'other'),
    ('URS0000661037_7955', 'tRNA'),
    ('URS000069D7FA_6239', 'tRNA'),
    ('URS00006A938C_10090', 'ribozyme'),
    ('URS00006B3271_10090', 'snoRNA'),
    ('URS00006BA413_9606', 'snoRNA'),
    ('URS00006C670E_30608', 'hammerhead_ribozyme'),
    ('URS00006CE02F_9606', 'snoRNA'),
    ('URS00006D80BC_9913', 'precursor_RNA'),
    ('URS00006DC8B9_6239', 'tRNA'),
    ('URS0000714027_9031', 'other'),
    ('URS00007150F8_9913', 'precursor_RNA'),
    ('URS0000732D5D_9606', 'lncRNA'),
    ('URS0000734D8F_9606', 'snRNA'),
    ('URS0000759BEC_9606', 'lncRNA'),
    ('URS000075A336_9606', 'miRNA'),
    ('URS000075A546_9606', 'precursor_RNA'),
    ('URS000075C290_9606', 'precursor_RNA'),
    ('URS000075C808_9606', 'lncRNA'),
    ('URS000075CC93_9606', 'precursor_RNA'),
    ('URS000075CF25_9913', 'precursor_RNA'),
    ('URS000075EF5D_9606', 'snRNA'),
    ('URS0000764CCC_1415657', 'RNase_P_RNA'),
    ('URS000077FBEB_9606', 'lncRNA'),
    ('URS00007A9FDC_6239', 'misc_RNA'),
    ('URS00007FD8A3_7227', 'lncRNA'),
    ('URS000080DD33_32630', 'ribozyme'),
    ('URS000080DFDA_32630', 'hammerhead_ribozyme'),
    ('URS000082AF7D_5699', 'guide_RNA'),
    ('URS000086852D_32630', 'hammerhead_ribozyme'),
    ('URS00008E398A_9606', 'snoRNA'),
    ('URS00008E3A1B_10090', 'lncRNA'),
    ('URS000092FF0A_9371', 'snoRNA'),
    ('URS00009E8F92_885695', 'rRNA'),
    ('URS0000A767C0_3702', 'lncRNA'),
    ('URS0000A86584_10090', 'lncRNA'),
    ('URS0000A8F612_9371', 'snoRNA'),
    ('URS0000A994FE_9606', 'other'),
    ('URS0000ABD7EF_9606', 'rRNA'),
    ('URS0000ABD87F_9606', 'rRNA'),
    ('URS000018B855_1270', 'misc_RNA'),
    ('URS0000127C85_175245', 'misc_RNA'),
    ('URS00002B64E6_7227', 'scaRNA'),
    ('URS00002BF60E_7227', 'scaRNA'),
    ('URS00008E39F3_7227', 'scaRNA'),
    ('URS0000508014_7227', 'scaRNA'),
    ('URS000049EC81_7227', 'scaRNA'),

    # pytest.param('URS00007CD270_1872691', 'rRNA', mark=pytest.mark.xfail("Rfam")),
    # pytest.param('URS0000A85A32_10090', 'tRNA', mark=pytest.mark.xfail("Rfam")),

    # pytest.param('URS000060C682_9606', 'vault_RNA', mark=pytest.mark.xfail("Inactive sequence")),
    # pytest.param('URS0000175007_7227', 'miRNA', mark=pytest.mark.xfail("Inactive sequence")),
    # pytest.param('URS000072A167_10141', 'Y_RNA', mark=pytest.mark.xfail("Inactive sequence")),
    # pytest.param('URS00006B14E9_6183', 'hammerhead_ribozyme', mark=pytest.mark.xfail("Inactive sequence")),
    # pytest.param('URS0000A17B82_640938', 'other', mark=pytest.mark.xfail("Inactive sequence")),
    # pyest.param('URS000083F182_242161', 'other', mark=pytest.mark.xfail("Inactive sequence")),
    # pytest.param('URS0000808D19_644', 'hammerhead_ribozyme', mark=pytest.mark.xfail("Inactive sequence")),
    # pytest.mark('URS0000808D70_1478174', 'tmRNA', mark=pytest.mark.xfail("Inactive sequence"),

])
def test_computes_correct_rna_types(rna_id, rna_type):
    data = load_data(rna_id)
    assert rna_type_of(data) == rna_type
<commit_msg>More examples to fix<commit_after># -*- coding: utf-8 -*-

"""
Copyright [2009-2018] EMBL-European Bioinformatics Institute
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import pytest

from rnacentral_pipeline.rnacentral.precompute.rna_type import rna_type_of

from .helpers import load_data

@pytest.mark.parametrize('rna_id,rna_type', [  # pylint: disable=no-member
    ('URS0000000DBF_6239', 'piRNA'),
    ('URS00000101E5_9606', 'lncRNA'),
    ('URS0000016972_6239', 'miRNA'),
    ('URS000001E7BA_559292', 'tRNA'),
    ('URS00000478B7_9606', 'SRP_RNA'),
    ('URS000006F31F_4932', 'snoRNA'),
    ('URS0000086133_9606', 'misc_RNA'),
    ('URS00000AEE53_380749', 'tmRNA'),
    ('URS00000B3045_7227', 'guide_RNA'),
    ('URS00000DA486_3702', 'other'),
    ('URS00000F9D45_9606', 'rRNA'),
    ('URS000012DE89_9606', 'autocatalytically_spliced_intron'),
    ('URS0000130A6B_3702', 'precursor_RNA'),
    ('URS000013DDAE_4932', 'snoRNA'),
    ('URS000013F331_9606', 'RNase_P_RNA'),
    ('URS0000157BA2_4896', 'antisense_RNA'),
    ('URS000015995E_4615', 'miRNA'),
    ('URS000018EB2E_3702', 'lncRNA'),
    ('URS0000193C7E_9606', 'scRNA'),
    ('URS000019E0CD_9606', 'lncRNA'),
    ('URS00001DEEBE_562', 'tRNA'),
    ('URS00001E2C22_3702', 'rRNA'),
    ('URS000021515D_322710', 'autocatalytically_spliced_intron'),
    ('URS000024083D_9606', 'SRP_RNA'),
    ('URS000024F0F7_4932', 'snoRNA'),
    ('URS000025C52E_9606', 'other'),
    ('URS00002963C4_4565', 'SRP_RNA'),
    ('URS00002AE808_10090', 'miRNA'),  # ENA has it as piRNA
    ('URS00002F216C_36329', 'antisense_RNA'),
    ('URS00002F21DA_7227', 'precursor_RNA'),
    ('URS00003054F4_6239', 'piRNA'),
    ('URS000032B6B6_9606', 'snRNA'),
    ('URS000034C5CB_7227', 'SRP_RNA'),
    ('URS000037602E_9606', 'tmRNA'),
    ('URS00003AC4AA_3702', 'other'),
    ('URS00003BECAC_9606', 'lncRNA'),
    ('URS00003CE153_9606', 'lncRNA'),
    ('URS00003EBD9A_9913', 'telomerase_RNA'),
    ('URS00003EE18C_9544', 'vault_RNA'),
    ('URS0000409697_3702', 'tRNA'),
    ('URS000040F7EF_4577', 'siRNA'),
    ('URS000045EBF2_9606', 'lncRNA'),
    ('URS0000466DE6_6239', 'miRNA'),
    ('URS000048B30C_3702', 'tRNA'),
    ('URS000049E122_9606', 'misc_RNA'),
    ('URS00004A2461_9606', 'Y_RNA'),
    ('URS00004B11CA_223283', 'scRNA'),
    ('URS00004E52D3_10090', 'lncRNA'),
    ('URS00004E9E38_7227', 'miRNA'),
    ('URS00004FB44B_6239', 'rRNA'),
    ('URS000051DCEC_10090', 'snoRNA'),
    ('URS00005511ED_6239', 'lncRNA'),
    ('URS000055786A_7227', 'miRNA'),
    ('URS0000563A36_7227', 'snoRNA'),
    ('URS0000564CC6_224308', 'tmRNA'),
    ('URS0000569A4A_9606', 'snoRNA'),
    ('URS000059A8B2_7227', 'rasiRNA'),
    ('URS000059EA49_32644', 'tmRNA'),
    ('URS00005A245E_10090', 'tRNA'),
    ('URS00005CDD41_352472', 'RNase_P_RNA'),
    ('URS00005CF03F_9606', 'Y_RNA'),
    ('URS00005D0BAB_9606', 'piRNA'),
    ('URS00005EF0FF_4577', 'siRNA'),
    ('URS00005F2C2D_4932', 'rRNA'),
    ('URS00005F4CAF_3702', 'tRNA'),
    ('URS000060B496_10090', 'snoRNA'),
    ('URS000061DECF_1235461', 'autocatalytically_spliced_intron'),
    ('URS000061F377_559292', 'rRNA'),
    ('URS00006233F9_9606', 'ribozyme'),
    ('URS000064A09E_13616', 'vault_RNA'),
    ('URS00006550DA_10090', 'snoRNA'),
    ('URS000065BB41_7955', 'other'),
    ('URS0000661037_7955', 'tRNA'),
    ('URS000069D7FA_6239', 'tRNA'),
    ('URS00006A938C_10090', 'ribozyme'),
    ('URS00006B3271_10090', 'snoRNA'),
    ('URS00006BA413_9606', 'snoRNA'),
    ('URS00006C670E_30608', 'hammerhead_ribozyme'),
    ('URS00006CE02F_9606', 'snoRNA'),
    ('URS00006D80BC_9913', 'precursor_RNA'),
    ('URS00006DC8B9_6239', 'tRNA'),
    ('URS0000714027_9031', 'other'),
    ('URS00007150F8_9913', 'precursor_RNA'),
    ('URS0000732D5D_9606', 'lncRNA'),
    ('URS0000734D8F_9606', 'snRNA'),
    ('URS0000759BEC_9606', 'lncRNA'),
    ('URS000075A336_9606', 'miRNA'),
    ('URS000075A546_9606', 'precursor_RNA'),
    ('URS000075C290_9606', 'precursor_RNA'),
    ('URS000075C808_9606', 'lncRNA'),
    ('URS000075CC93_9606', 'precursor_RNA'),
    ('URS000075CF25_9913', 'precursor_RNA'),
    ('URS000075EF5D_9606', 'snRNA'),
    ('URS0000764CCC_1415657', 'RNase_P_RNA'),
    ('URS000077FBEB_9606', 'lncRNA'),
    ('URS00007A9FDC_6239', 'misc_RNA'),
    ('URS00007FD8A3_7227', 'lncRNA'),
    ('URS000080DD33_32630', 'ribozyme'),
    ('URS000080DFDA_32630', 'hammerhead_ribozyme'),
    ('URS000082AF7D_5699', 'guide_RNA'),
    ('URS000086852D_32630', 'hammerhead_ribozyme'),
    ('URS00008E398A_9606', 'snoRNA'),
    ('URS00008E3A1B_10090', 'lncRNA'),
    ('URS000092FF0A_9371', 'snoRNA'),
    ('URS00009E8F92_885695', 'rRNA'),
    ('URS0000A767C0_3702', 'lncRNA'),
    ('URS0000A86584_10090', 'lncRNA'),
    ('URS0000A8F612_9371', 'snoRNA'),
    ('URS0000A994FE_9606', 'other'),
    ('URS0000ABD7EF_9606', 'rRNA'),
    ('URS0000ABD87F_9606', 'rRNA'),
    ('URS000018B855_1270', 'misc_RNA'),
    ('URS0000127C85_175245', 'misc_RNA'),
    ('URS00002B64E6_7227', 'scaRNA'),
    ('URS00002BF60E_7227', 'scaRNA'),
    ('URS00008E39F3_7227', 'scaRNA'),
    ('URS0000508014_7227', 'scaRNA'),
    ('URS000049EC81_7227', 'scaRNA'),
    ('URS00001F11B5_6239', 'misc_RNA'),
    ('URS00000490A3_6239', 'miRNA'),

    # pytest.param('URS00007CD270_1872691', 'rRNA', mark=pytest.mark.xfail("Rfam")),
    # pytest.param('URS0000A85A32_10090', 'tRNA', mark=pytest.mark.xfail("Rfam")),

    # pytest.param('URS000060C682_9606', 'vault_RNA', mark=pytest.mark.xfail("Inactive sequence")),
    # pytest.param('URS0000175007_7227', 'miRNA', mark=pytest.mark.xfail("Inactive sequence")),
    # pytest.param('URS000072A167_10141', 'Y_RNA', mark=pytest.mark.xfail("Inactive sequence")),
    # pytest.param('URS00006B14E9_6183', 'hammerhead_ribozyme', mark=pytest.mark.xfail("Inactive sequence")),
    # pytest.param('URS0000A17B82_640938', 'other', mark=pytest.mark.xfail("Inactive sequence")),
    # pyest.param('URS000083F182_242161', 'other', mark=pytest.mark.xfail("Inactive sequence")),
    # pytest.param('URS0000808D19_644', 'hammerhead_ribozyme', mark=pytest.mark.xfail("Inactive sequence")),
    # pytest.mark('URS0000808D70_1478174', 'tmRNA', mark=pytest.mark.xfail("Inactive sequence"),

])
def test_computes_correct_rna_types(rna_id, rna_type):
    data = load_data(rna_id)
    assert rna_type_of(data) == rna_type
<|endoftext|>


<commit_before>#!/usr/bin/python
# known_servers.py
#
# Copyright (C) 2008-2018 Veselin Penev, https://bitdust.io
#
# This file (known_servers.py) is part of BitDust Software.
#
# BitDust is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# BitDust Software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with BitDust Software.  If not, see <http://www.gnu.org/licenses/>.
#
# Please contact us if you have any questions at bitdust.io@gmail.com
#
#
#

def default_nodes():
    """
    A set of identity servers currently maintained.
    """
    return {
        # by Veselin Penev:
        'p2p-id.ru': (80, 6661),
        'datahaven.net': (80, 6661),
        # 'identity.datahaven.net': (80, 6661),
        'bitdust.io': (8084, 6661),
        'work.offshore.ai': (8084, 6661),
        'blog.bitdust.io': (8084, 6661),
        'bitdust.ai': (8084, 6661),
    }

def by_host():
    """
    Here is a well known identity servers to support the network.
    Keys are domain names or global IP address (not recommended) of the ID server.
    Values are ``Web port`` (reading) and ``TCP port`` (writing) numbers.

    This is some kind of "genesis" network.
    If you willing to support the project and started your own BitDust node on reliable machine,
    contact us and we will include your address here.
    So other nodes will be able to use your machine to host their identities.

    You can override those "genesis" nodes by configuring list of your preferred identity servers
    in the program settings:

        api.config_set(
            "services/identity-propagate/known-servers",
            "myfirstserver.net:80:6661, secondmachine.net:8080:6662, thirdnode.gov.eu:80:16661",
        )

    This way you can create your own BitDust network, under your full control.
    """

    try:
        from main import config
        overridden_identity_servers_str = str(config.conf().getData('services/identity-propagate/known-servers'))
    except:
        overridden_identity_servers_str = ''
    if not overridden_identity_servers_str:
        return default_nodes()

    overridden_identity_servers = {}
    for id_server_str in overridden_identity_servers_str.split(','):
        if id_server_str.strip():
            try:
                id_server = id_server_str.strip().split(':')
                id_server_host = id_server[0].strip()
                id_server_web_port = int(id_server[1].strip())
                id_server_tcp_port = int(id_server[2].strip())
            except:
                continue
            overridden_identity_servers[id_server_host] = (id_server_web_port, id_server_tcp_port, )

    if overridden_identity_servers:
        return overridden_identity_servers

    return default_nodes()
<commit_msg>	modified:   userid/known_servers.py<commit_after>#!/usr/bin/python
# known_servers.py
#
# Copyright (C) 2008-2018 Veselin Penev, https://bitdust.io
#
# This file (known_servers.py) is part of BitDust Software.
#
# BitDust is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# BitDust Software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with BitDust Software.  If not, see <http://www.gnu.org/licenses/>.
#
# Please contact us if you have any questions at bitdust.io@gmail.com
#
#
#

def default_nodes():
    """
    A set of identity servers currently maintained.
    """
    return {
        # by Veselin Penev:
        'p2p-id.ru': (80, 6661),
        'datahaven.net': (80, 6661),
        # 'identity.datahaven.net': (80, 6661),
        'bitdust.io': (8084, 6661),
        'work.offshore.ai': (8084, 6661),
        'blog.bitdust.io': (8084, 6661),
        'bitdust.ai': (80, 6661),
    }

def by_host():
    """
    Here is a well known identity servers to support the network.
    Keys are domain names or global IP address (not recommended) of the ID server.
    Values are ``Web port`` (reading) and ``TCP port`` (writing) numbers.

    This is some kind of "genesis" network.
    If you willing to support the project and started your own BitDust node on reliable machine,
    contact us and we will include your address here.
    So other nodes will be able to use your machine to host their identities.

    You can override those "genesis" nodes by configuring list of your preferred identity servers
    in the program settings:

        api.config_set(
            "services/identity-propagate/known-servers",
            "myfirstserver.net:80:6661, secondmachine.net:8080:6662, thirdnode.gov.eu:80:16661",
        )

    This way you can create your own BitDust network, under your full control.
    """

    try:
        from main import config
        overridden_identity_servers_str = str(config.conf().getData('services/identity-propagate/known-servers'))
    except:
        overridden_identity_servers_str = ''
    if not overridden_identity_servers_str:
        return default_nodes()

    overridden_identity_servers = {}
    for id_server_str in overridden_identity_servers_str.split(','):
        if id_server_str.strip():
            try:
                id_server = id_server_str.strip().split(':')
                id_server_host = id_server[0].strip()
                id_server_web_port = int(id_server[1].strip())
                id_server_tcp_port = int(id_server[2].strip())
            except:
                continue
            overridden_identity_servers[id_server_host] = (id_server_web_port, id_server_tcp_port, )

    if overridden_identity_servers:
        return overridden_identity_servers

    return default_nodes()
<|endoftext|>


<commit_before>from __future__ import print_function, absolute_import
import zmq.eventloop
zmq.eventloop.ioloop.install()

import contextlib
import s
import json
import functools
import zmq
import zmq.eventloop.zmqstream
import zmq.sugar
import uuid
import os
import datetime

def timeout(seconds):
    r = route()
    s.async.ioloop().add_timeout(
        datetime.timedelta(seconds=seconds),
        lambda: s.sock.push(r, '')
    )
    return s.sock.bind('pull', r)

def route():
    while True:
        route = '/tmp/{}'.format(uuid.uuid4())
        if not os.path.isfile(route):
            return 'ipc://' + route

_sockets = []

@contextlib.contextmanager
def close_all():
    while _sockets:
        _sockets.pop().close()

def new(action, kind, route, subscriptions=[""], sockopts={}, timeout=None, hwm=1):
    assert kind.lower() in ['pub', 'sub', 'req', 'rep', 'push', 'pull', 'router', 'dealer', 'pair'], 'invalid kind: {}'.format(kind)
    assert action in ['bind', 'connect'], 'invalid action: {}'.format(action)
    assert route.split('://')[0] in ['ipc', 'tcp', 'pgm', 'epgm'], 'invalid route: {}'.format(route)
    sock = zmq.Context().socket(getattr(zmq, kind.upper())) # TODO we should not recreate contexts in the same thread, only in diff procs
    _sockets.append(sock)
    for k, v in sockopts.items():
        setattr(sock, k, v)
    try:
        getattr(sock, action)(route)
    except:
        raise
    if kind.lower() == 'sub':
        assert isinstance(subscriptions, (list, tuple)), 'subscriptions not a list: {}'.format(subscriptions)
        for subscr in subscriptions:
            sock.setsockopt(zmq.SUBSCRIBE, subscr.encode('utf-8'))
    if timeout:
        sock.setsockopt(zmq.SNDTIMEO, timeout)
        sock.setsockopt(zmq.RCVTIMEO, timeout)
    sock.setsockopt(zmq.SNDHWM, hwm)
    sock.setsockopt(zmq.RCVHWM, hwm)
    return AsyncSock(sock)

bind = functools.partial(new, 'bind')
connect = functools.partial(new, 'connect')

_devices = {
    'forwarder': ['sub', 'pub'],
    'queue': ['router', 'dealer'],
    'streamer': ['pull', 'push'],
}

def device(kind, in_route, out_route, **kw):
    in_kind, out_kind = _devices[kind.lower()]
    in_sock = bind(in_kind, in_route, **kw)._sock.socket
    out_sock = bind(out_kind, out_route, **kw)._sock.socket
    return zmq.device(getattr(zmq, kind.upper()), in_sock, out_sock)

def process_recv(msg):
    msg = msg.decode('utf-8')
    msg = json.loads(msg)
    return msg

def process_send(msg):
    msg = json.dumps(msg)
    msg = msg.encode('utf-8')
    return msg

class AsyncSock(object):
    def __init__(self, sock):
        self._sock = zmq.eventloop.zmqstream.ZMQStream(sock)
        self.entered = False

    def __enter__(self, *a, **kw):
        self.entered = True
        return self

    def __exit__(self, *a, **kw):
        self._sock.close()

    def type(self):
        return self._sock.socket.type

    def recv(self):
        assert s.async.ioloop().started, 'you are using async recv, but an ioloop hasnt been started'
        assert self.entered, 'you must use sockets as context managers, so that they are closed at some point'
        assert not self._sock._recv_callback, 'there is already a recv callback registered'
        future = s.async.Future()
        def cb(msg):
            self._sock.stop_on_recv()
            if self.type() == zmq.SUB:
                topic, msg = msg
                msg = process_recv(msg)
                topic = topic.decode('utf-8')
                msg = topic, msg
                future.set_result(msg)
            elif self.type() in [zmq.ROUTER, zmq.DEALER]:
                identities, msg = msg[:-1], msg[-1]
                msg = process_recv(msg)
                msg = tuple(identities + [msg])
                future.set_result(msg)
            else:
                [msg] = msg
                msg = process_recv(msg)
                future.set_result(msg)
        self._sock.on_recv(cb)
        return future

    def send(self, msg, topic=''):
        assert not topic or self.type() == zmq.PUB, 'you can only use kwarg topic with pub sockets'
        assert s.async.ioloop().started, 'you are using async send, but an ioloop hasnt been started'
        assert self.entered, 'you must use sockets as context managers, so that they are closed at some point'
        assert msg is not None, 'you cannot use None as a message'
        future = s.async.Future()
        def fn(*_):
            self._sock.stop_on_send()
            future.set_result(None)
        if self.type() == zmq.PUB:
            msg = process_send(msg)
            topic = topic.encode('utf-8')
            msg = topic, msg
            self._sock.send_multipart(msg, callback=fn)
        elif self.type() in [zmq.ROUTER, zmq.DEALER]:
            identities, msg = msg[:-1], msg[-1]
            msg = process_send(msg)
            msg = identities + (msg,)
            self._sock.send_multipart(msg, callback=fn)
        else:
            msg = process_send(msg)
            self._sock.send(msg, callback=fn)
        return future

def select(*socks):
    future = s.async.Future()
    def fn(sock, msg):
        for x in socks:
            x._sock.stop_on_recv()
        if sock.type() == zmq.SUB:
            topic, msg = msg
            topic = topic.decode('utf-8')
            msg = process_recv(msg)
            future.set_result([id(sock), [topic, msg]])
        else:
            [msg] = msg
            msg = process_recv(msg)
            future.set_result([id(sock), msg])
    for sock in socks:
        sock._sock.on_recv(functools.partial(fn, sock))
    return future

# TODO who takes args and kwargs. socket? push? timeout?
# TODO None is not a valid value

@s.async.coroutine(AssertionError)
def open_use_close(kind, method, route, msg=None, **kw):
    with connect(kind, route, **kw) as sock:
        if method == 'send':
            val = yield sock.send(msg)
        elif method == 'recv':
            val = yield sock.recv()
        else:
            raise AssertionError('bad method: {method}'.format(**locals()))
    raise s.async.Return(val)

push = functools.partial(open_use_close, 'push', 'send')
pull = functools.partial(open_use_close, 'pull', 'recv')
<commit_msg>zmq context is unique per pid<commit_after>from __future__ import print_function, absolute_import
import zmq.eventloop
zmq.eventloop.ioloop.install()

import s
import json
import functools
import zmq
import zmq.eventloop.zmqstream
import zmq.sugar
import uuid
import os
import datetime

def timeout(seconds):
    r = route()
    s.async.ioloop().add_timeout(
        datetime.timedelta(seconds=seconds),
        lambda: s.sock.push(r, '')
    )
    return s.sock.bind('pull', r)

def route():
    while True:
        route = '/tmp/{}'.format(uuid.uuid4())
        if not os.path.isfile(route):
            return 'ipc://' + route

@s.cached.memoize
def context(pid):
    return zmq.Context()

def new(action, kind, route, subscriptions=[""], sockopts={}, timeout=None, hwm=1):
    assert kind.lower() in ['pub', 'sub', 'req', 'rep', 'push', 'pull', 'router', 'dealer', 'pair'], 'invalid kind: {}'.format(kind)
    assert action in ['bind', 'connect'], 'invalid action: {}'.format(action)
    assert route.split('://')[0] in ['ipc', 'tcp', 'pgm', 'epgm'], 'invalid route: {}'.format(route)
    sock = context(os.getpid()).socket(getattr(zmq, kind.upper()))
    for k, v in sockopts.items():
        setattr(sock, k, v)
    try:
        getattr(sock, action)(route)
    except:
        raise
    if kind.lower() == 'sub':
        assert isinstance(subscriptions, (list, tuple)), 'subscriptions not a list: {}'.format(subscriptions)
        for subscr in subscriptions:
            sock.setsockopt(zmq.SUBSCRIBE, subscr.encode('utf-8'))
    if timeout:
        sock.setsockopt(zmq.SNDTIMEO, timeout)
        sock.setsockopt(zmq.RCVTIMEO, timeout)
    sock.setsockopt(zmq.SNDHWM, hwm)
    sock.setsockopt(zmq.RCVHWM, hwm)
    return AsyncSock(sock)

bind = functools.partial(new, 'bind')
connect = functools.partial(new, 'connect')

_devices = {
    'forwarder': ['sub', 'pub'],
    'queue': ['router', 'dealer'],
    'streamer': ['pull', 'push'],
}

def device(kind, in_route, out_route, **kw):
    in_kind, out_kind = _devices[kind.lower()]
    in_sock = bind(in_kind, in_route, **kw)._sock.socket
    out_sock = bind(out_kind, out_route, **kw)._sock.socket
    return zmq.device(getattr(zmq, kind.upper()), in_sock, out_sock)

def process_recv(msg):
    msg = msg.decode('utf-8')
    msg = json.loads(msg)
    return msg

def process_send(msg):
    msg = json.dumps(msg)
    msg = msg.encode('utf-8')
    return msg

class AsyncSock(object):
    def __init__(self, sock):
        self._sock = zmq.eventloop.zmqstream.ZMQStream(sock)
        self.entered = False

    def __enter__(self, *a, **kw):
        self.entered = True
        return self

    def __exit__(self, *a, **kw):
        self._sock.close()

    def type(self):
        return self._sock.socket.type

    def recv(self):
        assert s.async.ioloop().started, 'you are using async recv, but an ioloop hasnt been started'
        assert self.entered, 'you must use sockets as context managers, so that they are closed at some point'
        assert not self._sock._recv_callback, 'there is already a recv callback registered'
        future = s.async.Future()
        def cb(msg):
            self._sock.stop_on_recv()
            if self.type() == zmq.SUB:
                topic, msg = msg
                msg = process_recv(msg)
                topic = topic.decode('utf-8')
                msg = topic, msg
                future.set_result(msg)
            elif self.type() in [zmq.ROUTER, zmq.DEALER]:
                identities, msg = msg[:-1], msg[-1]
                msg = process_recv(msg)
                msg = tuple(identities + [msg])
                future.set_result(msg)
            else:
                [msg] = msg
                msg = process_recv(msg)
                future.set_result(msg)
        self._sock.on_recv(cb)
        return future

    def send(self, msg, topic=''):
        assert not topic or self.type() == zmq.PUB, 'you can only use kwarg topic with pub sockets'
        assert s.async.ioloop().started, 'you are using async send, but an ioloop hasnt been started'
        assert self.entered, 'you must use sockets as context managers, so that they are closed at some point'
        assert msg is not None, 'you cannot use None as a message'
        future = s.async.Future()
        def fn(*_):
            self._sock.stop_on_send()
            future.set_result(None)
        if self.type() == zmq.PUB:
            msg = process_send(msg)
            topic = topic.encode('utf-8')
            msg = topic, msg
            self._sock.send_multipart(msg, callback=fn)
        elif self.type() in [zmq.ROUTER, zmq.DEALER]:
            identities, msg = msg[:-1], msg[-1]
            msg = process_send(msg)
            msg = identities + (msg,)
            self._sock.send_multipart(msg, callback=fn)
        else:
            msg = process_send(msg)
            self._sock.send(msg, callback=fn)
        return future

def select(*socks):
    future = s.async.Future()
    def fn(sock, msg):
        for x in socks:
            x._sock.stop_on_recv()
        if sock.type() == zmq.SUB:
            topic, msg = msg
            topic = topic.decode('utf-8')
            msg = process_recv(msg)
            future.set_result([id(sock), [topic, msg]])
        else:
            [msg] = msg
            msg = process_recv(msg)
            future.set_result([id(sock), msg])
    for sock in socks:
        sock._sock.on_recv(functools.partial(fn, sock))
    return future

# TODO who takes args and kwargs. socket? push? timeout?
# TODO None is not a valid value

@s.async.coroutine(AssertionError)
def open_use_close(kind, method, route, msg=None, **kw):
    with connect(kind, route, **kw) as sock:
        if method == 'send':
            val = yield sock.send(msg)
        elif method == 'recv':
            val = yield sock.recv()
        else:
            raise AssertionError('bad method: {method}'.format(**locals()))
    raise s.async.Return(val)

push = functools.partial(open_use_close, 'push', 'send')
pull = functools.partial(open_use_close, 'pull', 'recv')
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
    Pygments unit tests
    ~~~~~~~~~~~~~~~~~~

    Usage::

        python run.py [testfile ...]

    :copyright: Copyright 2006-2009 by the Pygments team, see AUTHORS.
    :license: BSD, see LICENSE for details.
"""

import sys, os

if sys.version_info >= (3,):
    # copy test suite over to "build/lib" and convert it
    print ('Copying and converting sources to build/lib/test...')
    from distutils.util import copydir_run_2to3
    testroot = os.path.dirname(__file__)
    newroot = os.path.join(testroot, '..', 'build/lib/test')
    copydir_run_2to3(testroot, newroot)
    os.chdir(os.path.join(newroot, '..'))

try:
    import nose
except ImportError:
    print ('nose is required to run the Pygments test suite')
    sys.exit(1)

try:
    # make sure the current source is first on sys.path
    sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
    import pygments
except ImportError, err:
    print ('Cannot find Pygments to test: %s' % err)
    sys.exit(1)
else:
    print ('Pygments %s test suite running (Python %s)...' %
           (pygments.__version__, sys.version.split()[0]))

nose.main()
<commit_msg>Fix Python 3 compatibility of test runner.<commit_after># -*- coding: utf-8 -*-
"""
    Pygments unit tests
    ~~~~~~~~~~~~~~~~~~

    Usage::

        python run.py [testfile ...]

    :copyright: Copyright 2006-2009 by the Pygments team, see AUTHORS.
    :license: BSD, see LICENSE for details.
"""

import sys, os

if sys.version_info >= (3,):
    # copy test suite over to "build/lib" and convert it
    print ('Copying and converting sources to build/lib/test...')
    from distutils.util import copydir_run_2to3
    testroot = os.path.dirname(__file__)
    newroot = os.path.join(testroot, '..', 'build/lib/test')
    copydir_run_2to3(testroot, newroot)
    # make nose believe that we run from the converted dir
    os.chdir(newroot)
    __file__ = os.path.join('run.py')

try:
    import nose
except ImportError:
    print ('nose is required to run the Pygments test suite')
    sys.exit(1)

try:
    # make sure the current source is first on sys.path
    sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
    import pygments
except ImportError:
    print ('Cannot find Pygments to test: %s' % sys.exc_info()[1])
    sys.exit(1)
else:
    print ('Pygments %s test suite running (Python %s)...' %
           (pygments.__version__, sys.version.split()[0]))

nose.main()
<|endoftext|>


<commit_before>import numpy as np
from . import pdf as  funcs
import scipy

def inverse_cdf(f, xN=1e3, xlim=(0,1), **kwargs):
    """Provides the inverse cumulative density function (CDF) of a probability
    density function

    ARGUMENTS
    ---------
    f : function
        The PDF to evaluate
    xN : integer
        The x-axis sampling of the PDF
    xlim : (integer, integer)
        The x-axis range overwhich the pdf is considered

    RETURN
    ------
    the inverse CDF
    """
    # Probability Density Function
    x = np.linspace(xlim[0],xlim[1],int(xN))
    #func = f(x)
    # Cumulative Distribution Function
    cdf = np.cumsum(f(x))
    # Normalisation to 1
    cdf = cdf/cdf.max()
    # Inverse CDF
    return scipy.interpolate.interp1d(cdf,x)

def sample(f, N=1e3, xN=1e3, xlim=(0,1), **kwargs):
    """Provides a random trial X over a given PDF following the Inverse
    Transform Sampling technique so that X=F^{-1}(U), where F^{-1} is the
    inverse cumulative distribution function of the PDF and U is a random
    variable uniformly distributed between 0 and 1.

    ARGUMENTS
    ---------
    f : function
        The PDF to evaluate
    N : Integer
        The number of random evaluation to make
    xN : integer
        The x-axis sampling of the PDF
    xlim : (integer, integer)
        The x-axis range overwhich the pdf is considered

    RETURN
    ------
    N random evaluation over the considered PDF
    """
    uniform_samples = np.random.random(int(N))
    required_samples = inverse_cdf(f, xN=xN, xlim=xlim)(uniform_samples)
    return required_samples

def pdf(func, params, method=None, **kwargs):
    """Provides a random trial X over a PDF in rsr.pdf

    ARGUMENTS
    ---------
    func : string
        PDF name in rsr.pdf
    params : dict
        parameters to be passed to the PDF function
    kwargs : dict
        Any arguments used bu sample.sample

    RETURN
    ------
    N random evaluation over the considered PDF
    """
    if method is None:
        f = lambda x: getattr(funcs, func)(params, x,)
    else:
        f = lambda x: getattr(funcs, func)(params, x, method=method)
    return sample(f, N=1000)
<commit_msg>kwargs added<commit_after>import numpy as np
from . import pdf as  funcs
import scipy

def inverse_cdf(f, xN=1e3, xlim=(0,1), **kwargs):
    """Provides the inverse cumulative density function (CDF) of a probability
    density function

    ARGUMENTS
    ---------
    f : function
        The PDF to evaluate
    xN : integer
        The x-axis sampling of the PDF
    xlim : (integer, integer)
        The x-axis range overwhich the pdf is considered

    RETURN
    ------
    the inverse CDF
    """
    # Probability Density Function
    x = np.linspace(xlim[0],xlim[1],int(xN))
    #func = f(x)
    # Cumulative Distribution Function
    cdf = np.cumsum(f(x))
    # Normalisation to 1
    cdf = cdf/cdf.max()
    # Inverse CDF
    return scipy.interpolate.interp1d(cdf,x)

def sample(f, N=1e3, xN=1e3, xlim=(0,1), **kwargs):
    """Provides a random trial X over a given PDF following the Inverse
    Transform Sampling technique so that X=F^{-1}(U), where F^{-1} is the
    inverse cumulative distribution function of the PDF and U is a random
    variable uniformly distributed between 0 and 1.

    ARGUMENTS
    ---------
    f : function
        The PDF to evaluate
    N : Integer
        The number of random evaluation to make
    xN : integer
        The x-axis sampling of the PDF
    xlim : (integer, integer)
        The x-axis range overwhich the pdf is considered

    RETURN
    ------
    N random evaluation over the considered PDF
    """
    uniform_samples = np.random.random(int(N))
    required_samples = inverse_cdf(f, xN=xN, xlim=xlim)(uniform_samples)
    return required_samples

def pdf(func, params, method=None, **kwargs):
    """Provides a random trial X over a PDF in rsr.pdf

    ARGUMENTS
    ---------
    func : string
        PDF name in rsr.pdf
    params : dict
        parameters to be passed to the PDF function
    kwargs : dict
        Any arguments used bu sample.sample

    RETURN
    ------
    N random evaluation over the considered PDF
    """
    if method is None:
        f = lambda x: getattr(funcs, func)(params, x,)
    else:
        f = lambda x: getattr(funcs, func)(params, x, method=method)
    return sample(f, **kwargs)
<|endoftext|>