<commit_before>from __future__ import print_function, absolute_import

import os
import plistlib

from fontTools.misc.py23 import tobytes, tounicode

from . import _psautohint


__version__ = _psautohint.version


def _font_is_ufo(path):
    if os.path.isdir(path) and path.lower().endswith('.ufo'):
        meta_path = os.path.join(path, 'metainfo.plist')
        if os.path.isfile(meta_path):
            metainfo = plistlib.readPlist(meta_path)
            keys = ('creator', 'formatVersion')
            if metainfo and all([key in metainfo for key in keys]):
                return True
    return False


def get_font_format(font_file_path):
    if os.path.isfile(font_file_path):
        with open(font_file_path, 'rb') as f:
            head = f.read(4)
            if head == b'OTTO':
                return 'OTF'
            elif head[0:2] == b'\x01\x00':
                return 'CFF'
            elif head[0:2] == b'\x80\x01':
                return 'PFB'
            elif head in (b'%!PS', b'%!Fo'):
                for fullhead in (b'%!PS-AdobeFont', b'%!FontType1'):
                    f.seek(0)
                    if f.read(len(fullhead)) == fullhead:
                        return 'PFA'
                for fullhead in (b'%!PS-Adobe-3.0 Resource-CIDFont', ):
                    f.seek(0)
                    if f.read(len(fullhead)) == fullhead:
                        return 'PFC'
        return None
    else:
        if _font_is_ufo(font_file_path):
            return "UFO"
        return None


def hint_bez_glyph(info, glyph, allow_edit=True, allow_hint_sub=True,
                   round_coordinates=True):
    hinted = _psautohint.autohint(tobytes(info),
                                  tobytes(glyph),
                                  allow_edit,
                                  allow_hint_sub,
                                  round_coordinates)

    return tounicode(hinted)

<commit_msg>Combine the two loops<commit_after>from __future__ import print_function, absolute_import

import os
import plistlib

from fontTools.misc.py23 import tobytes, tounicode

from . import _psautohint


__version__ = _psautohint.version


def _font_is_ufo(path):
    if os.path.isdir(path) and path.lower().endswith('.ufo'):
        meta_path = os.path.join(path, 'metainfo.plist')
        if os.path.isfile(meta_path):
            metainfo = plistlib.readPlist(meta_path)
            keys = ('creator', 'formatVersion')
            if metainfo and all([key in metainfo for key in keys]):
                return True
    return False


def get_font_format(font_file_path):
    if os.path.isfile(font_file_path):
        with open(font_file_path, 'rb') as f:
            head = f.read(4)
            if head == b'OTTO':
                return 'OTF'
            elif head[0:2] == b'\x01\x00':
                return 'CFF'
            elif head[0:2] == b'\x80\x01':
                return 'PFB'
            elif head in (b'%!PS', b'%!Fo'):
                for fullhead in (b'%!PS-AdobeFont', b'%!FontType1',
                                 b'%!PS-Adobe-3.0 Resource-CIDFont'):
                    f.seek(0)
                    if f.read(len(fullhead)) == fullhead:
                        if b"CID" not in fullhead:
                            return 'PFA'
                        else:
                            return 'PFC'
        return None
    else:
        if _font_is_ufo(font_file_path):
            return "UFO"
        return None


def hint_bez_glyph(info, glyph, allow_edit=True, allow_hint_sub=True,
                   round_coordinates=True):
    hinted = _psautohint.autohint(tobytes(info),
                                  tobytes(glyph),
                                  allow_edit,
                                  allow_hint_sub,
                                  round_coordinates)

    return tounicode(hinted)


{'fraction_non_alphanumeric': 0.10108864696734059, 'fraction_numerical': 0.01010886469673406, 'mean_word_length': 1.5455145118733509, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2109060', 'n_tokens_mistral': 1240, 'n_tokens_neox': 1177, 'n_words': 252}