Skip to content

Commit 856055d

Browse files
committed
Further improvements to unicode handling across all file operations
1 parent e8d3fd2 commit 856055d

File tree

18 files changed

+28
-28
lines changed

18 files changed

+28
-28
lines changed

src/programy/clients/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def parse_arguments(self, argument_parser):
105105

106106
def initiate_logging(self, arguments):
107107
if arguments.logging is not None:
108-
with open(arguments.logging, 'r+') as yml_data_file:
108+
with open(arguments.logging, 'r+', encoding="utf-8") as yml_data_file:
109109
logging_config = yaml.load(yml_data_file)
110110
logging.config.dictConfig(logging_config)
111111
if logging.getLogger().isEnabledFor(logging.INFO):

src/programy/clients/rest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def get_client_configuration(self):
3535

3636
def load_api_keys(self):
3737
if self.configuration.client_configuration.api_key_file is not None:
38-
with open(self.configuration.client_configuration.api_key_file, "r") as api_key_file:
38+
with open(self.configuration.client_configuration.api_key_file, "r", encoding="utf-8") as api_key_file:
3939
for api_key in api_key_file:
4040
self.api_keys.append(api_key.strip())
4141

src/programy/clients/twitter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def _get_last_message_ids(self):
219219
logging.debug("Reads messages ids from [%s]", self.configuration.client_configuration.storage_location)
220220
if os.path.exists(self.configuration.client_configuration.storage_location):
221221
try:
222-
with open(self.configuration.client_configuration.storage_location, "r") as idfile:
222+
with open(self.configuration.client_configuration.storage_location, "r", encoding="utf-8") as idfile:
223223
last_direct_message_id = int(idfile.readline().strip())
224224
last_status_id = int(idfile.readline().strip())
225225
except Exception as excep:
@@ -232,7 +232,7 @@ def _store_last_message_ids(self, last_direct_message_id, last_status_id):
232232
if logging.getLogger().isEnabledFor(logging.DEBUG):
233233
logging.debug("Writing messages ids to [%s]", self.configuration.client_configuration.storage_location)
234234
try:
235-
with open(self.configuration.client_configuration.storage_location, "w+") as idfile:
235+
with open(self.configuration.client_configuration.storage_location, "w+", encoding="utf-8") as idfile:
236236
idfile.write("%d\n"%last_direct_message_id)
237237
idfile.write("%d\n"%last_status_id)
238238
except Exception as excep:

src/programy/config/file/json_file.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def load_from_text(self, text, client_configuration, bot_root):
3636

3737
def load_from_file(self, filename, client_configuration, bot_root):
3838
configuration = ProgramyConfiguration(client_configuration)
39-
with open(filename, 'r+') as json_data_file:
39+
with open(filename, 'r+', encoding="utf-8") as json_data_file:
4040
self.json_data = json.load(json_data_file)
4141
configuration.load_config_data(self, bot_root)
4242
return configuration

src/programy/config/file/xml_file.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def load_from_text(self, text, client_configuration, bot_root):
3939

4040
def load_from_file(self, filename, client_configuration, bot_root):
4141
configuration = ProgramyConfiguration(client_configuration)
42-
with open(filename, 'r+') as xml_data_file:
42+
with open(filename, 'r+', encoding="utf-8") as xml_data_file:
4343
tree = ET.parse(xml_data_file, parser=LineNumberingParser())
4444
self.xml_data = tree.getroot()
4545
configuration.load_config_data(self, bot_root)

src/programy/config/file/yaml_file.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def load_from_text(self, text, client_configuration, bot_root):
3737

3838
def load_from_file(self, filename, client_configuration, bot_root):
3939
configuration = ProgramyConfiguration(client_configuration)
40-
with open(filename, 'r+') as yml_data_file:
40+
with open(filename, 'r+', encoding="utf-8") as yml_data_file:
4141
self.yaml_data = yaml.load(yml_data_file)
4242
configuration.load_config_data(self, bot_root)
4343
return configuration

src/programy/dialog.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ def save_conversation(self, conversation, clientid):
265265
if self._config._dir is not None:
266266
if os.path.exists(self._config._dir):
267267
filename = self._config._dir + os.sep + clientid + ".convo"
268-
with open(filename, "w+") as convo_file:
268+
with open(filename, "w+", encoding="utf-8") as convo_file:
269269
for name, value in conversation._properties.items():
270270
convo_file.write("%s:%s\n"%(name, value))
271271
convo_file.write("\n")
@@ -292,7 +292,7 @@ def load_conversation(self, conversation, clientid, restore_last_topic=False):
292292
if logging.getLogger().isEnabledFor(logging.DEBUG):
293293
logging.debug("Loading Conversation")
294294

295-
with open(filename, "r") as convo_file:
295+
with open(filename, "r", encoding="utf-8") as convo_file:
296296
for line in convo_file:
297297
if ':' in line:
298298
splits = line.split(":")

src/programy/mappings/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def process_line(self, line):
4444

4545
def load_from_filename(self, filename):
4646
count = 0
47-
with open(filename, "r") as data_file:
47+
with open(filename, "r", encoding="utf-8") as data_file:
4848
for line in data_file:
4949
if self.process_line(line):
5050
count += 1

src/programy/parser/factory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def load_nodes_config_from_file(self, filename=None):
7373
if filename is None or os.path.exists(filename) is False:
7474
filename = self.default_config_file()
7575

76-
with open(filename, "r") as node_file:
76+
with open(filename, "r", encoding="utf-8") as node_file:
7777
for line in node_file:
7878
line = line.strip()
7979
self.process_config_line(line)

src/programy/parser/template/nodes/learnf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def write_learnf_to_file(self, bot, clientid, category):
5555
logging.debug("Writing learnf to %s", learnf_path)
5656

5757
if os.path.isfile(learnf_path) is False:
58-
file = open(learnf_path, "w+")
58+
file = open(learnf_path, "w+", encoding="utf-8")
5959
file.write('<?xml version="1.0" encoding="UTF-8"?>\n')
6060
file.write('<aiml>\n')
6161
file.write('</aiml>\n')

src/programy/processors/processing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def load(self, filename, *args, **kw):
3030
if logging.getLogger().isEnabledFor(logging.DEBUG):
3131
logging.debug("Loading processors from file [%s]", filename)
3232
count = 0
33-
with open(filename, "r") as file:
33+
with open(filename, "r", encoding="utf-8") as file:
3434
for line in file:
3535
line = line.strip()
3636
if line:

src/programy/security/authorise/usergrouploader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def load_users_and_groups_from_text(self, text):
3030
return self.load_users_and_groups_from_yaml(yaml_data)
3131

3232
def load_users_and_groups_from_file(self, filename):
33-
with open(filename, 'r+') as yml_data_file:
33+
with open(filename, 'r+', encoding="utf-8") as yml_data_file:
3434
yaml_data = yaml.load(yml_data_file)
3535
return self.load_users_and_groups_from_yaml(yaml_data)
3636

src/programy/spelling/norvig.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def __init__(self, spelling_config=None):
2121

2222
if logging.getLogger().isEnabledFor(logging.INFO):
2323
logging.info("Loading spelling corpus [%s]", corpus_filename)
24-
self.words = Counter(self._all_words(open(corpus_filename).read()))
24+
self.words = Counter(self._all_words(open(corpus_filename, encoding="utf-8").read()))
2525
self.sum_of_words = sum(self.words.values())
2626

2727
def _all_words(self, text):

src/programy/utils/geo/geonames.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,20 @@ def _get_latlong_for_postcode_response(self, postcode):
6262
return json.loads(content.decode('utf8'))
6363

6464
def load_get_latlong_for_postcode_from_file(self, filename):
65-
with open(filename, "w+") as response_file:
65+
with open(filename, "w+", encoding="utf-8") as response_file:
6666
return json.load(response_file)
6767

6868
def store_get_latlong_for_postcode_to_file(self, postcode, filename):
6969
content = self._get_latlong_for_postcode_response(postcode)
70-
with open(filename, "w+") as response_file:
70+
with open(filename, "w+", encoding="utf-8") as response_file:
7171
json.dump(content, response_file, sort_keys=True, indent=2)
7272

7373
def get_latlong_for_postcode(self, postcode):
7474

7575
if GeoNamesApi.get_latlong_for_postcode_response_file is None:
7676
data = self._get_latlong_for_postcode_response(postcode)
7777
else:
78-
with open(GeoNamesApi.get_latlong_for_postcode_response_file, "r") as datafile:
78+
with open(GeoNamesApi.get_latlong_for_postcode_response_file, "r", encoding="utf-8") as datafile:
7979
data = json.load(datafile)
8080

8181
if 'postalCodes' not in data:

src/programy/utils/geo/google.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def get_latlong_for_location(self, location):
248248
else:
249249
if logging.getLogger().isEnabledFor(logging.DEBUG):
250250
logging.debug("get_latlong_for_location - using mock file")
251-
with open(self.response_file_for_get_latlong_for_location, "r") as response_file:
251+
with open(self.response_file_for_get_latlong_for_location, "r", encoding="utf-8") as response_file:
252252
response = json.load(response_file)
253253

254254
geodata = GoogelMapsResult()
@@ -257,7 +257,7 @@ def get_latlong_for_location(self, location):
257257

258258
def store_get_latlong_for_location_to_file(self, location, filename):
259259
response = self._get_latlong_for_location_response(location)
260-
with open(filename, "w+") as data_file:
260+
with open(filename, "w+", encoding="utf-8") as data_file:
261261
json.dump(response, data_file, sort_keys=True, indent=2)
262262

263263
##################
@@ -281,7 +281,7 @@ def get_distance_between_addresses(self, origin, destination, country="UK", mode
281281
else:
282282
if logging.getLogger().isEnabledFor(logging.DEBUG):
283283
logging.debug("get_distance_between_addresses - using mock file")
284-
with open(self.response_file_for_get_distance_between_addresses, "r") as response_file:
284+
with open(self.response_file_for_get_distance_between_addresses, "r", encoding="utf-8") as response_file:
285285
response = json.load(response_file)
286286

287287
if response['status'] == 'OK':
@@ -298,7 +298,7 @@ def get_distance_between_addresses(self, origin, destination, country="UK", mode
298298
def store_get_distance_between_addresses_as_file(self, origin, destination, filename, country="UK",
299299
mode="driving", units="imperial"):
300300
response = self._get_distance_between_addresses(origin, destination, country, mode, units)
301-
with open(filename, "w+") as data_file:
301+
with open(filename, "w+", encoding="utf-8") as data_file:
302302
json.dump(response, data_file, sort_keys=True, indent=2)
303303

304304
##################
@@ -322,7 +322,7 @@ def get_directions_between_addresses(self, origin, destination, country="UK", mo
322322
else:
323323
if logging.getLogger().isEnabledFor(logging.DEBUG):
324324
logging.debug("get_directions_between_addresses - using mock file")
325-
with open(self.response_file_for_get_directions_between_addresses, "r") as response_file:
325+
with open(self.response_file_for_get_directions_between_addresses, "r", encoding="utf-8") as response_file:
326326
response = json.load(response_file)
327327

328328
if response['status'] == 'OK':
@@ -339,5 +339,5 @@ def get_directions_between_addresses(self, origin, destination, country="UK", mo
339339
def store_get_directions_between_addresses_as_file(self, origin, destination, filename, country="UK",
340340
mode="driving", units="imperial"):
341341
response = self._get_directions_between_addresses_response(origin, destination, country, mode, units)
342-
with open(filename, "w+") as data_file:
342+
with open(filename, "w+", encoding="utf-8") as data_file:
343343
json.dump(response, data_file, sort_keys=True, indent=2)

src/programy/utils/license/keys.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def load_license_key_file(self, license_key_filename):
4646
try:
4747
if logging.getLogger().isEnabledFor(logging.INFO):
4848
logging.info("Loading license key file: [%s]", license_key_filename)
49-
with open(license_key_filename, "r") as license_file:
49+
with open(license_key_filename, "r", encoding="utf-8") as license_file:
5050
for line in license_file:
5151
self._process_license_key_line(line)
5252
except Exception:

src/programy/utils/newsapi/newsapi.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,12 +273,12 @@ def to_json(articles):
273273

274274
@staticmethod
275275
def json_to_file(filename, json_data):
276-
with open(filename, 'w+') as json_file:
276+
with open(filename, 'w+', encoding="utf-8") as json_file:
277277
json.dump(json_data, json_file)
278278

279279
@staticmethod
280280
def json_from_file(filename):
281-
with open(filename, 'r+') as json_file:
281+
with open(filename, 'r+', encoding="utf-8") as json_file:
282282
return json.load(json_file)
283283

284284
@staticmethod

src/programy/utils/weather/metoffice.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ def current_observation(self, lat, lon):
592592
return self.nearest_location_observation(lat, lon)
593593

594594
def write_datapoints_to_file(self, datapoints, filename):
595-
with open(filename, "w+") as data_file:
595+
with open(filename, "w+", encoding="utf-8") as data_file:
596596
json.dump(datapoints, data_file, sort_keys=True, indent=2)
597597

598598
def load_datapoints_from_file(self, filename):

0 commit comments

Comments
 (0)