<commit_before>import logging
from time import strftime

from ryu.services.protocols.bgp.operator.command import Command
from ryu.services.protocols.bgp.operator.command import CommandsResponse
from ryu.services.protocols.bgp.operator.command import STATUS_ERROR
from ryu.services.protocols.bgp.operator.command import STATUS_OK
from ryu.services.protocols.bgp.operator.commands.responses import \
    WrongParamResp
from ryu.services.protocols.bgp.operator.views.bgp import CoreServiceDetailView
from ryu.lib.packet.bgp import RF_IPv4_UC
from ryu.lib.packet.bgp import RF_IPv6_UC
from ryu.lib.packet.bgp import RF_IPv4_VPN
from ryu.lib.packet.bgp import RF_IPv6_VPN

LOG = logging.getLogger('bgpspeaker.operator.commands.show.neighbor')

class NeighborSummary(Command):
    help_msg = 'show summarized neighbor information'
    command = 'summary'

    def action(self, params):
        requested_peers = []
        if len(params) > 0:
            requested_peers = [str(p) for p in params]

        core_service = self.api.get_core_service()
        core_service_view = CoreServiceDetailView(core_service)
        peers_view = core_service_view.rel('peer_manager').rel('peers_summary')

        def filter_requested(peer_id, peer_obj):
            return not requested_peers or peer_id in requested_peers

        peers_view.apply_filter(filter_requested)
        ret = peers_view.encode()
        return CommandsResponse(STATUS_OK, ret)

class SentRoutes(Command):
    help_msg = 'paths sent and not withdrawn to given peer'
    command = 'sent-routes'
    param_help_msg = '<ip_addr> <addr_family>{vpnv4, vpnv6, ipv4, ipv6, all}'
    fmtstr = ' {0:<2s} {1:<19s} {2:<32s} {3:<8s} {4:<20s} '\
        '{5:<6s} {6:<6s} {7:<}\n'

    def action(self, params):
        if len(params) != 2:
            return WrongParamResp()
        ip_addr, addr_family = params

        if addr_family == 'ipv4':
            rf = RF_IPv4_UC
        elif addr_family == 'ipv6':
            rf = RF_IPv6_UC
        elif addr_family == 'vpnv4':
            rf = RF_IPv4_VPN
        elif addr_family == 'vpnv6':
            rf = RF_IPv6_VPN
        elif addr_family == 'all':
            rf = None
        else:
            return WrongParamResp('wrong addr_family name')

        ret = self._retrieve_paths(addr_family, rf, ip_addr).encode()
        return CommandsResponse(STATUS_OK, ret)

    def _retrieve_paths(self, addr_family, route_family, ip_addr):
        peer_view = self._retrieve_peer_view(ip_addr)
        adj_rib_out = peer_view.c_rel('adj_rib_out')
        adj_rib_out.apply_filter(lambda k, v: addr_family == 'all' or
                                 v.path.route_family == route_family)
        return adj_rib_out

    def _retrieve_peer_view(self, ip_addr):
        core_service = self.api.get_core_service()
        core_sv = CoreServiceDetailView(core_service)
        peers_view = core_sv.rel('peer_manager').rel('peers')
        peers_view.apply_filter(lambda k, v: v.ip_address == ip_addr)
        return peers_view

    @classmethod
    def cli_resp_formatter(cls, resp):
        if resp.status == STATUS_ERROR:
            return Command.cli_resp_formatter(resp)
        return cls._format_header() + cls._format_value(resp.value)

    @classmethod
    def _format_header(cls):
        ret = ''
        ret += ('Status codes: x filtered\n')
        ret += ('Origin codes: i - IGP, e - EGP, ? - incomplete\n')
        ret += cls.fmtstr.format('', 'Timestamp', 'Network', 'Labels',
                                 'Next Hop', 'Metric', 'LocPrf', 'Path')
        return ret

    @classmethod
    def _format_value(cls, value):
        ret = ''
        for v in value:
            path = v.get('path')
            aspath = path.get('as_path')
            origin = path.get('origin')
            if origin:
                aspath.append(origin)

            next_hop = path.get('nexthop')
            med = path.get('metric')
            labels = path.get('labels')
            localpref = path.get('local_pref')
            prefix = path.get('nlri').get('prefix')

            path_status = ''
            if v.get('filtered'):
                path_status = 'x'
            time = 'N/A'
            if v.get('timestamp'):
                time = strftime("%Y/%m/%d %H:%M:%S", v.get('timestamp'))
            ret += cls.fmtstr.format(path_status, time, prefix, labels,
                                     next_hop, str(med), str(localpref),
                                     ' '.join(map(str, aspath)))
        return ret

class ReceivedRoutes(SentRoutes):
    help_msg = 'paths received and not withdrawn by given peer'
    command = 'received-routes'

    def _retrieve_paths(self, addr_family, route_family, ip_addr):
        peer_view = self._retrieve_peer_view(ip_addr)
        adj_rib_in = peer_view.c_rel('adj_rib_in')
        adj_rib_in.apply_filter(lambda k, v: addr_family == 'all' or
                                v.path.route_family == route_family)
        return adj_rib_in

class Neighbor(Command):
    help_msg = 'show neighbor information'
    command = 'neighbor'
    subcommands = {
        'summary': NeighborSummary,
        'sent-routes': SentRoutes,
        'received-routes': ReceivedRoutes
    }

    fmtstr = ' {0:<12s} {1:<12s} {2:<}\n'

    def action(self, params):
        core_service = self.api.get_core_service()
        core_service_view = CoreServiceDetailView(core_service)
        peers_view = core_service_view.rel('peer_manager').rel('peers')

        ret = peers_view.encode()
        return CommandsResponse(STATUS_OK,
                                [{'ip_addr': k,
                                  'as_num': str(v['remote_as']),
                                  'bgp_state': v['stats']['bgp_state']}
                                 for k, v in ret.iteritems()])

    @classmethod
    def cli_resp_formatter(cls, resp):
        if resp.status == STATUS_ERROR:
            return Command.cli_resp_formatter(resp)
        return cls._format_header() + cls._format_value(resp.value)

    @classmethod
    def _format_header(cls):
        return cls.fmtstr.format('IP Address', 'AS Number', 'BGP State')

    @classmethod
    def _format_value(cls, value):
        ret = ''
        for v in value:
            ret += cls.fmtstr.format(v['ip_addr'], v['as_num'], v['bgp_state'])
        return ret
<commit_msg>bgp/cli: fix internel data destruction due to cli show command<commit_after>import logging
from time import strftime

from ryu.services.protocols.bgp.operator.command import Command
from ryu.services.protocols.bgp.operator.command import CommandsResponse
from ryu.services.protocols.bgp.operator.command import STATUS_ERROR
from ryu.services.protocols.bgp.operator.command import STATUS_OK
from ryu.services.protocols.bgp.operator.commands.responses import \
    WrongParamResp
from ryu.services.protocols.bgp.operator.views.bgp import CoreServiceDetailView
from ryu.lib.packet.bgp import RF_IPv4_UC
from ryu.lib.packet.bgp import RF_IPv6_UC
from ryu.lib.packet.bgp import RF_IPv4_VPN
from ryu.lib.packet.bgp import RF_IPv6_VPN

LOG = logging.getLogger('bgpspeaker.operator.commands.show.neighbor')

class NeighborSummary(Command):
    help_msg = 'show summarized neighbor information'
    command = 'summary'

    def action(self, params):
        requested_peers = []
        if len(params) > 0:
            requested_peers = [str(p) for p in params]

        core_service = self.api.get_core_service()
        core_service_view = CoreServiceDetailView(core_service)
        peers_view = core_service_view.rel('peer_manager').rel('peers_summary')

        def filter_requested(peer_id, peer_obj):
            return not requested_peers or peer_id in requested_peers

        peers_view.apply_filter(filter_requested)
        ret = peers_view.encode()
        return CommandsResponse(STATUS_OK, ret)

class SentRoutes(Command):
    help_msg = 'paths sent and not withdrawn to given peer'
    command = 'sent-routes'
    param_help_msg = '<ip_addr> <addr_family>{vpnv4, vpnv6, ipv4, ipv6, all}'
    fmtstr = ' {0:<2s} {1:<19s} {2:<32s} {3:<8s} {4:<20s} '\
        '{5:<6s} {6:<6s} {7:<}\n'

    def action(self, params):
        if len(params) != 2:
            return WrongParamResp()
        ip_addr, addr_family = params

        if addr_family == 'ipv4':
            rf = RF_IPv4_UC
        elif addr_family == 'ipv6':
            rf = RF_IPv6_UC
        elif addr_family == 'vpnv4':
            rf = RF_IPv4_VPN
        elif addr_family == 'vpnv6':
            rf = RF_IPv6_VPN
        elif addr_family == 'all':
            rf = None
        else:
            return WrongParamResp('wrong addr_family name')

        ret = self._retrieve_paths(addr_family, rf, ip_addr).encode()
        return CommandsResponse(STATUS_OK, ret)

    def _retrieve_paths(self, addr_family, route_family, ip_addr):
        peer_view = self._retrieve_peer_view(ip_addr)
        adj_rib_out = peer_view.c_rel('adj_rib_out')
        adj_rib_out.apply_filter(lambda k, v: addr_family == 'all' or
                                 v.path.route_family == route_family)
        return adj_rib_out

    def _retrieve_peer_view(self, ip_addr):
        core_service = self.api.get_core_service()
        core_sv = CoreServiceDetailView(core_service)
        peers_view = core_sv.rel('peer_manager').rel('peers')
        peers_view.apply_filter(lambda k, v: v.ip_address == ip_addr)
        return peers_view

    @classmethod
    def cli_resp_formatter(cls, resp):
        if resp.status == STATUS_ERROR:
            return Command.cli_resp_formatter(resp)
        return cls._format_header() + cls._format_value(resp.value)

    @classmethod
    def _format_header(cls):
        ret = ''
        ret += ('Status codes: x filtered\n')
        ret += ('Origin codes: i - IGP, e - EGP, ? - incomplete\n')
        ret += cls.fmtstr.format('', 'Timestamp', 'Network', 'Labels',
                                 'Next Hop', 'Metric', 'LocPrf', 'Path')
        return ret

    @classmethod
    def _format_value(cls, value):
        ret = ''
        for v in value:
            path = v.get('path')
            aspath = path.get('as_path')
            origin = path.get('origin')
            if origin:
                aspath = aspath + [origin]

            next_hop = path.get('nexthop')
            med = path.get('metric')
            labels = path.get('labels')
            localpref = path.get('local_pref')
            prefix = path.get('nlri').get('prefix')

            path_status = ''
            if v.get('filtered'):
                path_status = 'x'
            time = 'N/A'
            if v.get('timestamp'):
                time = strftime("%Y/%m/%d %H:%M:%S", v.get('timestamp'))
            ret += cls.fmtstr.format(path_status, time, prefix, labels,
                                     next_hop, str(med), str(localpref),
                                     ' '.join(map(str, aspath)))
        return ret

class ReceivedRoutes(SentRoutes):
    help_msg = 'paths received and not withdrawn by given peer'
    command = 'received-routes'

    def _retrieve_paths(self, addr_family, route_family, ip_addr):
        peer_view = self._retrieve_peer_view(ip_addr)
        adj_rib_in = peer_view.c_rel('adj_rib_in')
        adj_rib_in.apply_filter(lambda k, v: addr_family == 'all' or
                                v.path.route_family == route_family)
        return adj_rib_in

class Neighbor(Command):
    help_msg = 'show neighbor information'
    command = 'neighbor'
    subcommands = {
        'summary': NeighborSummary,
        'sent-routes': SentRoutes,
        'received-routes': ReceivedRoutes
    }

    fmtstr = ' {0:<12s} {1:<12s} {2:<}\n'

    def action(self, params):
        core_service = self.api.get_core_service()
        core_service_view = CoreServiceDetailView(core_service)
        peers_view = core_service_view.rel('peer_manager').rel('peers')

        ret = peers_view.encode()
        return CommandsResponse(STATUS_OK,
                                [{'ip_addr': k,
                                  'as_num': str(v['remote_as']),
                                  'bgp_state': v['stats']['bgp_state']}
                                 for k, v in ret.iteritems()])

    @classmethod
    def cli_resp_formatter(cls, resp):
        if resp.status == STATUS_ERROR:
            return Command.cli_resp_formatter(resp)
        return cls._format_header() + cls._format_value(resp.value)

    @classmethod
    def _format_header(cls):
        return cls.fmtstr.format('IP Address', 'AS Number', 'BGP State')

    @classmethod
    def _format_value(cls, value):
        ret = ''
        for v in value:
            ret += cls.fmtstr.format(v['ip_addr'], v['as_num'], v['bgp_state'])
        return ret
<|endoftext|>


<commit_before>#!/usr/bin/env python
import os
import uuid
import json
import argparse

def parse_args():
    my_parser = argparse.ArgumentParser()

    sub_parsers = my_parser.add_subparsers()

    create_configs_parser = sub_parsers.add_parser(
        "create-configs", conflict_handler='resolve')
    create_configs_parser.add_argument(
        "-n", "--num_runs", help="Number of runs to create configurations for.", dest="num_runs", required=False, type=int)
    create_configs_parser.add_argument(
        "-c", "--config_location", help="Path to a directory where the generated config files will be stored.", dest="config_location", required=True)
    create_configs_parser.add_argument(
        "-s", "--sample_location", help="Path to a directory where the sample files are stored.", dest="sample_location", required=True)

    create_configs_parser.set_defaults(func=create_configs_command)

    my_args = my_parser.parse_args()

    return my_args

def write_config_to_file(config, config_location):

    run_uuid = str(uuid.uuid4())

    my_file = open("{}/{}.json".format(config_location, run_uuid), "w")
    json.dump(config, my_file)
    my_file.close()

def create_configs_command(args):

    num_runs = args.num_runs
    config_location = args.config_location
    sample_location = args.sample_location

    num_configs = 0

    if (not os.path.isdir(config_location)):
        os.makedirs(config_location)

    for root, dirs, files in os.walk(sample_location):
        for filename in files:
            if filename.endswith(".gz"):
                if num_runs == None or num_configs < num_runs:
                    this_config_data = {
                                        "sample": {
                                                   "sample_id": os.path.basename(root),
                                                   "path_prefix": root,
                                                   "filename": filename
                                                   }
                                        }
                    write_config_to_file(this_config_data, config_location)
                    num_configs = num_configs + 1
                elif num_configs > num_runs:
                    break

    if (not os.path.isdir(config_location)):
        os.makedirs(config_location)

if __name__ == '__main__':
    args = parse_args()
    args.func(args)
<commit_msg>Break out of all loops instead of just inner loop when num configs reached.<commit_after>#!/usr/bin/env python
import os
import uuid
import json
import argparse

def parse_args():
    my_parser = argparse.ArgumentParser()

    sub_parsers = my_parser.add_subparsers()

    create_configs_parser = sub_parsers.add_parser(
        "create-configs", conflict_handler='resolve')
    create_configs_parser.add_argument(
        "-n", "--num_runs", help="Number of runs to create configurations for.", dest="num_runs", required=False, type=int)
    create_configs_parser.add_argument(
        "-c", "--config_location", help="Path to a directory where the generated config files will be stored.", dest="config_location", required=True)
    create_configs_parser.add_argument(
        "-s", "--sample_location", help="Path to a directory where the sample files are stored.", dest="sample_location", required=True)

    create_configs_parser.set_defaults(func=create_configs_command)

    my_args = my_parser.parse_args()

    return my_args

def write_config_to_file(config, config_location):

    run_uuid = str(uuid.uuid4())

    my_file = open("{}/{}.json".format(config_location, run_uuid), "w")
    json.dump(config, my_file)
    my_file.close()

def create_configs_command(args):

    num_runs = args.num_runs
    config_location = args.config_location
    sample_location = args.sample_location

    num_configs = 0

    if (not os.path.isdir(config_location)):
        os.makedirs(config_location)

    for root, dirs, files in os.walk(sample_location):
        for filename in files:
            if filename.endswith(".gz"):
                if num_runs == None or num_configs < num_runs:
                    this_config_data = {
                                        "sample": {
                                                   "sample_id": os.path.basename(root),
                                                   "path_prefix": root,
                                                   "filename": filename
                                                   }
                                        }
                    write_config_to_file(this_config_data, config_location)
                    num_configs = num_configs + 1
                elif num_configs >= num_runs:
                    return

if __name__ == '__main__':
    args = parse_args()
    args.func(args)
<|endoftext|>


<commit_before>#!/usr/bin/env python
import os
import uuid
import json
import argparse

def parse_args():
    my_parser = argparse.ArgumentParser()

    sub_parsers = my_parser.add_subparsers()

    create_configs_parser = sub_parsers.add_parser(
        "create-configs", conflict_handler='resolve')
    create_configs_parser.add_argument(
        "-n", "--num_runs", help="Number of runs to create configurations for.", dest="num_runs", required=False, type=int)
    create_configs_parser.add_argument(
        "-c", "--config_location", help="Path to a directory where the generated config files will be stored.", dest="config_location", required=True)
    create_configs_parser.add_argument(
        "-s", "--sample_location", help="Path to a directory where the sample files are stored.", dest="sample_location", required=True)

    create_configs_parser.set_defaults(func=create_configs_command)

    my_args = my_parser.parse_args()

    return my_args

def write_config_to_file(config, config_location):

    run_uuid = str(uuid.uuid4())

    my_file = open("{}/{}.json".format(config_location, run_uuid), "w")
    json.dump(config, my_file)
    my_file.close()

def create_configs_command(args):

    num_runs = args.num_runs
    config_location = args.config_location
    sample_location = args.sample_location

    num_configs = 0

    if (not os.path.isdir(config_location)):
        os.makedirs(config_location)

    for root, dirs, files in os.walk(sample_location):
        for filename in files:
            if filename.endswith(".gz"):
                if num_runs != None and num_configs < num_runs:
                    this_config_data = {
                                        "sample": {
                                                   "sample_id": root.basename(),
                                                   "path_prefix": root,
                                                   "filename": filename
                                                   }
                                        }
                    write_config_to_file(this_config_data, config_location)
                    num_configs = num_configs + 1

    if (not os.path.isdir(config_location)):
        os.makedirs(config_location)

if __name__ == '__main__':
    args = parse_args()
    args.func(args)
<commit_msg>Fix basename call.<commit_after>#!/usr/bin/env python
import os
import uuid
import json
import argparse

def parse_args():
    my_parser = argparse.ArgumentParser()

    sub_parsers = my_parser.add_subparsers()

    create_configs_parser = sub_parsers.add_parser(
        "create-configs", conflict_handler='resolve')
    create_configs_parser.add_argument(
        "-n", "--num_runs", help="Number of runs to create configurations for.", dest="num_runs", required=False, type=int)
    create_configs_parser.add_argument(
        "-c", "--config_location", help="Path to a directory where the generated config files will be stored.", dest="config_location", required=True)
    create_configs_parser.add_argument(
        "-s", "--sample_location", help="Path to a directory where the sample files are stored.", dest="sample_location", required=True)

    create_configs_parser.set_defaults(func=create_configs_command)

    my_args = my_parser.parse_args()

    return my_args

def write_config_to_file(config, config_location):

    run_uuid = str(uuid.uuid4())

    my_file = open("{}/{}.json".format(config_location, run_uuid), "w")
    json.dump(config, my_file)
    my_file.close()

def create_configs_command(args):

    num_runs = args.num_runs
    config_location = args.config_location
    sample_location = args.sample_location

    num_configs = 0

    if (not os.path.isdir(config_location)):
        os.makedirs(config_location)

    for root, dirs, files in os.walk(sample_location):
        for filename in files:
            if filename.endswith(".gz"):
                if num_runs == None or num_configs < num_runs:
                    this_config_data = {
                                        "sample": {
                                                   "sample_id": os.path.basename(root),
                                                   "path_prefix": root,
                                                   "filename": filename
                                                   }
                                        }
                    write_config_to_file(this_config_data, config_location)
                    num_configs = num_configs + 1
                elif num_configs > num_runs:
                    break

    if (not os.path.isdir(config_location)):
        os.makedirs(config_location)

if __name__ == '__main__':
    args = parse_args()
    args.func(args)
<|endoftext|>


<commit_before># Copyright 2013-2020 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

import glob
import subprocess
from os import path

from spack import *

releases = {'2021.1':
            {'irc_id': '17427', 'build': '2684'}}

class IntelOneapiCompilers(IntelOneApiPackage):
    """Intel oneAPI compilers.

    Contains icc, icpc, icx, icpx, dpcpp, ifort, ifx.

    """

    maintainers = ['rscohn2']

    homepage = 'https://software.intel.com/content/www/us/en/develop/tools/oneapi/components/dpc-compiler.html'

    version('2021.1', sha256='666b1002de3eab4b6f3770c42bcf708743ac74efeba4c05b0834095ef27a11b9', expand=False)

    depends_on('patchelf', type='build')

    def __init__(self, spec):
        self.component_info(
            dir_name='compiler',
            components=('intel.oneapi.lin.dpcpp-cpp-compiler-pro'
                        ':intel.oneapi.lin.ifort-compiler'),
            releases=releases,
            url_name='HPCKit')
        super(IntelOneapiCompilers, self).__init__(spec)

    def install(self, spec, prefix):
        super(IntelOneapiCompilers, self).install(spec, prefix)
        # For quick turnaround debugging, copy instead of install
        # copytree('/opt/intel/oneapi/compiler', path.join(prefix, 'compiler'),
        #          symlinks=True)
        rpath_dirs = ['lib',
                      'lib/x64',
                      'lib/emu',
                      'lib/oclfpga/host/linux64/lib',
                      'lib/oclfpga/linux64/lib',
                      'compiler/lib/intel64_lin',
                      'compiler/lib']
        patch_dirs = ['compiler/lib/intel64_lin',
                      'compiler/lib/intel64',
                      'bin']
        eprefix = path.join(prefix, 'compiler', 'latest', 'linux')
        rpath = ':'.join([path.join(eprefix, c) for c in rpath_dirs])
        for pd in patch_dirs:
            for file in glob.glob(path.join(eprefix, pd, '*')):
                # Try to patch all files, patchelf will do nothing if
                # file should not be patched
                subprocess.call(['patchelf', '--set-rpath', rpath, file])
<commit_msg>intel-oneapi-compilers package: correct module file (#20686)<commit_after># Copyright 2013-2020 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

import glob
import subprocess
from os import path

from spack import *

releases = {'2021.1':
            {'irc_id': '17427', 'build': '2684'}}

class IntelOneapiCompilers(IntelOneApiPackage):
    """Intel oneAPI compilers.

    Contains icc, icpc, icx, icpx, dpcpp, ifort, ifx.

    """

    maintainers = ['rscohn2']

    homepage = 'https://software.intel.com/content/www/us/en/develop/tools/oneapi/components/dpc-compiler.html'

    version('2021.1', sha256='666b1002de3eab4b6f3770c42bcf708743ac74efeba4c05b0834095ef27a11b9', expand=False)

    depends_on('patchelf', type='build')

    def __init__(self, spec):
        self.component_info(
            dir_name='compiler',
            components=('intel.oneapi.lin.dpcpp-cpp-compiler-pro'
                        ':intel.oneapi.lin.ifort-compiler'),
            releases=releases,
            url_name='HPCKit')
        super(IntelOneapiCompilers, self).__init__(spec)

    def install(self, spec, prefix):
        super(IntelOneapiCompilers, self).install(spec, prefix)
        # For quick turnaround debugging, copy instead of install
        # copytree('/opt/intel/oneapi/compiler', path.join(prefix, 'compiler'),
        #          symlinks=True)
        rpath_dirs = ['lib',
                      'lib/x64',
                      'lib/emu',
                      'lib/oclfpga/host/linux64/lib',
                      'lib/oclfpga/linux64/lib',
                      'compiler/lib/intel64_lin',
                      'compiler/lib']
        patch_dirs = ['compiler/lib/intel64_lin',
                      'compiler/lib/intel64',
                      'bin']
        eprefix = path.join(prefix, 'compiler', 'latest', 'linux')
        rpath = ':'.join([path.join(eprefix, c) for c in rpath_dirs])
        for pd in patch_dirs:
            for file in glob.glob(path.join(eprefix, pd, '*')):
                # Try to patch all files, patchelf will do nothing if
                # file should not be patched
                subprocess.call(['patchelf', '--set-rpath', rpath, file])

    def setup_run_environment(self, env):
        env.prepend_path('PATH', join_path(self.prefix,
                         'compiler', 'latest', 'linux', 'bin'))
        env.prepend_path('CPATH', join_path(self.prefix,
                         'compiler', 'latest', 'linux', 'include'))
        env.prepend_path('LIBRARY_PATH', join_path(self.prefix,
                         'compiler', 'latest', 'linux', 'lib'))
        env.prepend_path('LD_LIBRARY_PATH', join_path(self.prefix,
                         'compiler', 'latest', 'linux', 'lib'))
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Version info"""

short_version = '0.2'
version = '0.2.1'
<commit_msg>Bump to v1.0.0rc0<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Version info"""

short_version = '1.0rc0'
version = '1.0.0rc0'
<|endoftext|>


<commit_before>"""
    scell.selector
    ~~~~~~~~~~~~~~

    Implements the ``Selector`` class, a high level
    wrapper around ``~select.select``.
"""

from sys import version_info
from contextlib import contextmanager
from scell.core import select, Monitored, Event

class Selector(dict):
    """
    A selector object is a dictionary of file-like
    objects to ``Monitored`` objects.
    """

    def register(self, fp, mode, callback=None):
        """
        Register a given *fp* (file handle) under a
        given *mode*. The *mode* can either be ``r``,
        ``w``, or both. Returns the monitor object.

        :param fp: The file-like object.
        :param mode: Whether read and or write-ready
            events should be notified.
        :param callback: Optionally, attach a callback.
        """
        monitor = Monitored(
            fp=fp,
            wants_read='r' in mode,
            wants_write='w' in mode,
            callback=callback,
            )
        self[fp] = monitor
        return monitor

    info = dict.get
    unregister = dict.__delitem__
    registered = property(dict.items if version_info[0] == 3 else
                          dict.iteritems)

    def rwlist(self):
        """
        Returns ``(rl, wl)`` where ``rl`` and ``wl``
        are file objects interested in readability
        and writability, respectively.
        """
        rl, wl = [], []
        for fp, mon in self.registered:
            if mon.wants_read:
                rl.append(fp)
            if mon.wants_write:
                wl.append(fp)
        return rl, wl

    def select(self, timeout=None):
        """
        Returns an iterable of monitors which are readable
        or writable subject to *timeout*.

        :param timeout: Maximum number of seconds to
            wait until at least 1 file object is readable or
            writable. To block for an indefinite time, use
            ``None``.
        """
        rl, wl = select(*self.rwlist(), timeout=timeout)
        rl, wl = set(rl), set(wl)

        for fp in rl.union(wl):
            r_ok = fp in rl
            w_ok = fp in wl

            yield Event(monitored=self[fp],
                        readable=r_ok,
                        writable=w_ok)

    def ready(self):
        """
        Yields the registered monitors which are ready
        (their interests are satisfied).
        """
        for event in self.select():
            if event.ready:
                yield event

    @contextmanager
    def scoped(self, fps, mode='rw'):
        """
        A context manager that automatically unregisters
        **fps** once the block has finished executing.

        :param fps: Iterable of file objects.
        :param mode: Defaults to 'rw', the interests of
            every file handle.
        """
        monitors = tuple(self.register(fp, mode) for fp in fps)
        try:
            yield monitors
        finally:
            for mon in monitors:
                if mon.fp in self:
                    self.unregister(mon.fp)
<commit_msg>Move version check to global variable<commit_after>"""
    scell.selector
    ~~~~~~~~~~~~~~

    Implements the ``Selector`` class, a high level
    wrapper around ``~select.select``.
"""

from sys import version_info
from contextlib import contextmanager
from scell.core import select, Monitored, Event

_IS_PY3 = version_info[0] == 3

class Selector(dict):
    """
    A selector object is a dictionary of file-like
    objects to ``Monitored`` objects.
    """

    def register(self, fp, mode, callback=None):
        """
        Register a given *fp* (file handle) under a
        given *mode*. The *mode* can either be ``r``,
        ``w``, or both. Returns the monitor object.

        :param fp: The file-like object.
        :param mode: Whether read and or write-ready
            events should be notified.
        :param callback: Optionally, attach a callback.
        """
        monitor = Monitored(
            fp=fp,
            wants_read='r' in mode,
            wants_write='w' in mode,
            callback=callback,
            )
        self[fp] = monitor
        return monitor

    info = dict.get
    unregister = dict.__delitem__
    registered = property(dict.items if _IS_PY3 else
                          dict.iteritems)

    def rwlist(self):
        """
        Returns ``(rl, wl)`` where ``rl`` and ``wl``
        are file objects interested in readability
        and writability, respectively.
        """
        rl, wl = [], []
        for fp, mon in self.registered:
            if mon.wants_read:
                rl.append(fp)
            if mon.wants_write:
                wl.append(fp)
        return rl, wl

    def select(self, timeout=None):
        """
        Returns an iterable of monitors which are readable
        or writable subject to *timeout*.

        :param timeout: Maximum number of seconds to
            wait until at least 1 file object is readable or
            writable. To block for an indefinite time, use
            ``None``.
        """
        rl, wl = select(*self.rwlist(), timeout=timeout)
        rl, wl = set(rl), set(wl)

        for fp in rl.union(wl):
            r_ok = fp in rl
            w_ok = fp in wl

            yield Event(monitored=self[fp],
                        readable=r_ok,
                        writable=w_ok)

    def ready(self):
        """
        Yields the registered monitors which are ready
        (their interests are satisfied).
        """
        for event in self.select():
            if event.ready:
                yield event

    @contextmanager
    def scoped(self, fps, mode='rw'):
        """
        A context manager that automatically unregisters
        **fps** once the block has finished executing.

        :param fps: Iterable of file objects.
        :param mode: Defaults to 'rw', the interests of
            every file handle.
        """
        monitors = tuple(self.register(fp, mode) for fp in fps)
        try:
            yield monitors
        finally:
            for mon in monitors:
                if mon.fp in self:
                    self.unregister(mon.fp)
<|endoftext|>


<commit_before># coding: utf-8

import unittest
import time
from base.generics.test import GenericTest

class TestDemo(GenericTest):

    """
        This is a demo class who will show-up most of the functionalities of this small test environment.
        PLease check the kissenium.ini file for more configuration
        If you need to run your tests in a specific order, you can add an order number after the method keyword 'test'
        Like this:
            def test_4_test_name(self):
                # My test
    """

    @unittest.skip("This is an example of a skipped test.")
    def test_1_skip(self):
        """ This test is skipped """

    def test_2_adiuvo_otp(self):
        """
        Run a test on a website, and do an assert on a OTP page
        :return:
        """
        self.selenium.get('https://www.adiuvo.fr')
        self.selenium.alert("Here is a simple test demo in a website.", 4, 4)
        r = self.selenium.get_element_by_xpath("//h1[@class='splash-head']")
        self.l_assertEqual("DÉVÉLOPPEMENT INFORMATIQUE, ADMINISTRATION SYSTÈMES LINUX ET WINDOWS", r.text)
        self.selenium.alert("Page title has been tested", 2, 2)
        self.selenium.click_first_xpath("//a[contains(., 'Générateur de mots de passe')]")

        """ Testing the OTP form """
        self.selenium.alert("Testing the OTP form", 10, 1)
        """ Scroll to the form """
        self.selenium.scroll_to_xpath("//form[@id='otp_form']")

        """ Fill the form """
        self.selenium.alert("Sending challenge input", 2, 2)
        self.selenium.send_keys_by_id("challenge", "1 selenium")
        self.selenium.alert("Sending secret input", 2, 2)
        self.selenium.send_keys_by_id("secret", "selenium")

        """ Click on generate """
        self.selenium.alert("Click to generate", 2, 2)
        self.selenium.get_element_by_xpath("//button[@type='submit' and contains(., 'Lancez le calcul!')]").click()
        r = self.selenium.get_element_by_xpath("//textarea[@placeholder = 'Les mots générés apparaîtrons ici.']")\
                                         .get_attribute('value')
        self.l_assertEqual("VET GWEN GOOF TOO MANA CARL (43B2 3A3B 20DB 6EC7)", r)
        self.selenium.alert("Assert is ok", 2, 1)

    def test_3_from_google_search(self):
        """
        Run a test wich start from google
        :return:
        """
        """ Our first assert will be to check the google title """
        self.selenium.get('https://www.google.fr')
        self.selenium.alert("This test will go trough standard browsing on the web with some tests.", 5, 3)
        self.l_assertIn('Google', self.selenium.browser.title, False)

        """ We start by searching for selenium into google """
        self.selenium.alert("We are starting by searching for selenium in google", 2, 1)
        self.selenium.send_keys_by_xpath("//input[@title='Rechercher']", "selenium software")
        self.selenium.get_element_by_xpath("//input[@value='Recherche Google']").click()

        """ For the demo we will log every page name in the result of google """
        g_results = self.selenium.get_elements_by_xpath('//div[@id="res"]//h3//a')
        for gr in g_results:
            self.logger.info("Element iteration demo : " + gr.text)

        """ We first go into wikipedia, run an assert, and then go back """
        self.selenium.click_first_xpath("//a[contains(., 'Wikipédia')]")
        self.selenium.alert("Browsing wikipedia page", 2, 1)
        self.selenium.alert("Testing the page title", 2, 1)
        title = self.selenium.get_element_by_id('firstHeading')
        self.l_assertIn("Selenium", title.text)
        self.selenium.alert("Page title is ok!", 2, 1)
        self.selenium.alert("Go to the previous page (self.browser.page)", 1, 1)
        self.selenium.browser.back()

        """ We are going to the selenium official website """
        self.selenium.alert("Go to the selenium website", 2, 2)
        self.selenium.click_first_xpath("//a[contains(., 'Selenium - Web Browser Automation')]")

        """ Clicking on some links for the demo """
        self.selenium.alert("Click on link", 2, 2)
        self.selenium.click_first_xpath("//ul[@id='sitemap']//a[contains(., 'Selenium IDE')]")
        self.selenium.alert("Go to download page", 2, 2)
        self.selenium.click_first_xpath("//div[@class='downloadBox']//a")
        self.selenium.page_wait_for_xpath("//h2")
        r = self.selenium.get_element_by_xpath("//a[contains(., 'Mozilla GeckoDriver')]")

        """ Running an assert to check if the value is equal to what we expect """
        self.l_assertEqual("https://github.com/mozilla/geckodriver/", r.get_attribute("href"))
        self.selenium.alert("End of the browsing test", 2, 2)

    def test_4_resize_demo(self):
        """
        This test show us a browser resize demo
        :return:
        """
        self.selenium.get('http://www.kissenium.org')
        self.selenium.alert("Here we will show how to resize your browser window", 2, 2)
        self.selenium.resize_window(480, 800)
        self.selenium.alert("480 x 800", 2, 2)
        self.selenium.resize_window(800, 480)
        self.selenium.alert("800 x 480", 2, 2)
        self.selenium.resize_window(1366, 768)
        self.selenium.alert("1366 x 768", 2, 2)
        self.selenium.maximize()
        self.selenium.alert("Maximize the window", 2, 2)

    def test_5_documentation_mode(self):
        """
        This test show us a small "documentation mode" example
        :return:
        """
        self.selenium.get('http://www.kissenium.org')
        self.selenium.alert("Here we show the documentation mode.", 20, 2)
        self.selenium.alert("This mode is experimental, and work only with elements who have id\\'s", 2, 2)
        self.selenium.scroll_to_xpath("//h2[@id='kissenium--selenium-framework']")
        self.selenium.dim("kissenium--selenium-framework", 2)
        self.selenium.alert("Think about scrolling the element before dim the page arround it.", 4, 1)
        self.selenium.alert("Use \\'self.st.hover_by_xpath\\'", 3, 2)
        self.selenium.scroll_to_xpath("//h2[@id='functionalities-done-and-to-do']")
        self.selenium.dim("functionalities-done-and-to-do", 2)
        self.selenium.scroll_to_xpath("//h2[@id='authors-contributors']")
        self.selenium.dim("authors-contributors", 2)

    def test_6_assert_log(self):
        """
        This a example of log assert method.
        This test will fail
        In this you will see how you can manage your asserts to continue to run (or not) with some simple parameters.
        The parameter passed to the l_assert function (True|False) will always override the
        Config['Kissenium']['FailOnAssertError'] parameter.
        :return:
        """
        self.selenium.get('http://www.kissenium.org')
        self.selenium.alert("There are just asserts examples working in the background.", 30, 1)
        self.selenium.alert("You can change the parameter FailOnAssertError to see what\\'s going on.", 30, 1)
        self.l_assertEqual(1, 1, False)
        self.l_assertEqual(1, 2, False)
        self.l_assertNotEqual(1, 2, False)
        self.l_assertNotEqual(1, 1, False)
        self.l_assertFalse(False, False)
        self.l_assertFalse(True, False)
        self.l_assertTrue(True, False)
        self.l_assertTrue(False, False)
        self.l_assertIn("e", "test", False)
        self.l_assertIn("a", "test", False)
        self.l_assertNotIn("a", "test", False)
        self.l_assertNotIn("e", "test", False)
        self.l_assertIsNone(None, False)
        self.l_assertIsNone("test")
        # Thoses assert will never be run
        self.l_assertIsNotNone("test", False)
        self.l_assertIsNotNone(None, False)
<commit_msg>Changing comment format to better display in pycharm<commit_after># coding: utf-8

import unittest
import time
from base.generics.test import GenericTest

class TestDemo(GenericTest):

    """
        This is a demo class who will show-up most of the functionalities of this small test environment.
        PLease check the kissenium.ini file for more configuration
        If you need to run your tests in a specific order, you can add an order number after the method keyword 'test'
        Like this:
            def test_4_test_name(self):
                # My test
    """

    @unittest.skip("This is an example of a skipped test.")
    def test_1_skip(self):
        """ This test is skipped """

    def test_2_adiuvo_otp(self):
        """
        Run a test on a website, and do an assert on a OTP page
        :return:
        """
        self.selenium.get('https://www.adiuvo.fr')
        self.selenium.alert("Here is a simple test demo in a website.", 4, 4)
        r = self.selenium.get_element_by_xpath("//h1[@class='splash-head']")
        self.l_assertEqual("DÉVÉLOPPEMENT INFORMATIQUE, ADMINISTRATION SYSTÈMES LINUX ET WINDOWS", r.text)
        self.selenium.alert("Page title has been tested", 2, 2)
        self.selenium.click_first_xpath("//a[contains(., 'Générateur de mots de passe')]")

        # Testing the OTP form
        self.selenium.alert("Testing the OTP form", 10, 1)
        # Scroll to the form
        self.selenium.scroll_to_xpath("//form[@id='otp_form']")

        # Fill the form
        self.selenium.alert("Sending challenge input", 2, 2)
        self.selenium.send_keys_by_id("challenge", "1 selenium")
        self.selenium.alert("Sending secret input", 2, 2)
        self.selenium.send_keys_by_id("secret", "selenium")

        # Click on generate
        self.selenium.alert("Click to generate", 2, 2)
        self.selenium.get_element_by_xpath("//button[@type='submit' and contains(., 'Lancez le calcul!')]").click()
        r = self.selenium.get_element_by_xpath("//textarea[@placeholder = 'Les mots générés apparaîtrons ici.']")\
                                         .get_attribute('value')
        self.l_assertEqual("VET GWEN GOOF TOO MANA CARL (43B2 3A3B 20DB 6EC7)", r)
        self.selenium.alert("Assert is ok", 2, 1)

    def test_3_from_google_search(self):
        """
        Run a test wich start from google
        :return:
        """
        # Our first assert will be to check the google title
        self.selenium.get('https://www.google.fr')
        self.selenium.alert("This test will go trough standard browsing on the web with some tests.", 5, 3)
        self.l_assertIn('Google', self.selenium.browser.title, False)

        # We start by searching for selenium into google
        self.selenium.alert("We are starting by searching for selenium in google", 2, 1)
        self.selenium.send_keys_by_xpath("//input[@title='Rechercher']", "selenium software")
        self.selenium.get_element_by_xpath("//input[@value='Recherche Google']").click()

        # For the demo we will log every page name in the result of google
        g_results = self.selenium.get_elements_by_xpath('//div[@id="res"]//h3//a')
        for gr in g_results:
            self.logger.info("Element iteration demo : " + gr.text)

        # We first go into wikipedia, run an assert, and then go back
        self.selenium.click_first_xpath("//a[contains(., 'Wikipédia')]")
        self.selenium.alert("Browsing wikipedia page", 2, 1)
        self.selenium.alert("Testing the page title", 2, 1)
        title = self.selenium.get_element_by_id('firstHeading')
        self.l_assertIn("Selenium", title.text)
        self.selenium.alert("Page title is ok!", 2, 1)
        self.selenium.alert("Go to the previous page (self.browser.page)", 1, 1)
        self.selenium.browser.back()

        # We are going to the selenium official website
        self.selenium.alert("Go to the selenium website", 2, 2)
        self.selenium.click_first_xpath("//a[contains(., 'Selenium - Web Browser Automation')]")

        # Clicking on some links for the demo
        self.selenium.alert("Click on link", 2, 2)
        self.selenium.click_first_xpath("//ul[@id='sitemap']//a[contains(., 'Selenium IDE')]")
        self.selenium.alert("Go to download page", 2, 2)
        self.selenium.click_first_xpath("//div[@class='downloadBox']//a")
        self.selenium.page_wait_for_xpath("//h2")
        r = self.selenium.get_element_by_xpath("//a[contains(., 'Mozilla GeckoDriver')]")

        # Running an assert to check if the value is equal to what we expect
        self.l_assertEqual("https://github.com/mozilla/geckodriver/", r.get_attribute("href"))
        self.selenium.alert("End of the browsing test", 2, 2)

    def test_4_resize_demo(self):
        """
        This test show us a browser resize demo
        :return:
        """
        self.selenium.get('http://www.kissenium.org')
        self.selenium.alert("Here we will show how to resize your browser window", 2, 2)
        self.selenium.resize_window(480, 800)
        self.selenium.alert("480 x 800", 2, 2)
        self.selenium.resize_window(800, 480)
        self.selenium.alert("800 x 480", 2, 2)
        self.selenium.resize_window(1366, 768)
        self.selenium.alert("1366 x 768", 2, 2)
        self.selenium.maximize()
        self.selenium.alert("Maximize the window", 2, 2)

    def test_5_documentation_mode(self):
        """
        This test show us a small "documentation mode" example
        :return:
        """
        self.selenium.get('http://www.kissenium.org')
        self.selenium.alert("Here we show the documentation mode.", 20, 2)
        self.selenium.alert("This mode is experimental, and work only with elements who have id\\'s", 2, 2)
        self.selenium.scroll_to_xpath("//h2[@id='kissenium--selenium-framework']")
        self.selenium.dim("kissenium--selenium-framework", 2)
        self.selenium.alert("Think about scrolling the element before dim the page arround it.", 4, 1)
        self.selenium.alert("Use \\'self.st.hover_by_xpath\\'", 3, 2)
        self.selenium.scroll_to_xpath("//h2[@id='functionalities-done-and-to-do']")
        self.selenium.dim("functionalities-done-and-to-do", 2)
        self.selenium.scroll_to_xpath("//h2[@id='authors-contributors']")
        self.selenium.dim("authors-contributors", 2)

    def test_6_assert_log(self):
        """
        This a example of log assert method.
        This test will fail
        In this you will see how you can manage your asserts to continue to run (or not) with some simple parameters.
        The parameter passed to the l_assert function (True|False) will always override the
        Config['Kissenium']['FailOnAssertError'] parameter.
        :return:
        """
        self.selenium.get('http://www.kissenium.org')
        self.selenium.alert("There are just asserts examples working in the background.", 30, 1)
        self.selenium.alert("You can change the parameter FailOnAssertError to see what\\'s going on.", 30, 1)
        self.l_assertEqual(1, 1, False)
        self.l_assertEqual(1, 2, False)
        self.l_assertNotEqual(1, 2, False)
        self.l_assertNotEqual(1, 1, False)
        self.l_assertFalse(False, False)
        self.l_assertFalse(True, False)
        self.l_assertTrue(True, False)
        self.l_assertTrue(False, False)
        self.l_assertIn("e", "test", False)
        self.l_assertIn("a", "test", False)
        self.l_assertNotIn("a", "test", False)
        self.l_assertNotIn("e", "test", False)
        self.l_assertIsNone(None, False)
        self.l_assertIsNone("test")
        # Thoses assert will never be run
        self.l_assertIsNotNone("test", False)
        self.l_assertIsNotNone(None, False)
<|endoftext|>


<commit_before><commit_msg>elseviewer期刊采集<commit_after>#!usr/bin/env python
# -*- coding: utf-8 -*-
import re, time, logging, logging.handlers, copy
from common import base_dir, log_dir, journal_elsevier, get_html_text,\
    get_html_str, init_dir
from util import get_random_uniform, get_database_connect, get_phantomjs_page

# 保存下载文件的目录
root_dir = base_dir + 'elsevier/journals/'

# 程序运行日志文件
logfile = log_dir + 'log_science_direct.log'
logfile_size = 50 * 1024 * 1024  # 日志文件的最大容量，默认最大为50M
# 配置日志: 2个日志文件副本
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('science')

handler = logging.handlers.RotatingFileHandler(filename=logfile, maxBytes=logfile_size, backupCount=2, encoding='utf-8')
handler.setLevel(logging.INFO)

formatter = logging.Formatter('%(asctime)s [ %(name)s : %(levelname)s ] %(message)s')
handler.setFormatter(formatter)

logger.addHandler(handler)

# 处理一级页面
def handle_first_page(url, attrs):
    # 获得一级页面
    page_content = get_html_text(url)
    if page_content is None:
        logger.info('1级页面无法获取：' + str(url))
        return None
    raw_links = list()
    li_list = page_content.select('a[href^="http://dblp.uni-trier.de/db/journals/"]')
    for li in li_list:
        temp = li.get('href')
        if 'http://dblp.uni-trier.de/db/journals/' != temp:
            raw_links.append(temp)
    for url in raw_links:
        handle_second_page(url, attrs)
        # time.sleep(get_random_uniform(begin=2.0, end=60.0))

def handle_second_page(url, attrs):
    # 获得二级页面
    soup = get_html_text(url)
    if soup is None:
        return None
    # 优先使用DOI链接
    raw_links = soup.find_all(text=re.compile(r'electronic edition via DOI'))
    if len(raw_links) == 0:
        # 没有找到DOI链接，就选择使用通过 @ 找到的链接
        raw_links = soup.find_all(text=re.compile(r'electronic edition @'))
    links = map(lambda tmp: tmp.find_parent('a').get('href'), raw_links)
    if links is None:
        logger.info('处理二级页面，没有找到electronic edition链接' + str(url))
    handle_third_page(list(links), attrs)

def handle_third_page(urls, attrs):
    for url in urls:
        print(url)
        soup = get_html_str(get_phantomjs_page(url))
        if soup is None:
            logger.info('2级页面无法获取:' + str(url))
            return None
        else:
            link = soup.find('link', attrs={'rel': 'canonical'})
            if link:
                link = link.get('href')
            else:
                print('没有找到真正的链接')
                print(soup.prettify())
                return None
        soup = get_html_str(get_phantomjs_page(link))
        # 获取关于论文的描述信息:标题、作者、发表日期等等
        data_dict = copy.deepcopy(attrs)  # 深拷贝字典
        data_dict['url'] = link     # 保存论文的正真链接地址
        h1 = soup.find('h1', class_='svTitle')
        if h1:
            data_dict['title'] = h1.get_text().strip()
        ul = soup.find('ul', class_='authorGroup noCollab svAuthor')
        if ul:
            a_list = ul.find_all_next('a', class_='authorName svAuthor')
            authors_dict = dict()
            for a in a_list:
                affiliation_dict = dict()
                affiliation_dict['affiliation'] = ''
                affiliation_dict['affiliation_name'] = ''
                affiliation_dict['affiliation_country'] = ''
                author_name = a.get_text().strip()
                authors_dict[author_name] = affiliation_dict
            data_dict['author'] = authors_dict
        h2 = soup.find('h2', text=re.compile(r'Abstract'))
        if h2:
            p = h2.find_next_sibling('p')
            data_dict['abstract'] = p.get_text()
        h2 = soup.find('h2', text=re.compile(r'Keywords'))
        if h2:
            ul = h2.find_next_sibling('ul')
            keywords_list = ul.find_all_next('li', class_='svKeywords')
            keywords = list()
            for keyword in keywords_list:
                keywords.append(keyword.get_text().strip())
            data_dict['keywords'] = keywords
        h2 = soup.find('h2', text=re.compile(r'References'))
        if h2:
            li_list = h2.find_all_next('li', class_='title')
            references = list()
            for li in li_list:
                references.append(li.get_text().strip())
        print(data_dict)
        # time.sleep(get_random_uniform(begin=2.0, end=10.0))

def spider_science_direct(urls, attrs):
    init_dir(log_dir)
    init_dir(root_dir)
    for key, value in urls.items():
        attrs['rank'] = key
        for url in value:
            handle_first_page(url, attrs)

def run_science_direct():
    logger.warning('science_direct_spider正常启动!')
    try:
        spider_science_direct(journal_elsevier, attrs={'category': 'journal'})   # 采集期刊信息
    except Exception as e:
        logger.exception('science_direct_spider异常停止!')
    else:
        logger.warning('science_direct_spider正常停止!')

if __name__ == '__main__':
    # run_science_direct()
    handle_third_page(['http://dx.doi.org/10.1016/j.cose.2016.10.001',], {})<|endoftext|>


<commit_before>import os
import nose
import unittest

from django.test import TestCase as DjangoTestCase

from reviewboard.scmtools.core import SCMException, FileNotFoundException, \
                                      Revision, HEAD, PRE_CREATION, \
                                      ChangeSet
from reviewboard.scmtools.models import Repository, Tool

class CoreTests(unittest.TestCase):
    """Tests for the scmtools.core module"""

    def testGetTool(self):
        """Testing tool instantiation"""
        return

        # XXX This needs to be updated to use the new methods of loading
        #     tools.
        #tool = get_tool('reviewboard.scmtools.svn.SVNTool')
        #self.assertEqual(tool.__class__, SVNTool)

        #tool = get_tool('reviewboard.scmtools.perforce.PerforceTool')
        #self.assertEqual(tool.__class__, PerforceTool)

        #self.assertRaises(ImproperlyConfigured,
        #                  lambda: get_tool('blah.blah'))

        #self.assertRaises(ImproperlyConfigured,
        #                  lambda: get_tool('reviewboard.scmtools.svn.Foo'))

    def testInterface(self):
        """Testing basic scmtools.core API"""

        # Empty changeset
        cs = ChangeSet()
        self.assertEqual(cs.changenum, None)
        self.assertEqual(cs.summary, '')
        self.assertEqual(cs.description, '')
        self.assertEqual(cs.branch, '')
        self.assert_(len(cs.bugs_closed) == 0)
        self.assert_(len(cs.files) == 0)

class SubversionTests(unittest.TestCase):
    """Unit tests for subversion."""

    def setUp(self):
        svn_repo_path = os.path.join(os.path.dirname(__file__),
                                     'testdata/svn_repo')
        self.repository = Repository(name='Subversion SVN',
                                     path='file://' + svn_repo_path,
                                     tool=Tool.objects.get(name='Subversion'))

        try:
            self.tool = self.repository.get_scmtool()
        except ImportError:
            raise nose.SkipTest

    def testGetFile(self):
        """Testing SVNTool.get_file"""
        expected = 'include ../tools/Makefile.base-vars\nNAME = misc-docs\n' + \
                   'OUTNAME = svn-misc-docs\nINSTALL_DIR = $(DESTDIR)/usr/s' + \
                   'hare/doc/subversion\ninclude ../tools/Makefile.base-rul' + \
                   'es\n'

        # There are 3 versions of this test in order to get 100% coverage of
        # the svn module.
        rev = Revision('2')
        file = 'trunk/doc/misc-docs/Makefile'

        self.assertEqual(self.tool.get_file(file, rev), expected)

        self.assertEqual(self.tool.get_file('/' + file, rev), expected)

        self.assertEqual(self.tool.get_file(self.repository.path + '/' + file, rev),
                         expected)

        self.assert_(self.tool.file_exists('trunk/doc/misc-docs/Makefile'))
        self.assert_(not self.tool.file_exists('trunk/doc/misc-docs/Makefile2'))

        self.assertRaises(FileNotFoundException,
                          lambda: self.tool.get_file(''))

        self.assertRaises(FileNotFoundException,
                          lambda: self.tool.get_file('hello',
                                                     PRE_CREATION))

    def testRevisionParsing(self):
        """Testing revision number parsing"""
        self.assertEqual(self.tool.parse_diff_revision('', '(working copy)')[1],
                         HEAD)
        self.assertEqual(self.tool.parse_diff_revision('', '(revision 0)')[1],
                         PRE_CREATION)

        self.assertEqual(self.tool.parse_diff_revision('', '(revision 1)')[1],
                         '1')
        self.assertEqual(self.tool.parse_diff_revision('', '(revision 23)')[1],
                         '23')

        self.assertEqual(self.tool.parse_diff_revision('',
            '2007-06-06 15:32:23 UTC (rev 10958)')[1], '10958')

        self.assertRaises(SCMException,
                          lambda: self.tool.parse_diff_revision('', 'hello'))

    def testInterface(self):
        """Testing basic SVNTool API"""
        self.assertEqual(self.tool.get_diffs_use_absolute_paths(), False)

        self.assertRaises(NotImplementedError,
                          lambda: self.tool.get_changeset(1))

        self.assertRaises(NotImplementedError,
                          lambda: self.tool.get_pending_changesets(1))

class PerforceTests(unittest.TestCase):
    """Unit tests for perforce.

       This uses the open server at public.perforce.com to test various
       pieces.  Because we have no control over things like pending
       changesets, not everything can be tested.
       """

    def setUp(self):
        self.repository = Repository(name='Perforce.com',
                                     path='public.perforce.com:1666',
                                     tool=Tool.objects.get(name='Perforce'))

        try:
            self.tool = self.repository.get_scmtool()
        except ImportError:
            raise nose.SkipTest

    def testChangeset(self):
        """Testing PerforceTool.get_changeset"""

        desc = self.tool.get_changeset(157)
        self.assertEqual(desc.changenum, 157)
        self.assertEqual(hash(desc.description), -209144366)

        expected_files = [
            '//public/perforce/api/python/P4Client/P4Clientmodule.cc',
            '//public/perforce/api/python/P4Client/p4.py',
            '//public/perforce/api/python/P4Client/review.py',
            '//public/perforce/python/P4Client/P4Clientmodule.cc',
            '//public/perforce/python/P4Client/p4.py',
            '//public/perforce/python/P4Client/review.py',
        ]
        for file, expected in map(None, desc.files, expected_files):
            self.assertEqual(file, expected)

        self.assertEqual(hash(desc.summary), 588429333)

    def testGetFile(self):
        """Testing PerforceTool.get_file"""

        file = self.tool.get_file('//public/perforce/api/python/P4Client/p4.py',
                                  1)
        self.assertEqual(hash(file), 1392492355)

class VMWareTests(DjangoTestCase):
    """Tests for VMware specific code"""
    fixtures = ['vmware.json']

    def setUp(self):
        self.repository = Repository(name='VMware Test',
                                     path='perforce.eng.vmware.com:1666',
                                     tool=Tool.objects.get(name='VMware Perforce'))

        try:
            self.tool = self.repository.get_scmtool()
        except ImportError:
            raise nose.SkipTest

    def testParse(self):
        """Testing VMwarePerforceTool.parse_change_desc"""

        file = open(os.path.join(os.path.dirname(__file__), 'testdata',
                                 'vmware.changeset'), 'r')
        data = file.read().split('\n')
        file.close()

        changeset = self.tool.parse_change_desc(data, 123456)
        self.assertEqual(changeset.summary, "Emma")
        self.assertEqual(hash(changeset.description), 315618127)
        self.assertEqual(changeset.changenum, 123456)
        self.assertEqual(changeset.testing_done, "- Read a book.")

        self.assertEqual(len(changeset.bugs_closed), 1)
        self.assertEqual(changeset.bugs_closed[0], '128700')

        expected_files = [
            '//depot/bora/hosted07-rel/foo.cc',
            '//depot/bora/hosted07-rel/foo.hh',
            '//depot/bora/hosted07-rel/bar.cc',
            '//depot/bora/hosted07-rel/bar.hh',
        ]
        for file, expected in map(None, changeset.files, expected_files):
            self.assertEqual(file, expected)

        self.assertEqual(changeset.branch,
                         'hosted07-rel → hosted07 → bfg-main (manual)')

    def testParseSingleLineDesc(self):
        """
        Testing VMwarePerforceTool.parse_change_desc with a single line
        description.
        """
        file = open(os.path.join(os.path.dirname(__file__), 'testdata',
                                 'vmware-single-line-desc.changeset'), 'r')
        data = file.read().split('\n')
        file.close()

        changeset = self.tool.parse_change_desc(data, 1234567)
        self.assertEqual(changeset.summary,
                         "There is only a single line in this changeset description.")
        self.assertEqual(changeset.description,
                         "There is only a single line in this changeset description.")
        self.assertEqual(changeset.changenum, 1234567)
        self.assertEqual(changeset.testing_done, "")

        self.assertEqual(len(changeset.bugs_closed), 0)

        expected_files = [
            '//depot/qa/foo/bar',
        ]
        for file, expected in map(None, changeset.files, expected_files):
            self.assertEqual(file, expected)

    def testParseMultiLineSummary(self):
        """
        Testing VMwarePerforceTool.parse_change_desc with a summary spanning
        multiple lines.
        """
        file = open(os.path.join(os.path.dirname(__file__), 'testdata',
                                 'vmware-phil-is-crazy.changeset'), 'r')
        data = file.read().split('\n')
        file.close()

        changeset = self.tool.parse_change_desc(data, 123456)
        self.assertEqual(changeset.summary, "Changes: Emma")

        self.assertEqual(changeset.branch, 'bfg-main')
<commit_msg>Make unit test docstrings a single line so they'll be used as the test name<commit_after>import os
import nose
import unittest

from django.test import TestCase as DjangoTestCase

from reviewboard.scmtools.core import SCMException, FileNotFoundException, \
                                      Revision, HEAD, PRE_CREATION, \
                                      ChangeSet
from reviewboard.scmtools.models import Repository, Tool

class CoreTests(unittest.TestCase):
    """Tests for the scmtools.core module"""

    def testGetTool(self):
        """Testing tool instantiation"""
        return

        # XXX This needs to be updated to use the new methods of loading
        #     tools.
        #tool = get_tool('reviewboard.scmtools.svn.SVNTool')
        #self.assertEqual(tool.__class__, SVNTool)

        #tool = get_tool('reviewboard.scmtools.perforce.PerforceTool')
        #self.assertEqual(tool.__class__, PerforceTool)

        #self.assertRaises(ImproperlyConfigured,
        #                  lambda: get_tool('blah.blah'))

        #self.assertRaises(ImproperlyConfigured,
        #                  lambda: get_tool('reviewboard.scmtools.svn.Foo'))

    def testInterface(self):
        """Testing basic scmtools.core API"""

        # Empty changeset
        cs = ChangeSet()
        self.assertEqual(cs.changenum, None)
        self.assertEqual(cs.summary, '')
        self.assertEqual(cs.description, '')
        self.assertEqual(cs.branch, '')
        self.assert_(len(cs.bugs_closed) == 0)
        self.assert_(len(cs.files) == 0)

class SubversionTests(unittest.TestCase):
    """Unit tests for subversion."""

    def setUp(self):
        svn_repo_path = os.path.join(os.path.dirname(__file__),
                                     'testdata/svn_repo')
        self.repository = Repository(name='Subversion SVN',
                                     path='file://' + svn_repo_path,
                                     tool=Tool.objects.get(name='Subversion'))

        try:
            self.tool = self.repository.get_scmtool()
        except ImportError:
            raise nose.SkipTest

    def testGetFile(self):
        """Testing SVNTool.get_file"""
        expected = 'include ../tools/Makefile.base-vars\nNAME = misc-docs\n' + \
                   'OUTNAME = svn-misc-docs\nINSTALL_DIR = $(DESTDIR)/usr/s' + \
                   'hare/doc/subversion\ninclude ../tools/Makefile.base-rul' + \
                   'es\n'

        # There are 3 versions of this test in order to get 100% coverage of
        # the svn module.
        rev = Revision('2')
        file = 'trunk/doc/misc-docs/Makefile'

        self.assertEqual(self.tool.get_file(file, rev), expected)

        self.assertEqual(self.tool.get_file('/' + file, rev), expected)

        self.assertEqual(self.tool.get_file(self.repository.path + '/' + file, rev),
                         expected)

        self.assert_(self.tool.file_exists('trunk/doc/misc-docs/Makefile'))
        self.assert_(not self.tool.file_exists('trunk/doc/misc-docs/Makefile2'))

        self.assertRaises(FileNotFoundException,
                          lambda: self.tool.get_file(''))

        self.assertRaises(FileNotFoundException,
                          lambda: self.tool.get_file('hello',
                                                     PRE_CREATION))

    def testRevisionParsing(self):
        """Testing revision number parsing"""
        self.assertEqual(self.tool.parse_diff_revision('', '(working copy)')[1],
                         HEAD)
        self.assertEqual(self.tool.parse_diff_revision('', '(revision 0)')[1],
                         PRE_CREATION)

        self.assertEqual(self.tool.parse_diff_revision('', '(revision 1)')[1],
                         '1')
        self.assertEqual(self.tool.parse_diff_revision('', '(revision 23)')[1],
                         '23')

        self.assertEqual(self.tool.parse_diff_revision('',
            '2007-06-06 15:32:23 UTC (rev 10958)')[1], '10958')

        self.assertRaises(SCMException,
                          lambda: self.tool.parse_diff_revision('', 'hello'))

    def testInterface(self):
        """Testing basic SVNTool API"""
        self.assertEqual(self.tool.get_diffs_use_absolute_paths(), False)

        self.assertRaises(NotImplementedError,
                          lambda: self.tool.get_changeset(1))

        self.assertRaises(NotImplementedError,
                          lambda: self.tool.get_pending_changesets(1))

class PerforceTests(unittest.TestCase):
    """Unit tests for perforce.

       This uses the open server at public.perforce.com to test various
       pieces.  Because we have no control over things like pending
       changesets, not everything can be tested.
       """

    def setUp(self):
        self.repository = Repository(name='Perforce.com',
                                     path='public.perforce.com:1666',
                                     tool=Tool.objects.get(name='Perforce'))

        try:
            self.tool = self.repository.get_scmtool()
        except ImportError:
            raise nose.SkipTest

    def testChangeset(self):
        """Testing PerforceTool.get_changeset"""

        desc = self.tool.get_changeset(157)
        self.assertEqual(desc.changenum, 157)
        self.assertEqual(hash(desc.description), -209144366)

        expected_files = [
            '//public/perforce/api/python/P4Client/P4Clientmodule.cc',
            '//public/perforce/api/python/P4Client/p4.py',
            '//public/perforce/api/python/P4Client/review.py',
            '//public/perforce/python/P4Client/P4Clientmodule.cc',
            '//public/perforce/python/P4Client/p4.py',
            '//public/perforce/python/P4Client/review.py',
        ]
        for file, expected in map(None, desc.files, expected_files):
            self.assertEqual(file, expected)

        self.assertEqual(hash(desc.summary), 588429333)

    def testGetFile(self):
        """Testing PerforceTool.get_file"""

        file = self.tool.get_file('//public/perforce/api/python/P4Client/p4.py',
                                  1)
        self.assertEqual(hash(file), 1392492355)

class VMWareTests(DjangoTestCase):
    """Tests for VMware specific code"""
    fixtures = ['vmware.json']

    def setUp(self):
        self.repository = Repository(name='VMware Test',
                                     path='perforce.eng.vmware.com:1666',
                                     tool=Tool.objects.get(name='VMware Perforce'))

        try:
            self.tool = self.repository.get_scmtool()
        except ImportError:
            raise nose.SkipTest

    def testParse(self):
        """Testing VMware changeset parsing"""

        file = open(os.path.join(os.path.dirname(__file__), 'testdata',
                                 'vmware.changeset'), 'r')
        data = file.read().split('\n')
        file.close()

        changeset = self.tool.parse_change_desc(data, 123456)
        self.assertEqual(changeset.summary, "Emma")
        self.assertEqual(hash(changeset.description), 315618127)
        self.assertEqual(changeset.changenum, 123456)
        self.assertEqual(changeset.testing_done, "- Read a book.")

        self.assertEqual(len(changeset.bugs_closed), 1)
        self.assertEqual(changeset.bugs_closed[0], '128700')

        expected_files = [
            '//depot/bora/hosted07-rel/foo.cc',
            '//depot/bora/hosted07-rel/foo.hh',
            '//depot/bora/hosted07-rel/bar.cc',
            '//depot/bora/hosted07-rel/bar.hh',
        ]
        for file, expected in map(None, changeset.files, expected_files):
            self.assertEqual(file, expected)

        self.assertEqual(changeset.branch,
                         'hosted07-rel → hosted07 → bfg-main (manual)')

    def testParseSingleLineDesc(self):
        """Testing VMware changeset parsing with a single line description."""
        file = open(os.path.join(os.path.dirname(__file__), 'testdata',
                                 'vmware-single-line-desc.changeset'), 'r')
        data = file.read().split('\n')
        file.close()

        changeset = self.tool.parse_change_desc(data, 1234567)
        self.assertEqual(changeset.summary,
                         "There is only a single line in this changeset description.")
        self.assertEqual(changeset.description,
                         "There is only a single line in this changeset description.")
        self.assertEqual(changeset.changenum, 1234567)
        self.assertEqual(changeset.testing_done, "")

        self.assertEqual(len(changeset.bugs_closed), 0)

        expected_files = [
            '//depot/qa/foo/bar',
        ]
        for file, expected in map(None, changeset.files, expected_files):
            self.assertEqual(file, expected)

    def testParseMultiLineSummary(self):
        """Testing VMware changeset parsing with a summary spanning multiple lines."""
        file = open(os.path.join(os.path.dirname(__file__), 'testdata',
                                 'vmware-phil-is-crazy.changeset'), 'r')
        data = file.read().split('\n')
        file.close()

        changeset = self.tool.parse_change_desc(data, 123456)
        self.assertEqual(changeset.summary, "Changes: Emma")

        self.assertEqual(changeset.branch, 'bfg-main')
<|endoftext|>


<commit_before># coding: utf-8

from __future__ import print_function
import json
import time
from selenium.webdriver import PhantomJS
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.common.exceptions import WebDriverException
from django.test import TestCase
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
try:
    from unittest import mock
except ImportError:
    import mock

class RenderTinyMceWidgetTestCase(StaticLiveServerTestCase):
    def setUp(self):
        desired = DesiredCapabilities.PHANTOMJS
        desired['loggingPrefs'] = {'browser': 'ALL'}
        self.browser = PhantomJS(desired_capabilities=desired)
        super(RenderTinyMceWidgetTestCase, self).setUp()

    def tearDown(self):
        self.browser.quit()
        super(RenderTinyMceWidgetTestCase, self).tearDown()

    def test_rendering_tinymce4_widget(self):
        # Test if TinyMCE 4 widget is actually rendered by JavaScript
        self.browser.get(self.live_server_url + reverse('create'))
        try:
            self.browser.find_element_by_id('mceu_16')
        except WebDriverException:
            print('*** Start browser log ***')
            print(self.browser.get_log('browser'))
            print('**** End browser log ****')
            raise

    def test_rendering_with_different_language(self):
        with self.settings(LANGUAGE_CODE='fr-fr'):
            self.browser.get(self.live_server_url + reverse('create'))
            try:
                self.browser.find_element_by_id('mceu_16')
            except WebDriverException:
                print('*** Start browser log ***')
                print(self.browser.get_log('browser'))
                print('**** End browser log ****')
                raise
            else:
                self.assertTrue('Appuyer sur ALT-F9 pour le menu.' in
                                self.browser.page_source)

class RenderTinyMceAdminWidgetTestCase(StaticLiveServerTestCase):
    def setUp(self):
        desired = DesiredCapabilities.PHANTOMJS
        desired['loggingPrefs'] = {'browser': 'ALL'}
        self.browser = PhantomJS(desired_capabilities=desired)
        User.objects.create_superuser('test', 'test@test.com', 'test')
        self.browser.get(self.live_server_url + '/admin')
        self.browser.find_element_by_id('id_username').send_keys('test')
        self.browser.find_element_by_id('id_password').send_keys('test')
        self.browser.find_element_by_css_selector('input[type="submit"]').click()
        time.sleep(0.1)
        super(RenderTinyMceAdminWidgetTestCase, self).setUp()

    def tearDown(self):
        self.browser.quit()
        super(RenderTinyMceAdminWidgetTestCase, self).tearDown()

    def test_rendering_tinymce4_admin_widget(self):
        self.browser.get(self.live_server_url + '/admin/test_tinymce/testmodel/add/')
        time.sleep(0.1)
        editors = self.browser.find_elements_by_class_name('mce-tinymce')
        try:
            self.assertTrue(len(editors) == 2)
        except AssertionError:
            print('*** Start browser log ***')
            print(self.browser.get_log('browser'))
            print('**** End browser log ****')
            raise

    def test_adding_tinymce_widget_in_admin_inline(self):
        self.browser.get(self.live_server_url + '/admin/test_tinymce/testmodel/add/')
        time.sleep(0.1)
        self.browser.find_element_by_css_selector('div.add-row a').click()
        editors = self.browser.find_elements_by_class_name('mce-tinymce')
        try:
            self.assertTrue(len(editors) == 3)
        except AssertionError:
            print('*** Start browser log ***')
            print(self.browser.get_log('browser'))
            print('**** End browser log ****')
            raise

class SpellCheckViewTestCase(TestCase):
    def test_spell_check(self):
        import enchant
        languages = enchant.list_languages()
        lang = 'en_US'
        if lang not in languages:
            lang = lang[:2]
            if lang not in languages:
                raise RuntimeError('Enchant package does not have English spellckecker dictionary!')
        text = 'The quick brown fox jumps over the lazy dog.'
        data = {'id': '0', 'params': {'lang': lang, 'text': text}}
        response = self.client.post(reverse('tinymce-spellchecker'), data=json.dumps(data),
                                    content_type='application/json')
        self.assertTrue('result' in json.loads(response.content.decode('utf-8')))
        text = 'The quik brown fox jumps ower the lazy dog.'
        data['params']['text'] = text
        response = self.client.post(reverse('tinymce-spellchecker'), data=json.dumps(data),
                                    content_type='application/json')
        result = json.loads(response.content.decode('utf-8'))['result']
        self.assertEqual(len(result), 2)

    def test_missing_dictionary(self):
        data = {'id': '0', 'params': {'lang': 'fo_BA', 'text': 'text'}}
        response = self.client.post(reverse('tinymce-spellchecker'), data=json.dumps(data),
                                    content_type='application/json')
        self.assertTrue('error' in json.loads(response.content.decode('utf-8')))

class CssViewTestCase(TestCase):
    def test_css_view(self):
        response = self.client.get(reverse('tinymce-css'))
        self.assertContains(response, 'margin-left')

class FileBrowserViewTestCase(TestCase):
    @mock.patch('tinymce.views.reverse')
    def test_filebrowser_view(self, mock_reverse):
        mock_reverse.return_value = '/filebrowser'
        response = self.client.get(reverse('tinymce-filebrowser'))
        self.assertContains(response, '/filebrowser')
<commit_msg>Add test for removing and adding inline formset<commit_after># coding: utf-8

from __future__ import print_function
import json
import time
from selenium.webdriver import PhantomJS
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.common.exceptions import WebDriverException
from django.test import TestCase
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
try:
    from unittest import mock
except ImportError:
    import mock

class RenderTinyMceWidgetTestCase(StaticLiveServerTestCase):
    def setUp(self):
        desired = DesiredCapabilities.PHANTOMJS
        desired['loggingPrefs'] = {'browser': 'ALL'}
        self.browser = PhantomJS(desired_capabilities=desired)
        super(RenderTinyMceWidgetTestCase, self).setUp()

    def tearDown(self):
        self.browser.quit()
        super(RenderTinyMceWidgetTestCase, self).tearDown()

    def test_rendering_tinymce4_widget(self):
        # Test if TinyMCE 4 widget is actually rendered by JavaScript
        self.browser.get(self.live_server_url + reverse('create'))
        try:
            self.browser.find_element_by_id('mceu_16')
        except WebDriverException:
            print('*** Start browser log ***')
            print(self.browser.get_log('browser'))
            print('**** End browser log ****')
            raise

    def test_rendering_with_different_language(self):
        with self.settings(LANGUAGE_CODE='fr-fr'):
            self.browser.get(self.live_server_url + reverse('create'))
            try:
                self.browser.find_element_by_id('mceu_16')
            except WebDriverException:
                print('*** Start browser log ***')
                print(self.browser.get_log('browser'))
                print('**** End browser log ****')
                raise
            else:
                self.assertTrue('Appuyer sur ALT-F9 pour le menu.' in
                                self.browser.page_source)

class RenderTinyMceAdminWidgetTestCase(StaticLiveServerTestCase):
    def setUp(self):
        desired = DesiredCapabilities.PHANTOMJS
        desired['loggingPrefs'] = {'browser': 'ALL'}
        self.browser = PhantomJS(desired_capabilities=desired)
        User.objects.create_superuser('test', 'test@test.com', 'test')
        self.browser.get(self.live_server_url + '/admin')
        self.browser.find_element_by_id('id_username').send_keys('test')
        self.browser.find_element_by_id('id_password').send_keys('test')
        self.browser.find_element_by_css_selector('input[type="submit"]').click()
        time.sleep(0.1)
        super(RenderTinyMceAdminWidgetTestCase, self).setUp()

    def tearDown(self):
        self.browser.quit()
        super(RenderTinyMceAdminWidgetTestCase, self).tearDown()

    def test_rendering_tinymce4_admin_widget(self):
        self.browser.get(self.live_server_url + '/admin/test_tinymce/testmodel/add/')
        time.sleep(0.1)
        editors = self.browser.find_elements_by_class_name('mce-tinymce')
        try:
            self.assertEqual(len(editors), 2)
        except AssertionError:
            print('*** Start browser log ***')
            print(self.browser.get_log('browser'))
            print('**** End browser log ****')
            raise

    def test_adding_tinymce_widget_in_admin_inline(self):
        self.browser.get(self.live_server_url + '/admin/test_tinymce/testmodel/add/')
        time.sleep(0.1)
        self.browser.find_element_by_css_selector('div.add-row a').click()
        editors = self.browser.find_elements_by_class_name('mce-tinymce')
        try:
            self.assertEqual(len(editors), 3)
        except AssertionError:
            print('*** Start browser log ***')
            print(self.browser.get_log('browser'))
            print('**** End browser log ****')
            raise
        self.browser.find_element_by_css_selector('a.inline-deletelink').click()
        editors = self.browser.find_elements_by_class_name('mce-tinymce')
        self.assertEqual(len(editors), 2)
        self.browser.find_element_by_css_selector('div.add-row a').click()
        editors = self.browser.find_elements_by_class_name('mce-tinymce')
        try:
            self.assertEqual(len(editors), 3)
        except AssertionError:
            print('*** Start browser log ***')
            print(self.browser.get_log('browser'))
            print('**** End browser log ****')
            raise

class SpellCheckViewTestCase(TestCase):
    def test_spell_check(self):
        import enchant
        languages = enchant.list_languages()
        lang = 'en_US'
        if lang not in languages:
            lang = lang[:2]
            if lang not in languages:
                raise RuntimeError('Enchant package does not have English spellckecker dictionary!')
        text = 'The quick brown fox jumps over the lazy dog.'
        data = {'id': '0', 'params': {'lang': lang, 'text': text}}
        response = self.client.post(reverse('tinymce-spellchecker'), data=json.dumps(data),
                                    content_type='application/json')
        self.assertTrue('result' in json.loads(response.content.decode('utf-8')))
        text = 'The quik brown fox jumps ower the lazy dog.'
        data['params']['text'] = text
        response = self.client.post(reverse('tinymce-spellchecker'), data=json.dumps(data),
                                    content_type='application/json')
        result = json.loads(response.content.decode('utf-8'))['result']
        self.assertEqual(len(result), 2)

    def test_missing_dictionary(self):
        data = {'id': '0', 'params': {'lang': 'fo_BA', 'text': 'text'}}
        response = self.client.post(reverse('tinymce-spellchecker'), data=json.dumps(data),
                                    content_type='application/json')
        self.assertTrue('error' in json.loads(response.content.decode('utf-8')))

class CssViewTestCase(TestCase):
    def test_css_view(self):
        response = self.client.get(reverse('tinymce-css'))
        self.assertContains(response, 'margin-left')

class FileBrowserViewTestCase(TestCase):
    @mock.patch('tinymce.views.reverse')
    def test_filebrowser_view(self, mock_reverse):
        mock_reverse.return_value = '/filebrowser'
        response = self.client.get(reverse('tinymce-filebrowser'))
        self.assertContains(response, '/filebrowser')
<|endoftext|>