1+ from typing import Dict
2+
13import atexit
24import http .client
35import json
6+ import os
47import re
8+ import requests
59import socket
610import threading
711import urllib .parse
812from weakref import WeakValueDictionary
13+
14+ from .backports import subprocess
915from .download_lt import download_lt
1016from .language_tag import LanguageTag
1117from .match import Match
12- from .utils import *
18+ from .utils import (
19+ correct ,
20+ parse_url , get_locale_language , get_language_tool_directory , get_server_cmd ,
21+ FAILSAFE_LANGUAGE , startupinfo ,
22+ LanguageToolError , ServerError , JavaError , PathError )
1323
1424
1525DEBUG_MODE = False
@@ -106,12 +116,12 @@ def _spell_checking_categories(self):
106116 def check (self , text : str ) -> [Match ]:
107117 """Match text against enabled rules."""
108118 url = urllib .parse .urljoin (self ._url , 'check' )
109- response = self ._query_server (url , self ._encode (text ))
119+ response = self ._query_server (url , self ._create_params (text ))
110120 matches = response ['matches' ]
111121 return [Match (match ) for match in matches ]
112122
113- def _encode (self , text ) :
114- params = {'language' : self .language , 'text' : text . encode ( 'utf-8' ) }
123+ def _create_params (self , text : str ) -> Dict [ str , str ] :
124+ params = {'language' : str ( self .language ) , 'text' : text }
115125 if self .motherTongue is not None :
116126 params ['motherTongue' ] = self .motherTongue
117127 if self .disabled_rules :
@@ -124,7 +134,8 @@ def _encode(self, text):
124134 params ['disabledCategories' ] = ',' .join (self .disabled_categories )
125135 if self .enabled_categories :
126136 params ['enabledCategories' ] = ',' .join (self .enabled_categories )
127- return urllib .parse .urlencode (params ).encode ()
137+ # return urllib.parse.urlencode(params).encode()
138+ return params
128139
129140 def correct (self , text : str ) -> str :
130141 """Automatically apply suggestions to the text."""
@@ -187,18 +198,18 @@ def _update_remote_server_config(self, url):
187198 self ._url = url
188199 self ._remote = True
189200
190- def _query_server (self , url , data = None , num_tries = 2 ):
201+ def _query_server (self , url , params = None , num_tries = 2 ):
191202 if DEBUG_MODE :
192- print ('_query_server url:' , url , 'data :' , data )
203+ print ('_query_server url:' , url , 'params :' , params )
193204 for n in range (num_tries ):
194205 try :
195- with urlopen (url , data , self ._TIMEOUT ) as f :
196- raw_data = f .read ().decode ('utf-8' )
206+ with requests .get (url , params = params , timeout = self ._TIMEOUT ) as response :
197207 try :
198- return json . loads ( raw_data )
208+ return response . json ( )
199209 except json .decoder .JSONDecodeError as e :
200- print ('URL {url} and data {data} returned invalid JSON response:' )
201- print (raw_data )
210+ print (f'URL { url } and params { params } returned invalid JSON response:' )
211+ print (response )
212+ print (response .content )
202213 raise e
203214 except (IOError , http .client .HTTPException ) as e :
204215 if self ._remote is False :
0 commit comments