def load_character_file(filename):
    """Create a character object from the given definition file.
    
    The definition file should be an importable python file, filled
    with variables describing the character.
    
    Parameters
    ----------
    filename : str
      The path to the file that will be imported.
    
    """
    # Parse the file name
    dir_, fname = os.path.split(os.path.abspath(filename))
    module_name, ext = os.path.splitext(fname)
    if ext != '.py':
        raise ValueError(
            f"Character definition {filename} is not a python file.")
    # Check if this file contains the version string
    version_re = re.compile('dungeonsheets_version\s*=\s*[\'"]([0-9.]+)[\'"]')
    with open(filename, mode='r') as f:
        version = None
        for line in f:
            match = version_re.match(line)
            if match:
                version = match.group(1)
                break
        if version is None:
            # Not a valid DND character file
            raise exceptions.CharacterFileFormatError(
                f"No ``dungeonsheets_version = `` entry in `{filename}`.")
    # Import the module to extract the information
    spec = importlib.util.spec_from_file_location('module', filename)
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)
    # Prepare a list of properties for this character
    char_props = {}
    for prop_name in dir(module):
        if prop_name[0:2] != '__':
            char_props[prop_name] = getattr(module, prop_name)
    return char_props
Example #2
0
    def __call__(self):
        """Create a character object from the given definition file.

        The definition file should be an importable python file, filled
        with variables describing the character.

        Parameters
        ----------
        filename
          The path to the file that will be imported.

        """
        filename = self.filename
        # Check if this file contains the version string
        version_re = re.compile(
            r'dungeonsheets_version\s*=\s*[\'"]([0-9.]+)[\'"]')
        with open(filename, mode="r") as f:
            version = None
            for line in f:
                match = version_re.match(line)
                if match:
                    version = match.group(1)
                    break
            if version is None:
                # Not a valid DND character file
                raise exceptions.CharacterFileFormatError(
                    f"No ``dungeonsheets_version = `` entry in `{filename}`.")
        # Import the module to extract the information
        spec = importlib.util.spec_from_file_location("module", filename)
        module = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(module)
        # Prepare a list of properties for this character
        char_props = {}
        for prop_name in dir(module):
            if prop_name[0:2] != "__":
                char_props[prop_name] = getattr(module, prop_name)
        return char_props