papi-web-org / papi-web

Papi-web
7 stars 3 forks source link

Using the mapping protocol for ConfigReader #6

Closed Amaras closed 11 months ago

Amaras commented 12 months ago

The documentation for ConfigParser recommends using the mapping protocol for all new projects instead of the legacy API.

This PR aims to change all cases of this pattern:

section = 'XXX'
# here self is a `ConfigParser` subclass instance
if self.has_section(section):
  key = 'YYY' 
  if self.has_option(section, key):
    var = self.get(section, key)
    # do something with var
  else:
    # log
else:
  # log and return

The goal is to use the following pattern instead:

section_key = 'XXX'
try:
  section = self[section_key]
except KeyError:
  # log and return

key = 'YYY'
try:
  var = section[key]
except TypeError:
  # log and return : not a section
except KeyError:
  # log: no option YYY
# use var

I have also added several comments on parts of the code that could (theoretically) lead to a crash or security vulnerability

Amaras commented 11 months ago

There are some TODOs left, but the small-scale test proved it works