Skip to content

Commit

Permalink
[wip] One major bug fix (keep intact existing settings), previously b…
Browse files Browse the repository at this point in the history
…uildout deleting settings.json file

* default file associations and file excludes added for python file.
  • Loading branch information
nazrulworld committed Feb 14, 2019
1 parent c859222 commit dc8c3f8
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 6 deletions.
10 changes: 9 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ Changelog
0.1.2 (unreleased)
------------------

- Nothing changed yet.
New features

- default ``files.associations`` and ``files.exclude`` for python file now will be automatically included
if those are not in existing ``settings.json``

Bug fixes

- Normally buildout removed generated file/directory first if exists, that's why previously ``settings.json`` file
removed and ultimately existing settings were lost! [nazrulworld]


0.1.1 (2019-02-11)
Expand Down
35 changes: 34 additions & 1 deletion src/collective/recipe/vscode/recipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@
json_dump_params = {"sort_keys": True, "indent": 4, "separators": (",", ":")}
json_load_params = {}

python_file_defaults = {
"files.associations": {"*.zcml": "xml"},
"files.exclude": {
"**/*.py[co]": True,
"**/*.so": True,
"**/*.h*": True,
"**/__pycache__": True,
},
}


def ensure_unicode(string):
"""" """
Expand Down Expand Up @@ -128,7 +138,17 @@ def install(self):

self._write_project_file(vscode_settings, existing_settings)

return os.path.join(self.settings_dir, "settings.json")
# Write json file values only thosr are generated by this recipe.
# Also dodges (by giving fake like file) buildout to
# remove original settings.json file.
vs_generated_file = os.path.join(
self.settings_dir, "vs-recipe-generated-settings.json"
)
with io.open(vs_generated_file, "w", encoding="utf-8") as fp:
json_text = json.dumps(vscode_settings)
fp.write(ensure_unicode(json_text))

return vs_generated_file

update = install

Expand Down Expand Up @@ -344,6 +364,11 @@ def _prepare_linter_settings(self, settings, name, options, allow_key_error=Fals
def _write_project_file(self, settings, existing_settings):
"""Project File Writer:
This method is actual doing writting project file to file system."""
# Add some python file specific default setting
for key in python_file_defaults:
if key not in existing_settings:
settings[key] = python_file_defaults[key]

with io.open(
os.path.join(self.settings_dir, "settings.json"), "w", encoding="utf-8"
) as fp:
Expand Down Expand Up @@ -386,4 +411,12 @@ def uninstall(name, options):

logger = logging.getLogger(name)
logger.info("uninstalling ...")

project_root = options["project-root"]
settings_dir = os.path.join(project_root, ".vscode")

vs_generated_file = os.path.join(settings_dir, "vs-recipe-generated-settings.json")
if os.path.exists(vs_generated_file):
os.unlink(vs_generated_file)
logger.info("removing {0} ...".format(vs_generated_file))
# xxx: nothing for now, but may be removed what ever in options?
2 changes: 1 addition & 1 deletion src/collective/recipe/vscode/recipes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Install vscode recipe with stndard settings::
True

VS Code settings with all default options::

>>> os.unlink(os.path.join(settings_dir, 'settings.json'))
>>> write(sample_buildout, 'buildout.cfg',
... """
... [buildout]
Expand Down
5 changes: 4 additions & 1 deletion src/collective/recipe/vscode/settings_mappings.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@
"black-path": "python.formatting.blackPath",
"black-args": "python.formatting.blackArgs",
"jedi-enabled": "python.jediEnabled",
"jedi-path": "python.jediPath"
"jedi-path": "python.jediPath",
"rst-linter-path": "restructuredtext.linter.executablePath",
"rst-linter-enabled": "restructuredtext.linter.run",
"rst-linter-args": "restructuredtext.linter.extraArgs"
}
39 changes: 37 additions & 2 deletions src/collective/recipe/vscode/tests/test_recipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,37 @@ def test__write_project_file(self):
generated_settings[mappings["flake8-path"]], "/new/path/flake8"
)

def test_pyfile_defaults_settings(self):
""" """
from ..recipes import python_file_defaults
from ..recipes import Recipe

recipe_options = self.recipe_options.copy()
self.buildout["vscode"] = recipe_options

recipe = Recipe(self.buildout, "vscode", self.buildout["vscode"])
recipe._set_defaults()
recipe.install()

generated_settings = json.loads(
read(os.path.join(self.location, ".vscode", "settings.json"))
)
for key in python_file_defaults:
self.assertIn(key, generated_settings)

with open(os.path.join(self.location, ".vscode", "settings.json"), "w") as fp:
json.dump({"files.associations": {}, "files.exclude": []}, fp)

recipe = Recipe(self.buildout, "vscode", self.buildout["vscode"])
recipe._set_defaults()
recipe.install()

generated_settings = json.loads(
read(os.path.join(self.location, ".vscode", "settings.json"))
)
for key in python_file_defaults:
self.assertNotEqual(python_file_defaults[key], generated_settings[key])

def tearDown(self):
os.chdir(self.here)
rmtree.rmtree(self.location)
Expand All @@ -296,14 +327,17 @@ def setUp(self):

self.here = os.getcwd()

self.location = tempfile.mkdtemp(prefix="plone.recipe.vscode")
self.location = tempfile.mkdtemp(prefix="collective.recipe.vscode")
mkdir(self.location, "develop-eggs")
os.chdir(self.location)

self.buildout = Buildout()
# Set eggs
self.buildout["buildout"]["directory"] = self.location

self.recipe_options = dict(recipe="plone.recipe.vscode", overwrite="False")
self.recipe_options = dict(
recipe="collective.recipe.vscode", eggs="zc.recipe.egg\nzc.buildout"
)

def test_uninstall(self):
""" """
Expand All @@ -315,6 +349,7 @@ def test_uninstall(self):

recipe = Recipe(self.buildout, "vscode", self.buildout["vscode"])
recipe._set_defaults()
recipe.install()

uninstall(recipe.name, recipe.options)

Expand Down

0 comments on commit dc8c3f8

Please sign in to comment.