Skip to content

Commit

Permalink
Changes to use f-strings instead of format (#528)
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimmetz authored Oct 9, 2022
1 parent facefa9 commit c4ae057
Show file tree
Hide file tree
Showing 17 changed files with 235 additions and 254 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test_docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: test_docker
on: [push]
jobs:
test_fedora:
runs-on: ubuntu-22.04
runs-on: ubuntu-latest
strategy:
matrix:
version: ['36']
Expand Down Expand Up @@ -37,7 +37,7 @@ jobs:
python3 ./setup.py build
python3 ./setup.py install
test_ubuntu:
runs-on: ubuntu-22.04
runs-on: ubuntu-latest
strategy:
matrix:
version: ['22.04']
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test_docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ on:
- main
jobs:
build:
runs-on: ubuntu-22.04
runs-on: ubuntu-latest
strategy:
matrix:
include:
- python-version: '3.8'
toxenv: 'docs'
container:
image: ubuntu:20.04
image: ubuntu:22.04
steps:
- uses: actions/checkout@v2
- name: Set up container
Expand Down
6 changes: 2 additions & 4 deletions .github/workflows/test_tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ on:
- main
jobs:
build:
runs-on: ubuntu-22.04
runs-on: ubuntu-latest
strategy:
matrix:
include:
- python-version: '3.6'
toxenv: 'py36'
- python-version: '3.7'
toxenv: 'py37'
- python-version: '3.8'
Expand All @@ -26,7 +24,7 @@ jobs:
- python-version: '3.8'
toxenv: 'lint'
container:
image: ubuntu:20.04
image: ubuntu:22.04
steps:
- uses: actions/checkout@v2
- name: Set up container
Expand Down
1 change: 1 addition & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ disable=assignment-from-none,
locally-disabled,
locally-enabled,
logging-format-interpolation,
logging-fstring-interpolation,
metaclass-assignment,
missing-param-doc,
no-absolute-import,
Expand Down
4 changes: 2 additions & 2 deletions artifacts/artifact.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ def AppendSource(self, type_indicator, attributes):
type_indicator, attributes)
except (AttributeError, TypeError) as exception:
raise errors.FormatError((
'Unable to create source type: {0:s} for artifact definition: {1:s} '
'with error: {2!s}').format(type_indicator, self.name, exception))
f'Unable to create source type: {type_indicator:s} for artifact '
f'definition: {self.name:s} with error: {exception!s}'))

self.sources.append(source_object)
return source_object
Expand Down
49 changes: 22 additions & 27 deletions artifacts/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,16 @@ def _ReadSupportedOS(self, definition_values, definition_object, name):
"""
supported_os = definition_values.get('supported_os', [])
if not isinstance(supported_os, list):
supported_os_type = type(supported_os)
raise errors.FormatError(
'Invalid supported_os type: {0!s}'.format(type(supported_os)))
f'Invalid supported_os type: {supported_os_type!s}')

undefined_supported_os = set(supported_os).difference(self.supported_os)
if undefined_supported_os:
error_string = (
'Artifact definition: {0:s} undefined supported operating system: '
'{1:s}.').format(name, ', '.join(undefined_supported_os))
raise errors.FormatError(error_string)
undefined_supported_os = ', '.join(undefined_supported_os)
raise errors.FormatError((
f'Artifact definition: {name:s} undefined supported operating '
f'system: {undefined_supported_os:s}.'))

definition_object.supported_os = supported_os

Expand All @@ -142,13 +143,13 @@ def _ReadSources(self, artifact_definition_values, artifact_definition, name):
sources = artifact_definition_values.get('sources')
if not sources:
raise errors.FormatError(
'Invalid artifact definition: {0:s} missing sources.'.format(name))
f'Invalid artifact definition: {name:s} missing sources.')

for source in sources:
type_indicator = source.get('type', None)
if not type_indicator:
raise errors.FormatError(
'Invalid artifact definition: {0:s} source type.'.format(name))
f'Invalid artifact definition: {name:s} source type.')

attributes = source.get('attributes', None)

Expand All @@ -157,22 +158,21 @@ def _ReadSources(self, artifact_definition_values, artifact_definition, name):
type_indicator, attributes)
except errors.FormatError as exception:
raise errors.FormatError(
'Invalid artifact definition: {0:s}, with error: {1!s}'.format(
name, exception))
f'Invalid artifact definition: {name:s}, with error: {exception!s}')

# TODO: deprecate these left overs from the collector definition.
if source_type:
if source.get('returned_types', None):
raise errors.FormatError((
'Invalid artifact definition: {0:s} returned_types no longer '
'supported.').format(name))
f'Invalid artifact definition: {name:s} returned_types no longer '
f'supported.'))

self._ReadSupportedOS(source, source_type, name)
if set(source_type.supported_os) - set(
artifact_definition.supported_os):
raise errors.FormatError((
'Invalid artifact definition: {0:s} missing '
'supported_os.').format(name))
f'Invalid artifact definition: {name:s} missing '
f'supported_os.'))

def ReadArtifactDefinitionValues(self, artifact_definition_values):
"""Reads an artifact definition from a dictionary.
Expand All @@ -195,7 +195,7 @@ def ReadArtifactDefinitionValues(self, artifact_definition_values):
set(artifact_definition_values) - definitions.TOP_LEVEL_KEYS)
if different_keys:
different_keys = ', '.join(different_keys)
raise errors.FormatError('Undefined keys: {0:s}'.format(different_keys))
raise errors.FormatError(f'Undefined keys: {different_keys:s}')

name = artifact_definition_values.get('name', None)
if not name:
Expand All @@ -205,8 +205,7 @@ def ReadArtifactDefinitionValues(self, artifact_definition_values):
description = artifact_definition_values.get('doc', None)
if not description:
raise errors.FormatError(
'Invalid artifact definition: {0:s} missing description.'.format(
name))
f'Invalid artifact definition: {name:s} missing description.')

aliases = artifact_definition_values.get('aliases', None)

Expand All @@ -215,14 +214,12 @@ def ReadArtifactDefinitionValues(self, artifact_definition_values):

if artifact_definition_values.get('collectors', []):
raise errors.FormatError(
'Invalid artifact definition: {0:s} still uses collectors.'.format(
name))
f'Invalid artifact definition: {name:s} still uses collectors.')

urls = artifact_definition_values.get('urls', [])
if not isinstance(urls, list):
raise errors.FormatError(
'Invalid artifact definition: {0:s} urls is not a list.'.format(
name))
f'Invalid artifact definition: {name:s} urls is not a list.')

artifact_definition.provides = artifact_definition_values.get(
'provides', [])
Expand All @@ -245,7 +242,7 @@ def ReadDirectory(self, path, extension='yaml'):
ArtifactDefinition: an artifact definition.
"""
if extension:
glob_spec = os.path.join(path, '*.{0:s}'.format(extension))
glob_spec = os.path.join(path, f'*.{extension:s}')
else:
glob_spec = os.path.join(path, '*')

Expand Down Expand Up @@ -308,10 +305,9 @@ def ReadFileObject(self, file_object):
except errors.FormatError as exception:
error_location = 'At start'
if last_artifact_definition:
error_location = 'After: {0:s}'.format(last_artifact_definition.name)
error_location = f'After: {last_artifact_definition.name:s}'

raise errors.FormatError(
'{0:s} {1!s}'.format(error_location, exception))
raise errors.FormatError(f'{error_location:s} {exception!s}')

yield artifact_definition
last_artifact_definition = artifact_definition
Expand Down Expand Up @@ -343,10 +339,9 @@ def ReadFileObject(self, file_object):
except errors.FormatError as exception:
error_location = 'At start'
if last_artifact_definition:
error_location = 'After: {0:s}'.format(last_artifact_definition.name)
error_location = f'After: {last_artifact_definition.name:s}'

raise errors.FormatError(
'{0:s} {1!s}'.format(error_location, exception))
raise errors.FormatError(f'{error_location:s} {exception!s}')

yield artifact_definition
last_artifact_definition = artifact_definition
38 changes: 17 additions & 21 deletions artifacts/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ class ArtifactDefinitionsRegistry(object):
source_type.WindowsRegistryKeySourceType,
definitions.TYPE_INDICATOR_WINDOWS_REGISTRY_VALUE:
source_type.WindowsRegistryValueSourceType,
definitions.TYPE_INDICATOR_WMI_QUERY: source_type.WMIQuerySourceType,
}
definitions.TYPE_INDICATOR_WMI_QUERY: source_type.WMIQuerySourceType}

def __init__(self):
"""Initializes an artifact definitions registry."""
Expand All @@ -48,7 +47,7 @@ def CreateSourceType(cls, type_indicator, attributes):
"""
if type_indicator not in cls._source_type_classes:
raise errors.FormatError(
'Unsupported type indicator: {0:s}.'.format(type_indicator))
f'Unsupported type indicator: {type_indicator:s}.')

return cls._source_type_classes[type_indicator](**attributes)

Expand All @@ -65,14 +64,13 @@ def DeregisterDefinition(self, artifact_definition):
"""
artifact_definition_name = artifact_definition.name.lower()
if artifact_definition_name not in self._artifact_definitions_by_name:
raise KeyError(
'Artifact definition not set for name: {0:s}.'.format(
artifact_definition.name))
raise KeyError((
f'Artifact definition not set for name: '
f'{artifact_definition.name:s}.'))

for alias in artifact_definition.aliases:
if alias.lower() not in self._artifact_definitions_by_alias:
raise KeyError(
'Artifact definition not set for alias: {0:s}.'.format(alias))
raise KeyError(f'Artifact definition not set for alias: {alias:s}.')

del self._artifact_definitions_by_name[artifact_definition_name]

Expand All @@ -93,9 +91,9 @@ def DeregisterSourceType(cls, source_type_class):
indicator.
"""
if source_type_class.TYPE_INDICATOR not in cls._source_type_classes:
raise KeyError(
'Source type not set for type: {0:s}.'.format(
source_type_class.TYPE_INDICATOR))
raise KeyError((
f'Source type not set for type: '
f'{source_type_class.TYPE_INDICATOR:s}.'))

del cls._source_type_classes[source_type_class.TYPE_INDICATOR]

Expand Down Expand Up @@ -157,20 +155,18 @@ def RegisterDefinition(self, artifact_definition):
"""
artifact_definition_name = artifact_definition.name.lower()
if artifact_definition_name in self._artifact_definitions_by_name:
raise KeyError(
'Artifact definition already set for name: {0:s}.'.format(
artifact_definition.name))
raise KeyError((
f'Artifact definition already set for name: '
f'{artifact_definition.name:s}.'))

for alias in artifact_definition.aliases:
alias_lower = alias.lower()
if alias_lower in self._artifact_definitions_by_alias:
raise KeyError(
'Artifact definition already set for alias: {0:s}.'.format(alias))
raise KeyError(f'Artifact definition already set for alias: {alias:s}.')

if alias_lower in self._artifact_definitions_by_name:
raise KeyError(
'Artifact definition alias: {0:s} already used as name.'.format(
alias))
f'Artifact definition alias: {alias:s} already used as name.')

self._artifact_definitions_by_name[artifact_definition_name] = (
artifact_definition)
Expand All @@ -197,9 +193,9 @@ def RegisterSourceType(cls, source_type_class):
type indicator.
"""
if source_type_class.TYPE_INDICATOR in cls._source_type_classes:
raise KeyError(
'Source type already set for type: {0:s}.'.format(
source_type_class.TYPE_INDICATOR))
raise KeyError((
f'Source type already set for type: '
f'{source_type_class.TYPE_INDICATOR:s}.'))

cls._source_type_classes[source_type_class.TYPE_INDICATOR] = (
source_type_class)
Expand Down
27 changes: 12 additions & 15 deletions artifacts/source_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,7 @@ def ValidateKey(cls, key_path):
'HKEY_CURRENT_USER\\ is not supported instead use: '
'HKEY_USERS\\%%users.sid%%\\')

raise errors.FormatError(
'Unupported Registry key path: {0:s}'.format(key_path))
raise errors.FormatError(f'Unupported Registry key path: {key_path:s}')


class WindowsRegistryValueSourceType(SourceType):
Expand Down Expand Up @@ -327,12 +326,10 @@ def __init__(self, key_value_pairs=None):

if set(pair.keys()) != set(['key', 'value']):
key_value_pairs = ', '.join([
'{0:s}: {1:s}'.format(key, value) for key, value in key_value_pairs
])
error_message = (
'key_value_pair missing "key" and "value" keys, got: '
'{0:s}').format(key_value_pairs)
raise errors.FormatError(error_message)
f'{key:s}: {value:s}' for key, value in key_value_pairs])
raise errors.FormatError((
f'key_value_pair missing "key" and "value" keys, got: '
f'{key_value_pairs:s}'))

WindowsRegistryKeySourceType.ValidateKey(pair['key'])

Expand Down Expand Up @@ -421,7 +418,7 @@ def CreateSourceType(cls, type_indicator, attributes):
"""
if type_indicator not in cls._source_type_classes:
raise errors.FormatError(
'Unsupported type indicator: {0:s}.'.format(type_indicator))
f'Unsupported type indicator: {type_indicator:s}.')

return cls._source_type_classes[type_indicator](**attributes)

Expand All @@ -439,9 +436,9 @@ def DeregisterSourceType(cls, source_type_class):
indicator.
"""
if source_type_class.TYPE_INDICATOR not in cls._source_type_classes:
raise KeyError(
'Source type not set for type: {0:s}.'.format(
source_type_class.TYPE_INDICATOR))
raise KeyError((
f'Source type not set for type: '
f'{source_type_class.TYPE_INDICATOR:s}.'))

del cls._source_type_classes[source_type_class.TYPE_INDICATOR]

Expand Down Expand Up @@ -477,9 +474,9 @@ def RegisterSourceType(cls, source_type_class):
type indicator.
"""
if source_type_class.TYPE_INDICATOR in cls._source_type_classes:
raise KeyError(
'Source type already set for type: {0:s}.'.format(
source_type_class.TYPE_INDICATOR))
raise KeyError((
f'Source type already set for type: '
f'{source_type_class.TYPE_INDICATOR:s}.'))

cls._source_type_classes[source_type_class.TYPE_INDICATOR] = (
source_type_class)
Expand Down
11 changes: 10 additions & 1 deletion run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# -*- coding: utf-8 -*-
"""Script to run the tests."""

from __future__ import print_function

import sys
import unittest

Expand All @@ -12,7 +14,14 @@


if __name__ == '__main__':
print('Using Python version {0!s}'.format(sys.version))
print(f'Using Python version {sys.version!s}')

fail_unless_has_test_file = '--fail-unless-has-test-file' in sys.argv
setattr(unittest, 'fail_unless_has_test_file', fail_unless_has_test_file)
if fail_unless_has_test_file:
# Remove --fail-unless-has-test-file otherwise it will conflict with
# the argparse tests.
sys.argv.remove('--fail-unless-has-test-file')

dependency_helper = utils.dependencies.DependencyHelper()

Expand Down
Loading

0 comments on commit c4ae057

Please sign in to comment.