Add command line option to ask a simpler cairo drawing
[pysize.git] / pysize / main.py
blob267a8a3e7797a6de7c0cad9f585cf433deb49b70
1 import sys
2 import os
3 import optparse
4 import locale
6 from pysize.ui.ascii import ui_ascii
7 from pysize.ui.curses import ui_curses
8 from pysize.ui.gtk import ui_gtk
9 from pysize.core.sigquit_traceback import install_sigquit_traceback
11 def _ui_auto(options, args):
12 """Automatically choose the best available UI."""
13 if ui_gtk.is_available():
14 return ui_gtk.run(options, args)
16 if ui_curses.is_available():
17 return ui_curses.run(options, args)
19 if ui_ascii.is_available():
20 return ui_ascii.run(options, args)
22 print 'No UI available'
24 UI = {'ascii': ui_ascii.run, 'curses': ui_curses.run,
25 'gtk': ui_gtk.run, 'auto': _ui_auto}
27 def _profile(continuation):
28 try:
29 import cProfile
30 import pstats
31 print 'Profiling using cProfile'
32 cProfile.runctx('continuation()', globals(), locals(), 'pysize.prof')
33 stats = pstats.Stats('pysize.prof')
34 except ImportError:
35 import hotshot
36 import hotshot.stats
37 prof = hotshot.Profile('pysize.prof')
38 print 'Profiling using hotshot'
39 prof.runcall(continuation)
40 prof.close()
41 stats = hotshot.stats.load('pysize.prof')
42 stats.strip_dirs()
43 stats.sort_stats('time', 'calls')
44 stats.print_stats(40)
46 def _try_psyco():
47 try:
48 # Try to use psyco if available
49 import psyco
50 psyco.full()
51 except ImportError:
52 pass
54 def main():
55 install_sigquit_traceback()
56 _try_psyco()
57 locale.setlocale(locale.LC_ALL, '')
58 usage = '%s [OPTIONS] [DIRECTORIES...]' % (sys.argv[0])
59 parser = optparse.OptionParser(usage=usage)
60 parser.add_option('--ui', type='choice', choices=UI.keys(), default='auto',
61 help='choose the ui between: [auto], gtk, curses, ascii')
62 parser.add_option('--max-depth', type='int', dest='max_depth', default=5,
63 metavar='DEPTH',
64 help='maximum depth of the displayed tree [5]')
65 parser.add_option('--min-size', type='str', dest='min_size', default='auto',
66 metavar='SIZE',
67 help='minimum size to consider drawing [auto]')
68 parser.add_option('--fast-drawing', action='store_true', dest='fast',
69 default=False, help='faster but simpler drawing with GTK')
70 parser.add_option('--profile', action='store_true', dest='profile',
71 default=False, help=optparse.SUPPRESS_HELP)
72 (options, args) = parser.parse_args()
74 if options.max_depth < 2:
75 parser.error('maximum depth must be at least 2')
77 # Default path if none given
78 if len(args) == 0:
79 args = ['.']
81 args = map(os.path.realpath, args)
82 continuation = lambda: UI[options.ui](options, args)
84 if options.profile:
85 _profile(continuation)
86 else:
87 continuation()
89 if __name__ == '__main__':
90 main()