From d7b88de8176eea33987a9146d36b1251980da88e Mon Sep 17 00:00:00 2001 From: MyreMylar Date: Mon, 1 Jun 2020 20:02:20 +0100 Subject: [PATCH] Corrects several pylint warnings in sysfont module --- src_py/sysfont.py | 162 ++++++++++++++++++++++++++++------------------ 1 file changed, 100 insertions(+), 62 deletions(-) diff --git a/src_py/sysfont.py b/src_py/sysfont.py index 3162169743..4ce59f401b 100644 --- a/src_py/sysfont.py +++ b/src_py/sysfont.py @@ -22,8 +22,11 @@ import os import sys -from pygame.compat import xrange_, PY_MAJOR_VERSION from os.path import basename, dirname, exists, join, splitext + +from pygame.font import Font +from pygame.compat import xrange_, PY_MAJOR_VERSION + if sys.platform == 'darwin': import xml.etree.ElementTree as ET @@ -69,9 +72,6 @@ def initsysfonts_win32(): fontdir = join(os.environ.get('WINDIR', 'C:\\Windows'), 'Fonts') - TrueType_suffix = '(TrueType)' - mods = ('demibold', 'narrow', 'light', 'unicode', 'bt', 'mt') - fonts = {} # add fonts entered in the registry @@ -117,34 +117,49 @@ def initsysfonts_win32(): if not dirname(font): font = join(fontdir, font) - if name.endswith(TrueType_suffix): - name = name.rstrip(TrueType_suffix).rstrip() - name = name.lower().split() + _parse_font_entry_win(name, font, fonts) - bold = italic = 0 - for m in mods: - if m in name: - name.remove(m) - if 'bold' in name: - name.remove('bold') - bold = 1 - if 'italic' in name: - name.remove('italic') - italic = 1 - name = ''.join(name) + return fonts - name = _simplename(name) - _addfont(name, bold, italic, font, fonts) +def _parse_font_entry_win(name, font, fonts): + """ + Parse out a simpler name and the font style from the initial file name. - return fonts + :param name: The font name + :param font: The font file path + :param fonts: The pygame font dictionary + + :return: Tuple of (bold, italic, name) + """ + true_type_suffix = '(TrueType)' + mods = ('demibold', 'narrow', 'light', 'unicode', 'bt', 'mt') + if name.endswith(true_type_suffix): + name = name.rstrip(true_type_suffix).rstrip() + name = name.lower().split() + bold = italic = 0 + for mod in mods: + if mod in name: + name.remove(mod) + if 'bold' in name: + name.remove('bold') + bold = 1 + if 'italic' in name: + name.remove('italic') + italic = 1 + name = ''.join(name) + name = _simplename(name) + + _addfont(name, bold, italic, font, fonts) def _add_font_paths(sub_elements, fonts): """ Gets each element, checks its tag content, if wanted fetches the next value in the iterable """ - font_name = font_path = None + font_name = None + bold = False + italic = False for tag in sub_elements: if tag.text == "_name": font_file_name = next(sub_elements).text @@ -155,14 +170,14 @@ def _add_font_paths(sub_elements, fonts): italic = "italic" in font_name if tag.text == "path" and font_name is not None: font_path = next(sub_elements).text - _addfont(_simplename(font_name),bold,italic,font_path,fonts) + _addfont(_simplename(font_name), bold, italic, font_path, fonts) break def _system_profiler_darwin(): fonts = {} - flout, flerr = subprocess.Popen( - ' '.join(['system_profiler', '-xml','SPFontsDataType']), + flout, _ = subprocess.Popen( + ' '.join(['system_profiler', '-xml', 'SPFontsDataType']), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, @@ -175,7 +190,6 @@ def _system_profiler_darwin(): return fonts - def initsysfonts_darwin(): """ Read the fonts on MacOS, and OS X. """ @@ -206,41 +220,54 @@ def initsysfonts_unix(path="fc-list"): try: # note, we capture stderr so if fc-list isn't there to stop stderr # printing. - flout, flerr = subprocess.Popen('%s : file family style' % path, shell=True, - stdout=subprocess.PIPE, stderr=subprocess.PIPE, - close_fds=True).communicate() - except Exception: + flout, _ = subprocess.Popen('%s : file family style' % path, + shell=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + close_fds=True).communicate() + except (OSError, ValueError): return fonts entries = toascii(flout) try: - for line in entries.split('\n'): + for entry in entries.split('\n'): try: - filename, family, style = line.split(':', 2) - if splitext(filename)[1].lower() in OpenType_extensions: - bold = 'Bold' in style - italic = 'Italic' in style - oblique = 'Oblique' in style - for name in family.strip().split(','): - if name: - break - else: - name = splitext(basename(filename))[0] - - _addfont( - _simplename(name), bold, italic or oblique, filename, fonts) - - except Exception: + _parse_font_entry_unix(entry, fonts) + except ValueError: # try the next one. pass - except Exception: + except ValueError: pass return fonts +def _parse_font_entry_unix(entry, fonts): + """ + Parses an entry in the unix font data to add to the pygame font + dictionary. + + :param entry: A entry from the unix font list. + :param fonts: The pygame font dictionary to add the parsed font data to. + + """ + filename, family, style = entry.split(':', 2) + if splitext(filename)[1].lower() in OpenType_extensions: + bold = 'Bold' in style + italic = 'Italic' in style + oblique = 'Oblique' in style + for name in family.strip().split(','): + if name: + break + else: + name = splitext(basename(filename))[0] + + _addfont(_simplename(name), bold, italic or oblique, + filename, fonts) + + def create_aliases(): """ Map common fonts that are absent from the system to similar fonts that are installed in the system @@ -272,8 +299,13 @@ def create_aliases(): Sysalias[name] = found -# initialize it all, called once def initsysfonts(): + """ + Initialise the sysfont module, called once. Locates the installed fonts + and creates some aliases for common font categories. + + Has different initialisation functions for different platforms. + """ if sys.platform == 'win32': fonts = initsysfonts_win32() elif sys.platform == 'darwin': @@ -286,11 +318,19 @@ def initsysfonts(): Sysfonts[None] = None -# pygame.font specific declarations def font_constructor(fontpath, size, bold, italic): - import pygame.font + """ + pygame.font specific declarations + + :param fontpath: path to a font. + :param size: size of a font. + :param bold: bold style, True or False. + :param italic: italic style, True or False. + + :return: A font.Font object. + """ - font = pygame.font.Font(fontpath, size) + font = Font(fontpath, size) if bold: font.set_bold(True) if italic: @@ -332,16 +372,15 @@ def SysFont(name, size, bold=False, italic=False, constructor=None): gotbold = gotitalic = False fontname = None if name: - allnames = name - for name in allnames.split(','): - name = _simplename(name) - styles = Sysfonts.get(name) + for single_name in name.split(','): + single_name = _simplename(single_name) + styles = Sysfonts.get(single_name) if not styles: - styles = Sysalias.get(name) + styles = Sysalias.get(single_name) if styles: plainname = styles.get((False, False)) fontname = styles.get((bold, italic)) - if not fontname and not plainname: + if not (fontname or plainname): # Neither requested style, nor plain font exists, so # return a font with the name requested, but an # arbitrary style. @@ -399,12 +438,11 @@ def match_font(name, bold=0, italic=0): initsysfonts() fontname = None - allnames = name - for name in allnames.split(','): - name = _simplename(name) - styles = Sysfonts.get(name) + for single_name in name.split(','): + single_name = _simplename(single_name) + styles = Sysfonts.get(single_name) if not styles: - styles = Sysalias.get(name) + styles = Sysalias.get(single_name) if styles: while not fontname: fontname = styles.get((bold, italic))