Commits

Chris Mutel committed 43c4ff1

Improved compatibility for filename encodings

Comments (0)

Files changed (5)

bw2data/_config.py

 # -*- coding: utf-8 -*-
+import codecs
+import json
 import os
-import json
+import platform
 import tempfile
 import warnings
 
     """
     version = 0.1
     basic_directories = ("processed", "intermediate", "backups", "logs")
+    _windows = platform.system == "Windows"
 
     # TODO: Create directory if needed (and basic dirs)
 
                               u"\tSet a writeable directory!\n"
                               u"\tCurrent data directory is:\n"
                               u"\t%s" % self.dir, UserWarning
-                    )
+                              )
             # Has to come here, because web interface wants
             # to open a log ASAP
             self.create_basic_directories()
 
     def save_preferences(self):
         """Serialize preferences to disk."""
-        with open(os.path.join(self.dir, "preferences.json"), "w") as f:
+        with open(os.path.join(
+                self.dir,
+                "preferences.json"), "w") as f:
             json.dump(self.p, f, indent=2)
 
     def get_home_directory(self, path=None):
             return envvar
         for filename in (".brightway2path", "brightway2path.txt"):
             try:
-                candidate = open(os.path.join(user_dir, filename)).readline().strip()
+                candidate = codecs.open(
+                    os.path.join(user_dir, filename),
+                    encoding='utf-8'
+                ).readline().strip()
+                # config.dir should always be a bytestring, not unicode
+                if self._windows:
+                    candidate = candidate.encode('utf-16')
+                else:
+                    candidate = candidate.encode('utf-8')
                 assert os.path.exists(candidate)
                 self._dir_from = filename
                 return candidate
                 os.mkdir(os.path.join(self.dir, name))
 
     def _get_dir(self):
+        # This is a string in the correct encoding for the filesystem
         return self._dir
 
     def _set_dir(self, d):
 
     dir = property(_get_dir, _set_dir)
 
+    @property
+    def udir(self):
+        """Return `dir` in Unicode"""
+        return self.dir.decode('utf-8')
+
 
 config = Config()

bw2data/database.py

             Filepath of backup.
 
         """
-        filepath = os.path.join(config.request_dir("backups"), self.filename() + \
-            ".%s.backup" % int(time()))
+        filepath = os.path.join(
+            config.request_dir("backups"),
+            self.filename() + ".%s.backup" % int(time())
+        )
         with open(filepath, "wb") as f:
             pickle.dump(self.load(), f, protocol=pickle.HIGHEST_PROTOCOL)
         return filepath
             "depends": depends,
             "number": num_processes,
             "version": version or 0
-            }
+        }
 
     def deregister(self):
         """Remove a database from the metadata store. Does not delete any files."""
             Filename (not path)
 
         """
-        return "%s.%i.pickle" % (self.database,
-            version or self.version)
+        return "%s.%i.pickle" % (
+            self.database,
+            version or self.version
+        )
 
     def write(self, data):
         """Serialize data to disk.
         mapping.add(data.keys())
         for ds in data.values():
             ds["unit"] = normalize_units(ds["unit"])
-        geomapping.add([x["location"] for x in data.values() if \
-            x.get("location", False)])
+        geomapping.add([x["location"] for x in data.values() if
+                       x.get("location", False)])
         if config.p.get("use_cache", False) and self.database in config.cache:
             config.cache[self.database] = data
         filepath = os.path.join(config.dir, "intermediate", self.filename())
         """
         if self.database not in databases:
             raise UnknownObject("This database is not yet registered")
-        if version == None and config.p.get("use_cache", False) and \
+        if version is None and config.p.get("use_cache", False) and \
                 self.database in config.cache:
             return config.cache[self.database]
         try:
-            data = pickle.load(open(os.path.join(config.dir, "intermediate",
-                self.filename(version)), "rb"))
-            if version == None and config.p.get("use_cache", False):
+            data = pickle.load(open(os.path.join(
+                config.dir,
+                "intermediate",
+                self.filename(version)
+            ), "rb"))
+            if version is None and config.p.get("use_cache", False):
                 config.cache[self.database] = data
             return data
         except OSError:
         data = self.load(version)
         num_exchanges = sum([len(obj["exchanges"]) for obj in data.values()])
         assert data
-        dtype = [('uncertainty_type', np.uint8),
+        dtype = [
+            ('uncertainty_type', np.uint8),
             ('input', np.uint32),
             ('output', np.uint32),
             ('geo', np.uint32),
             ('sigma', np.float32),
             ('minimum', np.float32),
             ('maximum', np.float32),
-            ('negative', np.bool)]
+            ('negative', np.bool)
+        ]
         arr = np.zeros((num_exchanges + len(data), ), dtype=dtype)
         arr['minimum'] = arr['maximum'] = arr['sigma'] = np.NaN
         count = 0
         for key in sorted(data.keys(), key=lambda x: x[1]):
             production_found = False
-            for exc in sorted(data[key]["exchanges"],
+            for exc in sorted(
+                    data[key]["exchanges"],
                     key=lambda x: x["input"][1]):
                 if key == exc["input"]:
                     production_found = True
                     exc.get("minimum", np.NaN),
                     exc.get("maximum", np.NaN),
                     exc["amount"] < 0
-                    )
+                )
                 count += 1
             if not production_found and data[key]["type"] == "process":
                 # Add amount produced for each process (default 1)
         # The array is too big, because it can include a default production
         # amount for each activity. Trim to actual size.
         arr = arr[:count]
-        filepath = os.path.join(config.dir, "processed", "%s.pickle" % \
-            self.database)
+        filepath = os.path.join(
+            config.dir,
+            "processed",
+            "%s.pickle" % self.database
+        )
         with open(filepath, "wb") as f:
             pickle.dump(arr, f, protocol=pickle.HIGHEST_PROTOCOL)
 

bw2data/method.py

             * *name* (tuple, optional): Name of the new method.
 
         """
-        name = tuple(name) or self.method[:-1] + ("Copy of " + \
+        name = tuple(name) or self.method[:-1] + ("Copy of " +
             self.method[-1],)
         new_method = Method(name)
         metadata = copy(methods[self.method])
             "unit": unit,
             "description": description,
             "num_cfs": num_cfs
-            }
+        }
 
     def deregister(self):
         """Remove a method from the metadata store. Does not delete any files."""
             raise UnknownObject("This database is not yet registered")
         mapping.add(set([x[0] for x in data]))
         geomapping.add(set([x[2] for x in data]))
-        filepath = os.path.join(config.dir, "intermediate",
-            "%s.pickle" % self.get_abbreviation())
+        filepath = os.path.join(
+            config.dir,
+            "intermediate",
+            "%s.pickle" % self.get_abbreviation()
+        )
         with open(filepath, "wb") as f:
             pickle.dump(data, f, protocol=pickle.HIGHEST_PROTOCOL)
 
 
         """
         try:
-            return pickle.load(open(os.path.join(config.dir, "intermediate",
-                "%s.pickle" % self.get_abbreviation()), "rb"))
+            return pickle.load(open(os.path.join(
+                config.dir,
+                "intermediate",
+                "%s.pickle" % self.get_abbreviation()
+            ), "rb"))
         except OSError:
             raise MissingIntermediateData("Can't load intermediate data")
 
         Although it is not standard to provide uncertainty distributions for impact assessment methods, the structured array includes uncertainty fields.
 
         """
-        data = pickle.load(open(os.path.join(config.dir, "intermediate",
-            "%s.pickle" % self.get_abbreviation()), "rb"))
+        data = pickle.load(open(os.path.join(
+            config.dir,
+            "intermediate",
+            "%s.pickle" % self.get_abbreviation()
+        ), "rb"))
         assert data
-        dtype = [('uncertainty_type', np.uint8),
+        dtype = [
+            ('uncertainty_type', np.uint8),
             ('flow', np.uint32),
             ('index', np.uint32),
             ('geo', np.uint32),
             ('sigma', np.float32),
             ('minimum', np.float32),
             ('maximum', np.float32),
-            ('negative', np.bool)]
+            ('negative', np.bool)
+        ]
         arr = np.zeros((len(data), ), dtype=dtype)
         arr['minimum'] = arr['maximum'] = arr['sigma'] = np.NaN
         for i, (key, value, geo) in enumerate(data):
                 np.NaN,
                 np.NaN,
                 False
-                )
-        filepath = os.path.join(config.dir, "processed", "%s.pickle" % \
-            self.get_abbreviation())
+            )
+        filepath = os.path.join(config.dir, "processed", "%s.pickle" %
+                                self.get_abbreviation())
         with open(filepath, "wb") as f:
             pickle.dump(arr, f, protocol=pickle.HIGHEST_PROTOCOL)
 # -*- coding: utf-8 -*-
 from . import config, reset_meta
+import codecs
 import hashlib
 import os
-import platform
 import random
 import re
 import requests
     "production": 0,
     "technosphere": 1,
     "biosphere": 2,
-    }
+}
 
 DOWNLOAD_URL = "http://brightwaylca.org/data/"
 
 
 def random_string(length):
     return ''.join(random.choice(string.letters + string.digits
-        ) for i in xrange(length))
+                                 ) for i in xrange(length))
 
 
 def combine_methods(name, *ms):
         for key, amount in Method(m).load().iteritems():
             data[key] = data.get(key, 0) + amount
     meta = {
-        "description": "Combination of the following methods: " + \
+        "description": "Combination of the following methods: " +
             ", ".join([str(x) for x in ms]),
         "num_cfs": len(data),
         "unit": list(units)[0] if len(units) == 1 else "Unknown"
         os.mkdir(dirpath)
 
     user_dir = os.path.expanduser("~")
-    if platform.system == "Windows":
-        filename = "brightway2path.txt"
-    else:
-        filename = ".brightway2path"
-    with open(os.path.join(user_dir, filename), "w") as f:
+    filename = "brightway2path.txt" if config._windows else ".brightway2path"
+    with codecs.open(
+            os.path.join(user_dir, filename),
+            "w",
+            encoding="utf-8") as f:
         f.write(dirpath)
 
     config.reset()
 
 setup(
   name='bw2data',
-  version="0.9.0-alpha2",
+  version="0.9.0-alpha3",
   packages=packages,
   author="Chris Mutel",
   author_email="cmutel@gmail.com",