Package ajango :: Package generator :: Module renderer
[hide private]
[frames] | no frames]

Source Code for Module ajango.generator.renderer

 1  ########################################################################### 
 2  #                                                                         # 
 3  #  Copyright (C) 2016  Rafal Kobel <rafyco1@gmail.com>                    # 
 4  #                                                                         # 
 5  #  This program is free software: you can redistribute it and/or modify   # 
 6  #  it under the terms of the GNU General Public License as published by   # 
 7  #  the Free Software Foundation, either version 3 of the License, or      # 
 8  #  (at your option) any later version.                                    # 
 9  #                                                                         # 
10  #  This program is distributed in the hope that it will be useful,        # 
11  #  but WITHOUT ANY WARRANTY; without even the implied warranty of         # 
12  #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the           # 
13  #  GNU General Public License for more details.                           # 
14  #                                                                         # 
15  #  You should have received a copy of the GNU General Public License      # 
16  #  along with this program.  If not, see <http://www.gnu.org/licenses/>.  # 
17  #                                                                         # 
18  ########################################################################### 
19  """ Pakiet generowania plikow jezyka Python. """ 
20   
21  import abc 
22   
23  TABULATOR = '    ' 
24 25 -class Line(object):
26 """ Generowanie linni kodu. """
27 - def __init__(self, text, tab):
28 self.text = text 29 self.tab = tab
30 - def get_text(self):
31 """ Pobranie tekstu kodu. """ 32 return self.text
33 - def set_tab(self, tab=0):
34 """ Ustawienie ilosci tabulacji w linni. """ 35 self.tab = tab
36
37 #pylint: disable=W0612 38 -class BaseRenderer(object):
39 """ Klasa bazowa obiektow generujacych kod. """ 40 __metaclass__ = abc.ABCMeta 41 @staticmethod
42 - def cout(text, tab, enter=True):
43 """ Zwraca fragment kodu z odpowiednim odstepem. """ 44 result = "" 45 for i in range(tab): 46 result += TABULATOR 47 result = result + text 48 if enter == True: 49 result = result + "\n" 50 return result
51 @abc.abstractmethod
52 - def render(self, tab=0):
53 """ Renderowanie elementu. """ 54 raise NotImplementedError()
55
56 -class DefRenderer(BaseRenderer):
57 """ Klasa obiektu renderujacego funkcje. """
58 - def __init__(self, name, args=''):
59 """ Metoda inicjalizujaca. """ 60 self.name = name 61 self.args = args 62 self.line = []
63 - def add_line(self, text, tab=0):
64 """ Dodanie linni kodu w funkcji. """ 65 self.line.append(DefRenderer.cout(text, tab, False))
66 - def render(self, tab=0):
67 """ Renderowanie funkcji. """ 68 result = DefRenderer.cout("def %s(%s):" % (self.name, self.args), tab) 69 i = 0 70 for once in self.line: 71 result = result + DefRenderer.cout(once, tab + 1) 72 i = i + 1 73 if i == 0: 74 result = result + DefRenderer.cout("pass", tab + 1) 75 result = result + "\n" 76 return result
77
78 -class ImportRenderer(BaseRenderer):
79 """ Klasa obiektu generujacego importy. """
80 - def __init__(self):
81 self.imports = []
82 - def add_import(self, imp, from_import=None):
83 """ Dodanie pojedynczego importu. """ 84 if from_import == None: 85 self.imports.append("import %s" % imp) 86 else: 87 self.imports.append("from %s import %s" % (from_import, imp))
88 - def render(self, tab=0):
89 """ Renderowanie importow. """ 90 result = '' 91 imp = set(self.imports) 92 for once in imp: 93 result = result + self.cout(once, tab) 94 return result
95