SPPAS 4.22

https://sppas.org/

Module sppas.src.wkps

Class FileName

Description

Represent the data structure of a file.

Constructor

Constructor of a FileName.

From the identifier, the following properties are extracted:

  1. name (str) The base name of the file, without path nor ext
  2. extension (str) The extension of the file, or the mime type
  3. date (str) Time of the last modification
  4. size (str) Size of the file
  5. state (int) State of the file
Parameters
  • identifier: (str) Full name of a file
View Source
def __init__(self, identifier):
    """Constructor of a FileName.

    From the identifier, the following properties are extracted:

        1. name (str) The base name of the file, without path nor ext
        2. extension (str) The extension of the file, or the mime type
        3. date (str) Time of the last modification
        4. size (str) Size of the file
        5. state (int) State of the file

    :param identifier: (str) Full name of a file

    """
    super(FileName, self).__init__(identifier)
    fn, ext = os.path.splitext(self.get_id())
    self.__name = os.path.basename(fn)
    self.__extension = ext.upper()
    self.__date = None
    self.__filesize = 0
    self.update_properties()
    self.subjoined = None

Public functions

folder

Return the name of the directory of this file (str).

View Source
def folder(self):
    """Return the name of the directory of this file (str)."""
    return os.path.dirname(self.id)

get_name

Return the short name of the file (str).

The name is the filename without path nor extension.

View Source
def get_name(self):
    """Return the short name of the file (str).

        The name is the filename without path nor extension.

        """
    return self.__name

get_extension

Return the extension of the file in upper-cases (str).

View Source
def get_extension(self):
    """Return the extension of the file in upper-cases (str)."""
    return self.__extension

get_mime

Return the mime type of the file (str).

View Source
def get_mime(self):
    """Return the mime type of the file (str)."""
    m = mimetypes.guess_type(self.id)
    if m[0] is not None:
        return m[0]
    return 'unknown'

get_date

Return a string representing the date of the last modification.

View Source
def get_date(self):
    """Return a string representing the date of the last modification."""
    if self.__date is None:
        return ' -- '
    return '{:d}-{:d}-{:d} {:02d}:{:02d}:{:02d}'.format(self.__date.year, self.__date.month, self.__date.day, self.__date.hour, self.__date.minute, self.__date.second)

get_size

Return a string representing the size of the file.

View Source
def get_size(self):
    """Return a string representing the size of the file."""
    unit = ' Ko'
    file_size = self.__filesize / 1024
    if file_size > 1024 * 1024:
        file_size /= 1024
        unit = ' Mo'
    return str(int(file_size)) + unit

set_state

Override. Set a state value to this filename.

A LOCKED file can only be unlocked by assigning the CHECKED state. No other state than MISSING can be assigned if the file does not exists.

Parameters
  • value: (States)
Returns
  • (bool) this filename state has changed or not
View Source
def set_state(self, value):
    """Override. Set a state value to this filename.

        A LOCKED file can only be unlocked by assigning the CHECKED state.
        No other state than MISSING can be assigned if the file does not exists.

        :param value: (States)
        :returns: (bool) this filename state has changed or not

        """
    if self.__file_exists() is False:
        return self.update_properties()
    if value not in FileName.FILENAME_STATES:
        raise sppasTypeError(value, str(FileName.FILENAME_STATES))
    if value == States().MISSING:
        return False
    if self._state == States().LOCKED and value != States().CHECKED:
        return False
    if self._state == value:
        return False
    if self._state == States().MISSING:
        self.update_properties()
    self._state = value
    return True

update_properties

Update properties of the file (modified date and file size).

Returns
  • (bool) true if properties were changed
View Source
def update_properties(self):
    """Update properties of the file (modified date and file size).

        :returns: (bool) true if properties were changed

        """
    cur_state = self._state
    cur_date = self.__date
    cur_size = self.__filesize
    if self.__file_exists() is False:
        self.__date = None
        self.__filesize = 0
        self._state = States().MISSING
    else:
        try:
            self.__date = datetime.fromtimestamp(os.path.getmtime(self.get_id()))
        except ValueError:
            self.__date = None
        self.__filesize = os.path.getsize(self.get_id())
        if self._state == States().MISSING:
            self._state = States().UNUSED
    return cur_state != self._state or cur_date != self.__date or cur_size != self.__filesize

Protected functions

__file_exists

View Source
def __file_exists(self):
    return os.path.isfile(self.get_id())

Overloads

__hash__

View Source
def __hash__(self):
    return hash((self.__name, self.__date, self.__extension, self.__filesize, self.get_state(), self.id))