From abfee8308e85567467b5546eff9bb0400ddbd3c2 Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Fri, 10 May 2019 17:47:02 +0300 Subject: [PATCH] Switch Python or Platform dependent code to "compat" module --- platformio/commands/debug/client.py | 3 ++- platformio/commands/debug/process.py | 5 +++-- platformio/commands/home/rpc/handlers/app.py | 10 +++++----- platformio/commands/home/rpc/handlers/os.py | 6 +++--- platformio/commands/home/rpc/handlers/piocore.py | 10 +++++----- platformio/commands/home/rpc/handlers/project.py | 5 ++--- 6 files changed, 20 insertions(+), 19 deletions(-) diff --git a/platformio/commands/debug/client.py b/platformio/commands/debug/client.py index ac97243f..25a57db1 100644 --- a/platformio/commands/debug/client.py +++ b/platformio/commands/debug/client.py @@ -29,6 +29,7 @@ from platformio import app, exception, util from platformio.commands.debug import helpers, initcfgs from platformio.commands.debug.process import BaseProcess from platformio.commands.debug.server import DebugServer +from platformio.compat import PY2 from platformio.telemetry import MeasurementProtocol LOG_FILE = None @@ -60,7 +61,7 @@ class GDBClient(BaseProcess): # pylint: disable=too-many-instance-attributes def spawn(self, gdb_path, prog_path): session_hash = gdb_path + prog_path self._session_id = sha1( - session_hash if util.PY2 else session_hash.encode()).hexdigest() + session_hash if PY2 else session_hash.encode()).hexdigest() self._kill_previous_session() patterns = { diff --git a/platformio/commands/debug/process.py b/platformio/commands/debug/process.py index 0079e18c..0d70abe1 100644 --- a/platformio/commands/debug/process.py +++ b/platformio/commands/debug/process.py @@ -19,6 +19,7 @@ from twisted.internet import protocol # pylint: disable=import-error from platformio import util from platformio.commands.debug import helpers +from platformio.compat import string_types LOG_FILE = None @@ -42,13 +43,13 @@ class BaseProcess(protocol.ProcessProtocol, object): text = text.replace(pattern, value or "") return text - if isinstance(source, util.string_types): + if isinstance(source, string_types): source = _replace(source) elif isinstance(source, (list, dict)): items = enumerate(source) if isinstance(source, list) else source.items() for key, value in items: - if isinstance(value, util.string_types): + if isinstance(value, string_types): source[key] = _replace(value) elif isinstance(value, (list, dict)): source[key] = self.apply_patterns(value, patterns) diff --git a/platformio/commands/home/rpc/handlers/app.py b/platformio/commands/home/rpc/handlers/app.py index ce95a218..8241b03a 100644 --- a/platformio/commands/home/rpc/handlers/app.py +++ b/platformio/commands/home/rpc/handlers/app.py @@ -18,6 +18,7 @@ import json from os.path import expanduser, isfile, join from platformio import __version__, app, exception, util +from platformio.compat import path_to_unicode class AppRPC(object): @@ -55,12 +56,11 @@ class AppRPC(object): for key in storage['coreSettings']: if not key.endswith("dir"): continue - storage['coreSettings'][key][ - 'default_value'] = util.path_to_unicode( - storage['coreSettings'][key]['default_value']) - storage['coreSettings'][key]['value'] = util.path_to_unicode( + storage['coreSettings'][key]['default_value'] = path_to_unicode( + storage['coreSettings'][key]['default_value']) + storage['coreSettings'][key]['value'] = path_to_unicode( storage['coreSettings'][key]['value']) - storage['homeDir'] = util.path_to_unicode(expanduser("~")) + storage['homeDir'] = path_to_unicode(expanduser("~")) storage['projectsDir'] = storage['coreSettings']['projects_dir'][ 'value'] diff --git a/platformio/commands/home/rpc/handlers/os.py b/platformio/commands/home/rpc/handlers/os.py index 780dbec8..8b263632 100644 --- a/platformio/commands/home/rpc/handlers/os.py +++ b/platformio/commands/home/rpc/handlers/os.py @@ -17,7 +17,6 @@ from __future__ import absolute_import import glob import os import shutil -import sys from functools import cmp_to_key from os.path import expanduser, isdir, isfile, join @@ -26,6 +25,7 @@ from twisted.internet import defer # pylint: disable=import-error from platformio import app, util from platformio.commands.home import helpers +from platformio.compat import PY2, get_filesystem_encoding, path_to_unicode class OSRPC(object): @@ -81,7 +81,7 @@ class OSRPC(object): @staticmethod def reveal_file(path): return click.launch( - path.encode(sys.getfilesystemencoding()) if util.PY2 else path, + path.encode(get_filesystem_encoding()) if PY2 else path, locate=True) @staticmethod @@ -148,6 +148,6 @@ class OSRPC(object): items = [] for item in util.get_logical_devices(): if item['name']: - item['name'] = util.path_to_unicode(item['name']) + item['name'] = path_to_unicode(item['name']) items.append(item) return items diff --git a/platformio/commands/home/rpc/handlers/piocore.py b/platformio/commands/home/rpc/handlers/piocore.py index 80a642cb..b651b498 100644 --- a/platformio/commands/home/rpc/handlers/piocore.py +++ b/platformio/commands/home/rpc/handlers/piocore.py @@ -17,13 +17,13 @@ from __future__ import absolute_import import json import os import re -import sys import jsonrpc # pylint: disable=import-error from twisted.internet import utils # pylint: disable=import-error -from platformio import __version__, util +from platformio import __version__ from platformio.commands.home import helpers +from platformio.compat import get_filesystem_encoding, string_types class PIOCoreRPC(object): @@ -33,8 +33,8 @@ class PIOCoreRPC(object): json_output = "--json-output" in args try: args = [ - arg.encode(sys.getfilesystemencoding()) if isinstance( - arg, util.string_types) else str(arg) for arg in args + arg.encode(get_filesystem_encoding()) if isinstance( + arg, string_types) else str(arg) for arg in args ] except UnicodeError: raise jsonrpc.exceptions.JSONRPCDispatchException( @@ -54,7 +54,7 @@ class PIOCoreRPC(object): result = list(result) assert len(result) == 3 for i in (0, 1): - result[i] = result[i].decode(sys.getfilesystemencoding()).strip() + result[i] = result[i].decode(get_filesystem_encoding()).strip() out, err, code = result text = ("%s\n\n%s" % (out, err)).strip() if code != 0: diff --git a/platformio/commands/home/rpc/handlers/project.py b/platformio/commands/home/rpc/handlers/project.py index c3f48fcc..ae2eaa07 100644 --- a/platformio/commands/home/rpc/handlers/project.py +++ b/platformio/commands/home/rpc/handlers/project.py @@ -16,7 +16,6 @@ from __future__ import absolute_import import os import shutil -import sys import time from os.path import (basename, expanduser, getmtime, isdir, isfile, join, realpath, sep) @@ -26,6 +25,7 @@ import jsonrpc # pylint: disable=import-error from platformio import exception, util from platformio.commands.home.rpc.handlers.app import AppRPC from platformio.commands.home.rpc.handlers.piocore import PIOCoreRPC +from platformio.compat import get_filesystem_encoding from platformio.ide.projectgenerator import ProjectGenerator from platformio.managers.platform import PlatformManager from platformio.project.config import ProjectConfig @@ -215,8 +215,7 @@ class ProjectRPC(object): if isdir(src_dir): util.rmtree_(src_dir) shutil.copytree( - arduino_project_dir.encode(sys.getfilesystemencoding()), - src_dir) + arduino_project_dir.encode(get_filesystem_encoding()), src_dir) return project_dir @staticmethod