PyXR

c:\projects\bitpim\src \ bp.py



0001 #!/usr/bin/env python
0002 
0003 ### BITPIM
0004 ###
0005 ### Copyright (C) 2003-2006 Roger Binns <rogerb@rogerbinns.com>
0006 ###
0007 ### This program is free software; you can redistribute it and/or modify
0008 ### it under the terms of the BitPim license as detailed in the LICENSE file.
0009 ###
0010 ### $Id: bp.py 4436 2007-10-30 23:10:47Z djpham $
0011 
0012 """Main entry point to Bitpim
0013 
0014 It invokes BitPim in gui or commandline mode as appropriate
0015 
0016 @Note: Only gui mode is supported at the moment
0017 """
0018 from __future__ import with_statement
0019 import sys
0020 import bp_cli
0021 
0022 # only gui mode support at the moment
0023 
0024 if __debug__:
0025     def profile(filename, command):
0026         import hotshot, hotshot.stats, os
0027         file=os.path.abspath(filename)
0028         profile=hotshot.Profile(file)
0029         profile.run(command)
0030         profile.close()
0031         del profile
0032         howmany=100
0033         stats=hotshot.stats.load(file)
0034         stats.strip_dirs()
0035         stats.sort_stats('time', 'calls')
0036         stats.print_stats(100)
0037         stats.sort_stats('cum', 'calls')
0038         stats.print_stats(100)
0039         stats.sort_stats('calls', 'time')
0040         stats.print_stats(100)
0041         sys.exit(0)
0042         
0043 
0044 if __name__ == '__main__':
0045     import sys  
0046     import getopt
0047     import os.path
0048 
0049     # in production builds we don't need the stupid warnings
0050     if sys.platform=="darwin" and len(sys.argv)>1 and sys.argv[1].startswith("-psn_"):
0051         # get rid of the process serial number on mac
0052         sys.argv=sys.argv[:1]+sys.argv[2:]
0053     try:
0054         _options, _args=getopt.gnu_getopt(sys.argv[1:], 'c:d:f:p:')
0055         _invalid_args=[x for x in _args if x not in ['debug', 'bitfling'] and \
0056                        not bp_cli.valid_command(x)]
0057         if _invalid_args:
0058             raise getopt.GetoptError('Invalid argument(s): '+','.join(_invalid_args))
0059     except getopt.GetoptError:
0060         e=sys.exc_info()[1]
0061         _invalid_args=True
0062         _error_str=e.msg
0063     if _invalid_args:
0064         # invalid/unknown arguments
0065         try:
0066             import wx
0067             import guihelper
0068             _msg='%s\nUsage: %s [-c config file]|[-d config dir] [-p comm port] [-f phone model] [CLI Command] [debug] [bitfling]\n'%(_error_str, sys.argv[0])
0069             sys.stderr.write(_msg)
0070             # try to display an error message box
0071             _app=wx.PySimpleApp()
0072             guihelper.MessageDialog(None, _msg, 'BitPim Error',
0073                                     style=wx.OK|wx.ICON_ERROR)
0074         finally:
0075             sys.exit(1)
0076     _kwargs={}
0077     # check for debug flag
0078     _debug=bool(_args and 'debug' in _args)
0079     # CLI commands present?
0080     _clicmd=len(_args)==1 and bp_cli.valid_command(_args[0])
0081     if not _debug and not _clicmd:
0082         import warnings
0083         def ignorer(*args, **kwargs): pass
0084         warnings.showwarning=ignorer
0085 
0086         # on windows we set stdout, stderr to do nothing objects otherwise there
0087         # is an error after 4kb of output
0088         class _donowt:
0089             def __getattr__(self, _):
0090                 return self
0091 
0092             def __call__(self, *args, **kwargs):
0093                 pass
0094             
0095         # heck, do it for all platforms
0096         sys.stdout=_donowt()
0097         sys.stderr=_donowt()
0098 
0099     for _k,_v in _options:
0100         if _k=='-d':
0101             _kwargs['config_filename']=os.path.join(_v, '.bitpim')
0102         elif _k=='-c':
0103             _kwargs['config_filename']=_v
0104         elif _k=='-p':
0105             _kwargs['comm_port']=_v
0106         elif _k=='-f':
0107             _kwargs['phone_model']=_v
0108     if _args and _clicmd:
0109         # CLI detected
0110         _cli=bp_cli.CLI(_args[0], sys.stdin, sys.stdout, sys.stderr,
0111                         **_kwargs)
0112         if _cli.OK:
0113             _cli.run()
0114     elif _args and 'bitfling' in _args:
0115         import bitfling.bitfling
0116         #if True:
0117         #    profile("bitfling.prof", "bitfling.bitfling.run(sys.argv)")
0118         #else:
0119         bitfling.bitfling.run(sys.argv)
0120     else:
0121         import gui
0122         #if True:
0123         #    profile("bitpim.prof", "gui.run(sys.argv)")
0124         #else:
0125         gui.run(sys.argv, _kwargs)
0126 

Generated by PyXR 0.9.4