home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / python2.4 / site-packages / invest / quotes.py < prev    next >
Encoding:
Python Source  |  2006-08-30  |  3.0 KB  |  96 lines

  1. import os, time
  2. from os.path import *
  3. import gnomeapplet, gtk, gtk.gdk, gconf, gobject
  4. from gettext import gettext as _
  5. import gtk, gtk.glade, egg.trayicon, gobject, gnomevfs
  6. import csv, os
  7.  
  8. import invest, invest.about, invest.chart
  9.  
  10. class QuoteUpdater(gtk.ListStore):
  11.     __gsignals__ = {
  12.         "quotes-updated" : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, []),
  13.     }
  14.     
  15.     SYMBOL, TICKER_ONLY, BALANCE, BALANCE_PCT, VALUE, VARIATION = range(6)
  16.     def __init__ (self):
  17.         gtk.ListStore.__init__ (self, gobject.TYPE_STRING, bool, float, float, float, float)
  18.         gobject.timeout_add(invest.AUTOREFRESH_TIMEOUT, self.refresh)
  19.         
  20.     def refresh(self):
  21.         if len(invest.STOCKS) == 0:
  22.             return True
  23.             
  24.         s = ""
  25.         for ticker in invest.STOCKS.keys():
  26.             s += "%s+" % ticker
  27.         
  28.         gnomevfs.async.open(invest.QUOTES_URL % {"s": s[:-1]}, self.on_quotes_open)
  29.         return True
  30.         
  31.     def on_quotes_open(self, handle, exc_type):
  32.         if not exc_type:
  33.             handle.read(invest.GNOMEVFS_CHUNK_SIZE, lambda h,d,e,b: self.on_quotes_read(h,d,e,b, ""))
  34.         else:
  35.             handle.close(lambda *args: None)    
  36.  
  37.     def on_quotes_read(self, handle, data, exc_type, bytes_requested, read):
  38.         if not exc_type:
  39.             read += data
  40.             
  41.         if exc_type:
  42.             handle.close(lambda *args: None)
  43.             self.populate(self.parse_yahoo_csv(csv.reader(read.split("\n"))))
  44.         else:
  45.             handle.read(invest.GNOMEVFS_CHUNK_SIZE, lambda h,d,e,b: self.on_quotes_read(h,d,e,b, read))
  46.  
  47.     def parse_yahoo_csv(self, csvreader):
  48.         result = {}
  49.         for fields in csvreader:
  50.             if len(fields) == 0:
  51.                 continue
  52.  
  53.             result[fields[0]] = {}
  54.             for i, field in enumerate(invest.QUOTES_CSV_FIELDS):
  55.                 if type(field) == tuple:
  56.                     result[fields[0]][field[0]] = field[1](fields[i])
  57.                 else:
  58.                     result[fields[0]][field] = fields[i]
  59.                     
  60.         return result 
  61.  
  62.     def populate(self, quotes):
  63.         self.clear()
  64.         
  65.         #current = sum([sum([purchase["amount"]*val["trade"] for purchase in invest.STOCKS[ticker]]) for ticker, val in quotes.items()])
  66.         #paid = sum([sum([purchase["amount"]*purchase["bought"]+purchase["comission"] for purchase in invest.STOCKS[ticker]]) for ticker in quotes.keys()])
  67.         #balance = current - paid    
  68.                 
  69.         #self.append([_("Total"), balance, balance/paid*100, current, 0])
  70.         
  71.         for ticker, val in quotes.items():
  72.             # Check whether the symbol is a syimple quote, or a portfolio value
  73.             is_simple_quote = True
  74.             for purchase in invest.STOCKS[ticker]:
  75.                 if purchase["amount"] != 0:
  76.                     is_simple_quote = False
  77.                     break
  78.  
  79.             if is_simple_quote:
  80.                 self.append([ticker, True, 0, 0, val["trade"], val["variation"]])
  81.             else:
  82.                 current = sum([purchase["amount"]*val["trade"] for purchase in invest.STOCKS[ticker] if purchase["amount"] != 0])
  83.                 paid = sum([purchase["amount"]*purchase["bought"] + purchase["comission"] for purchase in invest.STOCKS[ticker] if purchase["amount"] != 0])
  84.                 balance = current - paid
  85.                 
  86.                 self.append([ticker, False, balance, balance/paid*100, val["trade"], val["variation"]])
  87.                 
  88.         self.emit("quotes-updated")
  89.  
  90. if gtk.pygtk_version < (2,8,0):
  91.     gobject.type_register(QuoteUpdater)
  92.  
  93. _updater = QuoteUpdater()
  94. def get_quotes_updater():
  95.     return _updater
  96.