1
0
mirror of https://github.com/gryf/ebook-converter.git synced 2026-04-12 08:03:33 +02:00

Removed iteritems and itervalues, which are redundant

This commit is contained in:
2020-04-21 18:20:08 +02:00
parent 9e076e0af4
commit 3234e4de27
82 changed files with 382 additions and 457 deletions

View File

@@ -6,7 +6,6 @@ from ebook_converter import walk, prints, as_unicode
from ebook_converter.constants import (config_dir, iswindows, isosx, plugins, DEBUG,
isworker, filesystem_encoding)
from ebook_converter.utils.fonts.metadata import FontMetadata, UnsupportedFont
from ebook_converter.polyglot.builtins import itervalues
__license__ = 'GPL v3'
@@ -149,14 +148,14 @@ def path_significance(path, folders):
def build_families(cached_fonts, folders, family_attr='font-family'):
families = defaultdict(list)
for font in itervalues(cached_fonts):
for font in cached_fonts.values():
if not font:
continue
lf = (font.get(family_attr) or '').lower()
if lf:
families[lf].append(font)
for fonts in itervalues(families):
for fonts in families.values():
# Look for duplicate font files and choose the copy that is from a
# more significant font directory (prefer user directories over
# system directories).
@@ -181,7 +180,7 @@ def build_families(cached_fonts, folders, family_attr='font-family'):
font_family_map = dict.copy(families)
font_families = tuple(sorted((font[0]['font-family'] for font in
itervalues(font_family_map))))
font_family_map.values())))
return font_family_map, font_families
# }}}

View File

@@ -6,7 +6,6 @@ from ebook_converter.utils.fonts.sfnt.errors import UnsupportedFont, NoGlyphs
from ebook_converter.utils.fonts.sfnt.cff.dict_data import TopDict, PrivateDict
from ebook_converter.utils.fonts.sfnt.cff.constants import (cff_standard_strings,
STANDARD_CHARSETS)
from ebook_converter.polyglot.builtins import iteritems, itervalues
__license__ = 'GPL v3'
@@ -195,8 +194,8 @@ class CFFTable(UnknownTable):
# Map codes from the cmap table to glyph names, this will be used to
# reconstruct character_map for the subset font
charset_map = {code:self.cff.charset.safe_lookup(glyph_id) for code,
glyph_id in iteritems(character_map)}
charset = set(itervalues(charset_map))
glyph_id in character_map.items()}
charset = set(charset_map.values())
charset.discard(None)
if not charset and character_map:
raise NoGlyphs('This font has no glyphs for the specified characters')
@@ -207,7 +206,7 @@ class CFFTable(UnknownTable):
# Rebuild character_map with the glyph ids from the subset font
character_map.clear()
for code, charname in iteritems(charset_map):
for code, charname in charset_map.items():
glyph_id = s.charname_map.get(charname, None)
if glyph_id:
character_map[code] = glyph_id

View File

@@ -2,7 +2,6 @@ from struct import unpack_from, calcsize
from collections import OrderedDict, namedtuple
from ebook_converter.utils.fonts.sfnt.errors import UnsupportedFont
from ebook_converter.polyglot.builtins import iteritems
__license__ = 'GPL v3'
@@ -78,7 +77,7 @@ class ListTable(OrderedDict):
def dump(self, prefix=''):
print(prefix, self.__class__.__name__, sep='')
prefix += ' '
for tag, child in iteritems(self):
for tag, child in self.items():
print(prefix, tag, sep='')
child.dump(prefix=prefix+' ')

View File

@@ -2,7 +2,6 @@ from struct import unpack_from
from collections import OrderedDict
from ebook_converter.utils.fonts.sfnt import UnknownTable
from ebook_converter.polyglot.builtins import iteritems
__license__ = 'GPL v3'
@@ -80,7 +79,7 @@ class GlyfTable(UnknownTable):
ans = OrderedDict()
offset = 0
block = []
for glyph_id, glyph in iteritems(sorted_glyph_map):
for glyph_id, glyph in sorted_glyph_map.items():
raw = glyph()
pad = 4 - (len(raw) % 4)
if pad < 4:

View File

@@ -6,7 +6,6 @@ from ebook_converter.utils.fonts.sfnt.errors import UnsupportedFont
from ebook_converter.utils.fonts.sfnt.common import (ScriptListTable, FeatureListTable,
SimpleListTable, LookupTable, ExtensionSubstitution,
UnknownLookupSubTable)
from ebook_converter.polyglot.builtins import iteritems, itervalues
__license__ = 'GPL v3'
@@ -29,7 +28,7 @@ class SingleSubstitution(UnknownLookupSubTable):
gid_index_map = self.coverage.coverage_indices(glyph_ids)
if self.format == 1:
return {gid + self.delta for gid in gid_index_map}
return {self.substitutes[i] for i in itervalues(gid_index_map)}
return {self.substitutes[i] for i in gid_index_map.values()}
class MultipleSubstitution(UnknownLookupSubTable):
@@ -42,7 +41,7 @@ class MultipleSubstitution(UnknownLookupSubTable):
def all_substitutions(self, glyph_ids):
gid_index_map = self.coverage.coverage_indices(glyph_ids)
ans = set()
for index in itervalues(gid_index_map):
for index in gid_index_map.values():
glyphs = set(self.coverage_to_subs_map[index])
ans |= glyphs
return ans
@@ -67,7 +66,7 @@ class LigatureSubstitution(UnknownLookupSubTable):
def all_substitutions(self, glyph_ids):
gid_index_map = self.coverage.coverage_indices(glyph_ids)
ans = set()
for start_glyph_id, index in iteritems(gid_index_map):
for start_glyph_id, index in gid_index_map.items():
for glyph_id, components in self.coverage_to_lig_map[index]:
components = (start_glyph_id,) + components
if set(components).issubset(glyph_ids):
@@ -126,7 +125,7 @@ class ReverseChainSingleSubstitution(UnknownLookupSubTable):
def all_substitutions(self, glyph_ids):
gid_index_map = self.coverage.coverage_indices(glyph_ids)
return {self.substitutes[i] for i in itervalues(gid_index_map)}
return {self.substitutes[i] for i in gid_index_map.values()}
subtable_map = {

View File

@@ -3,7 +3,6 @@ from operator import itemgetter
from itertools import repeat
from ebook_converter.utils.fonts.sfnt import UnknownTable
from ebook_converter.polyglot.builtins import iteritems
__license__ = 'GPL v3'
@@ -54,7 +53,7 @@ class LocaTable(UnknownTable):
max_glyph_id = max(max_glyph_id, current_max_glyph_id)
self.offset_map = list(repeat(0, max_glyph_id + 2))
glyphs = [(glyph_id, x[0], x[1]) for glyph_id, x in
iteritems(resolved_glyph_map)]
resolved_glyph_map.items()]
glyphs.sort(key=itemgetter(1))
for glyph_id, offset, sz in glyphs:
self.offset_map[glyph_id] = offset

View File

@@ -6,7 +6,6 @@ from functools import partial
from ebook_converter.utils.icu import safe_chr, ord_string
from ebook_converter.utils.fonts.sfnt.container import Sfnt
from ebook_converter.utils.fonts.sfnt.errors import UnsupportedFont, NoGlyphs
from ebook_converter.polyglot.builtins import iteritems, itervalues
__license__ = 'GPL v3'
@@ -17,7 +16,7 @@ __docformat__ = 'restructuredtext en'
def resolve_glyphs(loca, glyf, character_map, extra_glyphs):
unresolved_glyphs = set(itervalues(character_map)) | extra_glyphs
unresolved_glyphs = set(character_map.values()) | extra_glyphs
unresolved_glyphs.add(0) # We always want the .notdef glyph
resolved_glyphs = {}
@@ -33,7 +32,7 @@ def resolve_glyphs(loca, glyf, character_map, extra_glyphs):
if gid not in resolved_glyphs:
unresolved_glyphs.add(gid)
return OrderedDict(sorted(iteritems(resolved_glyphs), key=itemgetter(0)))
return OrderedDict(sorted(resolved_glyphs.items(), key=itemgetter(0)))
def subset_truetype(sfnt, character_map, extra_glyphs):
@@ -52,7 +51,7 @@ def subset_truetype(sfnt, character_map, extra_glyphs):
'set, subsetting it is pointless')
# Keep only character codes that have resolved glyphs
for code, glyph_id in tuple(iteritems(character_map)):
for code, glyph_id in tuple(character_map.items()):
if glyph_id not in resolved_glyphs:
del character_map[code]
@@ -154,7 +153,7 @@ def subset(raw, individual_chars, ranges=(), warnings=None):
gsub = sfnt[b'GSUB']
try:
gsub.decompile()
extra_glyphs = gsub.all_substitutions(itervalues(character_map))
extra_glyphs = gsub.all_substitutions(character_map.values())
except UnsupportedFont as e:
warn('Usupported GSUB table: %s'%e)
except Exception:
@@ -175,7 +174,7 @@ def subset(raw, individual_chars, ranges=(), warnings=None):
if b'kern' in sfnt:
try:
sfnt[b'kern'].restrict_to_glyphs(frozenset(itervalues(character_map)))
sfnt[b'kern'].restrict_to_glyphs(frozenset(character_map.values()))
except UnsupportedFont as e:
warn('kern table unsupported, ignoring: %s'%e)
except Exception:
@@ -214,8 +213,8 @@ def print_stats(old_stats, new_stats):
prints('Table', ' ', '%10s'%'Size', ' ', 'Percent', ' ', '%10s'%'New Size',
' New Percent')
prints('='*80)
old_total = sum(itervalues(old_stats))
new_total = sum(itervalues(new_stats))
old_total = sum(old_stats.values())
new_total = sum(new_stats.values())
tables = sorted(old_stats, key=lambda x:old_stats[x],
reverse=True)
for table in tables:
@@ -350,7 +349,8 @@ def all():
print('Failed!')
failed.append((font['full_name'], font['path'], str(e)))
else:
averages.append(sum(itervalues(new_stats))/sum(itervalues(old_stats)) * 100)
averages.append(sum(new_stats.values()) /
sum(old_stats.values()) * 100)
print('Reduced to:', '%.1f'%averages[-1] , '%')
if unsupported:
print('\n\nUnsupported:')
@@ -359,7 +359,7 @@ def all():
print()
if warnings:
print('\n\nWarnings:')
for name, w in iteritems(warnings):
for name, w in warnings.items():
if w:
print(name)
print('', '\n\t'.join(w), sep='\t')

View File

@@ -2,7 +2,7 @@ import struct
from io import BytesIO
from collections import defaultdict
from ebook_converter.polyglot.builtins import iteritems, itervalues, as_bytes
from ebook_converter.polyglot.builtins import as_bytes
__license__ = 'GPL v3'
@@ -162,7 +162,7 @@ def decode_name_record(recs):
return mac_names[0]
# Use unicode names
for val in itervalues(unicode_names):
for val in unicode_names.values():
return val
return None
@@ -223,9 +223,10 @@ def get_all_font_names(raw, raw_is_table=False):
records = _get_font_names(raw, raw_is_table)
ans = {}
for name, num in iteritems({'family_name':1, 'subfamily_name':2, 'full_name':4,
'preferred_family_name':16, 'preferred_subfamily_name':17,
'wws_family_name':21, 'wws_subfamily_name':22}):
for name, num in {'family_name': 1, 'subfamily_name': 2, 'full_name': 4,
'preferred_family_name': 16,
'preferred_subfamily_name': 17, 'wws_family_name': 21,
'wws_subfamily_name': 22}.items():
try:
ans[name] = decode_name_record(records[num])
except (IndexError, KeyError, ValueError):