<commit_before># Copyright (c) 2014-present PlatformIO <<EMAIL>>
#
# 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.

import os
import re
import sys

from platformio import fs
from platformio.compat import load_python_module
from platformio.package.meta import PackageItem
from platformio.platform import base
from platformio.platform.exception import UnknownPlatform


class PlatformFactory(object):
    @staticmethod
    def get_clsname(name):
        name = re.sub(r"[^\da-z\_]+", "", name, flags=re.I)
        return "%sPlatform" % name.lower().capitalize()

    @classmethod
    def load_platform_module(cls, name, path, by_fallback=False):
        try:
            return load_python_module("platformio.platform.%s" % name, path)
        except ImportError as exc:
            if "platformio.managers" in str(exc) and not by_fallback:
                sys.modules["platformio.managers.platform"] = base
                return cls.load_platform_module(name, path, by_fallback=True)
            raise UnknownPlatform(name)

    @classmethod
    def new(cls, pkg_or_spec, autoinstall=False) -> base.PlatformBase:
        # pylint: disable=import-outside-toplevel
        from platformio.package.manager.platform import PlatformPackageManager

        platform_dir = None
        platform_name = None
        if isinstance(pkg_or_spec, PackageItem):
            platform_dir = pkg_or_spec.path
            platform_name = pkg_or_spec.metadata.name
        elif isinstance(pkg_or_spec, (str, bytes)) and os.path.isdir(pkg_or_spec):
            platform_dir = pkg_or_spec
        else:
            pkg = PlatformPackageManager().get_package(pkg_or_spec)
            if pkg:
                platform_dir = pkg.path
                platform_name = pkg.metadata.name

        if not platform_dir or not os.path.isfile(
            os.path.join(platform_dir, "platform.json")
        ):
            if autoinstall:
                return cls.new(
                    PlatformPackageManager().install(
                        pkg_or_spec, skip_dependencies=True<commit_msg>Dynamically import legacy platform module<commit_after># Copyright (c) 2014-present PlatformIO <<EMAIL>>
#
# 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.

import os
import re
import sys

from platformio import fs
from platformio.compat import load_python_module
from platformio.package.meta import PackageItem
from platformio.platform import base
from platformio.platform.exception import UnknownPlatform


class PlatformFactory(object):
    @staticmethod
    def get_clsname(name):
        name = re.sub(r"[^\da-z\_]+", "", name, flags=re.I)
        return "%sPlatform" % name.lower().capitalize()

    @staticmethod
    def load_platform_module(cls, name, path):
        # support for legacy dev-platforms
        sys.modules["platformio.managers.platform"] = base
        try:
            return load_python_module("platformio.platform.%s" % name, path)
        except ImportError as exc:
            raise UnknownPlatform(name)

    @classmethod
    def new(cls, pkg_or_spec, autoinstall=False) -> base.PlatformBase:
        # pylint: disable=import-outside-toplevel
        from platformio.package.manager.platform import PlatformPackageManager

        platform_dir = None
        platform_name = None
        if isinstance(pkg_or_spec, PackageItem):
            platform_dir = pkg_or_spec.path
            platform_name = pkg_or_spec.metadata.name
        elif isinstance(pkg_or_spec, (str, bytes)) and os.path.isdir(pkg_or_spec):
            platform_dir = pkg_or_spec
        else:
            pkg = PlatformPackageManager().get_package(pkg_or_spec)
            if pkg:
                platform_dir = pkg.path
                platform_name = pkg.metadata.name

        if not platform_dir or not os.path.isfile(
            os.path.join(platform_dir, "platform.json")
        ):
            if autoinstall:
                return cls.new(
                    PlatformPackageManager().install(
                        pkg_or_spec, skip_dependencies=True
{'fraction_non_alphanumeric': 0.08012756627466613, 'fraction_numerical': 0.0031891568666533787, 'mean_word_length': 2.8159695817490493, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3720669', 'n_tokens_mistral': 1394, 'n_tokens_neox': 1322, 'n_words': 409}