0

I created a Python project that requires the camera to be enabled. In order to make it user friendly, I'm trying to enable the camera automatically without the user having to do it manually using raspi-config.

import configparser

def enable_camera():
    config_path = '/boot/config.txt'
    with open(config_path, 'r') as input_file:
        config_string = '[default]\n' + input_file.read()
    config = configparser.ConfigParser()
    config.read_string(config_string)
    config.set('default', 'start_x', '1')
    config.set('default', 'gpu_mem', '128')
    with open(config_path, 'w') as output_file:              
        config.write(output_file, space_around_delimiters=False)

After running the program this is the content of /boot/config/config.txt

[default]
dtparam=audio=on
start_x=1
gpu_mem=128
[pi4]
dtoverlay=vc4-fkms-v3d
max_framebuffers=2
[all]
4
  • 2
    Do you have a question? – joan Sep 4 '20 at 14:46
  • The issue was caused by configparser because it requires having sections so I wrote my own custom parser. – Amine Messaoudi Sep 4 '20 at 15:12
  • 3
    I’m voting to close this question because it's not a question. It's just to publish some python code the OP has created. – Dougie Sep 4 '20 at 16:05
  • Two things - you need to reboot to make the changes active and this is only valid for now. It's a lot simpler just to run github.com/RPi-Distro/raspi-config/blob/master/raspi-config than load your python program and run it, reboot and start esp as the process may be different on the computer modules and dual camera boards. Too risky in my book – user115418 Sep 6 '20 at 4:12
1

I was able to fix this by writing a custom config parser which does not require having sections. I hope that it will be useful for other readers

class ConfigParser:
    
    def __init__(self, path, line_delimiter='=', comment_delimiter='#'):
        self.path = path
        self.line_delimiter = line_delimiter
        self.comment_delimiter = comment_delimiter
        self.lines = self.split()
        
    def validate_line(self, line):
        return not line.strip().startswith(self.comment_delimiter) and line.count(self.line_delimiter) == 1
    
    def split(self):
        with open(self.path, 'r') as f:
            return f.read().splitlines()
        
    def set(self, key, value):
        flag = False
        for i in range(0, len(self.lines)):
            line = self.lines[i].replace(' ', '')
            if self.validate_line(line):
                k, v = line.split(self.line_delimiter)
                if key == k:
                    self.lines[i] = k + self.line_delimiter + value
                    flag = True
                    break
        if not flag:
            self.lines.append(key + self.line_delimiter + value)
                    
    def get(self, key):
        for i in range(0, len(self.lines)):
            line = self.lines[i].replace(' ', '')
            if self.validate_line(line):
                k, v = line.split(self.line_delimiter)
                if key == k:
                    return v
                
    def write(self, path):
        with open(path, 'w') as f:
            for line in self.lines:
                f.write("%s\n" % line)


if __name__ == '__main__':
    config = ConfigParser('config.txt')
    config.set('start_x', '1')
    config.set('gpu_mem', '128')
    config.write('config2.txt')

By default set() and get() ignore comments and only keep valid lines (key=value)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.