Commit 4991422e authored by Romain Bignon's avatar Romain Bignon

bnp_list.py is now bnporc.py executable with commands

parent 3bd28fad
#!/usr/bin/env python2.5
# -*- coding: utf-8 -*-
"""
Copyright(C) 2009 Romain Bignon
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
"""
import sys
from optparse import OptionParser
from bnporc.browser import BNPorc
def getparser():
parser = OptionParser(usage="%prog [options]")
parser.add_option('-l', '--login', help="Compte ID", type="str")
parser.add_option('-p', '--password', help="Compte password", type="str")
return parser
if __name__ == '__main__':
parser = getparser()
options, arguments = parser.parse_args()
if arguments or not options.login or not options.password:
parser.print_help()
sys.exit(1)
bnp = BNPorc(options.login, options.password)
comptes = bnp.get_comptes_list()
print ' Compte Solde À venir '
print '+---------------------+--------------+-------------+'
for compte in comptes:
print ' %-20s %11.2f %11.2f' % (compte['label'],
compte['solde'],
compte['avenir'])
#!/usr/bin/env python2.5
# -*- coding: utf-8 -*-
"""
Copyright(C) 2009 Romain Bignon
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
"""
from __future__ import with_statement
import sys, tty, termios
import os
from types import MethodType
from optparse import OptionParser
from ConfigParser import SafeConfigParser, NoSectionError
from bnporc.browser import BNPorc
from bnporc.error import LoginError
class Application:
CONFIG_FILE = '%s/.bnporc' % os.path.expanduser("~")
def __init__(self):
self.bnp = BNPorc()
def getparser(self):
config = SafeConfigParser()
login = ''
try:
config.read(self.CONFIG_FILE)
login = config.get('auth', 'login')
except (NoSectionError, ValueError), e:
pass
parser = OptionParser(usage="%prog [options] <command> [args ...]")
parser.add_option('-l', '--login', help="Compte ID", type="str", default=login)
parser.add_option('-p', '--password', help="Compte password", type="str")
return parser
def prompt_password(self, prompt):
sys.stdout.write(prompt)
attr = termios.tcgetattr(sys.stdin)
tty.setcbreak(sys.stdin)
line = sys.stdin.readline().split('\n')[0]
termios.tcsetattr(sys.stdin, termios.TCSAFLUSH, attr)
sys.stdout.write('\n')
return line
def main(self, argv):
parser = self.getparser()
options, arguments = parser.parse_args()
if not options.login or not arguments:
parser.print_help()
sys.exit(1)
if not options.password:
options.password = self.prompt_password('Password: ')
if not options.password.isdigit():
print >>sys.stderr, 'Error: please enter a numeric password'
return 1
config = SafeConfigParser()
config.add_section('auth')
config.set('auth', 'login', options.login)
with open(self.CONFIG_FILE, 'wb') as configfile:
config.write(configfile)
try:
self.bnp.login(options.login, options.password)
except LoginError, err:
print >>sys.stderr, 'Error: %s' % err
return 1
return self.command(arguments[0], *arguments[1:])
def getMethods(self, prefix):
services = {}
for attrname in dir(self):
if not attrname.startswith(prefix):
continue
attr = getattr(self, attrname)
if not isinstance(attr, MethodType):
continue
name = attrname[len(prefix):]
services[name] = attr
return services
def command(self, command, *args):
commands = self.getMethods('command_')
if not command in commands:
sys.stderr.write("No such command: %s" % command)
try:
commands[command](*args)
except TypeError, e:
try:
sys.stderr.write("Command %s takes %s arguments" % (command, int(str(e).split(' ')[3]) - 1))
except:
sys.stderr.write('%s' % e)
def command_list(self):
comptes = self.bnp.get_comptes_list()
print ' Compte Solde À venir '
print '+---------------------+--------------+-------------+'
for compte in comptes:
print ' %-20s %11.2f %11.2f' % (compte['label'],
compte['solde'],
compte['avenir'])
if __name__ == '__main__':
app = Application()
sys.exit(app.main(sys.argv))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment