Tested on:RPi3; Raspbian Stretch; Python3
Prerequisites:Raspberry Pi with Raspbian Jessie and internet connection (see steps 1-7 in the Introduction)
0

Decorator for easy access to protected Raspberry Pi system files

Scripts for autmatically installing software on a Raspberry Pi often have problems with reading protected files, like wpa_supplicant.conf for defining wifi networks. To solve this, a little Python decorator can be used to give full access to protected files.

Warning
Using this decorator incorrectly can result in damaging important system files. Therefore, make a backup of the target files before experimenting with the decorator!

Installation and Test
Enter in the terminal

The current content of the file wpa_supplicant.conf is shown in the terminal. For this demo, the file is only being read for not accidently damaging it.

Explanation
Python decorators wrap a function in additional functionality:

The decorator @write_access causes the function write_access to be processed before read_protected_file. read_protected_file is passed as func to func_wrapper and executed therein.
func_wrapper changes the owner and the permissions to allow access to protected files:

Full script
Click on Full Script to show all of the code.

Links
Decorators on ReadThedocs.com: https://pythonguide.readthedocs.io/en/latest/python/decorator.html

Knight of Pi

Johannes Bergs aka Knight of Pi. Diploma in Bioinformatics, some Webdesign and Python coding then. Living in the beautiful city of Vienna.

Comment

Time limit is exhausted. Please reload CAPTCHA.