@@ -15,15 +15,17 @@ def normalize(this):
15
15
"""apply https://peps.python.org/pep-0503/#normalized-names"""
16
16
return re .sub (r"[-_.]+" , "-" , this ).lower ()
17
17
18
- def get_packages_ini_metadata ( name ):
18
+ def get_package_metadata ( database , name , gotoWWW = False , update = False , suggested_summary = None ):
19
19
"""Extract infos (description, url) from the local database"""
20
+ # Note: we could use the PyPI database but this has been written on
21
+ # machine which is not connected to the internet
20
22
# we store only normalized names now (PEP 503)
21
- database = "packages.ini"
22
23
db = cp .ConfigParser ()
24
+ filepath = Path (database ) if Path (database ).is_absolute () else Path (DATA_PATH ) / database
23
25
try :
24
- db .read_file (open (str (Path ( DATA_PATH ) / database ), encoding = 'utf-8' ))
26
+ db .read_file (open (str (filepath ), encoding = 'utf-8' ))
25
27
except :
26
- db .read_file (open (str (Path ( DATA_PATH ) / database )))
28
+ db .read_file (open (str (filepath )))
27
29
my_metadata = dict (
28
30
description = "" ,
29
31
url = "https://pypi.org/project/" + name ,
@@ -36,9 +38,33 @@ def get_packages_ini_metadata(name):
36
38
break
37
39
except (cp .NoSectionError , cp .NoOptionError ):
38
40
pass
39
- db_desc = my_metadata ["description" ]
41
+ db_desc = my_metadata .get ("description" )
42
+
43
+ if my_metadata .get ("description" ) == "" and suggested_summary :
44
+ # nothing in package.ini, we look in our installed packages
45
+ try :
46
+ my_metadata ["description" ] = (
47
+ suggested_summary + "\n "
48
+ ).splitlines ()[0 ]
49
+ except :
50
+ pass
51
+
52
+ if update == True and db_desc == "" and my_metadata ["description" ] != "" :
53
+ # we add new findings in our packgages.ini list, if it's required
54
+ try :
55
+ db [normalize (name )] = {}
56
+ db [normalize (name )]["description" ] = my_metadata ["description" ]
57
+ with open (str (Path (DATA_PATH ) / database ), "w" , encoding = 'UTF-8' ) as configfile :
58
+ db .write (configfile )
59
+ except :
60
+ pass
40
61
return my_metadata
41
62
63
+
64
+ def get_packages_ini_metadata (name ):
65
+ """Extract infos (description, url) from the local database"""
66
+ return get_package_metadata ("packages.ini" , name , False , update = False , suggested_summary = None )
67
+
42
68
class pipdata :
43
69
"""Wrapper around Distribution.discover() or pip inspect"""
44
70
0 commit comments