当前位置: 首页>>代码示例>>Python>>正文


Python SafeFormat.safe_format方法代码示例

本文整理汇总了Python中calibre.ebooks.metadata.book.formatter.SafeFormat.safe_format方法的典型用法代码示例。如果您正苦于以下问题:Python SafeFormat.safe_format方法的具体用法?Python SafeFormat.safe_format怎么用?Python SafeFormat.safe_format使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在calibre.ebooks.metadata.book.formatter.SafeFormat的用法示例。


在下文中一共展示了SafeFormat.safe_format方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: template_to_attribute

# 需要导入模块: from calibre.ebooks.metadata.book.formatter import SafeFormat [as 别名]
# 或者: from calibre.ebooks.metadata.book.formatter.SafeFormat import safe_format [as 别名]
 def template_to_attribute(self, other, ops):
     '''
     Takes a list [(src,dest), (src,dest)], evaluates the template in the
     context of other, then copies the result to self[dest]. This is on a
     best-efforts basis. Some assignments can make no sense.
     '''
     if not ops:
         return
     from calibre.ebooks.metadata.book.formatter import SafeFormat
     formatter = SafeFormat()
     for op in ops:
         try:
             src = op[0]
             dest = op[1]
             val = formatter.safe_format(src, other, 'PLUGBOARD TEMPLATE ERROR', other)
             if dest == 'tags':
                 self.set(dest, [f.strip() for f in val.split(',') if f.strip()])
             elif dest == 'authors':
                 self.set(dest, [f.strip() for f in val.split('&') if f.strip()])
             else:
                 self.set(dest, val)
         except:
             if DEBUG:
                 traceback.print_exc()
开发者ID:amorphous1,项目名称:calibre,代码行数:26,代码来源:base.py

示例2: Metadata

# 需要导入模块: from calibre.ebooks.metadata.book.formatter import SafeFormat [as 别名]
# 或者: from calibre.ebooks.metadata.book.formatter.SafeFormat import safe_format [as 别名]
class Metadata(object):

    '''
    A class representing all the metadata for a book. The various standard metadata
    fields are available as attributes of this object. You can also stick
    arbitrary attributes onto this object.

    Metadata from custom columns should be accessed via the get() method,
    passing in the lookup name for the column, for example: "#mytags".

    Use the :meth:`is_null` method to test if a field is null.

    This object also has functions to format fields into strings.

    The list of standard metadata fields grows with time is in
    :data:`STANDARD_METADATA_FIELDS`.

    Please keep the method based API of this class to a minimum. Every method
    becomes a reserved field name.
    '''

    def __init__(self, title, authors=(_('Unknown'),), other=None, template_cache=None,
                 formatter=None):
        '''
        @param title: title or ``_('Unknown')``
        @param authors: List of strings or []
        @param other: None or a metadata object
        '''
        _data = copy.deepcopy(NULL_VALUES)
        _data.pop('language')
        object.__setattr__(self, '_data', _data)
        if other is not None:
            self.smart_update(other)
        else:
            if title:
                self.title = title
            if authors:
                # List of strings or []
                self.author = list(authors) if authors else []  # Needed for backward compatibility
                self.authors = list(authors) if authors else []
        from calibre.ebooks.metadata.book.formatter import SafeFormat
        self.formatter = SafeFormat() if formatter is None else formatter
        self.template_cache = template_cache

    def is_null(self, field):
        '''
        Return True if the value of field is null in this object.
        'null' means it is unknown or evaluates to False. So a title of
        _('Unknown') is null or a language of 'und' is null.

        Be careful with numeric fields since this will return True for zero as
        well as None.

        Also returns True if the field does not exist.
        '''
        try:
            null_val = NULL_VALUES.get(field, None)
            val = getattr(self, field, None)
            return not val or val == null_val
        except:
            return True

    def __getattribute__(self, field):
        _data = object.__getattribute__(self, '_data')
        if field in SIMPLE_GET:
            return _data.get(field, None)
        if field in TOP_LEVEL_IDENTIFIERS:
            return _data.get('identifiers').get(field, None)
        if field == 'language':
            try:
                return _data.get('languages', [])[0]
            except:
                return NULL_VALUES['language']
        try:
            return object.__getattribute__(self, field)
        except AttributeError:
            pass
        if field in _data['user_metadata'].iterkeys():
            d = _data['user_metadata'][field]
            val = d['#value#']
            if d['datatype'] != 'composite':
                return val
            if val is None:
                d['#value#'] = 'RECURSIVE_COMPOSITE FIELD (Metadata) ' + field
                val = d['#value#'] = self.formatter.safe_format(
                                            d['display']['composite_template'],
                                            self,
                                            _('TEMPLATE ERROR'),
                                            self, column_name=field,
                                            template_cache=self.template_cache).strip()
            return val
        if field.startswith('#') and field.endswith('_index'):
            try:
                return self.get_extra(field[:-6])
            except:
                pass
        raise AttributeError(
                'Metadata object has no attribute named: '+ repr(field))

    def __setattr__(self, field, val, extra=None):
#.........这里部分代码省略.........
开发者ID:amorphous1,项目名称:calibre,代码行数:103,代码来源:base.py


注:本文中的calibre.ebooks.metadata.book.formatter.SafeFormat.safe_format方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。