#! /usr/bin/python # -*- coding: UTF-8 -*- """ Gestionale pizzate: programma creato da Frafra (francesco.it@gmail.com) 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; either version 2 of the License, or (at your option) any later version. 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. """ __module_name__ = 'Gestionale pizzate' __module_version__ = __revision__ = versione = '0.2' __module_description__ = 'Per organizzare le pizzate' def ask(message, valid_chars, min_len = 1, max_len = 10, max_retry = 10): for each in xrange(max_retry): answer = raw_input(message + ' ') for char in answer: if char.lower() not in valid_chars: print 'Formato non valido, riprova.' break else: if min_len <= len(answer) <= max_len: return answer else: print 'Formato non valido, riprova.' print 'L\'utente e\' troppo stupido per usare questo programma.' def add(): global db nome = raw_input('Nome: ') conf = ask('Disponibile [s/n/?]:', ['s', 'n', '?'], 1, 1) note = raw_input('Note: ') db.append(nome+'\t'+conf+'\t'+note) print 'Entry numero '+str(len(db))+' inserita.' def edit(): global db num = int(ask('Numero entry:', num_range, 1, len(str(len(db))))) nome = raw_input('Nome: ') conf = ask('Disponibile [s/n/?]:', ['s', 'n', '?'], 1, 1) note = raw_input('Note: ') db[num] = nome+'\t'+conf+'\t'+note print 'Entry numero '+str(num)+' modificata.' def remove(): global db num = int(ask('Numero entry:', num_range, 1, len(str(len(db))))) del db[num] print 'Entry numero '+str(num)+' cancellata.' def order(): global db db.sort() print 'Database riordinato.' def display(): global db new_db = [] for x in xrange(len(db)): new_db.append(db[x].split('\t')[0]) col = max([len(x) for x in new_db])+1 for x in xrange(len(db)): try: nome, res, note = db[x].split('\t') print x, nome+' '*(col-len(nome))+res+' '+note except: nome, res = db[x].split('\t') print x, nome+' '*(col-len(nome))+res def save(): global db new_db = [] name = raw_input('Nome del file: ') report = open(name, 'w') for x in xrange(len(db)): new_db.append(db[x].split('\t')[0]) col = max([len(x) for x in new_db])+1 for x in xrange(len(db)): try: nome, res, note = db[x].split('\t') report.write(str(x)+' '+nome+' '*(col-len(nome))+res+' '+note+'\n') except: nome, res = db[x].split('\t') report.write(str(x)+' '+nome+' '*(col-len(nome))+res+'\n') report.close() print 'File salvato' def load(): global db new_db = [] name = raw_input('Nome del file: ') report = open(name, 'r') rw = ask('Sovrascrivere o Accodare? [s/a]:', ['s', 'a'], 1, 1) if rw == 's': db = [] while 1: line = report.readline() if not line: break new_db.append(line) report.close() for x in xrange(len(new_db)): db.append('\t'.join(new_db[x].split()[1:])) print 'File importato' def quit(): print 'Arrivederci.' raise SystemExit(0) sel = {'a':add, 'm':edit, 'c':remove, 'o':order, 'v':display, 's':save, 'i':load, 'e':quit} db = [] num_range = [str(x) for x in xrange(10)] while 1: print 'Aggiungi entry' print 'Modifica entry' print 'Cancella entry' print 'Ordina entries' print 'Visualizza' print 'Salva' print 'Importa' print 'Esci' sel[ask('Scelta:', sel.keys(), 1, 1)]() print