Ejemplo n.º 1
0
from jupyter_server.utils import url_path_join

# List of dependencies needed for this plugin.
pytest_plugins = [
    "pytest_tornasync",
    # Once the chunk below moves to Jupyter Core, we'll uncomment
    # This plugin and use the fixtures directly from Jupyter Core.
    # "jupyter_core.pytest_plugin"
]


import asyncio

if os.name == "nt" and sys.version_info >= (3, 7):
    asyncio.set_event_loop_policy(
        asyncio.WindowsSelectorEventLoopPolicy()  # type:ignore[attr-defined]
    )


# ============ Move to Jupyter Core =============


def mkdir(tmp_path, *parts):
    path = tmp_path.joinpath(*parts)
    if not path.exists():
        path.mkdir(parents=True)
    return path


@pytest.fixture
def jp_home_dir(tmp_path):
Ejemplo n.º 2
0
def main():
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
    tornado.options.parse_command_line()
    app = Application()
    app.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()
Ejemplo n.º 3
0
import os

from aiohttp import web
from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop

from meross_iot.controller.mixins.light import LightMixin
from meross_iot.controller.mixins.system import SystemAllMixin
from meross_iot.controller.mixins.toggle import ToggleXMixin
from meross_iot.manager import MerossManager
from meross_iot.model.enums import OnlineStatus
from tests import async_get_client


if os.name == 'nt':
    import asyncio
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
else:
    import asyncio


class TestUpdate(AioHTTPTestCase):
    async def get_application(self):
        return web.Application()

    async def setUpAsync(self):
        # Wait some time before next test-burst
        await asyncio.sleep(10)
        self.meross_client, self.requires_logout = await async_get_client()

        # Look for a device to be used for this test
        self.meross_manager = MerossManager(http_client=self.meross_client)
Ejemplo n.º 4
0
def configure_windows_event_loop() -> None:
    if sys.version_info >= (3, 8) and platform.system() == "Windows":
        import asyncio

        asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()
                                      )  # type: ignore[attr-defined]
Ejemplo n.º 5
0
def main():
    try:
        settings = toml.load(open('../settings.toml', 'r'))
    except ValueError:
        print('Failed to parse settings.toml')
        settings = {'auto_update': True}

    if settings['auto_update']:
        check_for_update()

    if sys.version_info[0] == 3 and sys.version_info[
            1] >= 8 and sys.platform.startswith('win'):
        asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

    if len(sys.argv) > 1:
        parser = argparse.ArgumentParser()
        parser.add_argument(
            '-d',
            '--date',
            type=str,
            metavar='',
            help=
            'Date or date range in format YYYY/MM/DD e.g 2020/01/19-2020/05/01'
        )
        parser.add_argument('-c',
                            '--course',
                            type=str,
                            metavar='',
                            help='Numeric course code e.g 20')
        parser.add_argument('-r',
                            '--region',
                            type=str,
                            metavar='',
                            help='Region code e.g ire')
        parser.add_argument(
            '-y',
            '--year',
            type=str,
            metavar='',
            help='Year or year range in format YYYY e.g 2018-2020')
        parser.add_argument('-t',
                            '--type',
                            type=str,
                            metavar='',
                            help='Race type [flat/jumps]')
        args = parser.parse_args()

        if args.date and any([args.course, args.year, args.type]):
            print(
                'Arguments not compatible with -d flag.\n\nFormat:\n\t\t-d YYYY/MM/DD -r [REGION CODE]\n\nExamples:\n\t\t-d 2020/01/19 -r gb\n'
            )
            print(
                'When scraping by date, if no region code is specified, all available races will be scraped by default.'
            )
            sys.exit()

        if args.date:
            if not check_date(args.date):
                sys.exit(
                    print(
                        'Invalid date.\n\nFormat:\n\t\tYYYY/MM/DD\n\t\tYYYY/MM/DD-YYYY/MM/DD\n\nExamples:\n\t\t2015/03/27\n\t\t2020/01/19-2020/05/01'
                    ))

            if args.region:
                if not valid_region(args.region):
                    sys.exit(
                        print(
                            'Invalid region code.\n\nExamples:\n\t\t-r gb\n\t\t-r ire'
                        ))
                region = args.region
            else:
                region = 'all'

            dates = get_dates(args.date)
            races = get_race_urls_date(dates, region)
            scrape_races(races, region, args.date.replace('/', '_'), '')

            sys.exit()

        if args.course:
            if not valid_course(args.course):
                sys.exit(
                    print(
                        'Invalid course code.\n\nExamples:\n\t\t-c 20\n\t\t-c 1083'
                    ))

        if args.region:
            if not valid_region(args.region):
                sys.exit(
                    print(
                        'Invalid region code.\n\nExamples:\n\t\t-r gb\n\t\t-r ire'
                    ))

        years = parse_years(args.year) if args.year else []

        if not years or not valid_years(years):
            sys.exit(
                print(
                    'Invalid year.\n\nFormat:\n\t\tYYYY\n\nExamples:\n\t\t-y 2015\n\t\t-y 2012-2017'
                ))

        if not args.type or args.type not in ['flat', 'jumps']:
            sys.exit(
                print(
                    'Invalid race type.\n\nMust be either flat or jumps.\n\nExamples:\n\t\t-t flat\n\t\t-t jumps'
                ))

        if not args.course and not args.region:
            sys.exit(print('Must supply a course or region code.'))

        target = args.region if args.region else course_name(args.course)

        if args.region:
            tracks = [course for course in courses(args.region)]
        else:
            tracks = [(args.course, course_name(args.course))]

        # races = get_race_urls(tracks, years, args.type, x_y())
        races = get_race_urls_async(tracks, years, args.type, x_y())

        scrape_races(races, target, args.year, args.type)

        sys.exit()

    try:
        import readline
        completions = Completer([
            'courses', 'regions', 'options', 'help', 'quit', 'exit', 'clear',
            'flat', 'jumps', 'date'
        ])
        readline.set_completer(completions.complete)
        readline.parse_and_bind('tab: complete')
    except ModuleNotFoundError:  # windows
        pass

    while True:
        args = input('[rpscrape]> ').lower().strip()
        parse_args([arg.strip() for arg in args.split()])
Ejemplo n.º 6
0
from mesa import Agent, Model
from mesa.time import BaseScheduler
from mesa.space import MultiGrid
import random
import time
import asyncio
from minimax import minimax

asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy(
))  #remove this line if you are not running windows

d = 6  #set this to a multiple of 3 to create a d x d board
depth = 1
weight = 1
eval_weight = 1


def is_valid_position(pos):
    '''
	Makes sure we don't enter letters, an incorrect number of inputs, or an out of bounds position
	'''
    agents = []
    coord = pos.split()
    if not coord or len(coord) != 2:
        return False

    try:
        x, y = int(coord[0]) - 1, int(coord[1]) - 1
    except:

        return False
Ejemplo n.º 7
0
def _configure_async_test_loop():
    if sys.version_info[0] == 3 and sys.version_info[
            1] >= 8 and sys.platform.startswith('win'):
        # use WindowsSelectorEventLoopPolicy to avoid aiohttp connexion close warnings
        # https://github.com/encode/httpx/issues/914#issuecomment-622586610
        asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
Ejemplo n.º 8
0
def write_to_files_with_ext_json(file_name, result_slice_up_to_ten):
    """
    Функция создает json файлы и записывает в них переданный слайс из списка словарей компаний
    :param file_name: путь к файлу
    :param result_slice_up_to_ten: слайс из всего списка словарей, отсортированных по какому то значению ключа
    """
    with open(file_name, "w") as f:
        json.dump(result_slice_up_to_ten, f, indent=4)


if __name__ == "__main__":
    start_time = time.time()

    asyncio.set_event_loop_policy(
        asyncio.WindowsSelectorEventLoopPolicy()
    )  # для винды нужно выбрать эту policy, иначе будет ошибка EventLoop is closed
    res = asyncio.run(main())

    top_ten_price = sorted(res, key=lambda k: k["price"], reverse=True)[:10]
    write_to_files_with_ext_json(
        "Task01/JSON`s for task01/Top_ten_companies_by_price.json",
        top_ten_price)

    low_top_PE = sorted(res, key=lambda k: k["P/E"])[:10]
    write_to_files_with_ext_json(
        "Task01/JSON`s for task01/Low_top_ten_companies_by_PE.json",
        low_top_PE)

    top_ten_growth = sorted(res, key=lambda k: k["growth"], reverse=True)[:10]
    write_to_files_with_ext_json(
Ejemplo n.º 9
0
def ws_gateway():
    # allow user to bypass the IP address auto-discovery. This is necessary if the component resides on a computer
    # other than the computing running the backplane.

    parser = argparse.ArgumentParser()
    parser.add_argument("-b",
                        dest="back_plane_ip_address",
                        default="None",
                        help="None or IP address used by Back Plane")
    # allow the user to specify a name for the component and have it shown on the console banner.
    # modify the default process name to one you wish to see on the banner.
    # change the default in the derived class to set the name
    parser.add_argument("-m",
                        dest="subscription_list",
                        default="from_arduino_gateway, "
                        "from_esp8266_gateway, "
                        "from_rpi_gateway, "
                        "from_microbit_gateway"
                        "from_picoboard_gateway"
                        "from_cpx_gateway",
                        nargs='+',
                        help="A space delimited list of topics")
    parser.add_argument("-i",
                        dest="server_ip_port",
                        default="9000",
                        help="Set the WebSocket Server IP Port number")
    parser.add_argument("-l",
                        dest="log",
                        default="False",
                        help="Set to True to turn logging on.")
    parser.add_argument("-n",
                        dest="process_name",
                        default="WebSocket Gateway",
                        help="Set process name in banner")
    parser.add_argument("-p",
                        dest="publisher_port",
                        default='43124',
                        help="Publisher IP port")
    parser.add_argument("-s",
                        dest="subscriber_port",
                        default='43125',
                        help="Subscriber IP port")

    args = parser.parse_args()

    subscription_list = args.subscription_list

    if len(subscription_list) > 1:
        subscription_list = args.subscription_list.split(',')

    kw_options = {
        'publisher_port': args.publisher_port,
        'subscriber_port': args.subscriber_port,
        'process_name': args.process_name,
        'server_ip_port': args.server_ip_port,
    }

    log = args.log.lower()
    if log == 'false':
        log = False
    else:
        log = True

    if args.back_plane_ip_address != 'None':
        kw_options['back_plane_ip_address'] = args.back_plane_ip_address

    # get the event loop
    # this is for python 3.8
    if sys.platform == 'win32':
        asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

    loop = asyncio.get_event_loop()

    try:
        WsGateway(subscription_list, **kw_options, event_loop=loop)
    except KeyboardInterrupt:
        for task in asyncio.Task.all_tasks():
            task.cancel()
        loop.stop()
        loop.close()
        sys.exit(0)
Ejemplo n.º 10
0
def main():
    # We need to install the asyncio reactor before we add any imports like
    # `twisted.internet.*` which install the default reactor.  We keep it here
    # and not at package level to avoid installing the reactor more than once.
    # Twisted throws an exception if you install the reactor more than once.
    import asyncio

    if sys.platform == 'win32' and sys.version_info >= (3, 7, 0):
        # we (or twisted) do not support the ProactorEventLoop as it does not
        # support adding file readers
        asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

    from twisted.internet import asyncioreactor
    asyncioreactor.install(asyncio.get_event_loop())

    from piqueserver.config import (config, TOML_FORMAT, JSON_FORMAT,
                                    SUPPORTED_PYTHONS)

    if (sys.version_info.major, sys.version_info.minor) not in SUPPORTED_PYTHONS:
        print('Warning: you are running on an unsupported Python version.\n'
              'The server may not run correctly.\n'
              'Please see https://piqueserver.readthedocs.io/en/v1.0.0/supported-python-environments.html for more information.')

    description = '%s is an open-source Python server implementation ' \
                  'for the voxel-based game "Ace of Spades".' % PKG_NAME
    arg_parser = argparse.ArgumentParser(
        prog=PKG_NAME, description=description)

    if not sys.warnoptions:
        import warnings
        warnings.filterwarnings("default", module="piqueserver[.*]")

    arg_parser.add_argument(
        '-c',
        '--config-file',
        default=None,
        help='specify the config file - '
        'default is "config.toml" in the config dir')

    arg_parser.add_argument(
        '-j',
        '--json-parameters',
        help='add extra settings in json format '
        '(overrides the config present in the config file)')

    arg_parser.add_argument(
        '-d',
        '--config-dir',
        default=config.config_dir,
        help='specify the directory which contains '
        'maps, scripts, etc (in correctly named '
        'subdirs) - default is %s' % config.config_dir)

    arg_parser.add_argument(
        '--copy-config',
        action='store_true',
        help='copies the default/example config dir to '
        'its default location or as specified by "-d"')

    arg_parser.add_argument(
        '--update-geoip',
        action='store_true',
        help='download the latest geoip database')

    arg_parser.add_argument(
        '--version',
        action='store_true',
        help='show the version and exit')

    args = arg_parser.parse_args()

    # update the config_dir from cli args
    config.config_dir = args.config_dir

    # run the required tasks if args given
    if args.copy_config or args.update_geoip:
        if args.copy_config:
            status = copy_config()
            if status != 0:
                sys.exit(status)

        if args.update_geoip:
            status = update_geoip(config.config_dir)
            if status != 0:
                sys.exit(status)

        return  # if we have done a task, don't run the server

    if args.version:
        import piqueserver
        print("piqueserver", piqueserver.__version__)
        return

    # TODO: set config/map/script/log/etc. dirs from config file, thus removing
    # the need for the --config-dir argument and the config file is then a
    # single source of configuration

    # find and load the config
    # search order:
    # - --config-file (must have toml or json file extension)
    # - --config-dir/config.toml
    # - --config-dir/config.json
    # - ~/.config/piqueserver/config.toml
    # - ~/.config/piqueserver/config.json
    format_ = None
    if args.config_file is None:
        for format__, ext in ((TOML_FORMAT, 'toml'), (JSON_FORMAT, 'json')):
            config_file = os.path.join(config.config_dir,
                                       'config.{}'.format(ext))
            format_ = format__
            if os.path.exists(config_file):
                break
    else:
        config_file = args.config_file
        ext = os.path.splitext(config_file)[1]
        if ext == '.json':
            format_ = JSON_FORMAT
        elif ext == '.toml':
            format_ = TOML_FORMAT
        else:
            raise ValueError(
                'Unsupported config file format! Must have json or toml extension.'
            )

    config.config_file = config_file
    print('Loading config from {!r}'.format(config_file))
    try:
        with open(config_file) as fobj:
            config.load_from_file(fobj, format_=format_)
    except FileNotFoundError as e:
        print("Could not open Config file")
        print(e)
        return e.errno

    # update config with cli overrides
    if args.json_parameters:
        config.update_from_dict(json.loads(args.json_parameters))

    from piqueserver import server
    server.run()
Ejemplo n.º 11
0
    def __init__(
        self,
        # NetGear_Async parameters
        address=None,
        port=None,
        protocol="tcp",
        pattern=0,
        receive_mode=False,
        timeout=0.0,
        # Videogear parameters
        enablePiCamera=False,
        stabilize=False,
        source=None,
        camera_num=0,
        stream_mode=False,
        backend=0,
        colorspace=None,
        resolution=(640, 480),
        framerate=25,
        time_delay=0,
        # common parameters
        logging=False,
        **options
    ):

        """
        This constructor method initializes the object state and attributes of the NetGear_Async class.

        Parameters:
            address (str): sets the valid network address of the Server/Client.
            port (str): sets the valid Network Port of the Server/Client.
            protocol (str): sets the valid messaging protocol between Server/Client.
            pattern (int): sets the supported messaging pattern(flow of communication) between Server/Client
            receive_mode (bool): select the NetGear_Async's Mode of operation.
            timeout (int/float): controls the maximum waiting time(in sec) after which Client throws `TimeoutError`.
            enablePiCamera (bool): provide access to PiGear(if True) or CamGear(if False) APIs respectively.
            stabilize (bool): enable access to Stabilizer Class for stabilizing frames.
            camera_num (int): selects the camera module index which will be used as Rpi source.
            resolution (tuple): sets the resolution (i.e. `(width,height)`) of the Rpi source.
            framerate (int/float): sets the framerate of the Rpi source.
            source (based on input): defines the source for the input stream.
            stream_mode (bool): controls the exclusive YouTube Mode.
            backend (int): selects the backend for OpenCV's VideoCapture class.
            colorspace (str): selects the colorspace of the input stream.
            logging (bool): enables/disables logging.
            time_delay (int): time delay (in sec) before start reading the frames.
            options (dict): provides ability to alter Tweak Parameters of NetGear_Async, CamGear, PiGear & Stabilizer.
        """
        # raise error(s) for critical Class imports
        import_dependency_safe("zmq" if zmq is None else "", min_version="4.0")
        import_dependency_safe("msgpack" if msgpack is None else "")
        import_dependency_safe("msgpack_numpy" if m is None else "")

        # enable logging if specified
        self.__logging = logging

        # define valid messaging patterns => `0`: PAIR, `1`:(REQ, REP), `2`:(SUB, PUB), `3`:(PUSH, PULL)
        valid_messaging_patterns = {
            0: (zmq.PAIR, zmq.PAIR),
            1: (zmq.REQ, zmq.REP),
            2: (zmq.PUB, zmq.SUB),
            3: (zmq.PUSH, zmq.PULL),
        }

        # check whether user-defined messaging pattern is valid
        if isinstance(pattern, int) and pattern in valid_messaging_patterns:
            # assign value
            self.__msg_pattern = pattern
            self.__pattern = valid_messaging_patterns[pattern]
        else:
            # otherwise default to 0:`zmq.PAIR`
            self.__msg_pattern = 0
            self.__pattern = valid_messaging_patterns[self.__msg_pattern]
            if self.__logging:
                logger.warning(
                    "Invalid pattern {pattern}. Defaulting to `zmq.PAIR`!".format(
                        pattern=pattern
                    )
                )

        # check  whether user-defined messaging protocol is valid
        if isinstance(protocol, str) and protocol in ["tcp", "ipc"]:
            # assign value
            self.__protocol = protocol
        else:
            # else default to `tcp` protocol
            self.__protocol = "tcp"
            if self.__logging:
                logger.warning("Invalid protocol. Defaulting to `tcp`!")

        # initialize Termination flag
        self.__terminate = False
        # initialize and assign `Receive Mode`
        self.__receive_mode = receive_mode
        # initialize stream handler
        self.__stream = None
        # initialize Messaging Socket
        self.__msg_socket = None
        # initialize NetGear_Async's configuration dictionary
        self.config = {}
        # asyncio queue handler
        self.__queue = None
        # define Bidirectional mode
        self.__bi_mode = False  # handles Bidirectional mode state

        # assign timeout for Receiver end
        if timeout and isinstance(timeout, (int, float)):
            self.__timeout = float(timeout)
        else:
            self.__timeout = 15.0

        # generate 8-digit random system id
        self.__id = "".join(
            secrets.choice(string.ascii_uppercase + string.digits) for i in range(8)
        )

        # Handle user-defined options dictionary values
        # reformat dictionary
        options = {str(k).strip(): v for k, v in options.items()}
        # handle bidirectional mode
        if "bidirectional_mode" in options:
            value = options["bidirectional_mode"]
            # also check if pattern and source is valid
            if isinstance(value, bool) and pattern < 2 and source is None:
                # activate Bidirectional mode if specified
                self.__bi_mode = value
            else:
                # otherwise disable it
                self.__bi_mode = False
                logger.warning("Bidirectional data transmission is disabled!")
            # handle errors and logging
            if pattern >= 2:
                # raise error
                raise ValueError(
                    "[NetGear_Async:ERROR] :: `{}` pattern is not valid when Bidirectional Mode is enabled. Kindly refer Docs for more Information!".format(
                        pattern
                    )
                )
            elif not (source is None):
                raise ValueError(
                    "[NetGear_Async:ERROR] :: Custom source must be used when Bidirectional Mode is enabled. Kindly refer Docs for more Information!".format(
                        pattern
                    )
                )
            elif isinstance(value, bool) and self.__logging:
                # log Bidirectional mode activation
                logger.debug(
                    "Bidirectional Data Transmission is {} for this connection!".format(
                        "enabled" if value else "disabled"
                    )
                )
            else:
                logger.error("`bidirectional_mode` value is invalid!")
            # clean
            del options["bidirectional_mode"]

        # define messaging asynchronous Context
        self.__msg_context = zmq.asyncio.Context()

        # check whether `Receive Mode` is enabled
        if receive_mode:
            # assign local IP address if None
            if address is None:
                self.__address = "*"  # define address
            else:
                self.__address = address
            # assign default port address if None
            if port is None:
                self.__port = "5555"
            else:
                self.__port = port
        else:
            # Handle video source
            if source is None:
                self.config = {"generator": None}
                if self.__logging:
                    logger.warning("Given source is of NoneType!")
            else:
                # define stream with necessary params
                self.__stream = VideoGear(
                    enablePiCamera=enablePiCamera,
                    stabilize=stabilize,
                    source=source,
                    camera_num=camera_num,
                    stream_mode=stream_mode,
                    backend=backend,
                    colorspace=colorspace,
                    resolution=resolution,
                    framerate=framerate,
                    logging=logging,
                    time_delay=time_delay,
                    **options
                )
                # define default frame generator in configuration
                self.config = {"generator": self.__frame_generator()}
            # assign local ip address if None
            if address is None:
                self.__address = "localhost"
            else:
                self.__address = address
            # assign default port address if None
            if port is None:
                self.__port = "5555"
            else:
                self.__port = port
            # add server task handler
            self.task = None

        # Setup and assign event loop policy
        if platform.system() == "Windows":
            # On Windows, VidGear requires the ``WindowsSelectorEventLoop``, and this is
            # the default in Python 3.7 and older, but new Python 3.8, defaults to an
            # event loop that is not compatible with it. Thereby, we had to set it manually.
            if sys.version_info[:2] >= (3, 8):
                asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
        else:
            if not (uvloop is None):
                # Latest uvloop eventloop is only available for UNIX machines.
                asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
            else:
                # log if not present
                import_dependency_safe("uvloop", error="log")

        # Retrieve event loop and assign it
        self.loop = asyncio.get_event_loop()
        # create asyncio queue if bidirectional mode activated
        self.__queue = asyncio.Queue() if self.__bi_mode else None
        # log eventloop for debugging
        if self.__logging:
            # debugging
            logger.info(
                "Using `{}` event loop for this process.".format(
                    self.loop.__class__.__name__
                )
            )
Ejemplo n.º 12
0
import os
import asyncio

from .base import check_links, check_local, check_remotes

if os.name == "nt":
    asyncio.set_event_loop_policy(
        asyncio.WindowsSelectorEventLoopPolicy())  # type: ignore
Ejemplo n.º 13
0
 def get_session(self, *arg, **kw):
     if self.session is None:
         if sys.platform.startswith('win'):
             asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
         self.session = aiohttp.ClientSession(*arg, **kw)
     return self.session
Ejemplo n.º 14
0
    def __init__(
        self,
        # NetGear_Async parameters
        address=None,
        port=None,
        protocol="tcp",
        pattern=0,
        receive_mode=False,
        timeout=0.0,
        # Videogear parameters
        enablePiCamera=False,
        stabilize=False,
        source=0,
        camera_num=0,
        stream_mode=False,
        backend=0,
        colorspace=None,
        resolution=(640, 480),
        framerate=25,
        time_delay=0,
        # common parameters
        logging=False,
        **options
    ):

        """
        This constructor method initializes the object state and attributes of the NetGear_Async class.

        Parameters:
            address (str): sets the valid network address of the Server/Client.
            port (str): sets the valid Network Port of the Server/Client.
            protocol (str): sets the valid messaging protocol between Server/Client.
            pattern (int): sets the supported messaging pattern(flow of communication) between Server/Client
            receive_mode (bool): select the Netgear's Mode of operation.
            timeout (int/float): controls the maximum waiting time(in sec) after which Client throws `TimeoutError`.
            enablePiCamera (bool): provide access to PiGear(if True) or CamGear(if False) APIs respectively.
            stabilize (bool): enable access to Stabilizer Class for stabilizing frames.
            camera_num (int): selects the camera module index which will be used as Rpi source.
            resolution (tuple): sets the resolution (i.e. `(width,height)`) of the Rpi source.
            framerate (int/float): sets the framerate of the Rpi source.
            source (based on input): defines the source for the input stream.
            stream_mode (bool): controls the exclusive YouTube Mode.
            backend (int): selects the backend for OpenCV's VideoCapture class.
            colorspace (str): selects the colorspace of the input stream.
            logging (bool): enables/disables logging.
            time_delay (int): time delay (in sec) before start reading the frames.
            options (dict): provides ability to alter Tweak Parameters of NetGear, CamGear, PiGear & Stabilizer.
        """

        # enable logging if specified
        self.__logging = logging

        # define valid messaging patterns => `0`: PAIR, `1`:(REQ, REP), `2`:(SUB, PUB), `3`:(PUSH, PULL)
        valid_messaging_patterns = {
            0: (zmq.PAIR, zmq.PAIR),
            1: (zmq.REQ, zmq.REP),
            2: (zmq.PUB, zmq.SUB),
            3: (zmq.PUSH, zmq.PULL),
        }

        # check whether user-defined messaging pattern is valid
        if isinstance(pattern, int) and pattern in valid_messaging_patterns:
            # assign value
            self.__msg_pattern = pattern
            self.__pattern = valid_messaging_patterns[pattern]
        else:
            # otherwise default to 0:`zmq.PAIR`
            self.__msg_pattern = 0
            self.__pattern = valid_messaging_patterns[self.__msg_pattern]
            if self.__logging:
                logger.warning(
                    "Invalid pattern {pattern}. Defaulting to `zmq.PAIR`!".format(
                        pattern=pattern
                    )
                )

        # check  whether user-defined messaging protocol is valid
        if isinstance(protocol, str) and protocol in ["tcp", "ipc"]:
            # assign value
            self.__protocol = protocol
        else:
            # else default to `tcp` protocol
            self.__protocol = "tcp"
            if self.__logging:
                logger.warning("Invalid protocol. Defaulting to `tcp`!")

        # initialize Termination flag
        self.__terminate = False
        # initialize and assign `Receive Mode`
        self.__receive_mode = receive_mode
        # initialize stream handler
        self.__stream = None
        # initialize Messaging Socket
        self.__msg_socket = None
        # initialize NetGear's configuration dictionary
        self.config = {}

        # assign timeout for Receiver end
        if timeout > 0 and isinstance(timeout, (int, float)):
            self.__timeout = float(timeout)
        else:
            self.__timeout = 15.0
        # define messaging asynchronous Context
        self.__msg_context = zmq.asyncio.Context()

        # check whether `Receive Mode` is enabled
        if receive_mode:
            # assign local ip address if None
            if address is None:
                self.__address = "*"  # define address
            else:
                self.__address = address
            # assign default port address if None
            if port is None:
                self.__port = "5555"
            else:
                self.__port = port
        else:
            # Handle video source if not None
            if source is None:
                self.config = {"generator": None}
                if self.__logging:
                    logger.warning("Given source is of NoneType!")
            else:
                # define stream with necessary params
                self.__stream = VideoGear(
                    enablePiCamera=enablePiCamera,
                    stabilize=stabilize,
                    source=source,
                    camera_num=camera_num,
                    stream_mode=stream_mode,
                    backend=backend,
                    colorspace=colorspace,
                    resolution=resolution,
                    framerate=framerate,
                    logging=logging,
                    time_delay=time_delay,
                    **options
                )
                # define default frame generator in configuration
                self.config = {"generator": self.__frame_generator()}
            # assign local ip address if None
            if address is None:
                self.__address = "localhost"
            else:
                self.__address = address
            # assign default port address if None
            if port is None:
                self.__port = "5555"
            else:
                self.__port = port
            # add server task handler
            self.task = None

        # Setup and assign event loop policy
        if platform.system() == "Windows":
            # On Windows, VidGear requires the ``WindowsSelectorEventLoop``, and this is
            # the default in Python 3.7 and older, but new Python 3.8, defaults to an
            # event loop that is not compatible with it. Thereby, we had to set it manually.
            if sys.version_info[:2] >= (3, 8):
                asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
        else:
            # import library
            import uvloop

            # uvloop eventloop is only available for UNIX machines.
            asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())

        # Retrieve event loop and assign it
        self.loop = asyncio.get_event_loop()

        # debugging
        logger.info(
            "Using `{}` event loop for this process.".format(
                self.loop.__class__.__name__
            )
        )
Ejemplo n.º 15
0
def init(server_config: ServerConfig,
         authenticator,
         authorizer,
         execution_service: ExecutionService,
         schedule_service: ScheduleService,
         execution_logging_service: ExecutionLoggingService,
         config_service: ConfigService,
         alerts_service: AlertsService,
         file_upload_feature: FileUploadFeature,
         file_download_feature: FileDownloadFeature,
         secret,
         server_version,
         conf_folder,
         *,
         start_server=True):
    ssl_context = None
    if server_config.is_ssl():
        ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
        ssl_context.load_cert_chain(server_config.get_ssl_cert_path(),
                                    server_config.get_ssl_key_path())

    auth = TornadoAuth(authenticator)
    if auth.is_enabled():
        identification = AuthBasedIdentification(auth)
    else:
        identification = IpBasedIdentification(server_config.ip_validator,
                                               server_config.user_header_name)

    downloads_folder = file_download_feature.get_result_files_folder()

    handlers = [
        (r'/conf', GetServerConf), (r'/scripts', GetScripts),
        (r'/scripts/([^/]*)', ScriptConfigSocket),
        (r'/scripts/([^/]*)/([^/]*)/list-files', ScriptParameterListFiles),
        (r'/executions/start', ScriptExecute),
        (r'/executions/stop/(.*)', ScriptStop),
        (r'/executions/kill/(.*)', ScriptKill),
        (r'/executions/io/(.*)', ScriptStreamSocket),
        (r'/executions/active', GetActiveExecutionIds),
        (r'/executions/config/(.*)', GetExecutingScriptConfig),
        (r'/executions/cleanup/(.*)', CleanupExecutingScript),
        (r'/executions/status/(.*)', GetExecutionStatus),
        (r'/history/execution_log/short', GetShortHistoryEntriesHandler),
        (r'/history/execution_log/long/(.*)', GetLongHistoryEntryHandler),
        (r'/schedule', AddSchedule), (r'/auth/info', AuthInfoHandler),
        (r'/result_files/(.*)', DownloadResultFile, {
            'path': downloads_folder
        }), (r'/admin/scripts', AdminUpdateScriptEndpoint),
        (r'/admin/scripts/(.*)', AdminGetScriptEndpoint),
        (r"/", ProxiedRedirectHandler, {
            "url": "/index.html"
        })
    ]

    if auth.is_enabled():
        handlers.append((r'/login', LoginHandler))
        handlers.append((r'/auth/config', AuthConfigHandler))
        handlers.append((r'/logout', LogoutHandler))

    handlers.append((r'/theme/(.*)', ThemeStaticFileHandler, {
        'path': os.path.join(conf_folder, 'theme')
    }))
    handlers.append((r"/(.*)", AuthorizedStaticFileHandler, {"path": "web"}))

    settings = {
        "cookie_secret": secret,
        "login_url": "/login.html",
        'websocket_ping_interval': 30,
        'websocket_ping_timeout': 300,
        'compress_response': True
    }

    application = tornado.web.Application(handlers, **settings)

    application.auth = auth

    application.server_config = server_config
    application.server_version = server_version
    application.authorizer = authorizer
    application.downloads_folder = downloads_folder
    application.file_download_feature = file_download_feature
    application.file_upload_feature = file_upload_feature
    application.execution_service = execution_service
    application.schedule_service = schedule_service
    application.execution_logging_service = execution_logging_service
    application.config_service = config_service
    application.alerts_service = alerts_service
    application.identification = identification
    application.max_request_size_mb = server_config.max_request_size_mb

    if os_utils.is_win() and env_utils.is_min_version('3.8'):
        asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
    io_loop = tornado.ioloop.IOLoop.current()

    global _http_server
    _http_server = httpserver.HTTPServer(application,
                                         ssl_options=ssl_context,
                                         max_buffer_size=10 * BYTES_IN_MB)
    _http_server.listen(server_config.port, address=server_config.address)

    intercept_stop_when_running_scripts(io_loop, execution_service)

    http_protocol = 'https' if server_config.ssl else 'http'
    print('Server is running on: %s://%s:%s' %
          (http_protocol, server_config.address, server_config.port))

    if start_server:
        io_loop.start()
Ejemplo n.º 16
0
def asyncio_setup() -> None:  # pragma: no cover
    if sys.version_info >= (3, 8) and sys.platform == "win32":
        asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
Ejemplo n.º 17
0
 def start_WebServerThread():
     if sys.version_info.minor > 7:
         asyncio.set_event_loop_policy(
             asyncio.WindowsSelectorEventLoopPolicy())
     asyncio.set_event_loop(asyncio.new_event_loop())
     ws.run()
Ejemplo n.º 18
0
	def initWindowsFix(self): #Required for async due to bug in Windows shitty proactor event loop that crashes asyncio
		if sys.version_info[0] == 3 and sys.version_info[1] >= 8 and sys.platform.startswith('win'):
			asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
Ejemplo n.º 19
0
async def main(lib_kwargs, **kwargs):
    async def start(gwy) -> None:
        protocol_factory = create_protocol_factory(LocalProtocol, gwy, None)

        gwy.pkt_protocol, gwy.pkt_transport = create_pkt_stack(
            gwy,
            None,
            packet_log=gwy._input_file,
            protocol_factory=protocol_factory,
        )
        if gwy.pkt_transport.get_extra_info(POLLER_TASK):
            gwy._tasks.append(gwy.pkt_transport.get_extra_info(POLLER_TASK))

    def setup_database(db_file: str):
        con = None
        try:
            con = sqlite3.connect(db_file)
        except sqlite3.Error as err:
            print(err)

        try:
            cur = con.cursor()
            cur.execute(SQL_CREATE_TABLE)
        except sqlite3.Error as err:
            print(err)

        return con

    def process_packet(pkt) -> None:
        global last_pkt

        def insert_pkt(pkt):
            global counter

            data_fields = (
                pkt.dtm,  # dtm
                pkt.packet[0:3],  # rssi
                pkt.packet[4:6],  # verb
                pkt.packet[7:10],  # seqn
                pkt.packet[11:20],  # dev0
                pkt.packet[21:30],  # dev1
                pkt.packet[31:40],  # dev2
                pkt.packet[41:45],  # code
                pkt.packet[46:49],  # len
                pkt.packet[50:],  # payload
            )

            try:
                # cur = con.cursor()
                cur.execute(SQL_UPSERT_ROW, data_fields)

            except sqlite3.Error as err:
                print(err)

            else:
                counter += 1
                if counter == 1000:
                    counter = 0

                    con.commit()
                    msg, hdr = f"{pkt.dtm} {pkt}", f"{Style.BRIGHT}{Fore.CYAN}"
                    print(f"{hdr}{msg[:CONSOLE_COLS]}")

            return cur.lastrowid

        if not pkt.is_valid:
            # msg, hdr = f"{pkt.dtm} {pkt._pkt_str}", f"{Fore.MAGENTA}"
            # print(f"{hdr}{msg[:CONSOLE_COLS]}")
            return

        if last_pkt:
            if all((
                    pkt.packet[4:6] == "RP",
                    pkt.packet[11:20] == last_pkt.packet[21:30],
                    pkt.packet[21:30] == last_pkt.packet[11:20],
                    pkt.packet[41:45] == last_pkt.packet[41:45],
            )):
                insert_pkt(last_pkt)
            last_pkt = None

        elif pkt.packet[4:6] == "RQ" and pkt.packet[11:13] == "18":
            last_pkt = pkt
            return

        else:
            insert_pkt(pkt)

    print("\r\nclient.py: Starting evohome_rf (utils)...")

    if sys.platform == "win32":
        asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

    colorama_init(autoreset=True)
    con = setup_database(kwargs[DATABASE])
    cur = con.cursor()

    gwy = Gateway(None, **lib_kwargs)
    await start(gwy)  # replaces asyncio.create_task(gwy.start())

    while gwy.pkt_protocol is None:
        await asyncio.sleep(0.05)
    gwy.pkt_protocol.pkt_callback = process_packet

    try:  # main code here
        await asyncio.gather(*gwy._tasks)

    except asyncio.CancelledError:
        msg = " - ended via: CancelledError (e.g. SIGINT)"
    except GracefulExit:
        msg = " - ended via: GracefulExit"
    except (KeyboardInterrupt, SystemExit):
        msg = " - ended via: KeyboardInterrupt"
    except EvohomeError as err:
        msg = f" - ended via: EvohomeError: {err}"
    else:  # if no Exceptions raised, e.g. EOF when parsing
        msg = " - ended without error (e.g. EOF)"

    con.commit()

    print(f"\r\nclient.py: Finished evohome_rf (utils).\r\n{msg}\r\n")
Ejemplo n.º 20
0
def event_loop():
    if platform.system() == "Windows":
        asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
    return asyncio.new_event_loop()
Ejemplo n.º 21
0
def run_notebook(nb_path, tmp_path, env=None, kernel=None, allow_errors=False):
    """Execute a Jupyter notebook via :mod:`nbclient` and collect output.

    Parameters
    ----------
    nb_path : path-like
        The notebook file to execute.
    tmp_path : path-like
        A directory in which to create temporary output.
    env : dict-like, optional
        Execution environment for ``nbconvert``.
        If not supplied, :obj:`os.environ` is used.
    kernel : str, optional
        Jupyter kernel to use. Default: 'python2' or 'python3', matching the
        current Python version.
    allow_errors : bool, optional
        Whether to pass the ``--allow-errors`` option to ``nbconvert``. If
        :obj:`True`, the execution always succeeds, and cell output contains
        exception information.

    Returns
    -------
    nb : :class:`nbformat.NotebookNode`
        Parsed and executed notebook.
    errors : list
        Any execution errors.
    """
    import nbformat
    from nbclient import NotebookClient

    # Workaround for https://github.com/jupyter/nbclient/issues/85
    if (
        sys.version_info[0] == 3
        and sys.version_info[1] >= 8
        and sys.platform.startswith("win")
    ):
        import asyncio

        asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

    # Read the notebook
    nb = nbformat.read(nb_path, as_version=4)

    # Create a client and use it to execute the notebook
    client = NotebookClient(
        nb,
        timeout=10,
        kernel_name=kernel or f"python{sys.version_info[0]}",
        allow_errors=allow_errors,
        resources=dict(metadata=dict(path=tmp_path)),
    )

    # Execute the notebook.
    # `env` is passed from nbclient to jupyter_client.launcher.launch_kernel()
    client.execute(env=env or os.environ.copy())

    # Retrieve error information from cells
    errors = [
        output
        for cell in nb.cells
        if "outputs" in cell
        for output in cell["outputs"]
        if output.output_type == "error"
    ]

    return nb, errors
Ejemplo n.º 22
0
def start(task, profile):
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
    asyncio.set_event_loop(asyncio.new_event_loop())
    asyncio.get_event_loop().run_until_complete(configure(task, profile))
Ejemplo n.º 23
0
async def main(command, lib_kwargs, **kwargs):
    def process_msg(msg, prev_msg=None) -> None:
        dtm = (msg.dtm.isoformat(timespec="microseconds")
               if kwargs["long_dates"] else f"{msg.dtm:%H:%M:%S.%f}"[:-3])
        if msg.src and msg.src.type == "18":
            print(
                f"{Style.BRIGHT}{COLORS.get(msg.verb)}{dtm} {msg}"[:
                                                                   CONSOLE_COLS]
            )
        # elif msg.code == "3B00":  # TODO: temp
        #     print(f"{Style.BRIGHT}{COLORS.get(msg.verb)}{dtm} {msg}"[:CONSOLE_COLS])
        else:
            print(f"{COLORS.get(msg.verb)}{dtm} {msg}"[:CONSOLE_COLS])

    print("\r\nclient.py: Starting ramses_rf...")

    if sys.platform == "win32":
        asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

    serial_port, lib_kwargs = normalise_config_schema(lib_kwargs)
    gwy = Gateway(serial_port, **lib_kwargs)

    if kwargs[REDUCE_PROCESSING] < DONT_CREATE_MESSAGES:
        # no MSGs will be sent to STDOUT, so send PKTs instead
        colorama_init(autoreset=True)  # TODO: remove strip=True
        gwy.create_client(process_msg)

    try:  # main code here
        if kwargs["restore_cache"]:
            print("Restoring client schema/state cache...")
            state = json.load(kwargs["restore_cache"])
            await gwy._set_state(**state["data"]["restore_cache"])

        gwy_task = asyncio.create_task(gwy.start())

        if command == EXECUTE:
            tasks = spawn_scripts(gwy, **kwargs)
            await asyncio.gather(*tasks)

        elif command in MONITOR:
            tasks = spawn_scripts(gwy, **kwargs)
            # await asyncio.sleep(5)
            # gwy.device_by_id["17:145039"].temperature = 19
            # gwy.device_by_id["34:145039"].temperature = 21.3
            await gwy_task

        else:  # elif command in (LISTEN, PARSE):
            await gwy_task

    except asyncio.CancelledError:
        msg = " - ended via: CancelledError (e.g. SIGINT)"
    except GracefulExit:
        msg = " - ended via: GracefulExit"
    except KeyboardInterrupt:
        msg = " - ended via: KeyboardInterrupt"
    except EvohomeError as err:
        msg = f" - ended via: EvohomeError: {err}"
    else:  # if no Exceptions raised, e.g. EOF when parsing
        msg = " - ended without error (e.g. EOF)"

    print("\r\nclient.py: Finished ramses_rf, results:\r\n")

    if False:  # or kwargs["show_state"]:
        _print_state(gwy, **kwargs)

    elif command == EXECUTE:
        print_results(gwy, **kwargs)

    print_summary(gwy, **kwargs)

    # if kwargs["save_state"]:
    #    _save_state(gwy)

    print(f"\r\nclient.py: Finished ramses_rf.\r\n{msg}\r\n")
Ejemplo n.º 24
0
def start():
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
    asyncio.run(local_run())
Ejemplo n.º 25
0
def configure_windows_event_loop():
    if sys.version_info >= (3, 8) and platform.system() == "Windows":
        import asyncio

        asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
Ejemplo n.º 26
0
def qcode_sign(enc):
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
    asyncio.run(qcode_run(enc))
Ejemplo n.º 27
0
    QWord,
    RandomData,
    Static,
    String,
    Word,
)
from .repeater import CountRepeater, Repeater, TimeRepeater
from .sessions import open_test_run, Session, Target

# workaround to make Tornado work in Python 3.8
# https://github.com/tornadoweb/tornado/issues/2608
if sys.platform == "win32" and sys.version_info >= (3, 8):
    import asyncio

    # noinspection PyUnresolvedReferences
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())  # pytype: disable=module-attr

__all__ = [
    "BasePrimitive",
    "BaseSocketConnection",
    "BIG_ENDIAN",
    "BitField",
    "Block",
    "blocks",
    "Byte",
    "Bytes",
    "Checksum",
    "CountRepeater",
    "DEFAULT_PROCMON_PORT",
    "Delim",
    "DWord",
Ejemplo n.º 28
0
import asyncio
asyncio.set_event_loop_policy(
    asyncio.WindowsSelectorEventLoopPolicy())  # python-3.8.0a4

import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options

define("port", default=8888, help="run on the given port", type=int)


class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("table 1 2 3 4 5 ; price $350 $250 $400 $500 $1500 ;")


def main():
    tornado.options.parse_command_line()
    application = tornado.web.Application([(r"/", MainHandler)])
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.current().start()


if __name__ == "__main__":
    main()
Ejemplo n.º 29
0
# File "...\twisted\internet\asyncioreactor.py", line 69, in __init__
#   super().__init__()
# File "...\twisted\internet\base.py", line 571, in __init__
#   self.installWaker()
# File "...\twisted\internet\posixbase.py", line 286, in installWaker
#   self.addReader(self.waker)
# File "...\twisted\internet\asyncioreactor.py", line 151, in addReader
#   self._asyncioEventloop.add_reader(fd, callWithLogger, reader,
# File "C:\Python38\lib\asyncio\events.py", line 501, in add_reader
#   raise NotImplementedError
# ```
# NOTE: Actually this is already done in the `event_loop` fixture in the
# `conftest.py` file, but this file imports earlier by the Pytest plugin
# `pytest_django`. So we do this here as well.
if sys.platform == "win32" and sys.version_info.minor >= 8:
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()  # pylint: disable=no-member
                                  )

BASE_DIR = pathlib.Path(__file__).absolute().parent.parent
SECRET_KEY = str(uuid.uuid4())
DEBUG = True
MIDDLEWARE = [
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
]
TEMPLATES = [{
    "BACKEND": "django.template.backends.django.DjangoTemplates",
    "APP_DIRS": True,
    "OPTIONS": {
        "context_processors": [
    manager = MerossManager(http_client=http_api_client)
    await manager.async_init()
    await manager.async_device_discovery()
    plugs = manager.find_devices(device_type=device_type)
    sleep(2)

    if len(plugs) < 1:
        print("No {device_type} plugs found...")
    else:
        plug_exsists = False
        x = 0
        for plug in plugs:
            if plug.name == device_name:
                plug_exsists = True
                plug_number = x
            x += 1
        if plug_exsists:
            dev = plugs[plug_number]
            await dev.async_turn_on(channel=0)
        else:
            print("Plug does not exsist")
    manager.close()
    await http_api_client.async_logout()

if __name__ == '__main__':
    # On Windows + Python 3.8, you should uncomment the following
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())    #  Comment out when used on Linux
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
    loop.close()