Be explicit about text file encoding 50/72850/1
authorGwenael Lambrouin <gwenael.lambrouin@orange.com>
Tue, 24 Aug 2021 09:42:26 +0000 (11:42 +0200)
committerGwenael Lambrouin <gwenael.lambrouin@orange.com>
Tue, 24 Aug 2021 12:41:58 +0000 (14:41 +0200)
Python PEP 597 (https://www.python.org/dev/peps/pep-0597) recommends to use an
explicit encoding for text files instead of the default locale encoding.
Pylint 2.10 adds a new checker named unspecified-encoding for that.

The present patch adds explicit utf-8 encoding to open() calls in nfvbench and
fixes pylint unspecified-encoding warnings.

Remark: this patch does not change nfvbench behaviour on systems where utf-8 is
the locale encoding, which is generally the case on Linux systems.

Change-Id: Ic4dfb37e1ea958452a0173f7630a68f0d95071ae
Signed-off-by: Gwenael Lambrouin <gwenael.lambrouin@orange.com>
nfvbench/chaining.py
nfvbench/config.py
nfvbench/credentials.py
nfvbench/nfvbench.py
nfvbench/traffic_gen/trex_gen.py
nfvbench/traffic_server.py
nfvbench/utils.py

index b983efb..5248d01 100644 (file)
@@ -500,7 +500,7 @@ class ChainVnf(object):
             vnf_gateway1_cidr = g1cidr
             vnf_gateway2_cidr = g2cidr
 
-        with open(BOOT_SCRIPT_PATHNAME, 'r') as boot_script:
+        with open(BOOT_SCRIPT_PATHNAME, 'r', encoding="utf-8") as boot_script:
             content = boot_script.read()
         vm_config = {
             'forwarder': config.vm_forwarder,
index c107958..8e77127 100644 (file)
@@ -23,7 +23,7 @@ def config_load(file_name, from_cfg=None, whitelist_keys=None):
     The config file content taking precedence in case of duplicate
     """
     try:
-        with open(file_name) as fileobj:
+        with open(file_name, encoding="utf-8") as fileobj:
             cfg = AttrDict(yaml.safe_load(fileobj))
     except IOError:
         raise Exception("Configuration file at '{}' was not found. Please use correct path "
index 7562896..7c48879 100644 (file)
@@ -138,7 +138,7 @@ class Credentials(object):
         if openrc_file:
             if isinstance(openrc_file, str):
                 if os.path.exists(openrc_file):
-                    with open(openrc_file) as rc_file:
+                    with open(openrc_file, encoding="utf-8") as rc_file:
                         self.__parse_openrc(rc_file)
                 else:
                     LOG.error('Error: rc file does not exist %s', openrc_file)
index 740dca2..891b2bb 100644 (file)
@@ -721,7 +721,7 @@ def main():
             sys.exit(0)
 
         if opts.summary:
-            with open(opts.summary) as json_data:
+            with open(opts.summary, encoding="utf-8") as json_data:
                 result = json.load(json_data)
                 if opts.user_label:
                     result['config']['user_label'] = opts.user_label
@@ -736,7 +736,7 @@ def main():
         # dump the contents of the trex log file
         if opts.show_trex_log:
             try:
-                with open('/tmp/trex.log') as trex_log_file:
+                with open('/tmp/trex.log', encoding="utf-8") as trex_log_file:
                     print(trex_log_file.read(), end="")
             except FileNotFoundError:
                 print("No TRex log file found!")
index 41768b1..dff72ac 100644 (file)
@@ -761,7 +761,7 @@ class TRex(AbstractTrafficGenerator):
         after = None
         last = None
         try:
-            with open('/tmp/trex.log', 'r') as trex_log:
+            with open('/tmp/trex.log', 'r', encoding="utf-8") as trex_log:
                 for _line in trex_log:
                     line = _line.strip()
                     if line.startswith('Usage:'):
@@ -912,7 +912,7 @@ class TRex(AbstractTrafficGenerator):
                         break
                     last_size = size
                     time.sleep(1)
-                with open(logpath, 'r') as f:
+                with open(logpath, 'r', encoding="utf-8") as f:
                     message = f.read()
             else:
                 message = e.message
index 2c85286..5111b32 100644 (file)
@@ -78,7 +78,7 @@ class TRexTrafficServer(TrafficServer):
     def __load_config(self, filename):
         result = {}
         if os.path.exists(filename):
-            with open(filename, 'r') as stream:
+            with open(filename, 'r', encoding="utf-8") as stream:
                 try:
                     result = yaml.safe_load(stream)
                 except yaml.YAMLError as exc:
@@ -90,7 +90,7 @@ class TRexTrafficServer(TrafficServer):
         yaml.safe_load(result)
         if os.path.exists(filename):
             os.remove(filename)
-        with open(filename, 'w') as f:
+        with open(filename, 'w', encoding="utf-8") as f:
             f.write(result)
         return filename
 
index 512422d..07a38cb 100644 (file)
@@ -66,7 +66,7 @@ def save_json_result(result, json_file, std_json_path, service_chain, service_ch
     if filepaths:
         for file_path in filepaths:
             LOG.info('Saving results in json file: %s...', file_path)
-            with open(file_path, 'w') as jfp:
+            with open(file_path, 'w', encoding="utf-8") as jfp:
                 json.dump(result,
                           jfp,
                           indent=4,