-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec.py
executable file
·135 lines (107 loc) · 3.38 KB
/
exec.py
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
#!/usr/bin/python
# -*- coding: utf8
# Carlos Celis Flen-Bers
import sys, getopt, random
from itertools import islice
from functions import *
help_msg = "Exec\n" + sys.argv[0] + " <package>"
def usage():
print help_msg
def about():
print "Programa de ejecución"
def salida():
about()
sys.exit()
def error_msg(error_status, str_search, count):
error_msg = {}
error_msg[0] = "No problem! ;-)"
error_msg[1] = "No se encontró el paquete: {} pero se encontraron {} parecidos".format(str_search, count)
error_msg[2] = "No se encontró el paquete: " + str_search + " ni nada parecido"
return error_msg[error_status]
def pysearch(pkg='pypi'):
# En representación del bot
res = xmlrpcsearch(pkg)
for i in res:
print ' -' + i['name'] + ': ' + i['version'] + '\n ' + i['summary']
def print_info_pkg(name, url):
print " * {}\n URL= {}{}".format(name, pypi_base_url, url)
def pypi(pkg='pypi'):
# En representación del bot
res_search = pkg in packages
if res_search == True:
res = pkg_info(pkg)
print ' ' + res['name'] + ': ' + res['version']
print ' ' + res['summary']
print ' Visite {} para ver paquete pypi'.format(res['package_url'])
print ' Visite {} para más info'.format(res['home_page'])
else:
# parecida
res = simple_search(pkg)
res_len = len(res)
if res_len > 0:
error_status = 1
print error_msg(error_status, pkg, res_len)
for p in res:
print_info_pkg(p, res[p])
else:
# TODO busqueda como pip search
error_status = 2
print error_msg(error_status, pkg, res_len)
def take(n, iterable):
#https://docs.python.org/2/library/itertools.html#recipes
"Return first n items of the iterable as a list"
return list(islice(iterable, n))
def too_many_packages(results):
response = '' #"{} Resultados:\nAlgunos de estos son:\n".format(count)
keys = results.keys()
random.shuffle(keys)
truncate_res = take(MAX_PACKAGE_RETURN, results.iteritems())
return truncate_res
def simple(pkg='pypi'):
# En representación del bot
error_status = 0 # No error
res = simple_search(pkg)
res_len = len(res)
if res_len == 0:
error_status = 1
print error_msg(error_status, pkg)
elif res_len <= MAX_PACKAGE_RETURN:
print "Su búsqueda por {} arrojó {} resultados:".format(pkg, res_len)
for p in res:
print_info_pkg(p, res[p])
else:
print "{} Resultados:\nAlgunos de estos son:\n".format(res_len)
truncate_res = too_many_packages(res)
for p in range(MAX_PACKAGE_RETURN):
print_info_pkg(truncate_res[p][0], truncate_res[p][1])
def main(argv):
package = ''
try:
opts, args = getopt.getopt(argv,"hp:i:s:",["help", "about", "pysearch=", "pypi=", "simple="])
except getopt.GetoptError as err:
print str(err)
usage()
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
sys.exit()
elif opt == '--about':
salida()
elif opt in ("-p", "--pysearch"):
print "pysearch: " + repr(arg)
if arg == '' or arg == None:
salida()
pysearch(arg)
elif opt in ("-i", "--pypi"):
print "pypi: " + repr(arg)
if arg == '' or arg == None:
salida()
pypi(arg)
elif opt in ("-s", "--simple"):
print "Simple: " + repr(arg)
if arg == '' or arg == None:
salida()
simple(arg)
if __name__ == "__main__":
main(sys.argv[1:])