Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/gtkintrospection.py
blob: 4003762b0793e58d1468a1f361e9fbd4fa8bca8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#   gtkintrospection.py por:
#   Flavio Danesse <fdanesse@activitycentral.com>
#   ActivityCentran

# 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.
#
# 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import os
import sys
import shelve

### Obtener los datos para hacer autocompletado.
path = os.path.join(sys.argv[1])

archivo = shelve.open(path)
lista = archivo["Lista"]
archivo.close()

imports = lista[:-1]
linea_activa = lista[-1]

### Hacer importaciones previas.
modulos = {}

def append_modulo(name):
    """
    Importa un módulo según su nombre y lo
    almacena importado.
    """
    
    # FIXME: Requiere analisis con mayor detenimiento.
    try:
        if not modulos.get(name, ""):
            modulos[name] = __import__(name)
            
    except:
        arch = open("/tmp/log", "w")
        arch.write(name)
        arch.close()
    
def append_modulo_to_prev(name, prev):
    """
    Importa un módulo que se encuentra dentro de
    un paquete, según su nombre y el nombre
    del paquete que lo contiene y lo almacena importado.
    """
    
    # FIXME: Requiere analisis con mayor detenimiento.
    try:
        if not modulos.get(prev, ""):
            append_modulo(prev)
            
        modulos[name] = modulos[prev].__getattribute__(name)
        
    except:
        pass
    
def append_modulo_to_prev_for_path(path):
    """
    Importa los módulos necesarios segun path y lo almacena.
    """
    
    # FIXME: Requiere analisis con mayor detenimiento.
    try:
        # FIXME: Esta función Falla con tipos from modulo1.modulo2.modulo3 import modulo4
        items = path.split(".")

        contador = 0
        prev = items[0]
        
        for item in items:
            contador += 1
            
            if not contador == len(items):
                dos = items[contador]
                prev = "%s.%s" % (prev, items[contador])
                
                mod = __import__(prev).__dict__[dos]
                
                if not modulos.get(prev, ""):
                    modulos[prev] = mod
                    
    except:
        pass
    
for im in imports:
    
    ### Caso 1: import os
    ### Caso Especial: import os, sys, ...
    if not "from " in im and not " *" in im and not " as" in im:
        temp_list = im.split()[1:]          # quitando "import" y separando los módulos.
        
        for item in temp_list:
            name = item.replace(",", "")    # quitando ",".
            append_modulo(name)             # importar y almacenar.
            
    ### Caso 1: import os
    ### Caso Especial: from os import *
    elif "from " in im and " *" in im and not " as" in im:
        pass
    
    ### Caso 2: from os import path
    ### Caso Especial: from os import path, chmod, ...
    elif "from " in im and not " *" in im and not "." in im and not " as" in im:
        temp_list = im.split()
        prev = temp_list[1]
        temp_list = temp_list[3:]
        
        for item in temp_list:
            name = item.replace(",", "")
            append_modulo_to_prev(name, prev)
            
    ### Caso 2: from os import path
    ### Caso Especial: from os import *
    elif "from " in im and " *" in im and not "." in im and not " as" in im:
        pass # http://stackoverflow.com/questions/2916374/how-to-import-with-import
        
    ### caso 3: from Coso.Ventana import JAMediaPlayer
    ### Caso Especial: from Coso.Ventana.Otro import JAMediaPlayer, OtraCosa, ...
    elif "from " in im and not " *" in im and "." in im and not " as" in im:
        temp_list = im.split()
        prev = temp_list[1]
        mod_temp_list = temp_list[3:]
        
        if len(prev.split(".")) < 3: # FIXME la funcion falla para caso especial
            ### Caso Especial: módulos de pygi.
            if "gi.repository" in prev:
                name = mod_temp_list[0]
                mod = __import__("%s.%s" % (prev, name))
                modulos[name] = mod.importer.modules.get(name)
                
            else:
                append_modulo_to_prev_for_path(prev)
                
                for item in mod_temp_list:
                    name = item.replace(",", "")
                    append_modulo_to_prev(name, prev)

### Importar el o los modulos sobre los que se está haciendo auto completado.
lista = []

# FIXME: Requiere análisis de casos especiales.
if len(linea_activa) == 1:
    name = linea_activa[0]
    #append_modulo(name)

    lista = dir(modulos[name])
    #print dir(modulos[name])
    
'''
for key in modulos.keys():
    print key, modulos[key]'''

### Guardar la lista para autocompletar.
path = os.path.join("/tmp", "shelveout")

archivo = shelve.open(path)
archivo["Lista"] = lista
archivo.close()

print path