Use io instead of codec

Drop the deprecated codec module for parsing files.
Better handling of file URIs
ui^2
Manuel Genovés 2020-01-24 21:24:44 +01:00
parent 66af5f8217
commit 0647b41340
1 changed files with 8 additions and 6 deletions

View File

@ -14,7 +14,7 @@
# with this program. If not, see <http://www.gnu.org/licenses/>. # with this program. If not, see <http://www.gnu.org/licenses/>.
# END LICENSE # END LICENSE
import codecs import io
import locale import locale
import logging import logging
import os import os
@ -218,7 +218,7 @@ class MainWindow(StyledWindow):
if self.filename: if self.filename:
LOGGER.info("saving") LOGGER.info("saving")
filename = self.filename filename = self.filename
file_to_save = codecs.open(filename, encoding="utf-8", mode='w') file_to_save = io.open(filename, encoding="utf-8", mode='w')
file_to_save.write(self.text_view.get_text()) file_to_save.write(self.text_view.get_text())
file_to_save.close() file_to_save.close()
if self.did_change: if self.did_change:
@ -252,7 +252,7 @@ class MainWindow(StyledWindow):
except: except:
pass pass
file_to_save = codecs.open(filename, encoding="utf-8", mode='w') file_to_save = io.open(filename, encoding="utf-8", mode='w')
file_to_save.write(self.text_view.get_text()) file_to_save.write(self.text_view.get_text())
file_to_save.close() file_to_save.close()
@ -293,7 +293,7 @@ class MainWindow(StyledWindow):
except: except:
pass pass
file_to_save = codecs.open(filename, encoding="utf-8", mode='w') file_to_save = io.open(filename, encoding="utf-8", mode='w')
file_to_save.write(self.text_view.get_text()) file_to_save.write(self.text_view.get_text())
file_to_save.close() file_to_save.close()
@ -437,16 +437,18 @@ class MainWindow(StyledWindow):
def load_file(self, filename=None): def load_file(self, filename=None):
"""Open File from command line or open / open recent etc.""" """Open File from command line or open / open recent etc."""
LOGGER.info("trying to open " + filename)
if self.check_change() == Gtk.ResponseType.CANCEL: if self.check_change() == Gtk.ResponseType.CANCEL:
return return
if filename: if filename:
print(urllib.parse.unquote(filename))
if filename.startswith('file://'): if filename.startswith('file://'):
filename = filename[7:] filename = urllib.parse.unquote(filename)[7:]
self.text_view.clear() self.text_view.clear()
try: try:
if os.path.exists(filename): if os.path.exists(filename):
with codecs.open(filename, encoding="utf-8", mode='r') as current_file: with io.open(filename, encoding="utf-8", mode='r') as current_file:
self.text_view.set_text(current_file.read()) self.text_view.set_text(current_file.read())
else: else:
dialog = Gtk.MessageDialog(self, dialog = Gtk.MessageDialog(self,