Add docstrings and ideas for command line options
[pyff3.git] / script.py
blobdff48fba92f8c80a31661f97748d064067302c2f
1 #!/usr/bin/env python
3 from optparse import OptionParser
4 import os
5 from stat import *
7 import lib
9 def font_times_font(font0, font1, button, steps):
10 """Interface to the main method of InterpolateFonts.
12 font1 and font2 are font files on disk.
14 The `binary operations' that follow are based on this function, ie
15 font_times_directory, etc.
17 """
18 myfont0_times_myfont1 = lib.InterpolateFonts(font0, font1, button, steps)
19 myfont0_times_myfont1.font_info()
20 myfont0_times_myfont1.interpolate()
22 def font_times_directory(font0, directory, button, steps):
23 """Interpolates font0 with all the fonts in directory."""
24 for dirpath, dirnames, filenames in os.walk(directory):
25 for font1 in filenames:
26 font_times_font(font0, os.path.join(dirpath, font1), button, steps)
28 def directory_times_font(directory, font1, button, steps):
29 """Interpolates all the fonts in directory with font1."""
30 for dirpath, dirnames, filenames in os.walk(directory):
31 for font0 in filenames:
32 font_times_font(os.path.join(dirpath, font0), font1, button, steps)
34 def directory_times_directory(directory0, directory1, button, steps):
35 """Interpolates all the fonts in directory0 with all the fonts in directory1."""
36 for dirpath0, dirnames0, filenames0 in os.walk(directory0):
37 for myfont0 in filenames0:
38 for dirpath1, dirnames1, filenames1 in os.walk(directory1):
39 for myfont1 in filenames1:
40 font_times_font(os.path.join(dirpath0, myfont0), os.path.join(dirpath1, myfont1), button, steps)
42 if __name__ == "__main__":
44 gui = OptionParser()
45 gui.add_option("-o", "--output", action="store", type="int", dest="output_files")
46 gui.add_option("-x", action="store_true", dest="x")
47 gui.add_option("-y", action="store_true", dest="y")
48 gui.add_option("-z", action="store_true", dest="z")
49 (options, paths) = gui.parse_args()
51 # The x, y, z flags correspond to the `button' attribute of the
52 # InterpolateFonts method, cf its docstring
54 if options.x:
55 button = "x"
56 elif options.y:
57 button = "y"
58 elif options.z:
59 button = "z"
61 # We use the stat module to tell files and directories apart
62 # http://docs.python.org/library/stat.html
64 mode0 = os.stat(paths[0])[ST_MODE]
65 mode1 = os.stat(paths[1])[ST_MODE]
67 if S_ISDIR(mode0) and S_ISDIR(mode1):
68 # Both locations are directories
69 directory_times_directory(paths[0], paths[1], button, options.output_files)
70 elif S_ISREG(mode0) and S_ISREG(mode1):
71 # Both locations are files
72 font_times_font(paths[0], paths[1], button, options.output_files)
73 elif S_ISREG(mode0):
74 # paths[0] is a file
75 font_times_directory(paths[0], paths[1], button, options.output_files)
76 elif S_ISREG(mode1):
77 # paths[1] is a file
78 directory_times_font(paths[0], paths[1], button, options.output_files)