aeidon/metadata.py

Source code for module aeidon.metadata from file aeidon/metadata.py.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# -*- coding: utf-8 -*-

# Copyright (C) 2007 Osmo Salomaa
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Metadata store for one item in a desktop-style file."""

import aeidon

__all__ = ("MetadataItem",)


class MetadataItem:

    """
    Metadata store for one item in a desktop-style file.

    :ivar fields: Dictionary mapping field names to their string values

    Common localized fields with custom handling are ``Name`` and
    ``Description``; arbitrary fields are accessible with :meth:`get_field`.
    Strings ``True`` and ``False`` are used for boolean fields.

    For the string syntax and especially the localization handling, see
    freedesktop.org_'s Desktop Entry Specification_.

    .. _freedesktop.org: http://www.freedesktop.org/
    .. _Specification: http://www.freedesktop.org/wiki/Specifications/desktop-entry-spec
    """

    def __init__(self, fields=None):
        """Initialize a :class:`MetadataItem` instance."""
        self.fields = fields or {}

    def get_description(self, localize=True):
        """Return description as defined by the ``Description`` field."""
        if not localize:
            return self.get_field("Description")
        return self._get_localized_field("Description")

    def get_field(self, name, fallback=None):
        """Return the string value of field or `fallback`."""
        if not name in self.fields:
            return fallback
        return self.fields[name]

    def get_field_boolean(self, name, fallback=None):
        """Return the boolean value of field or `fallback`."""
        if not name in self.fields:
            return fallback
        value = self.fields[name]
        if value == "True":
            return True
        if value == "False":
            return False
        raise ValueError("Invalid boolean value: {}"
                         .format(repr(value)))

    def get_field_list(self, name, fallback=None):
        """Return the list of strings value of field or `fallback`."""
        if not name in self.fields:
            return fallback
        lst = self.fields[name].split(";")
        if not lst[-1]: lst.pop(-1)
        return lst

    def _get_localized_field(self, name):
        """Return the localized value of field."""
        locale = aeidon.locales.get_system_code()
        modifier = aeidon.locales.get_system_modifier()
        if locale is None:
            return self.get_field(name)
        # 'xx_YY@Zzzz', fall back to 'xx@Zzzz'.
        if ("_" in locale) and (modifier is not None):
            key = "{}[{}@{}]".format(name, locale, modifier)
            if key in self.fields:
                return self.get_field(key)
            locale = locale[0:2]
        # 'xx_YY', fall back to 'xx'.
        if ("_" in locale) and (modifier is None):
            key = "{}[{}]".format(name, locale)
            if key in self.fields:
                return self.get_field(key)
            locale = locale[0:2]
        # 'xx@Zzzz', fall back to unlocalized.
        if (not "_" in locale) and (modifier is not None):
            key = "{}[{}@{}]".format(name, locale, modifier)
            if key in self.fields:
                return self.get_field(key)
            return self.get_field(name)
        # 'xx', fall back to unlocalized.
        if (not "_" in locale) and (modifier is None):
            key = "{}[{}]".format(name, locale)
            if key in self.fields:
                return self.get_field(key)
            return self.get_field(name)
        return self.get_field(name)

    def get_name(self, localize=True):
        """Return name as defined by the ``Name`` field."""
        if not localize:
            return self.get_field("Name")
        return self._get_localized_field("Name")

    def has_field(self, name):
        """Return ``True`` if field exists."""
        return (name in self.fields)

    def set_field(self, name, value):
        """Set the string value of field."""
        self.fields[name] = str(value)