| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- import gettext as gettext_module
- import os
- import sys
- import warnings
- from collections import OrderedDict
- from typing import Any
- import pygame
- import yaml
- from pygame.locals import *
- from jogai import exceptions as exc
- from jogai import logger, settings
- from jogai.translations import gettext as _
- def _load_image(directory: str, filename: str) -> list:
- if not isinstance(directory, str) or not isinstance(filename, str):
- raise TypeError(
- _('Both the directory and the filename must be strings.')
- )
- path = os.path.join(directory, filename)
- try:
- image = pygame.image.load(path)
- except FileNotFoundError as fnfe:
- raise FileNotFoundError(_('Image filename not found'))
- except pygame.error as pe:
- raise exc.UnsupportedFileFormatError(filename)
- rect = image.get_rect()
- return [image, rect]
- def _load_yaml(stream: str) -> OrderedDict:
- """Create an OrderedDict from a yaml string.
- Parameters:
- stream: data input stream
- Return:
- An OrderedDict with the input data.
- """
- class OrderedLoader(yaml.SafeLoader):
- pass
- def construct_mapping(loader, node):
- loader.flatten_mapping(node)
- return OrderedDict(loader.construct_pairs(node))
- OrderedLoader.add_constructor(
- yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, construct_mapping
- )
- return yaml.load(stream, OrderedLoader)
- def _load_yaml_from_filename(
- filename: str, directory=settings.DATA_DIR
- ) -> OrderedDict:
- path = os.path.join(directory, filename)
- with open(path) as file:
- content = file.read()
- return _load_yaml(content)
- def _convert_function_arguments_to_string(arguments: Any) -> str:
- """
- Convert function arguments from yaml data to string.
- Parameters:
- arguments: The function arguments from yaml item.
- Returns:
- A string with the input arguments
- Examples:
- >>> _convert_function_arguments_to_string({'x': 200, 'y': 400, 'key': 'a'})
- "x=200, y=400, key='a'"
- >>> _convert_function_arguments_to_string(['foo', 'bar'])
- "'foo', 'bar'"
- """
- if not isinstance(arguments, list) and not isinstance(arguments, dict):
- arguments = [arguments]
- if isinstance(arguments, list):
- processed_arguments = ', '.join([repr(v) for v in arguments])
- if isinstance(arguments, dict):
- processed_arguments = ', '.join(
- ['='.join((str(k), repr(v))) for k, v in arguments.items()]
- )
- return processed_arguments
- def _get_events() -> list:
- return pygame.event.get()
- def _get_keys() -> list:
- return pygame.key.get_pressed()
- def eval_key(key: str) -> int:
- """
- Evaluate a string as a pygame key.
- Parameters:
- key: string representation of a key with or without 'K_' prefix
- Examples:
- >>> key = eval_key('K_a')
- >>> key = eval_key('a')
- Returns:
- The integer representing the input key.
- """
- if not isinstance(key, str):
- raise exc.KeyPressedNotFoundError
- try:
- if key.startswith('K_'):
- return eval(key)
- else:
- return eval(f'K_{key}')
- except NameError:
- raise exc.KeyPressedNotFoundError
|