2019-05-10 17:26:10 +03:00
|
|
|
# Copyright (c) 2014-present PlatformIO <contact@platformio.org>
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2021-03-17 23:14:22 +02:00
|
|
|
# pylint: disable=unused-import,no-name-in-module
|
2019-05-17 13:18:15 +03:00
|
|
|
|
2019-09-30 17:59:06 +03:00
|
|
|
import inspect
|
2019-10-30 20:43:37 +02:00
|
|
|
import locale
|
2019-05-10 17:26:10 +03:00
|
|
|
import sys
|
|
|
|
|
2020-10-28 22:32:27 +02:00
|
|
|
from platformio.exception import UserSideException
|
|
|
|
|
2021-03-17 21:08:06 +02:00
|
|
|
if sys.version_info >= (3,):
|
|
|
|
if sys.version_info >= (3, 7):
|
|
|
|
from asyncio import create_task as aio_create_task
|
|
|
|
from asyncio import get_running_loop as aio_get_running_loop
|
|
|
|
else:
|
|
|
|
from asyncio import ensure_future as aio_create_task
|
|
|
|
from asyncio import get_event_loop as aio_get_running_loop
|
|
|
|
|
|
|
|
|
2019-05-10 17:26:10 +03:00
|
|
|
PY2 = sys.version_info[0] == 2
|
2021-03-19 00:21:44 +02:00
|
|
|
IS_CYGWIN = sys.platform.startswith("cygwin")
|
|
|
|
IS_WINDOWS = WINDOWS = sys.platform.startswith("win")
|
|
|
|
IS_MACOS = sys.platform.startswith("darwin")
|
2021-03-17 21:08:06 +02:00
|
|
|
string_types = (str,)
|
|
|
|
|
|
|
|
|
|
|
|
def is_bytes(x):
|
|
|
|
return isinstance(x, (bytes, memoryview, bytearray))
|
|
|
|
|
|
|
|
|
|
|
|
def ci_strings_are_equal(a, b):
|
|
|
|
if a == b:
|
|
|
|
return True
|
|
|
|
if not a or not b:
|
|
|
|
return False
|
|
|
|
return a.strip().lower() == b.strip().lower()
|
|
|
|
|
|
|
|
|
|
|
|
def hashlib_encode_data(data):
|
|
|
|
if is_bytes(data):
|
|
|
|
return data
|
|
|
|
if not isinstance(data, string_types):
|
|
|
|
data = str(data)
|
|
|
|
return data.encode()
|
|
|
|
|
|
|
|
|
|
|
|
def load_python_module(name, pathname):
|
|
|
|
import importlib.util # pylint: disable=import-outside-toplevel
|
|
|
|
|
|
|
|
spec = importlib.util.spec_from_file_location(name, pathname)
|
|
|
|
module = importlib.util.module_from_spec(spec)
|
|
|
|
spec.loader.exec_module(module)
|
|
|
|
return module
|
2019-05-10 17:26:10 +03:00
|
|
|
|
|
|
|
|
|
|
|
def get_filesystem_encoding():
|
|
|
|
return sys.getfilesystemencoding() or sys.getdefaultencoding()
|
|
|
|
|
|
|
|
|
2019-10-30 20:43:37 +02:00
|
|
|
def get_locale_encoding():
|
2019-11-13 16:48:04 +02:00
|
|
|
try:
|
|
|
|
return locale.getdefaultlocale()[1]
|
|
|
|
except ValueError:
|
|
|
|
return None
|
2019-10-30 20:43:37 +02:00
|
|
|
|
|
|
|
|
2020-03-12 14:24:20 +02:00
|
|
|
def get_object_members(obj, ignore_private=True):
|
|
|
|
members = inspect.getmembers(obj, lambda a: not inspect.isroutine(a))
|
|
|
|
if not ignore_private:
|
|
|
|
return members
|
2019-09-30 17:59:06 +03:00
|
|
|
return {
|
2020-03-12 14:24:20 +02:00
|
|
|
item[0]: item[1]
|
|
|
|
for item in members
|
|
|
|
if not (item[0].startswith("__") and item[0].endswith("__"))
|
2019-09-30 17:59:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-28 22:32:27 +02:00
|
|
|
def ensure_python3(raise_exception=True):
|
2020-12-26 16:10:07 +02:00
|
|
|
compatible = sys.version_info >= (3, 6)
|
|
|
|
if not raise_exception or compatible:
|
|
|
|
return compatible
|
2020-10-28 22:32:27 +02:00
|
|
|
raise UserSideException(
|
2020-12-26 16:10:07 +02:00
|
|
|
"Python 3.6 or later is required for this operation. \n"
|
2021-01-18 18:15:15 +02:00
|
|
|
"Please check a migration guide:\n"
|
|
|
|
"https://docs.platformio.org/en/latest/core/migration.html"
|
|
|
|
"#drop-support-for-python-2-and-3-5"
|
2020-10-28 22:32:27 +02:00
|
|
|
)
|
2021-03-20 10:31:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
def path_to_unicode(path):
|
|
|
|
"""
|
|
|
|
Deprecated: Compatibility with dev-platforms,
|
|
|
|
and custom device monitor filters
|
|
|
|
"""
|
|
|
|
return path
|