def compute_correct_file_name(self, position=None, extension=None):
        """Generate the name the file should take on disk, given metadata.

        Note 1: If a new Attachment is being created you must provide the position
        and extension arguments. Otherwise, this function expects this information
        is already available in the Attachment instance.

        Note 2: This function COMPUTES its result. It is not based on the ACTUAL properties
        of some file. That is, you should use this function to compute the properties of
        a file you need to place/name, NOT to get information about the actual file linked
        to by this Attachment. It is a prescriptive, not descriptive result. Examine the
        properties of the Attachment.attachment object for the actual properties of the
        real file.

        Args:
            position (int): The index of this Attachment's in its parent's list of files.
            extension (str): The file extension with dot included (e.g. '.midi, '.exe')

        Returns: A string with the file name the file should be saved with on disk.
        """
        # Generate a parent_str which contains piece and movement names.
        if self.pieces.all():
            parent = self.pieces.first()
            parent_str = parent.title.strip()
        elif self.movements.all():
            parent = self.movements.first()
            mov = parent
            if mov.piece:
                piece_str = mov.piece.title.strip()
                parent_str = piece_str + "_" + mov.title.strip()
            else:
                parent_str = mov.title.strip()
        else:
            msg = 'Attachment "{0}" is an orphan and should be deleted'
            raise ParentResolveError(msg.format(self.title))

        # Generate file name.
        position = str(position) if position else self.get_index_from_parent()
        extension = extension if extension else self.extension
        new_name = "{0}_{1}_{2}{3}".format(NameNormalizer.sanitize_name(parent_str),
                                           NameNormalizer.sanitize_name(parent.composer.name.strip()),
                                           "file" + str(position), extension)
        return NameNormalizer.normalize_name(new_name)
示例#2
0
 def normalize_name(self, name):
     return NameNormalizer.normalize_name(name)
示例#3
0
 def _normalize_name(self, name):
     """Call the standard name normalizer and return results"""
     file_name = NameNormalizer.normalize_name(name)
     return file_name