1
1
#!/usr/bin/python3
2
2
from neam .python .neam import main as run_neam
3
+ from neam .python .util import unverified_https_context
3
4
4
5
import nltk
5
6
6
- from contextlib import contextmanager
7
- import ssl
8
7
import sys
9
8
10
9
NEEDED_COLLECTIONS = {
@@ -18,28 +17,21 @@ def main():
18
17
Makes sure the NLTK dependencies have been downloaded, then passes off to
19
18
the real NEAM script
20
19
"""
21
- ensure_collections_exist (NEEDED_COLLECTIONS )
20
+ ensure_nltk_collections_exist (NEEDED_COLLECTIONS )
22
21
run_neam ()
23
22
24
23
25
- def ensure_collections_exist (collections ):
24
+ def ensure_nltk_collections_exist (collections ):
26
25
"""
27
- Ensures that all required collections exist on the system, and downloads
28
- them if they are not present
26
+ Ensures that all required NLTK collections exist on the system, and
27
+ downloads them if they are not present
29
28
30
29
:param collections: The collections that must be present - a dict of
31
30
folder/name pairs, corresponding to where the
32
31
collections should exist in the filesystem
33
32
:see: http://www.nltk.org/_modules/nltk/downloader.html
34
33
"""
35
- missing_collections = []
36
-
37
- for collection_type , collection_names in collections .items ():
38
- for collection in collection_names :
39
- try :
40
- nltk .data .find (f"{ collection_type } /{ collection } " )
41
- except LookupError :
42
- missing_collections .append (collection )
34
+ missing_collections = find_missing_nltk_collections (collections )
43
35
44
36
if missing_collections :
45
37
print ("Downloading NLTK models..." , file = sys .stderr )
@@ -49,23 +41,27 @@ def ensure_collections_exist(collections):
49
41
nltk .download (collection )
50
42
51
43
52
- @contextmanager
53
- def unverified_https_context ():
44
+ def find_missing_nltk_collections (collections ):
54
45
"""
55
- Turns off the SSL check for the duration of the function
46
+ Checks if a dict of NLTK collections exist on the system and returns a
47
+ list of collections that are missing
48
+
49
+ :param collections: The collections that must be present - a dict of
50
+ folder/name pairs, corresponding to where the
51
+ collections should exist in the filesystem
52
+ :return: A list of collections not present on the system
56
53
"""
57
- try :
58
- create_unverified_https_context = ssl ._create_unverified_context
59
- create_default_https_context = ssl .create_default_https_context
60
- except AttributeError :
61
- pass
62
- else :
63
- ssl ._create_default_https_context = create_unverified_https_context
54
+ missing_collections = []
55
+
56
+ for collection_type , collection_names in collections .items ():
57
+ for collection in collection_names :
58
+ try :
59
+ nltk .data .find (f"{ collection_type } /{ collection } " )
60
+ except LookupError :
61
+ missing_collections .append (collection )
64
62
65
- try :
66
- yield
67
- finally :
68
- ssl ._create_default_https_context = create_default_https_context
63
+ return missing_collections
69
64
70
65
71
- main ()
66
+ if __name__ == '__main__' :
67
+ main ()
0 commit comments