Last active
August 29, 2015 14:08
-
-
Save vfrico/cd3bf3dd920020549f09 to your computer and use it in GitHub Desktop.
extractttf.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# *-* coding: UTF-8 *-* | |
# | |
# ** extractttf.py ** | |
# Programa que extrae archivos ttf de carpeta con otros archivos | |
# | |
# Copyright 2012-2015 Víctor Fernández Rico <vfrico@gmail.com> | |
# | |
# This program is free software; you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation; either version 2 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program; if not, write to the Free Software | |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
# MA 02110-1301, USA. | |
# | |
SRC = './fonts/' | |
DST = './fontsgoogle/' | |
import os,shutil | |
def extraerarchivos(ruta): | |
todosarchivos = [] | |
carpetas = os.listdir(ruta) | |
for i in carpetas: | |
if os.path.isdir(ruta+i): | |
carpeta = ruta+i+'/' | |
archivos = os.listdir(carpeta) | |
for i in archivos: | |
todosarchivos.append(carpeta+i) | |
for i in extraerarchivos(carpeta): | |
todosarchivos.append(i) | |
return todosarchivos | |
def seleccionarext(ruta,ext): | |
"""A partir de una ruta, llama a la función 'extraerarchivos' y selecciona aquellos que cumplan con la extensión asignada""" | |
archivos = extraerarchivos(ruta) | |
devuelve = [] | |
for i in archivos: | |
if os.path.splitext(i)[1] == ext: | |
devuelve.append(i) | |
return devuelve | |
def copiararchivos(archivos,antes,despues): | |
"""Copia todos los elementos de archivos, sabiendo de la carpeta de la que provienen y a la que van | |
Usa 'antes' para "restar" la ruta final a 'despues'""" | |
try: | |
os.makedir(despues) | |
except: | |
print("Ya existe el directorio") | |
for archivo in archivos: | |
relativa = archivo.split(antes)[1] | |
carpetarelativa = os.path.split(relativa)[0] #No contiene nombre de archivo | |
try: | |
os.makedirs(despues+carpetarelativa) | |
except: | |
print("El directorio "+despues+carpetarelativa+" ya existe") | |
shutil.copy(archivo,despues+relativa) | |
def guessFolderSource(default): | |
user_input = input("Please, enter directory where google repo is located (default is {}): ".format(default)) | |
if user_input == "": | |
user_input = default | |
try: | |
os.listdir(user_input) | |
return user_input | |
except: | |
print("The folder '{}' cannot be opened".format(user_input)) | |
exit(1) | |
def guessFolderDest(default): | |
print(""" | |
0) Install all fonts on $HOME/.fonts | |
1) Only extract to folder | |
""") | |
first_choice = input("Choose your option [1]: ") | |
if first_choice == "0": | |
folder = os.path.normpath(os.path.expanduser('~')+"/.fonts/googlefonts/") | |
try: | |
os.makedirs(folder) | |
return folder+"/" | |
except FileExistsError: | |
return folder+"/" | |
except Exception as exc: | |
print ("An error occurred: {}. \nExiting...".format(exc)) | |
exit(1) | |
# elif first_choice == "1": | |
else: | |
user_input = input("Please, enter directory where fonts will be extracted (default is {}): ".format(default)) | |
if user_input == "": | |
user_input = default | |
try: | |
os.makedirs(user_input) | |
return user_input | |
except FileExistsError: | |
yn = input("Folder '{}' exists and can be overwritten. Continue? [y/n]: ".format(user_input)) | |
if yn == "y": | |
return user_input | |
else: | |
print("Exiting") | |
exit(1) | |
except: | |
print("The folder '{}' cannot be created. Exiting".format(user_input)) | |
exit(1) | |
directorio = guessFolderSource(SRC) | |
destino = guessFolderDest(DST) | |
copiararchivos(seleccionarext(directorio,'.ttf'),directorio,destino) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment