Skip to content

Commit

Permalink
Merge 061e29b into 313d72e
Browse files Browse the repository at this point in the history
  • Loading branch information
parruc committed May 28, 2019
2 parents 313d72e + 061e29b commit e8303b3
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Expand Up @@ -6,6 +6,7 @@ Changelog

- Issue#3 `Fixed typo <https://github.com/nazrulworld/collective.recipe.vscode/issues/3>`_
- Issue#4 `Documented isort <https://github.com/nazrulworld/collective.recipe.vscode/issues/4>`_
- Issue#5 `Automatic .env file generation`_


0.1.3 (2019-03-12)
Expand Down
9 changes: 8 additions & 1 deletion README.rst
Expand Up @@ -205,6 +205,13 @@ packages

Location of some python scripts or non standard modules (don't have setup file), you want to be in system path.

generate-envfile
Required: No

Default: false

Generate .env file to add eggs to PYTHONPATH

Links
=====

Expand All @@ -227,4 +234,4 @@ Issue Tracker:
.. _`Plone`: https://plone.org/
.. _`Flake8`: https://pypi.python.org/pypi/flake8
.. _`Python`: https://www.python.org/
.. _`autocomplete and intelliSense`: https://code.visualstudio.com/docs/languages/python#_autocomplete-and-intellisense
.. _`autocomplete and intelliSense`: https://code.visualstudio.com/docs/languages/python#_autocomplete-and-intellisense
2 changes: 0 additions & 2 deletions buildout.cfg
Expand Up @@ -47,5 +47,3 @@ eggs =
[versions]
# Don't use a relased version of collective.recipe.vscode
collective.recipe.vscode =


15 changes: 15 additions & 0 deletions src/collective/recipe/vscode/recipes.py
Expand Up @@ -183,6 +183,9 @@ def normalize_options(self):
# pep8 check: Issue#1
self._normalize_boolean("pep8-enabled", options)

# generate .env file
self._normalize_boolean("generate-envfile", options)

# autocomplete
options["autocomplete-use-omelette"] = self.options[
"autocomplete-use-omelette"
Expand Down Expand Up @@ -277,6 +280,7 @@ def _set_defaults(self):
self.options.setdefault("ignore-develop", "False")
self.options.setdefault("ignores", "")
self.options.setdefault("packages", "")
self.options.setdefault("generate-envfile", "False")

def _prepare_settings(
self, eggs_locations, develop_eggs_locations, existing_settings
Expand All @@ -291,6 +295,11 @@ def _prepare_settings(

settings[mappings["autocomplete-extrapaths"]] = eggs_locations

if options["generate-envfile"]:
path = os.path.join(self.settings_dir, ".env")
settings["python.envFile"] = path
self._write_env_file(eggs_locations, path)

if options["autocomplete-use-omelette"]:
# Add the omelette and the development eggs to the jedi list.
# This has the advantage of opening files at the omelette location,
Expand Down Expand Up @@ -403,6 +412,12 @@ def _write_project_file(self, settings, existing_settings):
# catching any json error
raise UserError(str(exc))

def _write_env_file(self, eggs_locations, path):
with io.open(path, "w", encoding="utf-8") as fp:
paths = os.pathsep.join(eggs_locations)
path_format = "PYTHONPATH={paths}:${{PYTHONPATH}}"
fp.write(ensure_unicode(path_format.format(paths=paths)))

def _resolve_executable_path(self, path_):
""" """
# Noramalized Path on demand
Expand Down
21 changes: 13 additions & 8 deletions src/collective/recipe/vscode/tests/test_recipes.py
Expand Up @@ -53,6 +53,8 @@ def test_install(self):
"flake8-args": "--max-line-length 88",
"flake8-path": "${buildout:directory}/bin/flake8",
"isort-enabled": "True",
"isort-path": "${buildout:directory}/bin/isort",
"generate-envfile": "True",
}
)
buildout["vscode"] = recipe_options
Expand All @@ -69,20 +71,23 @@ def test_install(self):
self.assertEqual(
generated_settings[mappings["flake8-path"]], self.location + "/bin/flake8"
)

print(generated_settings)
# Isort executable should get automatically
self.assertEqual(
os.path.dirname(generated_settings[mappings["python-path"]]) + "/isort",
generated_settings[mappings["isort-path"]],
self.location + "/bin/isort"
)

# Test existence and configuration of env file
envfile_path = os.path.join(self.location, ".vscode", ".env")
self.assertEqual(generated_settings["python.envFile"], envfile_path)
self.assertTrue(os.path.isfile(envfile_path))

# Test with custom location with package
buildout["vscode"].update(
{
"packages": "/fake/path",
"project-root": os.path.join(tempfile.gettempdir(), "hshdshgdrts"),
}
)
buildout["vscode"].update({
"packages": "/fake/path",
"project-root": os.path.join(tempfile.gettempdir(), "hshdshgdrts"),
})

recipe = Recipe(buildout, "vscode", buildout["vscode"])
recipe.install()
Expand Down

0 comments on commit e8303b3

Please sign in to comment.