metamorphose / metamorphose2

Métamorphose Renamer v2
http://file-folder-ren.sourceforge.net
146 stars 33 forks source link

LookupError: unknown encoding: None #16

Open bastafidli opened 7 years ago

bastafidli commented 7 years ago

In Ubuntu 16.04 the latest code crashes with error when loading config file pointing to directory containing some files to rename

Traceback (most recent call last): File "/usr/share/metamorphose2/MainWindow/init.py", line 687, in load_config configs.load(self) File "/usr/share/metamorphose2/configs.py", line 66, in load LoadConfig(main, dlg.GetPath()) File "/usr/share/metamorphose2/configs.py", line 192, in init self.load_file(configFilePath) File "/usr/share/metamorphose2/configs.py", line 365, in __load_file main.picker.view.reset_dirpicker_on_config_load() File "/usr/share/metamorphose2/picker/wxPickerView.py", line 560, in reset_dirpicker_on_config_load self._refresh_items(True) File "/usr/share/metamorphose2/picker/wxPickerView.py", line 508, in _refresh_items self.Core.refresh(True) File "/usr/share/metamorphose2/picker/init.py", line 369, in refresh self.select_all() File "/usr/share/metamorphose2/picker/init.py", line 112, in select_all self.view.select_all(event) File "/usr/share/metamorphose2/picker/wxPickerView.py", line 625, in select_all main.show_preview(event) File "/usr/share/metamorphose2/MainWindow/init.py", line 620, in show_preview self.on_preview_button(False) File "/usr/share/metamorphose2/MainWindow/init.py", line 922, in on_preview_button self.renamer.preview(event) File "/usr/share/metamorphose2/renamer/init.py", line 72, in preview self.preview.generate_names(event) File "/usr/share/metamorphose2/renamer/preview.py", line 225, in generate_names self.run(operations) File "/usr/share/metamorphose2/renamer/preview.py", line 304, in run newExt, itemToRename) File "/usr/share/metamorphose2/operations/replace.py", line 91, in rename_item newName = operations.parse_input(text, original, self) File "/usr/share/metamorphose2/operations/opButtons.py", line 185, in parse_input return self.parser.parse_input(text, file, operation) File "/usr/share/metamorphose2/operations/opParser.py", line 439, in parse_input value = self.__date_time(0, file, operation) File "/usr/share/metamorphose2/operations/opParser.py", line 296, in __date_time return utils.udate(main, dateTime[op + 1], itemDateTime) File "/usr/share/metamorphose2/utils.py", line 208, in udate udate = time.strftime(format.encode(main.encoding), itemDateTime) LookupError: unknown encoding: None

bastafidli commented 7 years ago

I tried to debug the issue and it seems, the problem is in init.py in call to

self.encoding = unicode(locale.getlocale()[1])

which results in "None". More precisely, the call

locale.getlocale()

returns None,None. This is caused by call at line 561

localename = _setlocale(category)

in locale.py whichresults localename to be 'C'. Any suggestion, what is the expected normal behavior and how to fix it would be greatly appreciated.

bl1ndm0nk commented 7 years ago

Hi, stumbled upon your comments, here is my few cents. Love this program but older version. Would love to see it progress. Installing it via ppa from webupd8 or deb it sais ver. 0.8.4, it installs 0.9.0 which could be a good thing, BUT. When renaming photos by retrieving date&time from pictures it freezes on generating names preview, not only thumbnails and makes it useless. It is due to either this version not reading system locale encoding properly, or because it uses wxPython 3.0.2.0 and it doesn't read it. You can see it by running it in terminal with debug switch -d . First you will notice it in block ==Interface Localization== you will see "Set encoding: None". and when it crashes you will see error as the last line LookupError: unknown encoding: None if you uninstall it and download version 0.8.2 (beta) deb package it will install wxPython 2.8.12.1 as well and it does work properly, it retrieves "System encoding : UTF-8". without any problems. you could see it in init block. Hope that helps. My system Mint 18 mate 64bit uname -a Linux Satellite-P305 4.4.0-78-generic #99-Ubuntu SMP Thu Apr 27 15:29:09 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

bastafidli commented 7 years ago

Here is a patch which provides a workaround for this issue Proposed_workaround_for__16.patch.zip

savoury1 commented 6 years ago

Similar to the crash mentioned by OP relating to loading a pre-saved config file, when attempting to save a config file the following exception occurs:

Traceback (most recent call last): File "/usr/share/metamorphose2/MainWindow/init.py", line 684, in save_config configs.save(self) File "/usr/share/metamorphose2/configs.py", line 50, in save utils.write_file(cfgFilePath, SaveConfig(main).cfgFile) File "/usr/share/metamorphose2/configs.py", line 78, in init self.cfgFile = self.__create_file() File "/usr/share/metamorphose2/configs.py", line 153, in __create_file datetime = utils.udate(main, '%Y-%m-%d %H:%M:%S', time.localtime()) File "/usr/share/metamorphose2/utils.py", line 208, in udate udate = time.strftime(format.encode(main.encoding), itemDateTime) LookupError: unknown encoding: None

This appears to be due a locale issue in Python 2.7 (used by metamorphose2) as compared to Python 3.5, with this issue demonstrated by the following Python commands in an interactive Python shell for each of the two Python versions (test system is Linux Mint 18.1 so based on Ubuntu 16.04 LTS):

Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> locale.getlocale()
(None, None)
>>> locale.setlocale(locale.LC_ALL,'')
'en_US.UTF-8'
>>> locale.getlocale()
('en_US', 'UTF-8')
>>>
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> locale.getlocale()
('en_US', 'UTF-8')
>>>

As demonstrated by the Python 2.7 commands above, the locale related exception can thus be fixed by adding a single line of code to MainWindow/init.py at line 500:

        locale.setlocale(locale.LC_ALL,'')

Thus, the code for lines 500 and 501 then reads:

        locale.setlocale(locale.LC_ALL,'')
        self.encoding = unicode(locale.getlocale()[1])

Adding this line fixes all locale related exception errors with metamorphose2 on the mentioned test system, allowing successful saving & loading of config files. Attached is a patch file with this alternate workaround for the issue.

alternate_workaround_issue_16.zip

savoury1 commented 5 years ago

For all interested in a patched build see #26