diff --git a/adafruit_platformdetect/__init__.py b/adafruit_platformdetect/__init__.py index b57395b4..b889d899 100644 --- a/adafruit_platformdetect/__init__.py +++ b/adafruit_platformdetect/__init__.py @@ -48,11 +48,10 @@ def get_cpuinfo_field(self, field): with open("/proc/cpuinfo", "r") as infile: cpuinfo = infile.read().split("\n") - infile.close() - for line in cpuinfo: - match = re.search(pattern, line, flags=re.IGNORECASE) - if match: - return match.group(1) + for line in cpuinfo: + match = re.search(pattern, line, flags=re.IGNORECASE) + if match: + return match.group(1) return None def check_dt_compatible_value(self, value): @@ -82,7 +81,6 @@ def get_armbian_release_field(self, field): match = re.search(pattern, line) if match: field_value = match.group(1) - release_file.close() except FileNotFoundError: pass @@ -93,60 +91,44 @@ def get_device_model(self): Search /proc/device-tree/model for the device model and return its value, if found, otherwise None. """ - model = None - try: with open("/proc/device-tree/model", "r") as model_file: - model = model_file.read() - model_file.close() + return model_file.read() except FileNotFoundError: pass - - return model + return None def get_device_compatible(self): """ Search /proc/device-tree/compatible for the compatible chip name. """ - model = None - try: with open("/proc/device-tree/compatible", "r") as model_file: - model = model_file.read() - model_file.close() + return model_file.read() except FileNotFoundError: pass - - return model + return None def check_board_asset_tag_value(self): """ Search /sys/devices/virtual/dmi/id for the device model and return its value, if found, otherwise None. """ - tag = None - try: with open("/sys/devices/virtual/dmi/id/board_asset_tag", "r") as tag_file: - tag = tag_file.read().strip() - tag_file.close() + return tag_file.read().strip() except FileNotFoundError: pass - - return tag + return None def check_board_name_value(self): """ Search /sys/devices/virtual/dmi/id for the board name and return its value, if found, otherwise None. Debian/ubuntu based """ - board_name = None - try: with open("/sys/devices/virtual/dmi/id/board_name", "r") as board_name_file: - board_name = board_name_file.read().strip() - board_name.close() + return board_name_file.read().strip() except FileNotFoundError: pass - - return board_name + return None