Module geode.lib.console

Expand source code
# ------------------------------------------------------------------------------
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 3 of the License.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
# ------------------------------------------------------------------------------

# API
from geode.lib.game import Game

from geode.lib.common import CommonObject

# Regex
from re import IGNORECASE

from re import compile as re_compile

# ------------------------------------------------------------------------------
#   Class
# ------------------------------------------------------------------------------

class Console(CommonObject):

    attributes = (
        ("metadata", "name", None),
        ("metadata", "extensions", list()),
        ("path", "roms", None),
        ("option", "emulator", None),
        ("option", "recursive", False))


    def __init__(self, *args):
        """ Constructor
        """

        super(Console, self).__init__(*args)

        # ----------------------------------------
        #   Variables
        # ----------------------------------------

        # Replace string value with Emulator object value
        if self.emulator is not None:
            self.emulator = self.api.get_emulator(self.emulator)

        # Store games list
        self.__games = list()

        # ----------------------------------------
        #   Initialization
        # ----------------------------------------

        # Retrieve games list
        self.__init_games()


    def __init_games(self):
        """ Retrieve games from roms folder
        """

        # Reset storage content
        self.__games.clear()

        if self.roms is not None and self.roms.exists() and self.roms.is_dir():

            for extension in self.extensions:
                pattern = "*.%s" % extension

                # Add recursive mode if the option is activate
                if self.recursive:
                    pattern = "**/%s" % pattern

                for game in sorted(self.roms.glob(pattern)):
                    self.__games.append(Game(self.api, self, game))


    def get_games(self):
        """ Retrieve games list

        Returns
        -------
        list
            Games list
        """

        return self.__games


    def get_game(self, key):
        """ Retrieve a specific game

        Parameters
        ----------
        key : str
            Game identifier key

        Returns
        -------
        geode.lib.game.Game or None
            Game instance if found, None otherwise
        """

        return next((game for game in self.__games if game.id == key), None)


    def search_game(self, key):
        """ Search games from a specific key

        Parameters
        ----------
        key : str
            Key to search in games list (based on identifier and name)

        Returns
        -------
        generator
            Game instances
        """

        regex = re_compile(key, IGNORECASE)

        return (game for game in self.__games \
            if regex.search(game.name) or regex.search(game.id))


    def delete_game(self, key):
        """ Delete a specific game

        This function only remove the game from database. If you want to remove
        the game file, you need to do it manually.

        Parameters
        ----------
        key : str or geode.lib.game.Game
            Game identifier key or instance
        """

        if type(key) is Game:
            game = key

        elif type(key) is str:
            game = self.get_game(key)

        if game is not None:

            try:
                # Remove instance from database
                game.delete()

                # Remove instance from storage
                self.__games.remove(game)

                # Remove instance from memory
                del game

                return True

            except Exception as error:
                return False

        return False

Classes

class Console (*args)

Constructor

Expand source code
class Console(CommonObject):

    attributes = (
        ("metadata", "name", None),
        ("metadata", "extensions", list()),
        ("path", "roms", None),
        ("option", "emulator", None),
        ("option", "recursive", False))


    def __init__(self, *args):
        """ Constructor
        """

        super(Console, self).__init__(*args)

        # ----------------------------------------
        #   Variables
        # ----------------------------------------

        # Replace string value with Emulator object value
        if self.emulator is not None:
            self.emulator = self.api.get_emulator(self.emulator)

        # Store games list
        self.__games = list()

        # ----------------------------------------
        #   Initialization
        # ----------------------------------------

        # Retrieve games list
        self.__init_games()


    def __init_games(self):
        """ Retrieve games from roms folder
        """

        # Reset storage content
        self.__games.clear()

        if self.roms is not None and self.roms.exists() and self.roms.is_dir():

            for extension in self.extensions:
                pattern = "*.%s" % extension

                # Add recursive mode if the option is activate
                if self.recursive:
                    pattern = "**/%s" % pattern

                for game in sorted(self.roms.glob(pattern)):
                    self.__games.append(Game(self.api, self, game))


    def get_games(self):
        """ Retrieve games list

        Returns
        -------
        list
            Games list
        """

        return self.__games


    def get_game(self, key):
        """ Retrieve a specific game

        Parameters
        ----------
        key : str
            Game identifier key

        Returns
        -------
        geode.lib.game.Game or None
            Game instance if found, None otherwise
        """

        return next((game for game in self.__games if game.id == key), None)


    def search_game(self, key):
        """ Search games from a specific key

        Parameters
        ----------
        key : str
            Key to search in games list (based on identifier and name)

        Returns
        -------
        generator
            Game instances
        """

        regex = re_compile(key, IGNORECASE)

        return (game for game in self.__games \
            if regex.search(game.name) or regex.search(game.id))


    def delete_game(self, key):
        """ Delete a specific game

        This function only remove the game from database. If you want to remove
        the game file, you need to do it manually.

        Parameters
        ----------
        key : str or geode.lib.game.Game
            Game identifier key or instance
        """

        if type(key) is Game:
            game = key

        elif type(key) is str:
            game = self.get_game(key)

        if game is not None:

            try:
                # Remove instance from database
                game.delete()

                # Remove instance from storage
                self.__games.remove(game)

                # Remove instance from memory
                del game

                return True

            except Exception as error:
                return False

        return False

Ancestors

Class variables

var attributes

Built-in immutable sequence.

If no argument is given, the constructor returns an empty tuple. If iterable is specified the tuple is initialized from iterable's items.

If the argument is a tuple, the return value is the same object.

Methods

def delete_game(self, key)

Delete a specific game

This function only remove the game from database. If you want to remove the game file, you need to do it manually.

Parameters

key : str or Game
Game identifier key or instance
Expand source code
def delete_game(self, key):
    """ Delete a specific game

    This function only remove the game from database. If you want to remove
    the game file, you need to do it manually.

    Parameters
    ----------
    key : str or geode.lib.game.Game
        Game identifier key or instance
    """

    if type(key) is Game:
        game = key

    elif type(key) is str:
        game = self.get_game(key)

    if game is not None:

        try:
            # Remove instance from database
            game.delete()

            # Remove instance from storage
            self.__games.remove(game)

            # Remove instance from memory
            del game

            return True

        except Exception as error:
            return False

    return False
def get_game(self, key)

Retrieve a specific game

Parameters

key : str
Game identifier key

Returns

Game or None
Game instance if found, None otherwise
Expand source code
def get_game(self, key):
    """ Retrieve a specific game

    Parameters
    ----------
    key : str
        Game identifier key

    Returns
    -------
    geode.lib.game.Game or None
        Game instance if found, None otherwise
    """

    return next((game for game in self.__games if game.id == key), None)
def get_games(self)

Retrieve games list

Returns

list
Games list
Expand source code
def get_games(self):
    """ Retrieve games list

    Returns
    -------
    list
        Games list
    """

    return self.__games
def search_game(self, key)

Search games from a specific key

Parameters

key : str
Key to search in games list (based on identifier and name)

Returns

generator
Game instances
Expand source code
def search_game(self, key):
    """ Search games from a specific key

    Parameters
    ----------
    key : str
        Key to search in games list (based on identifier and name)

    Returns
    -------
    generator
        Game instances
    """

    regex = re_compile(key, IGNORECASE)

    return (game for game in self.__games \
        if regex.search(game.name) or regex.search(game.id))

Inherited members