From 0726aec642c52ea3a4f39370c3f181069f0cbbbb Mon Sep 17 00:00:00 2001 From: Nate Prewitt Date: Thu, 9 Feb 2017 09:21:03 -0700 Subject: [PATCH] simplifying writes --- pipenv/cli.py | 2 +- pipenv/project.py | 22 +++++++--------------- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/pipenv/cli.py b/pipenv/cli.py index c39f4ab3..d5d8ad47 100644 --- a/pipenv/cli.py +++ b/pipenv/cli.py @@ -103,7 +103,7 @@ def ensure_pipfile(validate=True): # Write changes out to disk. if changed: click.echo(crayons.yellow('Fixing package names in Pipfile...'), err=True) - project.write(p) + project.write_toml(p) def ensure_virtualenv(three=None, python=None): diff --git a/pipenv/project.py b/pipenv/project.py index a3fdd9b7..f167963a 100644 --- a/pipenv/project.py +++ b/pipenv/project.py @@ -96,7 +96,6 @@ class Project(object): @property def parsed_pipfile(self): with open(self.pipfile_location, 'r') as f: - # return toml.load(f) return toml.load(f, _dict=OrderedDict) @property @@ -114,12 +113,12 @@ class Project(object): def create_pipfile(self): data = {u'source': [{u'url': u'https://pypi.python.org/simple', u'verify_ssl': True}], u'packages': {}, 'dev-packages': {}} - with open('Pipfile', 'w') as f: - f.write(toml.dumps(data)) + self.write_toml(data, 'Pipfile') - def write(self, data): - # format TOML data. - with open(self.pipfile_location, 'w') as f: + def write_toml(self, data, path=None): + if path is None: + path = self.pipfile_location + with open(path, 'w') as f: f.write(format_toml(toml.dumps(data))) @property @@ -147,15 +146,10 @@ class Project(object): del p[key][package_name] # Write Pipfile. - data = format_toml(toml.dumps(p)) - with open(pipfile_path, 'w') as f: - f.write(data) + self.write_toml(p) def add_package_to_pipfile(self, package_name, dev=False): - # Find the Pipfile. - pipfile_path = pipfile.Pipfile.find() - # Read and append Pipfile. p = self.parsed_pipfile @@ -172,6 +166,4 @@ class Project(object): p[key][package_name] = package[package_name] # Write Pipfile. - data = format_toml(toml.dumps(p)) - with open(pipfile_path, 'w') as f: - f.write(data) + self.write_toml(p)