From e6c88107426c589157f61292d67bacc76320fb7b Mon Sep 17 00:00:00 2001 From: Marian Sigler Date: Mon, 9 Apr 2018 00:39:52 +0200 Subject: fix unicode problems: always encode before doing filesystem stuff, always decode after. --- app/backend.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/app/backend.py b/app/backend.py index e601c0c..94e7c67 100644 --- a/app/backend.py +++ b/app/backend.py @@ -9,8 +9,8 @@ class Storage: self.path = path_app self.root = path_rel - def _join(self, *arg): - return os.path.join(self.path, *arg) + def _join(self, *components): + return os.path.join(self.path, *(ensureutf8(c) for c in components)) def get_courses(self): """ Lists all courses of a study """ @@ -35,7 +35,7 @@ class Storage: def get_exams(self, name): """ Lists all exams of a given course """ # loop over all directories which do not contain any subdirs - for root, dirs, files in os.walk(self._join(name).encode('utf-8')): + for root, dirs, files in os.walk(self._join(name)): if len(dirs) == 0: # metainformation is encoded in path: course/year/exam.pdf splitted = root.split(os.path.sep) @@ -45,5 +45,12 @@ class Storage: if year.isdigit(): # yield entries as tuples (name, path) grouped by years func = partial(os.path.join, self.root, course, year) - entries = [(f, func(f)) for f in files] + # filenames created by this app are always ascii-only, but let's + # decode it nonetheless just to be sure... + entries = [(f.decode('utf-8'), func(f)) for f in files] yield (year, entries) + +def ensureutf8(s): + if isinstance(s, unicode): + return s.encode('utf-8') + return s -- cgit v1.2.3-1-g7c22