Skip to content

Commit

Permalink
Fix newly reported pylint issues
Browse files Browse the repository at this point in the history
Change-Id: Ie2fbd510ceefee844f3dbdbd6f4ce763beeb31da
  • Loading branch information
volans- committed Aug 27, 2021
1 parent 33054d3 commit 116e8f4
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 deletions.
6 changes: 3 additions & 3 deletions cumin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ def parse_config(config_file):
"""
try:
with open(os.path.expanduser(config_file), 'r') as f:
with open(os.path.expanduser(config_file), 'r', encoding='utf8') as f:
config = yaml.safe_load(f)
except IOError as e:
raise CuminError('Unable to read configuration file: {message}'.format(message=e))
raise CuminError('Unable to read configuration file: {message}'.format(message=e)) from e
except yaml.parser.ParserError as e:
raise CuminError("Unable to parse configuration file '{config}':\n{message}".format(
config=config_file, message=e))
config=config_file, message=e)) from e

if config is None:
config = {}
Expand Down
2 changes: 1 addition & 1 deletion cumin/backends/knownhosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def _load_known_hosts(self):

for filename in known_hosts_filenames:
hosts = set()
with open(filename, 'r') as known_hosts_file:
with open(filename, 'r', encoding='utf8') as known_hosts_file:
for lineno, line in enumerate(known_hosts_file, 1):
try:
found, skipped = KnownHostsQuery.parse_known_hosts_line(line)
Expand Down
10 changes: 5 additions & 5 deletions cumin/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,25 +123,25 @@ def _import_backend(module, available_backends):
backend = importlib.import_module(module)
except ImportError as e:
if not module.startswith(INTERNAL_BACKEND_PREFIX):
raise CuminError("Unable to import backend '{module}': {e}".format(module=module, e=e))
raise CuminError("Unable to import backend '{module}': {e}".format(module=module, e=e)) from e

return (None, None) # Internal backend not available, are all the dependencies installed?

name = module.split('.')[-1]
message = "Unable to register backend '{name}' in module '{module}'".format(name=name, module=module)
try:
keyword = backend.GRAMMAR_PREFIX
except AttributeError:
raise CuminError('{message}: GRAMMAR_PREFIX module attribute not found'.format(message=message))
except AttributeError as e:
raise CuminError('{message}: GRAMMAR_PREFIX module attribute not found'.format(message=message)) from e

if keyword in available_backends:
raise CuminError(("{message}: keyword '{key}' already registered: {backends}").format(
message=message, key=keyword, backends=available_backends))

try:
class_obj = backend.query_class
except AttributeError:
raise CuminError('{message}: query_class module attribute not found'.format(message=message))
except AttributeError as e:
raise CuminError('{message}: query_class module attribute not found'.format(message=message)) from e

if not issubclass(class_obj, backends.BaseQuery):
raise CuminError('{message}: query_class module attribute is not a subclass of cumin.backends.BaseQuery')
Expand Down
8 changes: 5 additions & 3 deletions cumin/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ def execute(self, query_string):
try: # No default backend set, using directly the global grammar
return super().execute(query_string)
except ParseException as e:
raise InvalidQueryError(("Unable to parse the query '{query}' with the global grammar and no "
"default backend is set:\n{error}").format(query=query_string, error=e))
raise InvalidQueryError(
("Unable to parse the query '{query}' with the global grammar and no "
"default backend is set:\n{error}").format(query=query_string, error=e)) from e

try: # Default backend set, trying it first
hosts = self._query_default_backend(query_string)
Expand All @@ -65,7 +66,8 @@ def execute(self, query_string):
raise InvalidQueryError(
("Unable to parse the query '{query}' neither with the default backend '{name}' nor with the "
"global grammar:\n{name}: {e_def}\nglobal: {e_glob}").format(
query=query_string, name=self.config['default_backend'], e_def=e_default, e_glob=e_global))
query=query_string, name=self.config['default_backend'], e_def=e_default, e_glob=e_global)
) from e_global

return hosts

Expand Down
2 changes: 1 addition & 1 deletion cumin/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def get_fixture(path, as_string=False):
as_string: return the content as a multiline string instead of a list of lines [optional, default: False]
"""
with open(get_fixture_path(path)) as f:
with open(get_fixture_path(path), encoding='utf8') as f:
if as_string:
content = f.read()
else:
Expand Down

0 comments on commit 116e8f4

Please sign in to comment.