Implement PIO Home PlatformRPC

This commit is contained in:
Ivan Kravets
2023-04-17 13:15:21 +03:00
parent e8ffa244e5
commit 4444a0db99
2 changed files with 62 additions and 0 deletions

View File

@ -0,0 +1,60 @@
# 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.
from platformio.package.manager.platform import PlatformPackageManager
from platformio.platform.factory import PlatformFactory
class PlatformRPC:
@staticmethod
def list_installed(options=None):
result = []
options = options or {}
def _matchSearchQuery(p):
searchQuery = options.get("searchQuery")
if not searchQuery:
return True
content_blocks = [p.name, p.title, p.description]
if p.frameworks:
content_blocks.append(" ".join(p.frameworks.keys()))
for board in p.get_boards().values():
board_data = board.get_brief_data()
for key in ("id", "mcu", "vendor"):
content_blocks.append(board_data.get(key))
return searchQuery.strip() in " ".join(content_blocks)
pm = PlatformPackageManager()
for pkg in pm.get_installed():
p = PlatformFactory.new(pkg)
if not _matchSearchQuery(p):
continue
result.append(
dict(
__pkg_path=pkg.path,
__pkg_meta=pkg.metadata.as_dict(),
name=p.name,
title=p.title,
description=p.description,
)
)
return result
@staticmethod
def get_boards(spec):
p = PlatformFactory.new(spec)
return sorted(
[b.get_brief_data() for b in p.get_boards().values()],
key=lambda item: item["name"],
)

View File

@ -32,6 +32,7 @@ from platformio.home.rpc.handlers.ide import IDERPC
from platformio.home.rpc.handlers.misc import MiscRPC
from platformio.home.rpc.handlers.os import OSRPC
from platformio.home.rpc.handlers.piocore import PIOCoreRPC
from platformio.home.rpc.handlers.platform import PlatformRPC
from platformio.home.rpc.handlers.project import ProjectRPC
from platformio.home.rpc.handlers.registry import RegistryRPC
from platformio.home.rpc.server import WebSocketJSONRPCServerFactory
@ -73,6 +74,7 @@ def run_server(host, port, no_open, shutdown_timeout, home_url):
ws_rpc_factory.add_object_handler(OSRPC(), namespace="os")
ws_rpc_factory.add_object_handler(PIOCoreRPC(), namespace="core")
ws_rpc_factory.add_object_handler(ProjectRPC(), namespace="project")
ws_rpc_factory.add_object_handler(PlatformRPC(), namespace="platform")
ws_rpc_factory.add_object_handler(RegistryRPC(), namespace="registry")
path = urlparse(home_url).path