WhintPy 1.1

https://sourceforge.net/projects/whintpy/

Module whintpy.deposit

Class DocumentUtils

Public functions

get_filetype

Get the filetype of a document.

If filename is an extension (like '.txt'), treat it as an extension without a name. If there is no dot or only an initial dot without extension, return an empty string.

Parameters
  • filename: (str) The name of the document
Returns
  • (str) The filetype of the document, or an empty string if none is found.
View Source
@staticmethod
def get_filetype(filename: str) -> str:
    """Get the filetype of a document.

        If filename is an extension (like '.txt'), treat it as an extension without a name.
        If there is no dot or only an initial dot without extension, return an empty string.

        :param filename: (str) The name of the document
        :return: (str) The filetype of the document, or an empty string if none is found.

        """
    if filename.startswith('.') and filename.count('.') == 1:
        return filename[1:]
    ext = os.path.splitext(filename)[1]
    return ext.lstrip('.')

get_folder_name

Generate a folder name string by combining the given parameters.

Parameters
  • author: (str)
  • filename: (str)
  • date: (datetime | date | None)
  • filetype: (str | None)
Returns
  • (str) A name of folder from given parameters
View Source
@staticmethod
def get_folder_name(author: str, filename: str, date: datetime.datetime | datetime.date | None, filetype: str | None) -> str:
    """Generate a folder name string by combining the given parameters.

        :param author: (str)
        :param filename: (str)
        :param date: (datetime | date | None)
        :param filetype: (str | None)
        :return: (str) A name of folder from given parameters

        """
    sep = ws.FOLDER_NAME_SEPARATOR
    return sep.join((DocumentUtils.format_author(author), DocumentUtils.date_to_str(date), DocumentUtils.format_filetype(filetype), DocumentUtils.format_filename(filename)))

format_author

Return the formatted given author name.

Parameters
  • author: (str) The author to be formatted
Returns
  • (str) The formatted filetype or extension
View Source
@staticmethod
def format_author(author: str) -> str:
    """Return the formatted given author name.

        :param author: (str) The author to be formatted
        :return: (str) The formatted filetype or extension

        """
    author = TypesDealer.cast_types(author, str)
    author = author.replace(ws.FOLDER_NAME_SEPARATOR, ws.FIELDS_NAME_SEPARATOR)
    return TypesDealer.clear_whitespace(TypesDealer.clear_string(author, ws.INVALID_CHARS_FOR_FOLDERS), ws.FIELDS_NAME_SEPARATOR)

format_filename

Return the formatted basename of the given file.

Parameters
  • filename: (str) The name of the document
Returns
  • (str) The formatted basename of the document
View Source
@staticmethod
def format_filename(filename: str) -> str:
    """Return the formatted basename of the given file.

        :param filename: (str) The name of the document
        :return: (str) The formatted basename of the document

        """
    filename = TypesDealer.cast_types(filename, str)
    base_filename, _ = os.path.splitext(filename)
    base_filename = base_filename.replace(ws.FOLDER_NAME_SEPARATOR, ws.FIELDS_NAME_SEPARATOR)
    return TypesDealer.clear_whitespace(TypesDealer.clear_string(base_filename, ws.INVALID_CHARS_FOR_FIELDS), ws.FIELDS_NAME_SEPARATOR)

format_date

Return the formatted given date.

Parameters
  • date: (datetime.datetime or datetime.date) The date to be formatted
Returns
  • (date) The formatted given date
View Source
@staticmethod
def format_date(date: datetime.datetime | datetime.date | None) -> datetime.date:
    """Return the formatted given date.

        :param date: (datetime.datetime or datetime.date) The date to be formatted
        :return: (date) The formatted given date

        """
    if isinstance(date, datetime.datetime) is True:
        return datetime.date(date.year, date.month, date.day)
    elif isinstance(date, datetime.date) is True:
        return date
    date = datetime.datetime.now()
    return datetime.date(date.year, date.month, date.day)

format_filetype

Return the formatted given file type.

Parameters
  • filetype: (str) The filetype or extension to be formatted
Returns
  • (str) The formatted filetype or extension
View Source
@staticmethod
def format_filetype(filetype: str) -> str:
    """Return the formatted given file type.

        :param filetype: (str) The filetype or extension to be formatted
        :return: (str) The formatted filetype or extension

        """
    filetype = str(filetype)
    if filetype.startswith('.'):
        filetype = filetype[1:]
    return filetype.lower()

format_description

Return the formatted given description.

Parameters
  • description: (str) The description to be formatted
Returns
  • (str) The formatted description
View Source
@staticmethod
def format_description(description: str) -> str:
    """Return the formatted given description.

        :param description: (str) The description to be formatted
        :return: (str) The formatted description

        """
    return TypesDealer.cast_types(description, str)

str_to_date

Return the date matching the given string.

Parameters
  • entry: (str) The date to be formatted
Returns
  • (datetime.date) Date matching the given entry
View Source
@staticmethod
def str_to_date(entry: str) -> datetime.date:
    """Return the date matching the given string.

        :param entry: (str) The date to be formatted
        :return: (datetime.date) Date matching the given entry

        """
    TypesDealer.check_types('', [(entry, str)])
    entry = entry.replace(ws.FIELDS_NAME_SEPARATOR, '-')
    return DocumentUtils.format_date(datetime.datetime.strptime(entry, '%Y-%m-%d'))

date_to_str

Return the stringified date matching the given entry.

Parameters
  • date: (datetime.datetime | datetime.date | None) The date to be formatted
Returns
  • (str) The string representing the given date
View Source
@staticmethod
def date_to_str(date: datetime.datetime | datetime.date | None) -> str:
    """Return the stringified date matching the given entry.

        :param date: (datetime.datetime | datetime.date | None) The date to be formatted
        :return: (str) The string representing the given date

        """
    if isinstance(date, (datetime.datetime, datetime.date)) is False:
        date = datetime.datetime.now()
    date_str = date.strftime('%Y-%m-%d')
    return date_str.replace('-', ws.FIELDS_NAME_SEPARATOR)