<commit_before>#!/usr/bin/env python
# encoding: utf-8
"""
console.py

Created by Scott on 2012-12-24.
Copyright (c) 2012 <NAME>. All rights reserved.

This class represents the Console datatype. Each ROM is associated with a
Console, and each Console has many ROMs. A Console also is associated with an
emulator, which can be used to play a ROM.

Functionality should be added to this class/module if it deals with Consoles or
their emulators. This includes finding a list of ROMs in this console's folder.
"""

import os

import settings
import platform_helper as pf
import filesystem_helper
import emulator_manager
from ice_logging import log_file, log_both
from rom import ROM

class Console():

    @classmethod
    def settings_consoles(self):
        consoles = []
        consoles_dict = settings.consoles()
        for name in consoles_dict.keys():
            console_data = consoles_dict[name]
            nickname = name
            if 'nickname' in console_data:
                nickname = console_data['nickname']
            extensions = ""
            if 'extensions' in console_data:
    	        extensions = console_data['extensions']
            console = Console(nickname, name, extensions)
            consoles.append(console)
        return consoles

    def __init__(self,shortname,fullname,extensions):
        self.shortname = shortname
        self.fullname = fullname
        self.extensions = extensions
        self.emulator = emulator_manager.lookup_emulator(self)
        self.__create_directories_if_needed__()
        
    def __repr__(self):
        return self.shortname
        
    def __create_directories_if_needed__(self):
        """
        Creates directories that the console will need if they don't exist yet
        """
        # If the emulator doesn't exist, don't even bother creating the folders
        # for the console
        if self.emulator is None:
            return
        def create_directory_if_needed(dir):
            if not os.path.exists(dir):
                os.makedirs(dir)
        create_directory_if_needed(self.roms_directory())
        
    def roms_directory(self):
        """
        Should return a directory with a decent name for each console, such as
        C:\Users\Scott\Documents\ROMs\N64
        or
        C:\Users\Scott\Documents\ROMs\PS2
        """
        return os.path.join(filesystem_helper.roms_directory(),self.shortname)
      
    def valid_rom(self,path):
        """
        This function determines if a given path is actually a valid ROM file.
        If a list of extensions is supplied for this console, we check if the path has a valid extension
        If no extensions are defined for this console, we just accept any file
        """

        if self.extensions == "":
            return True
        return any(path.lower().endswith('.'+x) for x in self.extensions.split(' '))
  
    def find_all_roms(self):
        """
        Reads a list of all the ROMs from the appropriate directory for the
        console
        """
        roms = []
        # If the emulator is not functional, we pretend it doesn't have any
        # ROMs
        if not self.emulator.is_functional():
            return roms
        for filename in os.listdir(self.roms_directory()):
            file_path = os.path.join(self.roms_directory(),filename)
            if not os.path.isdir(file_path):<commit_msg>Now uses coma delimited string rather than just space delimited<commit_after>#!/usr/bin/env python
# encoding: utf-8
"""
console.py

Created by Scott on 2012-12-24.
Copyright (c) 2012 <NAME>. All rights reserved.

This class represents the Console datatype. Each ROM is associated with a
Console, and each Console has many ROMs. A Console also is associated with an
emulator, which can be used to play a ROM.

Functionality should be added to this class/module if it deals with Consoles or
their emulators. This includes finding a list of ROMs in this console's folder.
"""

import os

import settings
import platform_helper as pf
import filesystem_helper
import emulator_manager
from ice_logging import log_file, log_both
from rom import ROM

class Console():

    @classmethod
    def settings_consoles(self):
        consoles = []
        consoles_dict = settings.consoles()
        for name in consoles_dict.keys():
            console_data = consoles_dict[name]
            nickname = name
            if 'nickname' in console_data:
                nickname = console_data['nickname']
            extensions = ""
            if 'extensions' in console_data:
    	        extensions = console_data['extensions']
            console = Console(nickname, name, extensions)
            consoles.append(console)
        return consoles

    def __init__(self,shortname,fullname,extensions):
        self.shortname = shortname
        self.fullname = fullname
        self.extensions = extensions
        self.emulator = emulator_manager.lookup_emulator(self)
        self.__create_directories_if_needed__()
        
    def __repr__(self):
        return self.shortname
        
    def __create_directories_if_needed__(self):
        """
        Creates directories that the console will need if they don't exist yet
        """
        # If the emulator doesn't exist, don't even bother creating the folders
        # for the console
        if self.emulator is None:
            return
        def create_directory_if_needed(dir):
            if not os.path.exists(dir):
                os.makedirs(dir)
        create_directory_if_needed(self.roms_directory())
        
    def roms_directory(self):
        """
        Should return a directory with a decent name for each console, such as
        C:\Users\Scott\Documents\ROMs\N64
        or
        C:\Users\Scott\Documents\ROMs\PS2
        """
        return os.path.join(filesystem_helper.roms_directory(),self.shortname)
      
    def valid_rom(self,path):
        """
        This function determines if a given path is actually a valid ROM file.
        If a list of extensions is supplied for this console, we check if the path has a valid extension
        If no extensions are defined for this console, we just accept any file
        """

        if self.extensions == "":
            return True
        extension = os.path.splitext(path)[1].lower()
        return any(extension == ('.'+x.strip().lower()) for x in self.extensions.split(','))
  
    def find_all_roms(self):
        """
        Reads a list of all the ROMs from the appropriate directory for the
        console
        """
        roms = []
        # If the emulator is not functional, we pretend it doesn't have any
        # ROMs
        if not self.emulator.is_functional():
            return roms
        for filename in os.listdir(self.roms_directory()):
            file_path = os.path.join(self.roms_directory(),filename)
            if not os.path.isdir(file_path):
{'fraction_non_alphanumeric': 0.0754416449464234, 'fraction_numerical': 0.0047784535186794095, 'mean_word_length': 2.3676255485129207, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2794912', 'n_tokens_mistral': 1918, 'n_tokens_neox': 1821, 'n_words': 633}