Oups, forgot self argument
[pysize.git] / pysize / main.py
bloba7e78f076e24d70009c4bfbdb1be0fc4d1f57ce8
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', help='minimum size to consider drawing')
67 parser.add_option('--profile', action='store_true', dest='profile',
68 default=False, help=optparse.SUPPRESS_HELP)
69 (options, args) = parser.parse_args()
71 if options.max_depth < 2:
72 parser.error('maximum depth must be at least 2')
74 # Default path if none given
75 if len(args) == 0:
76 args = ['.']
78 args = map(os.path.realpath, args)
79 continuation = lambda: UI[options.ui](options, args)
81 if options.profile:
82 _profile(continuation)
83 else:
84 continuation()
86 if __name__ == '__main__':
87 main()