Transferred help text into README.md file.
[btcspy.git] / lib / commands.py
1 #!/usr/bin/env python
2 #
3 # Electrum - lightweight Bitcoin client
4 # Copyright (C) 2011 thomasv@gitorious
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 import time
20 from util import *
21 #from bitcoin import *
22 from decimal import Decimal
23 #import bitcoin
24 #from transaction import Transaction
25
26 class Command:
27 def __init__(self, name, min_args, max_args, requires_network, requires_wallet, requires_password, description, syntax = '', options_syntax = ''):
28 self.name = name
29 self.min_args=min_args
30 self.max_args = max_args
31 self.requires_network = requires_network
32 self.requires_wallet = requires_wallet
33 self.requires_password = requires_password
34 self.description = description
35 self.syntax = syntax
36 self.options = options_syntax
37
38 known_commands = {}
39 def register_command(*args):
40 global known_commands
41 name = args[0]
42 known_commands[name] = Command(*args)
43
44
45
46 payto_options = ' --fee, -f: set transaction fee\n --fromaddr, -F: send from address -\n --changeaddr, -c: send change to address'
47 listaddr_options = " -a: show all addresses, including change addresses\n -l: include labels in results"
48 restore_options = " accepts a seed or master public key."
49 mksendmany_syntax = 'mksendmanytx <recipient> <amount> [<recipient> <amount> ...]'
50 payto_syntax = "payto <recipient> <amount> [label]\n<recipient> can be a bitcoin address or a label"
51 paytomany_syntax = "paytomany <recipient> <amount> [<recipient> <amount> ...]\n<recipient> can be a bitcoin address or a label"
52 signmessage_syntax = 'signmessage <address> <message>\nIf you want to lead or end a message with spaces, or want double spaces inside the message make sure you quote the string. I.e. " Hello This is a weird String "'
53 verifymessage_syntax = 'verifymessage <address> <signature> <message>\nIf you want to lead or end a message with spaces, or want double spaces inside the message make sure you quote the string. I.e. " Hello This is a weird String "'
54
55
56 # command
57 # requires_network
58 # requires_wallet
59 # requires_password
60 register_command('history', 0, 0, True, True, False, 'Returns the transaction history of your wallet')
61 register_command('help', 0, 1, False, False, False, 'Prints this help')
62
63
64
65
66 class Commands:
67
68 def __init__(self, wallet, callback = None):
69 self.wallet = wallet
70 #self.network = network
71 self._callback = callback
72 self.password = None
73
74
75 def _run(self, method, args, password_getter):
76 cmd = known_commands[method]
77 if cmd.requires_password and self.wallet.use_encryption:
78 self.password = apply(password_getter,())
79 f = getattr(self, method)
80 result = f(*args)
81 self.password = None
82 if self._callback:
83 apply(self._callback, ())
84 return result
85
86
87 def help(self, cmd=None):
88 if cmd not in known_commands:
89 print_msg("\nList of commands:", ', '.join(sorted(known_commands)))
90 else:
91 cmd = known_commands[cmd]
92 print_msg(cmd.description)
93 if cmd.syntax: print_msg("Syntax: " + cmd.syntax)
94 if cmd.options: print_msg("options:\n" + cmd.options)
95 return None
96
97
98 def history(self):
99 import datetime
100 balance = 0
101 out = []
102 for item in self.wallet.get_tx_history():
103 tx_hash, conf, is_mine, value, fee, balance, timestamp = item
104 try:
105 time_str = datetime.datetime.fromtimestamp( timestamp).isoformat(' ')[:-3]
106 except Exception:
107 time_str = "----"
108
109 label, is_default_label = self.wallet.get_label(tx_hash)
110
111 out.append({'txid':tx_hash, 'date':"%16s"%time_str, 'label':label, 'value':format_satoshis(value)})
112 return out
113
114
115
116