From 6cd4484be919dfee2333e1c96b35dee39f60ffa3 Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Tue, 7 May 2019 19:57:24 +0300 Subject: [PATCH] Init new project using new ProjectConfig API --- platformio/commands/ci.py | 3 +-- platformio/commands/init.py | 42 +++++++++++++---------------------- platformio/project/config.py | 37 +++++++++++++++++++++++------- platformio/projectconftpl.ini | 9 -------- platformio/util.py | 14 +++++------- setup.py | 1 - 6 files changed, 51 insertions(+), 55 deletions(-) delete mode 100644 platformio/projectconftpl.ini diff --git a/platformio/commands/ci.py b/platformio/commands/ci.py index 18845592..f9e93ace 100644 --- a/platformio/commands/ci.py +++ b/platformio/commands/ci.py @@ -165,5 +165,4 @@ def _copy_project_conf(build_dir, project_conf): config = ProjectConfig(project_conf, parse_extra=False) if config.has_section("platformio"): config.remove_section("platformio") - with open(join(build_dir, "platformio.ini"), "wb") as fp: - config.write(fp) + config.save(join(build_dir, "platformio.ini")) diff --git a/platformio/commands/init.py b/platformio/commands/init.py index db45ddfc..f08a52c2 100644 --- a/platformio/commands/init.py +++ b/platformio/commands/init.py @@ -16,7 +16,6 @@ from os import getcwd, makedirs from os.path import isdir, isfile, join -from shutil import copyfile import click @@ -89,7 +88,8 @@ def cli( "platformio.ini", fg="cyan")) is_new_project = not util.is_platformio_project(project_dir) - init_base_project(project_dir) + if is_new_project: + init_base_project(project_dir) if board: fill_project_envs(ctx, project_dir, board, project_option, env_prefix, @@ -144,13 +144,7 @@ def get_best_envname(project_dir, boards=None): def init_base_project(project_dir): - if util.is_platformio_project(project_dir): - return - - copyfile( - join(util.get_source_dir(), "projectconftpl.ini"), - join(project_dir, "platformio.ini")) - + ProjectConfig(join(project_dir, "platformio.ini")).save() with util.cd(project_dir): dir_to_readme = [ (util.get_projectsrc_dir(), None), @@ -362,12 +356,9 @@ def init_cvs_ignore(project_dir): def fill_project_envs(ctx, project_dir, board_ids, project_option, env_prefix, force_download): - content = [] + config = ProjectConfig( + join(project_dir, "platformio.ini"), parse_extra=False) used_boards = [] - used_platforms = [] - - config_path = join(project_dir, "platformio.ini") - config = util.load_project_config(config_path) for section in config.sections(): cond = [ section.startswith("env:"), @@ -377,12 +368,15 @@ def fill_project_envs(ctx, project_dir, board_ids, project_option, env_prefix, used_boards.append(config.get(section, "board")) pm = PlatformManager() + used_platforms = [] + modified = False for id_ in board_ids: board_config = pm.board_config(id_) used_platforms.append(board_config['platform']) if id_ in used_boards: continue used_boards.append(id_) + modified = True envopts = {"platform": board_config['platform'], "board": id_} # find default framework for board @@ -396,22 +390,18 @@ def fill_project_envs(ctx, project_dir, board_ids, project_option, env_prefix, _name, _value = item.split("=", 1) envopts[_name.strip()] = _value.strip() - content.append("") - content.append("[env:%s%s]" % (env_prefix, id_)) - for name, value in envopts.items(): - content.append("%s = %s" % (name, value)) + section = "env:%s%s" % (env_prefix, id_) + config.add_section(section) + + for option, value in envopts.items(): + config.set(section, option, value) if force_download and used_platforms: _install_dependent_platforms(ctx, used_platforms) - if not content: - return - - with open(config_path, "a") as f: - content.append("") - f.write("\n".join(content)) - - ProjectConfig.reset_instances() + if modified: + config.save() + config.reset_instances() def _install_dependent_platforms(ctx, platforms): diff --git a/platformio/project/config.py b/platformio/project/config.py index f827020c..46a99425 100644 --- a/platformio/project/config.py +++ b/platformio/project/config.py @@ -26,6 +26,18 @@ try: except ImportError: import configparser as ConfigParser +CONFIG_HEADER = """ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html +""" + KNOWN_PLATFORMIO_OPTIONS = [ "description", "env_default", @@ -118,6 +130,7 @@ class ProjectConfig(object): VARTPL_RE = re.compile(r"\$\{([^\.\}]+)\.([^\}]+)\}") + expand_interpolations = True _instances = {} _parser = None _parsed = [] @@ -140,8 +153,6 @@ class ProjectConfig(object): @staticmethod def get_instance(path): - if not isfile(path): - raise exception.NotPlatformIOProject(path) if path not in ProjectConfig._instances: ProjectConfig._instances[path] = ProjectConfig(path) return ProjectConfig._instances[path] @@ -150,13 +161,13 @@ class ProjectConfig(object): def reset_instances(): ProjectConfig._instances = {} - def __init__(self, path, parse_extra=True): - if not isfile(path): - raise exception.NotPlatformIOProject(path) + def __init__(self, path, parse_extra=True, expand_interpolations=True): self.path = path + self.expand_interpolations = expand_interpolations self._parsed = [] self._parser = ConfigParser.ConfigParser() - self.read(path, parse_extra) + if isfile(path): + self.read(path, parse_extra) def __getattr__(self, name): return getattr(self._parser, name) @@ -197,6 +208,8 @@ class ProjectConfig(object): for option in self.options(section)] def get(self, section, option): + if not self.expand_interpolations: + return self._parser.get(section, option) try: value = self._parser.get(section, option) except ConfigParser.Error as e: @@ -227,6 +240,8 @@ class ProjectConfig(object): return self.parse_multi_values(self.get("platformio", "env_default")) def validate(self, envs=None): + if not isfile(self.path): + raise exception.NotPlatformIOProject(self.path) # check envs known = set(self.envs()) if not known: @@ -246,7 +261,7 @@ class ProjectConfig(object): KNOWN_PLATFORMIO_OPTIONS) if unknown: warnings.add( - "Ignore unknown `%s` options in `[platformio]` section" % + "Ignore unknown `%s` options in section `[platformio]`" % ", ".join(unknown)) # check [env:*] sections @@ -257,7 +272,7 @@ class ProjectConfig(object): # obsolete if option in RENAMED_OPTIONS: warnings.add( - "`%s` option in `[%s]` section is deprecated and will " + "`%s` option in section `[%s]` is deprecated and will " "be removed in the next release! Please use `%s` " "instead" % (option, section, RENAMED_OPTIONS[option])) # rename on-the-fly @@ -281,3 +296,9 @@ class ProjectConfig(object): click.secho("Warning! %s" % warning, fg="yellow") return True + + def save(self, path=None): + with open(path or self.path, "wb") as fp: + fp.write(CONFIG_HEADER.strip()) + fp.write("\n\n") + self._parser.write(fp) diff --git a/platformio/projectconftpl.ini b/platformio/projectconftpl.ini deleted file mode 100644 index ebc510b2..00000000 --- a/platformio/projectconftpl.ini +++ /dev/null @@ -1,9 +0,0 @@ -; PlatformIO Project Configuration File -; -; Build options: build flags, source filter -; Upload options: custom upload port, speed and extra flags -; Library options: dependencies, extra library storages -; Advanced options: extra scripting -; -; Please visit documentation for the other options and examples -; https://docs.platformio.org/page/projectconf.html diff --git a/platformio/util.py b/platformio/util.py index 85a55dc6..8df17ec9 100644 --- a/platformio/util.py +++ b/platformio/util.py @@ -179,15 +179,11 @@ def get_project_optional_dir(name, default=None): if var_name in os.environ: paths = os.getenv(var_name) else: - try: - config = ProjectConfig.get_instance( - join(get_project_dir(), "platformio.ini")) - if (config.has_section("platformio") - and config.has_option("platformio", name)): - paths = config.get("platformio", name) - except exception.NotPlatformIOProject: - pass - + config = ProjectConfig.get_instance( + join(get_project_dir(), "platformio.ini")) + if (config.has_section("platformio") + and config.has_option("platformio", name)): + paths = config.get("platformio", name) if not paths: return default diff --git a/setup.py b/setup.py index 747d6cc2..667a2842 100644 --- a/setup.py +++ b/setup.py @@ -41,7 +41,6 @@ setup( packages=find_packages() + ["scripts"], package_data={ "platformio": [ - "projectconftpl.ini", "ide/tpls/*/.*.tpl", "ide/tpls/*/*.tpl", "ide/tpls/*/*/*.tpl",