4444
4545# pep503 defines normalized package names: www.python.org/dev/peps/pep-0503
4646def normalize (name ):
47+ """ return normalized (unique) name of a package"""
4748 return re .sub (r"[-_.]+" , "-" , name ).lower ()
4849
4950def 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 ''
6667
6768def get_package_metadata (database , name , gotoWWW = False , update = False ):
6869 """Extract infos (description, url) from the local database"""
@@ -76,34 +77,33 @@ def get_package_metadata(database, name, gotoWWW=False, update=False):
7677 url = 'https://pypi.org/project/' + name ,
7778 )
7879 for key in my_metadata :
79- name1 = name .lower ()
8080 # 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 )):
8882 try :
8983 my_metadata [key ] = db .get (name2 , key )
9084 break
9185 except (cp .NoSectionError , cp .NoOptionError ):
9286 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
9591 try :
9692 my_metadata ['description' ]= (
9793 metadata (name )['Summary' ]+ '\n ' ).splitlines ()[0 ]
9894 except :
9995 pass
96+
10097 if my_metadata ['description' ] == '' and gotoWWW :
98+ # still nothing, try look on pypi
10199 the_official = get_official_description (name )
102100 if the_official != '' :
103101 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
105105 try :
106- db [normalize (name )]= {}
106+ db [normalize (name )] = {}
107107 db [normalize (name )]['description' ] = my_metadata ['description' ]
108108 with open (osp .join (DATA_PATH , database ), 'w' ) as configfile :
109109 db .write (configfile )
0 commit comments