summaryrefslogtreecommitdiff
path: root/reconfigure/configs/base.py
diff options
context:
space:
mode:
authorAndrew Shadura <andrew@shadura.me>2015-08-20 15:58:27 +0200
committerAndrew Shadura <andrew@shadura.me>2015-08-20 15:58:27 +0200
commit25d6c405aff4167e801d0a4995083a56160b969e (patch)
tree070a400e9627b5b392dc3b57be4f889646ec2ea9 /reconfigure/configs/base.py
parentff1408420159488a106492ccd11dd234967029b6 (diff)
Imported Upstream version 0.1.39
Diffstat (limited to 'reconfigure/configs/base.py')
-rw-r--r--reconfigure/configs/base.py18
1 files changed, 11 insertions, 7 deletions
diff --git a/reconfigure/configs/base.py b/reconfigure/configs/base.py
index 0753246..5d0bb61 100644
--- a/reconfigure/configs/base.py
+++ b/reconfigure/configs/base.py
@@ -35,12 +35,13 @@ class Reconfig (object):
if self.origin:
self.content = open(self.origin, 'r').read()
- try:
- self.content = self.content.decode('utf8')
- self.encoding = 'utf8'
- except UnicodeDecodeError:
- self.encoding = chardet.detect(self.content)['encoding']
- self.content = self.content.decode(self.encoding)
+ self.encoding = 'utf8'
+ if hasattr(self.content, 'decode'): # str (2) or bytes (3)
+ try:
+ self.content = self.content.decode('utf8')
+ except (UnicodeDecodeError, AttributeError):
+ self.encoding = chardet.detect(self.content)['encoding']
+ self.content = self.content.decode(self.encoding)
self.nodetree = self.parser.parse(self.content)
if self.includer is not None:
@@ -63,7 +64,10 @@ class Reconfig (object):
result = {}
for k in nodetree:
- result[k or self.origin] = self.parser.stringify(nodetree[k]).encode(self.encoding)
+ v = self.parser.stringify(nodetree[k])
+ if self.encoding != 'utf8':
+ v = v.encode(self.encoding)
+ result[k or self.origin] = v
if self.origin is not None:
for k in result: