home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / python-support / gnome-applets-data / invest / __init__.py next >
Encoding:
Python Source  |  2009-04-23  |  3.1 KB  |  112 lines

  1. import os, sys
  2. from os.path import join, exists, isdir, isfile, dirname, abspath, expanduser
  3.  
  4. import gtk, gtk.gdk, gconf, gobject
  5. import cPickle
  6.  
  7. # Autotools set the actual data_dir in defs.py
  8. from defs import *
  9.  
  10. DEBUGGING = False
  11.  
  12. # Allow to use uninstalled invest ---------------------------------------------
  13. UNINSTALLED_INVEST = False
  14. def _check(path):
  15.     return exists(path) and isdir(path) and isfile(path+"/ChangeLog")
  16.     
  17. name = join(dirname(__file__), '..')
  18. if _check(name):
  19.     UNINSTALLED_INVEST = True
  20.     
  21. # Sets SHARED_DATA_DIR to local copy, or the system location
  22. # Shared data dir is most the time /usr/share/invest-applet
  23. if UNINSTALLED_INVEST:
  24.     SHARED_DATA_DIR = abspath(join(dirname(__file__), '..', 'data'))
  25.     BUILDER_DATA_DIR = SHARED_DATA_DIR
  26.     ART_DATA_DIR = join(SHARED_DATA_DIR, 'art')
  27. else:
  28.     SHARED_DATA_DIR = join(DATA_DIR, "gnome-applets", "invest-applet")
  29.     BUILDER_DATA_DIR = BUILDERDIR
  30.     ART_DATA_DIR = SHARED_DATA_DIR
  31. if DEBUGGING:
  32.     print "Data Dir: %s" % SHARED_DATA_DIR
  33.  
  34. USER_INVEST_DIR = expanduser("~/.gnome2/invest-applet")
  35. if not exists(USER_INVEST_DIR):
  36.     try:
  37.         os.makedirs(USER_INVEST_DIR, 0744)
  38.     except Exception , msg:
  39.         print 'Error:could not create user dir (%s): %s' % (USER_INVEST_DIR, msg)
  40. # ------------------------------------------------------------------------------
  41.  
  42. # Set the cwd to the home directory so spawned processes behave correctly
  43. # when presenting save/open dialogs
  44. os.chdir(expanduser("~"))
  45.  
  46. #Gconf client
  47. GCONF_CLIENT = gconf.client_get_default()
  48.  
  49. # GConf directory for invest in window mode and shared settings
  50. GCONF_DIR = "/apps/invest"
  51.  
  52. # GConf key for list of enabled handlers, when uninstalled, use a debug key to not conflict
  53. # with development version
  54. #GCONF_ENABLED_HANDLERS = GCONF_DIR + "/enabled_handlers"
  55.     
  56. # Preload gconf directories
  57. #GCONF_CLIENT.add_dir(GCONF_DIR, gconf.CLIENT_PRELOAD_RECURSIVE)
  58.  
  59. STOCKS_FILE = join(USER_INVEST_DIR, "stocks.pickle")
  60.  
  61. try:
  62.     STOCKS = cPickle.load(file(STOCKS_FILE))
  63. except Exception, msg:
  64.     STOCKS = {}
  65.     
  66. #STOCKS = {
  67. #    "AAPL": {
  68. #        "amount": 12,
  69. #        "bought": 74.94,
  70. #        "comission": 31,
  71. #    },
  72. #    "INTC": {
  73. #        "amount": 30,
  74. #        "bought": 25.85,
  75. #        "comission": 31,
  76. #    },
  77. #    "GOOG": {
  78. #        "amount": 1,
  79. #        "bought": 441.4,
  80. #        "comission": 31,
  81. #    },
  82. #}
  83.  
  84. client = gconf.client_get_default()
  85.  
  86. # borrowed from Ross Burton
  87. # http://burtonini.com/blog/computers/postr
  88. def get_gnome_proxy(client):
  89.     if client.get_bool("/system/http_proxy/use_http_proxy"):
  90.         host = client.get_string("/system/http_proxy/host")
  91.         port = client.get_int("/system/http_proxy/port")
  92.         if host is None or host == "" or port == 0:
  93.             # gnome proxy is not valid, use enviroment if available
  94.             return None
  95.         
  96.         if client.get_bool("/system/http_proxy/use_authentication"):
  97.             user = client.get_string("/system/http_proxy/authentication_user")
  98.             password = client.get_string("/system/http_proxy/authentication_password")
  99.             if user and user != "":
  100.                 url = "http://%s:%s@%s:%d" % (user, password, host, port)
  101.             else:
  102.                 url = "http://%s:%d" % (host, port)
  103.         else:
  104.             url = "http://%s:%d" % (host, port)
  105.  
  106.         return {'http': url}
  107.     else:
  108.         # gnome proxy is not set, use enviroment if available
  109.         return None
  110.  
  111. PROXY = get_gnome_proxy(client)
  112.