open file with encoding (python 2/3 supported)
[rainbowstream.git] / rainbowstream / config.py
index 1245ca456371d6a9de4d8f0944d7368c6be6da5d..860646d78ca29f62375fac264246733a2b699b15 100644 (file)
@@ -2,6 +2,7 @@ import json
 import re
 import os
 import os.path
+from io import open
 from collections import OrderedDict
 
 # Regular expression for comments in config file
@@ -29,7 +30,7 @@ def load_config(filepath):
     """
     Load config from filepath
     """
-    with open(filepath) as f:
+    with open(filepath, encoding='utf-8') as f:
         content = ''.join(f.readlines())
         match = comment_re.search(content)
         while match:
@@ -42,15 +43,12 @@ def get_all_config():
     """
     Get all config
     """
-    path = os.environ.get(
-        'HOME',
-        os.environ.get(
-            'USERPROFILE',
-            '')) + os.sep + '.rainbow_config.json'
+    path = os.path.expanduser("~") + os.sep + '.rainbow_config.json'
     data = load_config(path)
     # Hard to set from prompt
     data.pop('ONLY_LIST', None)
     data.pop('IGNORE_LIST', None)
+    data.pop('FORMAT', None)
     return data
 
 
@@ -86,12 +84,12 @@ def set_config(key, value):
     elif value.lower() == 'false':
         value = False
     # Fix up
-    path = os.environ.get(
-        'HOME',
-        os.environ.get(
-            'USERPROFILE',
-            '')) + os.sep + '.rainbow_config.json'
-    data = load_config(path)
+    path = os.path.expanduser("~") + os.sep + '.rainbow_config.json'
+    data = {}
+    try:
+        data = load_config(path)
+    except:
+        pass
     # Update global config
     c[key] = value
     # Update config file
@@ -100,7 +98,7 @@ def set_config(key, value):
     else:
         data[key] = value
     # Save
-    with open(path, 'w') as out:
+    with open(path, 'w', encoding='utf-8') as out:
         json.dump(data, out, indent=4)
     os.system('chmod 777 ' + path)
 
@@ -109,11 +107,7 @@ def delete_config(key):
     """
     Delete a config key
     """
-    path = os.environ.get(
-        'HOME',
-        os.environ.get(
-            'USERPROFILE',
-            '')) + os.sep + '.rainbow_config.json'
+    path = os.path.expanduser("~") + os.sep + '.rainbow_config.json'
     data = load_config(path)
     # Drop key
     if key in data and key in c:
@@ -126,7 +120,7 @@ def delete_config(key):
     else:
         raise Exception('No such config key.')
     # Save
-    with open(path, 'w') as out:
+    with open(path, 'w', encoding='utf-8') as out:
         json.dump(data, out, indent=4)
     os.system('chmod 777 ' + path)
 
@@ -135,11 +129,7 @@ def reload_config():
     """
     Reload config
     """
-    rainbow_config = os.environ.get(
-        'HOME',
-        os.environ.get(
-            'USERPROFILE',
-            '')) + os.sep + '.rainbow_config.json'
+    rainbow_config = os.path.expanduser("~") + os.sep + '.rainbow_config.json'
     try:
         data = load_config(rainbow_config)
         for d in data:
@@ -162,17 +152,13 @@ def init_config():
     except:
         pass
     # Load user's config
-    rainbow_config = os.environ.get(
-        'HOME',
-        os.environ.get(
-            'USERPROFILE',
-            '')) + os.sep + '.rainbow_config.json'
+    rainbow_config = os.path.expanduser("~") + os.sep + '.rainbow_config.json'
     try:
         data = load_config(rainbow_config)
         for d in data:
             c[d] = data[d]
     except:
-        print('It seems that ~/.rainbow_config.json has wrong format :(')
+        pass
     # Load default theme
     theme_file = os.path.dirname(
         __file__) + '/colorset/' + c['THEME'] + '.json'