def _parse_pipfile(self, contents): # If any outline tables are present... if ('[packages.' in contents) or ('[dev-packages.' in contents): data = toml.loads(contents) # Convert all outline tables to inline tables. for section in ('packages', 'dev-packages'): for package in data.get(section, {}): # Convert things to inline tables — fancy :) if hasattr(data[section][package], 'keys'): _data = data[section][package] data[section][package] = toml._get_empty_inline_table( dict) data[section][package].update(_data) # We lose comments here, but it's for the best.) try: return contoml.loads(toml.dumps(data, preserve=True)) except RuntimeError: return toml.loads(toml.dumps(data, preserve=True)) else: # Fallback to toml parser, for large files. try: return contoml.loads(contents) except Exception: return toml.loads(contents)
def write_toml(self, data, path=None): """Writes the given data structure out as TOML.""" if path is None: path = self.pipfile_location try: formatted_data = contoml.dumps(data).rstrip() except Exception: for section in ('packages', 'dev-packages'): for package in data.get(section, {}): # Convert things to inline tables — fancy :) if hasattr(data[section][package], 'keys'): _data = data[section][package] data[section][package] = toml._get_empty_inline_table( dict) data[section][package].update(_data) formatted_data = toml.dumps(data).rstrip() if Path(path).absolute() == Path(self.pipfile_location).absolute(): newlines = self._pipfile_newlines else: newlines = DEFAULT_NEWLINES formatted_data = cleanup_toml(formatted_data) with io.open(path, 'w', newline=newlines) as f: f.write(formatted_data) # pipfile is mutated! self.clear_pipfile_cache()
def write_toml(self, data, path=None): """Writes the given data structure out as TOML.""" if path is None: path = self.pipfile_location try: formatted_data = contoml.dumps(data) except RuntimeError: import toml for section in ('packages', 'dev-packages'): for package in data[section]: # Convert things to inline tables — fancy :) if hasattr(data[section][package], 'keys'): _data = data[section][package] data[section][package] = toml._get_empty_inline_table( dict) data[section][package].update(_data) formatted_data = toml.dumps(data) else: pass finally: pass with open(path, 'w') as f: f.write(formatted_data)
def parsed_pipfile(self): # Open the pipfile, read it into memory. with open(self.pipfile_location) as f: contents = f.read() # If any outline tables are present... if ('[packages.' in contents) or ('[dev-packages.' in contents): data = toml.loads(contents) # Convert all outline tables to inline tables. for section in ('packages', 'dev-packages'): for package in data.get(section, {}): # Convert things to inline tables — fancy :) if hasattr(data[section][package], 'keys'): _data = data[section][package] data[section][package] = toml._get_empty_inline_table( dict) data[section][package].update(_data) # We lose comments here, but it's for the best.) return contoml.loads(toml.dumps(data, preserve=True)) else: return contoml.loads(contents)
def write_toml(self, data, path=None): """Writes the given data structure out as TOML.""" if path is None: path = self.pipfile_location try: formatted_data = contoml.dumps(data).rstrip() except Exception: for section in ("packages", "dev-packages"): for package in data.get(section, {}): # Convert things to inline tables — fancy :) if hasattr(data[section][package], "keys"): _data = data[section][package] data[section][package] = toml._get_empty_inline_table(dict) data[section][package].update(_data) formatted_data = toml.dumps(data).rstrip() if Path(path).absolute() == Path(self.pipfile_location).absolute(): newlines = self._pipfile_newlines else: newlines = DEFAULT_NEWLINES formatted_data = cleanup_toml(formatted_data) with io.open(path, "w", newline=newlines) as f: f.write(formatted_data) # pipfile is mutated! self.clear_pipfile_cache()
def _parse_pipfile(self, contents): # If any outline tables are present... if ("[packages." in contents) or ("[dev-packages." in contents): data = toml.loads(contents) # Convert all outline tables to inline tables. for section in ("packages", "dev-packages"): for package in data.get(section, {}): # Convert things to inline tables — fancy :) if hasattr(data[section][package], "keys"): _data = data[section][package] data[section][package] = toml._get_empty_inline_table(dict) data[section][package].update(_data) # We lose comments here, but it's for the best.) try: return contoml.loads(toml.dumps(data, preserve=True)) except RuntimeError: return toml.loads(toml.dumps(data, preserve=True)) else: # Fallback to toml parser, for large files. try: return contoml.loads(contents) except Exception: return toml.loads(contents)
def parsed_pipfile(self): # Open the pipfile, read it into memory. with open(self.pipfile_location) as f: contents = f.read() # If any outline tables are present... if ('[packages.' in contents) or ('[dev-packages.' in contents): data = toml.loads(contents) # Convert all outline tables to inline tables. for section in ('packages', 'dev-packages'): for package in data.get(section, {}): # Convert things to inline tables — fancy :) if hasattr(data[section][package], 'keys'): _data = data[section][package] data[section][package] = toml._get_empty_inline_table(dict) data[section][package].update(_data) # We lose comments here, but it's for the best.) try: return contoml.loads(toml.dumps(data, preserve=True)) except RuntimeError: return toml.loads(toml.dumps(data, preserve=True)) else: # Fallback to toml parser, for large files. try: return contoml.loads(contents) except Exception: return toml.loads(contents)
def write_toml(self, data, path=None): """Writes the given data structure out as TOML.""" if path is None: path = self.pipfile_location try: formatted_data = contoml.dumps(data).rstrip() except Exception: for section in ('packages', 'dev-packages'): for package in data[section]: # Convert things to inline tables — fancy :) if hasattr(data[section][package], 'keys'): _data = data[section][package] data[section][package] = toml._get_empty_inline_table( dict) data[section][package].update(_data) formatted_data = toml.dumps(data).rstrip() formatted_data = cleanup_toml(formatted_data) with open(path, 'w') as f: f.write(formatted_data) # pipfile is mutated! self.clear_pipfile_cache()
def write_toml(self, data, path=None): """Writes the given data structure out as TOML.""" if path is None: path = self.pipfile_location try: formatted_data = contoml.dumps(data).rstrip() except Exception: for section in ('packages', 'dev-packages'): for package in data[section]: # Convert things to inline tables — fancy :) if hasattr(data[section][package], 'keys'): _data = data[section][package] data[section][package] = toml._get_empty_inline_table(dict) data[section][package].update(_data) formatted_data = toml.dumps(data).rstrip() formatted_data = cleanup_toml(formatted_data) with open(path, 'w') as f: f.write(formatted_data)