def cleanup_font_files(): """Remove stale fonts.""" try: files = FONT_STORAGE.listdir(".")[1] except OSError: return for name in files: if name == "fonts.conf": continue if not Font.objects.filter(font=name).exists(): FONT_STORAGE.delete(name)
def test_cleanup(self): configure_fontconfig() cleanup_font_files() # There should always be fonts.conf present self.assertEqual(len(FONT_STORAGE.listdir(".")[1]), 1) font = self.add_font() self.assertEqual(len(FONT_STORAGE.listdir(".")[1]), 2) cleanup_font_files() self.assertEqual(len(FONT_STORAGE.listdir(".")[1]), 2) font.delete() self.assertEqual(len(FONT_STORAGE.listdir(".")[1]), 2) cleanup_font_files() self.assertEqual(len(FONT_STORAGE.listdir(".")[1]), 1)
def assert_font_files(self, expected: int): result = 0 excluded = {"fonts.conf", ".uuid"} for name in FONT_STORAGE.listdir(".")[1]: if name not in excluded: result += 1 self.assertEqual(result, expected)
def add_font(self): with open(FONT, "rb") as handle: fontfile = FONT_STORAGE.save("DroidSansFallback.ttf", handle) return Font.objects.create(font=fontfile, project=self.project, user=self.user)