From e5f07ba0f8fe3d756e49384310c2be42a2e97384 Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Thu, 13 Mar 2014 07:51:39 -0300 Subject: [PATCH] UI: interface de menus MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Um menu com opções para alterações dos dados pessoais. A classe base de UI permite implementar novos tipos de interface. --- src/baseui.py | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/menu.py | 81 ++++++++++++++++++++++++++++++++ 2 files changed, 207 insertions(+) create mode 100644 src/baseui.py create mode 100644 src/menu.py diff --git a/src/baseui.py b/src/baseui.py new file mode 100644 index 0000000..4d340ed --- /dev/null +++ b/src/baseui.py @@ -0,0 +1,126 @@ +# -*- mode: python; encoding: utf-8; -*- +# +# Copyright 2014 Thadeu Lima de Souza Cascardo +# +# 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 3 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. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# -*- mode: python; encoding: utf-8; -*- + +class BaseForm(): + def __init__(self): + self.type = "none" + self.name = "Entry" + def get_type(self): + return self.type + def get_name(self): + return self.name + +class StringForm(BaseForm): + def __init__(self, name, value): + self.type = "string" + self.name = name + self.value = value + def get_value(self): + return self.value + def set_value(self, value): + self.value = value + +class OptionsForm(BaseForm): + def __init__(self, name, options, value): + self.type = "options" + self.name = name + self.options = options + self.value = value + self.index = self.find_index() + def find_index(self): + return map(lambda x: x[0], self.options).index(self.value) + def get_length(self): + return len(self.options) + def get_display(self, index): + return self.options[index][1] + def get_value(self, index): + return self.options[index][0] + def get_cur_display(self): + return self.options[self.index][1] + def get_cur_value(self): + return self.value + def get_index(self): + return self.index + def set_value(self, value): + self.value = value + self.index = self.find_index() + +class BaseUI(): + def get_string(self, prompt=""): + return raw_input(prompt.encode("utf-8")) + def menu(self, list): + i = 1 + for l in list: + self.show_item(str(i) + ". " + l) + i += 1 + ans = self.get_string("Option: ") + try: + opt = int(ans) + if opt >= i: + return -1 + return opt - 1 + except Exception, e: + return -1 + def show_item(self, s): + print s + def form(self, fs): + exit = False + while not exit: + self.show_item("0. Exit") + i = 1 + for f in fs: + if f.get_type() == "string": + self.show_item(str(i) + ". " + f.get_name() + ": " + f.get_value()) + elif f.get_type() == "options": + self.show_item(str(i) + ". " + f.get_name() + ": " + f.get_cur_display()) + i += 1 + ans = self.get_string("Option: ") + try: + opt = int(ans) + if opt >= i: + # raise/show error/message about wrong option + opt = -1 + except Exception, e: + raise e + if opt == 0: + exit = True + else: + self.show_form(fs[opt - 1]) + def show_form(self, f): + if f.get_type() == "string": + self.show_item(f.get_name() + ": " + f.get_value()) + s = self.get_string(f.get_name() + ": ") + f.set_value(s) + elif f.get_type() == "options": + self.show_options(f) + def show_options(self, f): + self.show_item(f.get_name() + ": " + f.get_cur_display()) + for i in range(f.get_length()): + self.show_item(str(i) + ". " + f.get_display(i)) + ans = self.get_string("Option: ") + try: + opt = int(ans) + if opt >= i: + # raise/show error/message about wrong option + opt = -1 + except Exception, e: + raise e + f.set_value(f.get_value(opt)) + + +# vim:tabstop=4:expandtab:smartindent diff --git a/src/menu.py b/src/menu.py new file mode 100644 index 0000000..c66d17d --- /dev/null +++ b/src/menu.py @@ -0,0 +1,81 @@ +# -*- mode: python; encoding: utf-8; -*- +# +# Copyright 2013 Thadeu Lima de Souza Cascardo +# +# 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 3 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. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +import baseui +import contribuinte +import ocupacoes +import sys + +class OcupacaoForm(baseui.OptionsForm): + def __init__(self, ocupacoes, contribuinte): + g = ocupacoes.groups() + l = [] + for i in sorted(g): + l.extend(g[i]) + o = map(lambda x: (x[0], x[3]), l) + baseui.OptionsForm.__init__(self, u"Ocupações", o, contribuinte.get_campo_contribuinte("ocupacaoPrincipal")) + self.ocupacoes = ocupacoes + self.contribuinte = contribuinte + def set_value(self, value): + baseui.OptionsForm.set_value(self, value) + self.contribuinte.set_campo_contribuinte("ocupacaoPrincipal", value) + +class ContribuinteForm(baseui.StringForm): + def __init__(self, name, attr, contribuinte): + self.contribuinte = contribuinte + self.attr = attr + baseui.StringForm.__init__(self, name, self.contribuinte.get_campo_contribuinte(self.attr)) + def set_value(self, value): + baseui.StringForm.set_value(self, value) + self.contribuinte.set_campo_contribuinte(self.attr, value) + +def DadosPessoais(UI, contrib): + form = [] + ocup = ocupacoes.Ocupacoes() + form.append(ContribuinteForm("Nome", "nome", contrib)) + form.append(OcupacaoForm(ocup, contrib)) + for i in contribuinte.contribuinte_attributes: + form.append(ContribuinteForm(i, i, contrib)) + UI.form(form) + return True + +def menu(UI, contrib): + m = [ "Sair", "Dados Pessoais" ] + f = [ None, DadosPessoais ] + exit = False + while not exit: + r = UI.menu(m) + if r <= 0: + exit = True + else: + f[r](UI, contrib) + +def main(): + ret = False + UI = baseui.BaseUI() + while ret == False: + cpf = UI.get_string("Digite seu CPF: ") + try: + contrib = contribuinte.Contribuinte(cpf) + ret = menu(UI, contrib) + except RuntimeError, e: + print "CPF invalido" + +if __name__ == '__main__': + main() + +# vim:tabstop=4:expandtab:smartindent -- 2.20.1