| 1 |
import os |
|---|
| 2 |
import re |
|---|
| 3 |
import sys |
|---|
| 4 |
import time |
|---|
| 5 |
import types |
|---|
| 6 |
import urllib |
|---|
| 7 |
|
|---|
| 8 |
from trac.core import * |
|---|
| 9 |
from trac.env import IEnvironmentSetupParticipant |
|---|
| 10 |
|
|---|
| 11 |
from tracmarket.api import * |
|---|
| 12 |
from tracmarket.command import * |
|---|
| 13 |
|
|---|
| 14 |
SequenceType = (types.ListType, types.TupleType, types.GeneratorType) |
|---|
| 15 |
|
|---|
| 16 |
class Traders(Component): |
|---|
| 17 |
implements(IMessageBusSubscriber) |
|---|
| 18 |
|
|---|
| 19 |
def __init__(self): |
|---|
| 20 |
self.exchange_authname = self.config.get( |
|---|
| 21 |
'market', 'exchange_authname', 'tracmarket') |
|---|
| 22 |
self.currency = self.config.get('market', 'currency', 'TMP') |
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
def solicit(self, bus): |
|---|
| 27 |
bus.subscribe('market.cmd.traders', reader=self.recv_cmd) |
|---|
| 28 |
|
|---|
| 29 |
def recv_cmd(self, bus, group, msg): |
|---|
| 30 |
verb = msg.verb |
|---|
| 31 |
args = msg.args |
|---|
| 32 |
func = self.parse_verb(self, verb) |
|---|
| 33 |
if args: |
|---|
| 34 |
func(bus, *args) |
|---|
| 35 |
else: |
|---|
| 36 |
func(bus) |
|---|
| 37 |
|
|---|
| 38 |
def parse_verb(self, interpreter, verb): |
|---|
| 39 |
method = "cmd_%s" % verb |
|---|
| 40 |
if not hasattr(interpreter, method): |
|---|
| 41 |
raise SyntaxError, "unknown verb: %s" % ( verb) |
|---|
| 42 |
return getattr(interpreter, method) |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
def cmd_show_worth(self, bus, order='desc', limit=5): |
|---|
| 47 |
|
|---|
| 48 |
def by_desc_worth(x, y): |
|---|
| 49 |
return cmp(worth[x] > worth[y]) |
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
ctx = bus.ctx |
|---|
| 53 |
db = ctx.db |
|---|
| 54 |
ledger = ctx.ledger |
|---|
| 55 |
worth = {} |
|---|
| 56 |
balances = ledger.balances(db=db, symbol=self.currency) |
|---|
| 57 |
for entity,account,symbol,balance in balances: |
|---|
| 58 |
if entity in ('anonymous', self.exchange_authname): |
|---|
| 59 |
continue |
|---|
| 60 |
worth.setdefault(entity, 0) |
|---|
| 61 |
acct = ledger.getacct(db=db, entity=entity, account=account) |
|---|
| 62 |
type = acct.type |
|---|
| 63 |
if type == 'asset': |
|---|
| 64 |
worth[entity] += balance |
|---|
| 65 |
if type == 'liability': |
|---|
| 66 |
worth[entity] -= balance |
|---|
| 67 |
|
|---|
| 68 |
keys = worth.keys() |
|---|
| 69 |
keys.sort(lambda x,y: cmp(worth[y], worth[x])) |
|---|
| 70 |
net_worth = [] |
|---|
| 71 |
for entity in keys: |
|---|
| 72 |
net_worth.append(dict(entity=entity, worth=worth[entity])) |
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
ctx.res.market.traders.net_worth = net_worth |
|---|
| 76 |
|
|---|