44
44
45
45
# pep503 defines normalized package names: www.python.org/dev/peps/pep-0503
46
46
def normalize (name ):
47
+ """ return normalized (unique) name of a package"""
47
48
return re .sub (r"[-_.]+" , "-" , name ).lower ()
48
49
49
50
def get_official_description (name ):
50
- from winpython import utils
51
- dir_path = os . path . dirname ( sys . executable )
52
- this = normalize (name )
53
- this_len = len (this )
54
- pip_ask = ['pip' , 'search' , this , '--retries' , '0' ]
55
- if len (this )< 2 : # don't ask stupid things
56
- return ''
57
- try :
58
- # .run work when .popen fails when no internet
59
- pip_res = (utils .exec_run_cmd (pip_ask )+ '\n ' ).splitlines ()
60
- pip_filter = [l for l in pip_res if this + " (" ==
61
- normalize (l [:this_len ])+ l [this_len :this_len + 2 ]]
62
- pip_desc = (pip_filter [0 ][len (this )+ 1 :]).split (" - " , 1 )[1 ]
63
- return pip_desc .replace ("://" , " " )
64
- except :
65
- return ''
51
+ """Extract package Summary description from pypi.org"""
52
+ from winpython import utils
53
+ this = normalize (name )
54
+ this_len = len (this )
55
+ pip_ask = ['pip' , 'search' , this , '--retries' , '0' ]
56
+ if len (this )< 2 : # don't ask stupid things
57
+ return ''
58
+ try :
59
+ # .run work when .popen fails when no internet
60
+ pip_res = (utils .exec_run_cmd (pip_ask )+ '\n ' ).splitlines ()
61
+ pip_filter = [l for l in pip_res if this + " (" ==
62
+ normalize (l [:this_len ])+ l [this_len :this_len + 2 ]]
63
+ pip_desc = (pip_filter [0 ][len (this )+ 1 :]).split (" - " , 1 )[1 ]
64
+ return pip_desc .replace ("://" , " " )
65
+ except :
66
+ return ''
66
67
67
68
def get_package_metadata (database , name , gotoWWW = False , update = False ):
68
69
"""Extract infos (description, url) from the local database"""
@@ -76,34 +77,33 @@ def get_package_metadata(database, name, gotoWWW=False, update=False):
76
77
url = 'https://pypi.org/project/' + name ,
77
78
)
78
79
for key in my_metadata :
79
- name1 = name .lower ()
80
80
# wheel replace '-' per '_' in key
81
- for name2 in (
82
- name1 ,
83
- name1 .split ('-' )[0 ],
84
- name1 .replace ('-' , '_' ),
85
- '-' .join (name1 .split ('_' )),
86
- normalize (name ),
87
- ):
81
+ for name2 in (name , normalize (name )):
88
82
try :
89
83
my_metadata [key ] = db .get (name2 , key )
90
84
break
91
85
except (cp .NoSectionError , cp .NoOptionError ):
92
86
pass
93
- database_desc = my_metadata .get ('description' )
94
- if my_metadata .get ('description' ) == '' and metadata : # nothing in package.ini
87
+ db_desc = my_metadata .get ('description' )
88
+
89
+ if my_metadata .get ('description' ) == '' and metadata :
90
+ # nothing in package.ini, we look in our installed packages
95
91
try :
96
92
my_metadata ['description' ]= (
97
93
metadata (name )['Summary' ]+ '\n ' ).splitlines ()[0 ]
98
94
except :
99
95
pass
96
+
100
97
if my_metadata ['description' ] == '' and gotoWWW :
98
+ # still nothing, try look on pypi
101
99
the_official = get_official_description (name )
102
100
if the_official != '' :
103
101
my_metadata ['description' ] = the_official
104
- if update == True and database_desc == '' and my_metadata ['description' ] != '' :
102
+
103
+ if update == True and db_desc == '' and my_metadata ['description' ] != '' :
104
+ # we add new findings in our packgages.ini list, if it's required
105
105
try :
106
- db [normalize (name )]= {}
106
+ db [normalize (name )] = {}
107
107
db [normalize (name )]['description' ] = my_metadata ['description' ]
108
108
with open (osp .join (DATA_PATH , database ), 'w' ) as configfile :
109
109
db .write (configfile )
0 commit comments