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.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-08-31  |  4.2 KB  |  123 lines

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