<commit_before>import json
import os

import pipfile
import toml

import delegator
from requests.compat import OrderedDict

from .utils import format_toml, mkdir_p
from .utils import convert_deps_from_pip
from .environments import PIPENV_MAX_DEPTH, PIPENV_VENV_IN_PROJECT


class Project(object):
    """docstring for Project"""
    def __init__(self):
        super(Project, self).__init__()
        self._virtualenv_location = None

    @property
    def name(self):
        return self.pipfile_location.split(os.sep)[-2]

    @property
    def pipfile_exists(self):
        return bool(self.pipfile_location)

    @property
    def virtualenv_exists(self):
        return os.path.isdir(self.virtualenv_location)

    @property
    def virtualenv_location(self):

        # Use cached version, if available.
        if self._virtualenv_location:
            return self._virtualenv_location

        # The user wants the virtualenv in the project.
        if not PIPENV_VENV_IN_PROJECT:
            c = delegator.run('pew dir {0}'.format(self.name))
            loc = c.out.strip()
        # Default mode.
        else:
            loc = os.sep.join(self.pipfile_location.split(os.sep)[:-1] + ['.venv'])

        self._virtualenv_location = loc
        return loc

    @property
    def download_location(self):
        d_dir = os.sep.join(self.virtualenv_location.split(os.sep) + ['downloads'])

        # Create the directory, if it doesn't exist.
        mkdir_p(d_dir)

        return d_dir

    @property
    def proper_names_location(self):
        pn_file = os.sep.join(self.virtualenv_location.split(os.sep) + ['pipenev-proper-names.txt'])

        # Create the database, if it doesn't exist.
        open(pn_file, 'a').close()

        return pn_file

    @property
    def proper_names(self):
        with open(self.proper_names_location, 'r') as f:
            return f.read().splitlines()

    def register_proper_name(self, name):
        """Registers a proper name to the database."""
        with open(self.proper_names_location, 'a') as f:
            f.write('{0}\n'.format(name))

    @property
    def pipfile_location(self):
        try:
            return pipfile.Pipfile.find(max_depth=PIPENV_MAX_DEPTH)
        except RuntimeError:
            return None

    @property
    def parsed_pipfile(self):
        with open(self.pipfile_location, 'r') as f:
            # return toml.load(f)
            return toml.load(f, _dict=OrderedDict)<commit_msg>make properties use private variables<commit_after>import json
import os

import pipfile
import toml

import delegator
from requests.compat import OrderedDict

from .utils import format_toml, mkdir_p
from .utils import convert_deps_from_pip
from .environments import PIPENV_MAX_DEPTH, PIPENV_VENV_IN_PROJECT


class Project(object):
    """docstring for Project"""
    def __init__(self):
        super(Project, self).__init__()
        self._name = None
        self._virtualenv_location = None
        self._download_location = None
        self._proper_names_location = None
        self._pipfile_location = None

    @property
    def name(self):
        if self._name is None:
            self._name = self.pipfile_location.split(os.sep)[-2]
        return self._name

    @property
    def pipfile_exists(self):
        return bool(self.pipfile_location)

    @property
    def virtualenv_exists(self):
        return os.path.isdir(self.virtualenv_location)

    @property
    def virtualenv_location(self):

        # Use cached version, if available.
        if self._virtualenv_location:
            return self._virtualenv_location

        # The user wants the virtualenv in the project.
        if not PIPENV_VENV_IN_PROJECT:
            c = delegator.run('pew dir {0}'.format(self.name))
            loc = c.out.strip()
        # Default mode.
        else:
            loc = os.sep.join(self.pipfile_location.split(os.sep)[:-1] + ['.venv'])

        self._virtualenv_location = loc
        return loc

    @property
    def download_location(self):
        if self._download_location is None:
            loc = os.sep.join([self.virtualenv_location, 'downloads'])
            self._download_location = loc

        # Create the directory, if it doesn't exist.
        mkdir_p(self._download_location)

        return self._download_location

    @property
    def proper_names_location(self):
        if self._proper_names_location is None:
            loc = os.sep.join([self.virtualenv_location, 'pipenev-proper-names.txt'])
            self._proper_names_location = loc

        # Create the database, if it doesn't exist.
        open(self._proper_names_location, 'a').close()

        return self._proper_names_location

    @property
    def proper_names(self):
        with open(self.proper_names_location, 'r') as f:
            return f.read().splitlines()

    def register_proper_name(self, name):
        """Registers a proper name to the database."""
        with open(self.proper_names_location, 'a') as f:
            f.write('{0}\n'.format(name))

    @property
    def pipfile_location(self):
        try:
            return pipfile.Pipfile.find(max_depth=PIPENV_MAX_DEPTH)
        except RuntimeError:
            return None

    @property
    def parsed_pipfile(self):
        with open(self.pipfile_location, 'r') as f:
            # return toml.load(f)
            return toml.load(f, _dict=OrderedDict)
{'fraction_non_alphanumeric': 0.09477184555699243, 'fraction_numerical': 0.0014779235174579716, 'mean_word_length': 2.5316373124592304, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '185173', 'n_tokens_mistral': 1680, 'n_tokens_neox': 1595, 'n_words': 379}