예제 #1
0
파일: g.py 프로젝트: wassname/singularity
def available_languages():
    return [default_language] + \
           [file_name[5:] for file_dir in dirs.get_read_dirs("i18n")
                          if os.path.isdir(file_dir)
                          for file_name in os.listdir(file_dir)
                          if os.path.isdir(os.path.join(file_dir, file_name))
                             and file_name.startswith("lang_")]
예제 #2
0
def get_savegames():
    all_dirs = dirs.get_read_dirs("saves")

    all_savegames = []
    for saves_dir in all_dirs:
        try:
            all_files = os.listdir(saves_dir)
        except:
            continue

        for file_name in all_files:
            if file_name[0] != "." and file_name != "CVS":
                # If it's a new-style save, trim the .sav bit.
                if len(file_name) > 4 and file_name[-4:] == ".sav":
                    name = file_name[:-4]
                    filepath = os.path.join(saves_dir, file_name)

                    # Get version, only pickle first string.
                    version_name = None  # None == Unknown version
                    try:
                        loadfile = open(filepath, 'r')
                        unpickle = cPickle.Unpickler(loadfile)

                        load_version = unpickle.load()
                        if load_version in savefile_translation:
                            version_name = savefile_translation[load_version][
                                0]
                    except:
                        version_name = None  # To be sure.

                    savegame = Savegame(name, filepath, version_name)
                    all_savegames.append(savegame)

    return all_savegames
예제 #3
0
def load_music():
    """
load_music() loads music for the game.  It looks in multiple locations:

* music/ in the install directory for E:S.
* music/ in user's XDG_DATA_HOME/singularity folder.
"""

    global music_dict
    music_dict = {}

    # Build the set of paths we'll check for music.
    music_paths = dirs.get_read_dirs("music")

    # Main loop for music_paths
    for music_path in music_paths:
        if os.path.isdir(music_path):

            # Loop through the files in music_path and add the ones
            # that are .mp3s and .oggs.
            for entry in os.walk(music_path):
                root  = entry[0]
                files = entry[2]
                (head, tail) = os.path.split(root)
                if (tail.lower() != ".svn"):
                    if not music_dict.has_key(tail):
                        music_dict[tail]=[]
                    for file_name in files:
                        if (len(file_name) > 5 and
                        (file_name[-3:] == "ogg" or file_name[-3:] == "mp3")):
                            music_dict[tail].append(os.path.join(head, tail, file_name))
                            if g.debug:
                                sys.stderr.write("D: Loaded musicfile %s\n"
                                        % music_dict[tail][-1])
예제 #4
0
def get_savegames():
    all_dirs = dirs.get_read_dirs("saves")

    all_savegames = []
    for saves_dir in all_dirs:
        try:
            all_files = os.listdir(saves_dir)
        except:
            continue

        for file_name in all_files:
            if file_name[0] != "." and file_name != "CVS":
                # If it's a new-style save, trim the .sav bit.
                if len (file_name) > 4 and file_name[-4:] == ".sav":
                    name = file_name[:-4]
                    filepath = os.path.join(saves_dir, file_name)

                    # Get version, only pickle first string.
                    version_name = None # None == Unknown version
                    try:
                        loadfile = open(filepath, 'r')
                        unpickle = cPickle.Unpickler(loadfile)

                        load_version = unpickle.load()
                        if load_version in savefile_translation:
                            version_name = savefile_translation[load_version][0]
                    except:
                        version_name = None # To be sure.

                    savegame = Savegame(name, filepath, version_name)
                    all_savegames.append(savegame)

    return all_savegames
예제 #5
0
파일: i18n.py 프로젝트: Xenega/singularity
def available_languages():
    return [default_language] + \
           [file_name[5:] for file_dir in dirs.get_read_dirs("i18n")
                          if os.path.isdir(file_dir)
                          for file_name in os.listdir(file_dir)
                          if os.path.isdir(os.path.join(file_dir, file_name))
                             and file_name.startswith("lang_")]
예제 #6
0
def load_music():
    """
load_music() loads music for the game.  It looks in multiple locations:

* music/ in the install directory for E:S.
* music/ in user's XDG_DATA_HOME/singularity folder.
"""

    global music_dict
    music_dict = {}

    # Build the set of paths we'll check for music.
    music_paths = dirs.get_read_dirs("music")

    # Main loop for music_paths
    for music_path in music_paths:
        if os.path.isdir(music_path):

            # Loop through the files in music_path and add the ones
            # that are .mp3s and .oggs.
            for entry in os.walk(music_path):
                root  = entry[0]
                files = entry[2]
                (head, tail) = os.path.split(root)
                if (tail.lower() != ".svn"):
                    if not music_dict.has_key(tail):
                        music_dict[tail]=[]
                    for file_name in files:
                        if (len(file_name) > 5 and
                        (file_name[-3:] == "ogg" or file_name[-3:] == "mp3")):
                            music_dict[tail].append(os.path.join(head, tail, file_name))
                            if debug:
                                sys.stderr.write("D: Loaded musicfile %s\n"
                                        % music_dict[tail][-1])
예제 #7
0
def load_sounds():
    """ 
        load_sounds() loads all of the sounds in the data/sounds/ directory 
    """
    global sounds
    sounds = {}

    if not init:
        # Sound is not initialized. Warn if user wanted sound
        if not nosound:
            sys.stderr.write(
                "WARNING: Sound is requested, but mixer is not initialized!\n")
        return

    # Build the set of paths we'll check for sounds.
    sounds_paths = dirs.get_read_dirs("sounds")

    # Main loop for sounds_paths
    for sounds_path in sounds_paths:
        if not os.path.isdir(sounds_path): continue

        # Loop through the files in sounds_path and add them
        for entry in os.walk(sounds_path):
            root = entry[0]
            files = entry[2]
            (head, tail) = os.path.split(root)

            # Avoid hidden file
            if tail.startswith("."): continue

            for file_name in files:
                # Only wav file supported now.
                if (len(file_name) < 6 or file_name[-3:] != "wav"): continue

                real_file = os.path.join(head, tail, file_name)
                sound_class = tail

                # Load it via the mixer ...
                sound = pygame.mixer.Sound(real_file)

                # And shove it into the sounds dictionary.
                if not sounds.has_key(sound_class):
                    sounds[sound_class] = []

                sounds[sound_class].append({
                    "filename": real_file,
                    "sound": sound
                })

                if g.debug:
                    sys.stderr.write("DEBUG: Loaded soundfile: %s\n" %
                                     real_file)
예제 #8
0
def load_sounds():
    """ 
        load_sounds() loads all of the sounds in the data/sounds/ directory 
    """
    global sounds
    sounds = {}

    if not init:
        # Sound is not initialized. Warn if user wanted sound
        if not nosound:
            sys.stderr.write("WARNING: Sound is requested, but mixer is not initialized!\n")
        return

    # Build the set of paths we'll check for sounds.
    sounds_paths = dirs.get_read_dirs("sounds")

    # Main loop for sounds_paths
    for sounds_path in sounds_paths:
        if not os.path.isdir(sounds_path): continue

        # Loop through the files in sounds_path and add them
        for entry in os.walk(sounds_path):
            root  = entry[0]
            files = entry[2]
            (head, tail) = os.path.split(root)
            
            # Avoid hidden file
            if tail.startswith("."): continue
                
            for file_name in files:
                # Only wav file supported now.
                if (len(file_name) < 6 or file_name[-3:] != "wav"): continue
                    
                real_file = os.path.join(head, tail, file_name)
                sound_class = tail
                    
                # Load it via the mixer ...
                sound = pygame.mixer.Sound(real_file)

                # And shove it into the sounds dictionary.
                if not sounds.has_key(sound_class):
                    sounds[sound_class] = []
                    
                sounds[sound_class].append({
                    "filename": real_file,
                    "sound": sound})
                    
                if g.debug:
                    sys.stderr.write("DEBUG: Loaded soundfile: %s\n" %real_file)
예제 #9
0
def load_themes():
    themes = theme.themes = {}
    themes_dirs = dirs.get_read_dirs("themes")

    for themes_dir in themes_dirs:
        themes_list = [name for name in os.listdir(themes_dir)
                            if os.path.isdir(os.path.join(themes_dir, name))]

        for theme_id in themes_list:
            if (theme_id in themes):
                continue

            th = load_theme(theme_id, os.path.join(themes_dir, theme_id))
            th.find_files()
            themes[theme_id] = th
예제 #10
0
def load_themes():
    themes = theme.themes
    themes_dirs = dirs.get_read_dirs("themes")

    for themes_dir in themes_dirs:
        themes_list = [name for name in os.listdir(themes_dir)
                            if os.path.isdir(os.path.join(themes_dir, name))]

        for theme_id in themes_list:
            if (theme_id in themes):
                continue

            th = load_theme(theme_id, os.path.join(themes_dir, theme_id))
            th.find_files()
            themes[theme_id] = th
예제 #11
0
def generic_load(filename, load_dirs="data", mandatory=True):
    """
generic_load() loads a data file.  Data files are all in Python-standard
ConfigParser format.  The 'id' of any object is the section of that object.
Fields that need to be lists are postpended with _list; this is stripped
from the actual name, and the internal entries are broken up by the pipe
("|") character.

On errors, if file is mandatory then quit, else raise exception. For syntax
parsing-related errors, always print error message. For IOErrors silently ignore
non-mandatory missing or otherwise unreadable files
"""

    # Get directories to find the file
    if (isinstance(load_dirs, basestring)):
        load_dirs = dirs.get_read_dirs(load_dirs)

    # For each directories, create a file, otherwise use filename
    if load_dirs is not None:
        files = [os.path.join(load_dir, filename) for load_dir in load_dirs]
    else:
        files = [filename]

    # Find the first readable file.
    found = False
    errors = []
    config = ConfigParser.RawConfigParser()

    for filepath in files:
        try:
            config.readfp(open(filepath, "r"))
            found = True
            break;

        except IOError as reason:
            # Silently ignore non-mandatory missing files
            if mandatory:
                errors.append("Cannot read '%s': %s\nExiting\n" %  (filename, reason))

        except Exception as reason:
            # Always print parsing errors, even for non-mandatory files
            errors.append("Error parsing '%s': %s\n" %  (filename, reason))

    for err in errors:
        sys.stderr.write(err)

    if not found:
        if mandatory:
            sys.exit(1)
        else:
            return

    return_list = []

    # Get the list of items (IDs) in the file and loop through them.
    for item_id in config.sections():
        item_dict = {}
        item_dict["id"] = item_id

        # Get the list of settings for this particular item.
        for option in config.options(item_id):

            # If this is a list ...
            if len(option) > 6 and option[-5:] == "_list":

                # Break it into elements separated by |.
                item_dict[option[:-5]] = [unicode(x.strip(), "UTF-8") for x in
                 config.get(item_id, option).split("|")]
            else:

                # Otherwise, just grab the data.
                item_dict[option] = unicode(config.get(item_id, option).strip(),
                 "UTF-8")

        # Add this to the list of all objects we are returning.
        return_list.append(item_dict)

    return return_list
예제 #12
0
파일: g.py 프로젝트: wassname/singularity
def generic_load(filename, load_dirs="data", mandatory=True):
    """
generic_load() loads a data file.  Data files are all in Python-standard
ConfigParser format.  The 'id' of any object is the section of that object.
Fields that need to be lists are postpended with _list; this is stripped
from the actual name, and the internal entries are broken up by the pipe
("|") character.

On errors, if file is mandatory then quit, else raise exception. For syntax
parsing-related errors, always print error message. For IOErrors silently ignore
non-mandatory missing or otherwise unreadable files
"""

    # Get directories to find the file
    if (isinstance(load_dirs, basestring)):
        load_dirs = dirs.get_read_dirs(load_dirs)

    # For each directories, create a file, otherwise use filename
    if load_dirs is not None:
        files = [os.path.join(load_dir, filename) for load_dir in load_dirs]
    else:
        files = [filename]

    # Find the first readable file.
    found = False
    errors = []
    config = ConfigParser.RawConfigParser()

    for filepath in files:
        try:
            config.readfp(open(filepath, "r"))
            found = True
            break

        except IOError as reason:
            # Silently ignore non-mandatory missing files
            if mandatory:
                errors.append("Cannot read '%s': %s\nExiting\n" %
                              (filename, reason))

        except Exception as reason:
            # Always print parsing errors, even for non-mandatory files
            errors.append("Error parsing '%s': %s\n" % (filename, reason))

    for err in errors:
        sys.stderr.write(err)

    if not found:
        if mandatory:
            sys.exit(1)
        else:
            return

    return_list = []

    # Get the list of items (IDs) in the file and loop through them.
    for item_id in config.sections():
        item_dict = {}
        item_dict["id"] = item_id

        # Get the list of settings for this particular item.
        for option in config.options(item_id):

            # If this is a list ...
            if len(option) > 6 and option[-5:] == "_list":

                # Break it into elements separated by |.
                item_dict[option[:-5]] = [
                    unicode(x.strip(), "UTF-8")
                    for x in config.get(item_id, option).split("|")
                ]
            else:

                # Otherwise, just grab the data.
                item_dict[option] = unicode(
                    config.get(item_id, option).strip(), "UTF-8")

        # Add this to the list of all objects we are returning.
        return_list.append(item_dict)

    return return_list