custom-components / pyscript

Pyscript adds rich Python scripting to HASS
Apache License 2.0
861 stars 46 forks source link

Support Python under Win10 #145

Closed itispip closed 3 years ago

itispip commented 3 years ago

OS: Win10 Python: 3.8.6

Error Msg below due to Windows doesn't support nl_langinfo:

Traceback (most recent call last): File "d:\homeassistant\lib\site-packages\homeassistant\config_entries.py", line 236, in async_setup result = await component.async_setup_entry(hass, self) # type: ignore File "C:\Users\Alex\AppData\Roaming.homeassistant\custom_components\pyscript__init__.py", line 123, in async_setup_entry TrigTime.init(hass) File "C:\Users\Alex\AppData\Roaming.homeassistant\custom_components\pyscript\trigger.py", line 142, in init cls.dow2int[locale.nllanginfo(getattr(locale, f"ABDAY{i + 1}")).lower()] = i AttributeError: module 'locale' has no attribute 'nl_langinfo'

craigbarratt commented 3 years ago

As you discovered, pyscript hasn't been tested on Windows. That particular code gets language (locale) specific strings for days of the week. Is there some other way to get that in Python on Windows?

You could temporarily hardcode it in English to at least see whether there are subsequent Windows-related issues.

itispip commented 3 years ago

Hardcode below. No error. But if user's Windows language is not English(US) will it be a problem?

    cls.dow2int['sun'] = 0
    cls.dow2int['sunday'] = 0
    cls.dow2int['mon'] = 1
    cls.dow2int['monday'] = 1
    cls.dow2int['tue'] = 2
    cls.dow2int['tuesday'] = 2
    cls.dow2int['wed'] = 3
    cls.dow2int['wednesday'] = 3
    cls.dow2int['thu'] = 4
    cls.dow2int['thursday'] = 4
    cls.dow2int['fri'] = 5
    cls.dow2int['friday'] = 5
    cls.dow2int['sat'] = 6
    cls.dow2int['saturday'] = 6
itispip commented 3 years ago

Another error is pyscript cannot handle multi-bytes character?

Logger: custom_components.pyscript Source: C:\Users\Alex\AppData\Roaming.homeassistant\custom_components\pyscript__init__.py:348 Integration: Pyscript Python scripting (documentation, issues)

load_scripts: skipping C:\Users\Alex\AppData\Roaming.homeassistant\pyscript\example.py due to exception 'gbk' codec can't decode byte 0xaf in position 199: illegal multibyte sequence

craigbarratt commented 3 years ago

I guess we could hardcode the days-of-week on Windows, but, yes, it would only be in English.

For the 2nd error, Python should be expecting unicode with utf8 encoding. Do you know the encoding of your files on Windows? What are the hex byte values around byte #199?

itispip commented 3 years ago

It's not a file encoding issue. The py file encoding is utf8 no BOM.

This error happen when I use the example code in tutorial to call tts.google_translate_say service. If I pass a multi-byte string to the "message" parameter of google_translate_say, no matter Japanese, Chinese, Korean or Thai, the example.py script cannot be loaded.

@service def hello_world(action=None, id=None): """hello_world example using pyscript.""" log.info(f"hello world: got action {action} id {id}") tts.google_translate_say(entity_id='media_player.vlc', message='นี่คือการทดสอบ')

Same file but pass an English string to message is fine.

craigbarratt commented 3 years ago

Can you try something simpler, eg just do this:

log.info('นี่คือการทดสอบ')

That works for me on MacOSX.

itispip commented 3 years ago

Can you try something simpler, eg just do this:

log.info('นี่คือการทดสอบ')

That works for me on MacOSX.

No, still the same.

I have tried to change Windows Display language to English, but the error is the same, so it's not related with system locale.

craigbarratt commented 3 years ago

It looks like on windows open() doesn't default to utf-8 encoding. I added encoding="utf-8" to each open() call. This should fix the issue.