Skip to content

Instantly share code, notes, and snippets.

@weex
Created June 19, 2011 10:01
Show Gist options
  • Save weex/1034031 to your computer and use it in GitHub Desktop.
Save weex/1034031 to your computer and use it in GitHub Desktop.
diff --cc goxsh.py
index 62cdea6,590609a..0000000
--- a/goxsh.py
+++ b/goxsh.py
@@@ -165,47 -127,111 +169,138 @@@ class GoxSh(object)
self.__btc_precision = Decimal("0.00000001")
self.__usd_precision = Decimal("0.00001")
self.__usd_re = re.compile(r"^\$(\d*\.?\d+)$")
++<<<<<<< HEAD
+ #self.__mtgox_commission = mtgox.get_commission()
++=======
+ self.__mtgox_commission = Decimal("0.0065")
+ collapse_escapes = partial(re.compile(r"\\(.)", re.UNICODE).sub, "\\g<1>")
+ self.__token_types = (
+ ( # naked (unquoted)
+ re.compile(r"(?:\\[\s\\\"';#]|[^\s\\\"';#])+", re.UNICODE),
+ collapse_escapes
+ ),
+ ( # double-quoted
+ re.compile(r"\"(?:\\[\\\"]|[^\\])*?\"", re.UNICODE),
+ lambda matched: collapse_escapes(matched[1:-1])
+ ),
+ ( # single-quoted
+ re.compile(r"'(?:\\[\\']|[^\\])*?'", re.UNICODE),
+ lambda matched: collapse_escapes(matched[1:-1])
+ ),
+ ( # whitespace and comments
+ re.compile(r"(?:\s|#.*)+", re.UNICODE),
+ lambda matched: None
+ ),
+ ( # semicolon
+ re.compile(r";", re.UNICODE),
+ lambda matched: matched
+ )
+ )
+
+ def __tokenize_command(self, line):
+ remaining = line
+ while remaining:
+ found = False
+ for (pattern, sub) in self.__token_types:
+ match = pattern.match(remaining)
+ if match:
+ raw_token = match.group(0)
+ assert len(raw_token) > 0, u"empty token"
+ token = sub(raw_token)
+ if token != None:
+ yield token
+ remaining = remaining[len(raw_token):]
+ found = True
+ break
+ if not found:
+ message = "\n".join((
+ u" %s^" % (u" " * (len(line) - len(remaining))),
+ u"Syntax error."
+ ))
+ raise TokenizationError(message)
+
+ def __parse_tokens(self, tokens):
+ cmd = None
+ args = []
+ for token in tokens:
+ if token == u";":
+ if cmd != None:
+ yield (cmd, args)
+ cmd = None
+ args = []
+ elif cmd == None:
+ cmd = token
+ else:
+ args.append(token)
+ if cmd != None:
+ yield (cmd, args)
++>>>>>>> 6dd7577486dac994518f4cfcda072cb185a56584
def prompt(self):
- proc = None
+ procs = []
args = []
try:
+ raw_line = None
try:
++<<<<<<< HEAD
+ text = u"%s%s$ " % (self.__mtgox.get_username() or u"", self.__mtgox.get_servername() or u"")
+ line = raw_input(text).decode(self.__encoding).replace("@"," ").split()
+ if line:
+ cmd, args = line[0], line[1:]
+ proc = self.__get_cmd_proc(cmd, self.__unknown(cmd))
++=======
+ text = u"%s$ " % (self.__mtgox.get_username() or u"")
+ line = raw_input(text).decode(self.__encoding)
++>>>>>>> 6dd7577486dac994518f4cfcda072cb185a56584
except EOFError, e:
print u"exit"
- proc = self.__cmd_exit__
- if proc != None:
- (min_arity, max_arity) = self.__get_proc_arity(proc)
- arg_count = len(args)
- if min_arity <= arg_count and (max_arity == None or arg_count <= max_arity):
- proc(*args)
- else:
- if min_arity == max_arity:
- arity_text = unicode(min_arity)
- elif max_arity == None:
- arity_text = u"%s+" % min_arity
+ self.__cmd_exit__()
+ commands = self.__parse_tokens(self.__tokenize_command(line))
+ for (cmd, args) in commands:
+ try:
+ proc = self.__get_cmd_proc(cmd, self.__unknown(cmd))
+ (min_arity, max_arity) = self.__get_proc_arity(proc)
+ arg_count = len(args)
+ if min_arity <= arg_count and (max_arity == None or arg_count <= max_arity):
+ proc(*args)
else:
++<<<<<<< HEAD
+ arity_text = u"%s-%s" % (min_arity, max_arity)
+ arg_text = u"argument" + (u"" if arity_text == u"1" else u"s")
+ raise ArityError(u"Expected %s %s, got %s." % (arity_text, arg_text, arg_count))
+ except MtGoxError, e:
+ print u"Mt. Gox error: %s" % e
+ except CommandError, e:
+ print e
+ except ArityError, e:
+ print e
+ except NoCredentialsError:
+ print u"No login credentials entered. Use the login command first."
+ except LoginError:
+ print u"Mt. Gox rejected the login credentials. Maybe you made a typo?"
++>>>>>>> 6dd7577486dac994518f4cfcda072cb185a56584
except EOFError, e:
raise e
- except CommandError, e:
- print e
- except ArityError, e:
+ except TokenizationError, e:
print e
++<<<<<<< HEAD
+ except NoCredentialsError:
+ print u"No login credentials entered. Use the login command first."
+ except LoginError:
+ print u"%s rejected the login credentials. Maybe you made a typo?" % self.__mtgox.get_exchangename()
++=======
++>>>>>>> 6dd7577486dac994518f4cfcda072cb185a56584
except KeyboardInterrupt:
print
except Exception, e:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment