Skip to content

Commit

Permalink
System font infromation gathering #11
Browse files Browse the repository at this point in the history
  • Loading branch information
lpsandaruwan committed Feb 3, 2017
1 parent 9842670 commit 5e4115f
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 26 deletions.
1 change: 1 addition & 0 deletions blueprint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
from blueprint.fontfaces_controller import fontfaces_blueprint
from blueprint.fonts_controller import fonts_blueprint
from blueprint.teams_controller import teams_blueprint
from blueprint.typecase_controller import typecase_blueprint
17 changes: 17 additions & 0 deletions blueprint/typecase_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
""" Typecase controller
Rest API to handle system fonts and curated/user collections.
Created by Lahiru Pathirage @ Mooniak <[email protected]> on 2/2/2017
"""

from flask import Blueprint, jsonify

from utility import FontManager

typecase_blueprint = Blueprint("typecase_blueprint", __name__)


@typecase_blueprint.route("/system/fonts")
def get_active_fonts():
return jsonify(FontManager().get_active_fonts_list())
5 changes: 3 additions & 2 deletions core-runnner.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"""

import socket
import sys
import threading

from flask import Flask
Expand All @@ -17,6 +16,7 @@
from blueprint import fontfaces_blueprint
from blueprint import fonts_blueprint
from blueprint import teams_blueprint
from blueprint import typecase_blueprint
from utility import FileManager
from utility import initialize
from utility import run_tasks
Expand All @@ -31,6 +31,7 @@ def run_flask_app():
fms.register_blueprint(fontfaces_blueprint)
fms.register_blueprint(fonts_blueprint)
fms.register_blueprint(teams_blueprint)
fms.register_blueprint(typecase_blueprint)

fms.run(host="0.0.0.0", threaded=True)

Expand All @@ -41,7 +42,7 @@ def main():

if con is not 0:
if FileManager().is_file_exists("./data/fontman.db"):
threading.Thread(target=run_tasks).start()
# threading.Thread(target=run_tasks).start()
run_flask_app()

else:
Expand Down
91 changes: 67 additions & 24 deletions utility/FontManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@
Created by Lahiru Pathirage @ Mooniak<[email protected]> on 28/12/2016
"""

from os import walk
import os
import platform

if platform.system() in "Windows":
from utility.win_fix import fixed_install_font

from consumer import FontsConsumer
from service import FontFaceService
from service import FontFileService
Expand All @@ -20,11 +13,18 @@
from service import SystemService
from utility import FileManager

from os import walk
import os
import platform

if platform.system() in "Windows":
from utility.win_fix import fixed_install_font


def find_files_by_extension(source_dir, extension):
files_list = []

for root, dirs, files in walk(source_dir):
for root, dirs, files in walk(os.path.expanduser(source_dir)):
for file in files:
if file.endswith(extension):
files_list.append(
Expand All @@ -37,33 +37,81 @@ def find_files_by_extension(source_dir, extension):
return files_list



class FontManager:

def __init__(self):
self.__system = SystemService().find_system_info()

def get_system_font_list(self):
def get_active_fonts_list(self):
active_fonts = []
font_data = []
fonts_dirs = []

if self.__system.platform in "Windows":
return os.listdir(os.path.join(os.environ["WINDIR"], "Fonts"))
fonts_dirs.append(
os.path.join(os.environ["WINDIR"], "Fonts")
)

elif self.__system.platform in "Linux":
fonts_dirs.append("/usr/share/fonts")

try:
fonts_dirs.append(os.path.join("~", ".fonts"))

except:
print("No user font directory")

else:
import fontconfig
return fontconfig.query()
fonts_dirs.append(os.path.join("~", "Library/Fonts"))
fonts_dirs.append("/System/Library/Fonts")

# list font files in font directories
for dir in fonts_dirs:
active_fonts += find_files_by_extension(dir, ".ttf")
active_fonts += find_files_by_extension(dir, ".otf")

for font in active_fonts:
font_info = []

if "-" in font["name"]:
font_info = font["name"].split(".")[0].split("-")

else:
font_info.append(font["name"].split(".")[0])
font_info.append("Regular")

trigger = True

for element in font_data:
if font_info[0] in element["name"]:
element["fontfaces"].append({
"fontface": font_info[1],
"resource_path": font["file_path"]
})

trigger = False
break

if trigger:
font_data.append({
"name": font_info[0],
"fontfaces": [{
"fontface": font_info[1],
"resource_path": font["file_path"]
}]
})

return font_data

def install_font(self, font_id, rel_id):
font_dir = "./data/" + font_id
sys_font_dir = self.__system.font_directory
sys_fonts_list = self.get_system_font_list()
artifacts_dir = "./data/" + font_id + "/extracted"

FileManager().create_directory(artifacts_dir)

requested_font = FontService().find_by_font_id(font_id).first()

for font in sys_fonts_list:
if requested_font.name in font:
print("Warning! Font already exists")

if rel_id in "devel":
try:
fontfaces = FontFaceService().find_by_font_id(font_id)
Expand Down Expand Up @@ -146,16 +194,11 @@ def install_font(self, font_id, rel_id):
artifacts_dir, ".otf"
)

if fontfaces is []:
fontfaces = find_files_by_extension(
artifacts_dir, ".ufo"
)

for fontface in fontfaces:
if "Windows" in self.__system.platform:
fixed_install_font(fontface["file_path"])

else :
else:
FileManager().move_file(
fontface["name"],
sys_font_dir,
Expand Down

0 comments on commit 5e4115f

Please sign in to comment.