Пример #1
0
 async def execute(self, *args, **kwargs):
     executor = self._worker
     if executor is None:
         executor = current_thread().claim_executor()
     result_proxy = await executor.execute(
         alchemy_incendiary(self._engine.execute, args, kwargs))
     return AsyncResultProxy(result_proxy, executor)
Пример #2
0
 async def table_names(self, schema=None, connection=None):
     task = alchemy_incendiary(
         self._engine.table_names,
         (schema, None if connection else connection._connection))
     executor = self._worker
     if executor is None:
         return await current_thread().run_in_executor(task)
     else:
         return await executor.execute(task)
Пример #3
0
 async def __new__(cls, url, stream=True):
     """
     Creates a new ``YTAudio``.
     
     This method is a coroutine.
     
     Parameters
     ----------
     url : `str`
         The url or the title of the video.
     stream : `bool` = `True`, Optional
         Whether the audio should be streamed.
     
     Returns
     -------
     self : ``YTAudio``
     
     Raises
     ------
     DownloadError
         Downloading the audio source failed.
     PermissionError
         The given file started to be played at the same time by an other player as well.
     TypeError
         - If `pipe` was given as `True` meanwhile `source` was not given as a `file-like` supporting `.fileno()`
             method.
         - If `pipe` was given as `False`, meanwhile `source` was not given as `str`, `Path`.
     """
     path, data, args = await KOKORO.run_in_executor(alchemy_incendiary(cls._preprocess,(cls, url, stream)))
     
     # Create self only at the end, so the `__del__` wont pick it up
     self = object.__new__(cls)
     self._process_args = args
     self.process = None
     self._stdout = None
     self.path = path
     self.title = data.get('title', None)
     self.url = data.get('url', None)
     
     return self
Пример #4
0
 async def has_table(self, table_name, schema=None):
     return await current_thread().run_in_executor(
         alchemy_incendiary(self._engine.has_table, (table_name, schema)))
Пример #5
0
 async def __aenter__(self):
     self._context = await self.executor.execute(
         alchemy_incendiary(self._engine._engine.begin,
                            (self._close_with_result, )))
     return AsyncConnection(self._context.__enter__(), self.executor)
Пример #6
0
 async def __aexit__(self, exc_type, exc_val, exc_tb):
     return await self.executor.execute(
         alchemy_incendiary(
             self._context.__exit__,
             (exc_type, exc_val, exc_tb),
         ))
Пример #7
0
 async def close(self, *args, **kwargs):
     await self.executor.execute(
         alchemy_incendiary(self._connection.close, args, kwargs))
Пример #8
0
 async def scalar(self, *args, **kwargs):
     result_proxy = await self.executor.execute(
         alchemy_incendiary(self._connection.execute, args, kwargs))
     async_result_proxy = AsyncResultProxy(result_proxy, self.executor)
     return await async_result_proxy.scalar()
Пример #9
0
 async def execute(self, *args, **kwargs):
     result_proxy = await self.executor.execute(
         alchemy_incendiary(self._connection.execute, args, kwargs))
     return AsyncResultProxy(result_proxy, self.executor)
Пример #10
0
    async def _load_extension(self, extension):
        """
        Loads the extension. If the extension is loaded, will do nothing.
        
        Loading an exception can be separated to 4 parts:
        
        - Assign the default variables.
        - Load the module.
        - Find the entry point (if needed).
        - Ensure the entry point (if found).
        
        If any of these fails, an ``ExtensionError`` will be raised. If step 1 raises, then a traceback will be
        included as well.
        
        This method is a coroutine.
        
        Parameters
        ----------
        extension : ``Extension``
            The extension to load.
        
        Raises
        ------
        ExtensionError
            Extension entry point raised.
        """
        self._execute_counter += 1
        try:
            try:
                # loading blocks, but unloading does not
                lib = await KOKORO.run_in_executor(extension._load)
            except GeneratorExit:
                raise

            except BaseException as err:
                message = await KOKORO.run_in_executor(
                    alchemy_incendiary(
                        self._render_exc,
                        (
                            err,
                            [
                                'Exception occurred meanwhile loading an extension: `',
                                extension.name,
                                '`.\n\n',
                            ],
                        ),
                    ), )

                raise ExtensionError(message) from None

            if lib is None:
                return  # already loaded

            entry_point = extension._entry_point
            if entry_point is None:
                entry_point = self._default_entry_point
                if entry_point is None:
                    return

            if isinstance(entry_point, str):
                entry_point = getattr(lib, entry_point, None)
                if entry_point is None:
                    return

            try:
                if is_coroutine_function(entry_point):
                    await entry_point(lib)
                else:
                    entry_point(lib)
            except GeneratorExit:
                raise

            except BaseException as err:
                message = await KOKORO.run_in_executor(
                    alchemy_incendiary(self._render_exc, (
                        err,
                        [
                            'Exception occurred meanwhile entering an extension: `',
                            extension.name,
                            '`.\nAt entry_point:',
                            repr(entry_point),
                            '\n\n',
                        ],
                    )))

                raise ExtensionError(message) from None
        finally:
            self._execute_counter -= 1
Пример #11
0
    async def _unload_extension(self, extension, check_for_syntax):
        """
        Unloads the extension. If the extension is not loaded, will do nothing.
        
        Loading an exception can be separated to 3 parts:
        
        - Find the exit point (if needed).
        - Ensure the exit point (if found).
        - Remove the default variables.
        
        If any of these fails, an ``ExtensionError`` will be raised. If step 2 raises, then a traceback will be
        included as well.
        
        This method is a coroutine.
        
        Parameters
        ----------
        extension : ``Extension``
            The extension to unload.
        check_for_syntax : `bool`
            Whether the file's new syntax should be checked before unloading.
            
            This parameter is used when reloading, to avoid unloading un-reloadable files.
        
        Raises
        ------
        ExtensionError
            Extension exit point raised.
        """
        self._execute_counter += 1
        try:
            # loading blocks, but unloading does not
            lib = extension._unload(check_for_syntax)

            if lib is None:
                return  # not loaded

            try:
                exit_point = extension._exit_point
                if exit_point is None:
                    exit_point = self._default_exit_point
                    if exit_point is None:
                        return

                if isinstance(exit_point, str):
                    exit_point = getattr(lib, exit_point, None)
                    if exit_point is None:
                        return

                try:

                    if is_coroutine_function(exit_point):
                        await exit_point(lib)
                    else:
                        exit_point(lib)
                except GeneratorExit:
                    raise

                except BaseException as err:
                    message = await KOKORO.run_in_executor(
                        alchemy_incendiary(self._render_exc, (
                            err,
                            [
                                'Exception occurred meanwhile unloading an extension: `',
                                extension.name,
                                '`.\nAt exit_point:',
                                repr(exit_point),
                                '\n\n',
                            ],
                        )))

                    raise ExtensionError(message) from None

            finally:
                extension._unassign_variables()

                keys = []
                lib_globals = lib.__dict__
                for key in lib_globals:
                    if (not key.startswith('__')) and (not key.endswith('__')):
                        keys.append(key)

                for key in keys:
                    del lib_globals[key]
        finally:
            self._execute_counter -= 1