- Platform information:
- Hardware: Mac mini
- OS: 10.13.6
- Java Runtime Environment:
openjdk version “1.8.0_252”
OpenJDK Runtime Environment (Zulu 8.46.0.19-CA-macosx) (build 1.8.0_252-b14)
OpenJDK 64-Bit Server VM (Zulu 8.46.0.19-CA-macosx) (build 25.252-b14, mixed mode) - openHAB version: 2.5.5 release
- Problem summary:
Scripts do not compile / cannot contain special characters / log outputs garbage, or nothing.
I have now googled many hours and found all kind of more or less puzzling stuff, and not much related to OpenHab / jsr223 unicode. Very brief summary:
- Python 2.7 is not Unicode by default.
- I was able to make Python 2.7 Unicode-aware in Terminal.app, and in VS Code.
Unicode Tests A (not ideal approach).
Here I tried to interpret Unicode in code, which works for string literals, but not in a function. It’s a very clumsy approach anyway, to ‘mark’ strings with ‘u’…
from core.rules import rule
from core.triggers import when
from org.slf4j import LoggerFactory
gRuleName = "Unicode Tests A"
gRuleLogLevel = 3 # 0=Off 1=Basic 2=Detail 3=All
@rule(gRuleName, description="", tags=[""])
@when("Time cron 0/10 * * * * ?")
def module_cris(event):
lg("Fired.", 2)
LoggerFactory.getLogger("jsr223.jython").info(u"Hällo Unicode Wörld, declared unicode string.") # ok
# --> 2020-05-26 12:00:30.881 [INFO ] [jsr223.jython ] - Hällo Unicode Declared String Wörld!
LoggerFactory.getLogger("jsr223.jython").info("Hällo Unicode Wörld, non-declared string.") # garbage
# --> 2020-05-26 12:00:20.876 [INFO ] [jsr223.jython ] - Hällo Unicode Wörld! 2
lg("Hällo Unicode Wörld, non-declared string, Function.")
# --> See Function.
lg("Ended.", 2)
# Rule End
##### Functions
# Log fast
def lg(msg, logLevel = 3, prefix = gRuleName):
if gRuleLogLevel < logLevel:
return
#LoggerFactory.getLogger("jsr223.jython").info( (prefix + ": " + msg))
""" -->
2020-05-26 12:04:50.899 [ERROR] [jsr223.jython.Module cris Python ] - Traceback (most recent call last):
File "/Applications/openhab/conf/automation/lib/python/core/log.py", line 51, in wrapper
return fn(*args, **kwargs)
File "<script>", line 19, in module_cris
File "<script>", line 36, in lg
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1: ordinal not in range(128)
"""
# https://docs.python.org/2/howto/unicode.html
# LoggerFactory.getLogger("jsr223.jython").info( unicode(prefix + ": " + msg))
# --> Same error message as above.
# LoggerFactory.getLogger("jsr223.jython").info( unicode((prefix + ": " + msg), errors='ignore'))
# --> Same error message as above.
# LoggerFactory.getLogger("jsr223.jython").info( unicode("äää", errors='ignore'))
# --> produces an empty log line:
# 2020-05-26 12:22:00.957 [INFO ] [jsr223.jython ] -
m = sr(msg, ["ä", "ü", "ö"], ["ae", "ue", "oe"]) # Workaround, Umlaute can't be logged.
LoggerFactory.getLogger("jsr223.jython").info(prefix + ": " + m)
# -->
# 2020-05-26 12:24:00.939 [INFO ] [jsr223.jython ] - Module cris Python: Haello Unicode Woerld, non-declared string, Function.
# String replace
def sr(theString, theSearchList, theReplaceList):
# loop with i
c = len(theSearchList)
for i in range(c):
theSearch = theSearchList[i]
theReplace = theReplaceList[i]
theString = theString.replace(theSearch, theReplace)
return theString
Unicode Tests B (better, but still not ideal approach).
Here I’m trying to give the .py file an encoding information in the header (must be in the first 2 lines).
#!/path/doesn't/matter/at/all,/it/seems?/
# encoding: utf-8
# --> As long as the "encoding: utf-8" is in the header:
"""
2020-05-26 12:33:59.942 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/Applications/openhab/conf/automation/jsr223/python/personal/python_unicode_b.py': org.python.antlr.ParseException: org.python.antlr.ParseException: encoding declaration in Unicode string
"""
# Variations I tried:
#!/Applications/openhab/conf/automation/jsr223
#!/Applications/openhab/conf/automation/jython
#!/Applications/openhab/conf/automation/lib/python/
#!/Applications/openhab/conf/automation/jython/jython-standalone-2.7.0.jar
#!/Applications/openhab/conf/automation/jython/jython-standalone-2.7.0
# coding: utf-8
# ...
from core.rules import rule
from core.triggers import when
from org.slf4j import LoggerFactory
#import sys
#reload(sys)
#sys.setdefaultencoding('utf8') # no
gRuleName = "Unicode Tests B"
@rule(gRuleName, description="", tags=[""])
@when("Time cron 0/10 * * * * ?")
def module_cris(event):
LoggerFactory.getLogger("jsr223.jython").info("Hällo Unicode Wörld")
# Rule End
I saw a few other posts where people also had the same problem, without any solution.
It seems this must be configured somewhere deeper, but I do not understand where that could be.
(Original plan: double click an app, add z-wave hardware, write rules. Far far away from that )
Questions:
- Does anyone know how to make it work?
- Could this work out of the box, when the jsr223 stuff is installed? How long is Unicode the standard now?