#!/usr/bin/env python
"""Some examples:

  $ discos-simulator start -s active_surface
  $ discos-simulator stop -s active_surface
  $ discos-simulator start -s if_distributor
  $ discos-simulator stop -s if_distributor
"""
from __future__ import print_function
import importlib
import subprocess
import sys
import time
import socket
from argparse import ArgumentParser, ArgumentTypeError

from simulators.server import Simulator
from simulators.utils import list_simulators

AVAILABLE_SIMULATORS = list_simulators()


def system_from_arg(system_name):
    try:
        return importlib.import_module('simulators.%s' % system_name)
    except ImportError as e:
        error = e.args[0]
        if 'No module named' in error:
            error = 'System "%s" unavailable.' % system_name
        else:
            error = error % system_name
        raise ArgumentTypeError(error)

parser = ArgumentParser()
parser.add_argument(
    'action',
    choices=['start', 'stop', 'list']
)
parser.add_argument(
    '-s', '--system',
    type=system_from_arg,
    required=False,
    help='System name: active_surface, acu, ...',
)
parser.add_argument(
    '-t', '--type',
    type=str,
    required=False,
    help='System configuration type: IFD_14_channels for if_distributor, ...',
)
args = parser.parse_args()

if args.action == 'list':
    print(
        "Available simulators: '"
        + "', '".join(AVAILABLE_SIMULATORS)
        + "'."
    )

kwargs = {}

if args.type:
    if not args.system:
        parser.error(
            "The '--type' argument only has to be used "
            + "in conjunction with the '--system' argument."
        )
    try:
        systems = getattr(args.system, 'systems')
    except AttributeError:
        parser.error(
            ('System %s has no configurations other than the default one. '
            % args.system.__name__.rsplit('.', 1)[1])
            + "Omit the '--type' argument to start the simulator properly."
        )
    if args.type not in systems:
        err_string = (
            'Configuration %s for system %s not found.\n' %
            (args.type, args.system.__name__.rsplit('.', 1)[1])
        )
        err_string += 'Available configurations for chosen simulator:\n'
        err_string += "'" + "', ".join(systems) + "'."
        parser.error(err_string)
    kwargs['system_type'] = args.type

if args.action == 'start':
    if args.system:
        try:
            simulator = Simulator(args.system, **kwargs)
            simulator.start()
        except socket.error:
            print(
                "Cannot start simulator '%s', address already in use." %
                args.system.__name__.rsplit('.', 1)[1]
            )
            sys.exit(1)
    else:
        for sim in AVAILABLE_SIMULATORS:
            subprocess.Popen([
                sys.argv[0],
                'start',
                '-s',
                sim
            ])
        time.sleep(3)
elif args.action == 'stop':
    if args.system:
        simulator = Simulator(args.system, **kwargs)
        simulator.stop()
    else:
        for sim in AVAILABLE_SIMULATORS:
            sim = importlib.import_module('simulators.%s' % sim)
            simulator = Simulator(sim, **kwargs)
            simulator.stop()

c6c9fa394f763650601ff82a5fa7c1d7a4b9f563
<mask token>


def system_from_arg(system_name):
    try:
        return importlib.import_module('simulators.%s' % system_name)
    except ImportError as e:
        error = e.args[0]
        if 'No module named' in error:
            error = 'System "%s" unavailable.' % system_name
        else:
            error = error % system_name
        raise ArgumentTypeError(error)


<mask token>

<mask token>


def system_from_arg(system_name):
    try:
        return importlib.import_module('simulators.%s' % system_name)
    except ImportError as e:
        error = e.args[0]
        if 'No module named' in error:
            error = 'System "%s" unavailable.' % system_name
        else:
            error = error % system_name
        raise ArgumentTypeError(error)


<mask token>
parser.add_argument('action', choices=['start', 'stop', 'list'])
parser.add_argument('-s', '--system', type=system_from_arg, required=False,
    help='System name: active_surface, acu, ...')
parser.add_argument('-t', '--type', type=str, required=False, help=
    'System configuration type: IFD_14_channels for if_distributor, ...')
<mask token>
if args.action == 'list':
    print("Available simulators: '" + "', '".join(AVAILABLE_SIMULATORS) + "'.")
<mask token>
if args.type:
    if not args.system:
        parser.error("The '--type' argument only has to be used " +
            "in conjunction with the '--system' argument.")
    try:
        systems = getattr(args.system, 'systems')
    except AttributeError:
        parser.error(
            'System %s has no configurations other than the default one. ' %
            args.system.__name__.rsplit('.', 1)[1] +
            "Omit the '--type' argument to start the simulator properly.")
    if args.type not in systems:
        err_string = 'Configuration %s for system %s not found.\n' % (args.
            type, args.system.__name__.rsplit('.', 1)[1])
        err_string += 'Available configurations for chosen simulator:\n'
        err_string += "'" + "', ".join(systems) + "'."
        parser.error(err_string)
    kwargs['system_type'] = args.type
if args.action == 'start':
    if args.system:
        try:
            simulator = Simulator(args.system, **kwargs)
            simulator.start()
        except socket.error:
            print("Cannot start simulator '%s', address already in use." %
                args.system.__name__.rsplit('.', 1)[1])
            sys.exit(1)
    else:
        for sim in AVAILABLE_SIMULATORS:
            subprocess.Popen([sys.argv[0], 'start', '-s', sim])
        time.sleep(3)
elif args.action == 'stop':
    if args.system:
        simulator = Simulator(args.system, **kwargs)
        simulator.stop()
    else:
        for sim in AVAILABLE_SIMULATORS:
            sim = importlib.import_module('simulators.%s' % sim)
            simulator = Simulator(sim, **kwargs)
            simulator.stop()

<mask token>
AVAILABLE_SIMULATORS = list_simulators()


def system_from_arg(system_name):
    try:
        return importlib.import_module('simulators.%s' % system_name)
    except ImportError as e:
        error = e.args[0]
        if 'No module named' in error:
            error = 'System "%s" unavailable.' % system_name
        else:
            error = error % system_name
        raise ArgumentTypeError(error)


parser = ArgumentParser()
parser.add_argument('action', choices=['start', 'stop', 'list'])
parser.add_argument('-s', '--system', type=system_from_arg, required=False,
    help='System name: active_surface, acu, ...')
parser.add_argument('-t', '--type', type=str, required=False, help=
    'System configuration type: IFD_14_channels for if_distributor, ...')
args = parser.parse_args()
if args.action == 'list':
    print("Available simulators: '" + "', '".join(AVAILABLE_SIMULATORS) + "'.")
kwargs = {}
if args.type:
    if not args.system:
        parser.error("The '--type' argument only has to be used " +
            "in conjunction with the '--system' argument.")
    try:
        systems = getattr(args.system, 'systems')
    except AttributeError:
        parser.error(
            'System %s has no configurations other than the default one. ' %
            args.system.__name__.rsplit('.', 1)[1] +
            "Omit the '--type' argument to start the simulator properly.")
    if args.type not in systems:
        err_string = 'Configuration %s for system %s not found.\n' % (args.
            type, args.system.__name__.rsplit('.', 1)[1])
        err_string += 'Available configurations for chosen simulator:\n'
        err_string += "'" + "', ".join(systems) + "'."
        parser.error(err_string)
    kwargs['system_type'] = args.type
if args.action == 'start':
    if args.system:
        try:
            simulator = Simulator(args.system, **kwargs)
            simulator.start()
        except socket.error:
            print("Cannot start simulator '%s', address already in use." %
                args.system.__name__.rsplit('.', 1)[1])
            sys.exit(1)
    else:
        for sim in AVAILABLE_SIMULATORS:
            subprocess.Popen([sys.argv[0], 'start', '-s', sim])
        time.sleep(3)
elif args.action == 'stop':
    if args.system:
        simulator = Simulator(args.system, **kwargs)
        simulator.stop()
    else:
        for sim in AVAILABLE_SIMULATORS:
            sim = importlib.import_module('simulators.%s' % sim)
            simulator = Simulator(sim, **kwargs)
            simulator.stop()

<mask token>
from __future__ import print_function
import importlib
import subprocess
import sys
import time
import socket
from argparse import ArgumentParser, ArgumentTypeError
from simulators.server import Simulator
from simulators.utils import list_simulators
AVAILABLE_SIMULATORS = list_simulators()


def system_from_arg(system_name):
    try:
        return importlib.import_module('simulators.%s' % system_name)
    except ImportError as e:
        error = e.args[0]
        if 'No module named' in error:
            error = 'System "%s" unavailable.' % system_name
        else:
            error = error % system_name
        raise ArgumentTypeError(error)


parser = ArgumentParser()
parser.add_argument('action', choices=['start', 'stop', 'list'])
parser.add_argument('-s', '--system', type=system_from_arg, required=False,
    help='System name: active_surface, acu, ...')
parser.add_argument('-t', '--type', type=str, required=False, help=
    'System configuration type: IFD_14_channels for if_distributor, ...')
args = parser.parse_args()
if args.action == 'list':
    print("Available simulators: '" + "', '".join(AVAILABLE_SIMULATORS) + "'.")
kwargs = {}
if args.type:
    if not args.system:
        parser.error("The '--type' argument only has to be used " +
            "in conjunction with the '--system' argument.")
    try:
        systems = getattr(args.system, 'systems')
    except AttributeError:
        parser.error(
            'System %s has no configurations other than the default one. ' %
            args.system.__name__.rsplit('.', 1)[1] +
            "Omit the '--type' argument to start the simulator properly.")
    if args.type not in systems:
        err_string = 'Configuration %s for system %s not found.\n' % (args.
            type, args.system.__name__.rsplit('.', 1)[1])
        err_string += 'Available configurations for chosen simulator:\n'
        err_string += "'" + "', ".join(systems) + "'."
        parser.error(err_string)
    kwargs['system_type'] = args.type
if args.action == 'start':
    if args.system:
        try:
            simulator = Simulator(args.system, **kwargs)
            simulator.start()
        except socket.error:
            print("Cannot start simulator '%s', address already in use." %
                args.system.__name__.rsplit('.', 1)[1])
            sys.exit(1)
    else:
        for sim in AVAILABLE_SIMULATORS:
            subprocess.Popen([sys.argv[0], 'start', '-s', sim])
        time.sleep(3)
elif args.action == 'stop':
    if args.system:
        simulator = Simulator(args.system, **kwargs)
        simulator.stop()
    else:
        for sim in AVAILABLE_SIMULATORS:
            sim = importlib.import_module('simulators.%s' % sim)
            simulator = Simulator(sim, **kwargs)
            simulator.stop()

#!/usr/bin/env python
"""Some examples:

  $ discos-simulator start -s active_surface
  $ discos-simulator stop -s active_surface
  $ discos-simulator start -s if_distributor
  $ discos-simulator stop -s if_distributor
"""
from __future__ import print_function
import importlib
import subprocess
import sys
import time
import socket
from argparse import ArgumentParser, ArgumentTypeError

from simulators.server import Simulator
from simulators.utils import list_simulators

AVAILABLE_SIMULATORS = list_simulators()


def system_from_arg(system_name):
    try:
        return importlib.import_module('simulators.%s' % system_name)
    except ImportError as e:
        error = e.args[0]
        if 'No module named' in error:
            error = 'System "%s" unavailable.' % system_name
        else:
            error = error % system_name
        raise ArgumentTypeError(error)

parser = ArgumentParser()
parser.add_argument(
    'action',
    choices=['start', 'stop', 'list']
)
parser.add_argument(
    '-s', '--system',
    type=system_from_arg,
    required=False,
    help='System name: active_surface, acu, ...',
)
parser.add_argument(
    '-t', '--type',
    type=str,
    required=False,
    help='System configuration type: IFD_14_channels for if_distributor, ...',
)
args = parser.parse_args()

if args.action == 'list':
    print(
        "Available simulators: '"
        + "', '".join(AVAILABLE_SIMULATORS)
        + "'."
    )

kwargs = {}

if args.type:
    if not args.system:
        parser.error(
            "The '--type' argument only has to be used "
            + "in conjunction with the '--system' argument."
        )
    try:
        systems = getattr(args.system, 'systems')
    except AttributeError:
        parser.error(
            ('System %s has no configurations other than the default one. '
            % args.system.__name__.rsplit('.', 1)[1])
            + "Omit the '--type' argument to start the simulator properly."
        )
    if args.type not in systems:
        err_string = (
            'Configuration %s for system %s not found.\n' %
            (args.type, args.system.__name__.rsplit('.', 1)[1])
        )
        err_string += 'Available configurations for chosen simulator:\n'
        err_string += "'" + "', ".join(systems) + "'."
        parser.error(err_string)
    kwargs['system_type'] = args.type

if args.action == 'start':
    if args.system:
        try:
            simulator = Simulator(args.system, **kwargs)
            simulator.start()
        except socket.error:
            print(
                "Cannot start simulator '%s', address already in use." %
                args.system.__name__.rsplit('.', 1)[1]
            )
            sys.exit(1)
    else:
        for sim in AVAILABLE_SIMULATORS:
            subprocess.Popen([
                sys.argv[0],
                'start',
                '-s',
                sim
            ])
        time.sleep(3)
elif args.action == 'stop':
    if args.system:
        simulator = Simulator(args.system, **kwargs)
        simulator.stop()
    else:
        for sim in AVAILABLE_SIMULATORS:
            sim = importlib.import_module('simulators.%s' % sim)
            simulator = Simulator(sim, **kwargs)
            simulator.stop()
