Skip to content

Commit

Permalink
Fixed linter warnings (#141)
Browse files Browse the repository at this point in the history
Mainly f-strings.
---------

Co-authored-by: Martin Rys <[email protected]>
  • Loading branch information
freemanjp and C0rn3j authored Feb 18, 2024
1 parent 6d3f0f6 commit d7f9817
Showing 1 changed file with 29 additions and 28 deletions.
57 changes: 29 additions & 28 deletions library/visual_studio_code_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@


def is_extension_installed(module, executable, name):
rc, out, err = module.run_command([executable, '--list-extensions', name])
if rc != 0 or err:
rc, stdout, stderr = module.run_command(
[executable, '--list-extensions', name])
if rc != 0 or stderr:
module.fail_json(
msg='Error querying installed extensions [%s]: %s' % (name,
out + err))
msg=(f'Error querying installed extensions [{name}]: '
f'{stdout + stderr }'))
lowername = name.lower()
match = next((x for x in out.splitlines() if x.lower() == lowername), None)
match = next((x for x in stdout.splitlines()
if x.lower() == lowername), None)
return match is not None


def list_extension_dirs(module, executable):
def list_extension_dirs(executable):
dirname = '.vscode'
if executable == 'code-insiders':
dirname += '-insiders'
Expand All @@ -40,44 +42,43 @@ def install_extension(module, executable, name):
if is_extension_installed(module, executable, name):
# Use the fact that extension directories names contain the version
# number
before_ext_dirs = list_extension_dirs(module, executable)
before_ext_dirs = list_extension_dirs(executable)
# Unfortunately `--force` suppresses errors (such as extension not
# found)
rc, out, err = module.run_command(
rc, stdout, stderr = module.run_command(
[executable, '--install-extension', name, '--force'])
# Whitelist: [DEP0005] DeprecationWarning: Buffer() is deprecated due
# to security and usability issues.
if rc != 0 or (err and '[DEP0005]' not in err):
if rc != 0 or (stderr and '[DEP0005]' not in stderr):
module.fail_json(
msg='Error while upgrading extension [%s]: (%d) %s' %
(name, rc, out + err))
after_ext_dirs = list_extension_dirs(module, executable)
msg=(f'Error while upgrading extension [{name}]: '
f'({rc}) {stdout + stderr}'))
after_ext_dirs = list_extension_dirs(executable)
changed = before_ext_dirs != after_ext_dirs
return changed, 'upgrade'
else:
rc, out, err = module.run_command(
rc, stdout, stderr = module.run_command(
[executable, '--install-extension', name])
# Whitelist: [DEP0005] DeprecationWarning: Buffer() is deprecated due
# to security and usability issues.
if rc != 0 or (err and '[DEP0005]' not in err):
if rc != 0 or (stderr and '[DEP0005]' not in stderr):
module.fail_json(
msg='Error while installing extension [%s]: (%d) %s' %
(name, rc, out + err))
changed = 'already installed' not in out
msg=(f'Error while installing extension [{name}]: '
f'({rc}) {stdout + stderr}'))
changed = 'already installed' not in stdout
return changed, 'install'


def uninstall_extension(module, executable, name):
if is_extension_installed(module, executable, name):
rc, out, err = module.run_command(
rc, stdout, stderr = module.run_command(
[executable, '--uninstall-extension', name])
if 'successfully uninstalled' not in (out + err):
if 'successfully uninstalled' not in (stdout + stderr):
module.fail_json(
msg=('Error while uninstalling extension [%s]:'
' unexpected response: %s') % (name, out + err))
msg=((f'Error while uninstalling extension [{name}] '
f'unexpected response: {stdout + stderr}')))
return True
else:
return False
return False


def run_module():
Expand Down Expand Up @@ -115,19 +116,19 @@ def run_module():
changed = uninstall_extension(module, executable, name)

if changed:
msg = '%s is now uninstalled' % name
msg = f'{name} is now uninstalled'
else:
msg = '%s is not installed' % name
msg = f'{name} is not installed'
else:
changed, change = install_extension(module, executable, name)

if changed:
if change == 'upgrade':
msg = '%s was upgraded' % name
msg = f'{name} was upgraded'
else:
msg = '%s is now installed' % name
msg = f'{name} is now installed'
else:
msg = '%s is already installed' % name
msg = f'{name} is already installed'

module.exit_json(changed=changed, msg=msg)

Expand Down

0 comments on commit d7f9817

Please sign in to comment.