@@ -35,7 +35,9 @@ class LanguageTool:
3535
3636 def __init__ (self , language = None , motherTongue = None , remote_server = None , newSpellings = None ):
3737 if newSpellings :
38- self ._register_spellings (newSpellings )
38+ self .new_spellings = newSpellings
39+ self .new_spellings_only_current_session = False
40+ self ._register_spellings (self .new_spellings )
3941 if remote_server is not None :
4042 self ._remote = True
4143 self ._url = parse_url (remote_server )
@@ -57,14 +59,23 @@ def __init__(self, language=None, motherTongue=None, remote_server=None, newSpel
5759 self .enabled_rules_only = False
5860 self ._instances [id (self )] = self
5961
60- def __del__ (self ):
61- if not self ._instances and self ._server_is_alive ():
62- self ._terminate_server ()
62+ def __enter__ (self ):
63+ return self
64+
65+ def __exit__ (self , exc_type , exc_val , exc_tb ):
66+ self .close ()
6367
6468 def __repr__ (self ):
6569 return '{}(language={!r}, motherTongue={!r})' .format (
6670 self .__class__ .__name__ , self .language , self .motherTongue )
6771
72+ def close (self ):
73+ if not self ._instances and self ._server_is_alive ():
74+ self ._terminate_server ()
75+ if self .new_spellings_only_current_session :
76+ self .new_spellings_only_current_session = False
77+ self ._unregister_spellings ()
78+
6879 @property
6980 def language (self ):
7081 """The language to be used."""
@@ -126,14 +137,33 @@ def disable_spellchecking(self):
126137 """Disable spell-checking rules."""
127138 self .disabled_categories .update (self ._spell_checking_categories )
128139
129- def _register_spellings (self , spellings ):
140+ @staticmethod
141+ def _get_valid_spelling_file_path () -> str :
130142 library_path = get_language_tool_directory ()
131143 spelling_file_path = os .path .join (library_path , "org/languagetool/resource/en/hunspell/spelling.txt" )
132144 if not os .path .exists (spelling_file_path ):
133- raise FileNotFoundError ("Failed to find the spellings file at {}\n Please file an issue at https://github.com/jxmorris12/language_tool_python" .format (spelling_file_path ))
145+ raise FileNotFoundError ("Failed to find the spellings file at {}\n Please file an issue at "
146+ "https://github.com/jxmorris12/language_tool_python/issues"
147+ .format (spelling_file_path ))
148+ return spelling_file_path
149+
150+ def _register_spellings (self , spellings ):
151+ spelling_file_path = self ._get_valid_spelling_file_path ()
134152 with open (spelling_file_path , "a+" ) as spellings_file :
135153 spellings_file .write ("\n " + "\n " .join ([word for word in spellings ]))
136- print ("Updated the spellings at {}" .format (spelling_file_path ))
154+ print ("Registered new spellings at {}" .format (spelling_file_path ))
155+
156+ def _unregister_spellings (self ):
157+ spelling_file_path = self ._get_valid_spelling_file_path ()
158+ with open (spelling_file_path , 'r+' ) as spellings_file :
159+ spellings_file .seek (0 , os .SEEK_END )
160+ for _ in range (len (self .new_spellings )):
161+ while spellings_file .read (1 ) != '\n ' :
162+ spellings_file .seek (spellings_file .tell () - 2 , os .SEEK_SET )
163+ spellings_file .seek (spellings_file .tell () - 2 , os .SEEK_SET )
164+ spellings_file .seek (spellings_file .tell () + 1 , os .SEEK_SET )
165+ spellings_file .truncate ()
166+ print ("Unregistered new spellings at {}" .format (spelling_file_path ))
137167
138168 def _get_languages (self ) -> set :
139169 """Get supported languages (by querying the server)."""
0 commit comments