gabrielkrell / finsta

Web page running on remote Raspberry Pis for students to explore.
0 stars 0 forks source link

Move wrapper logic out of take_picture. #8

Open gabrielkrell opened 6 years ago

gabrielkrell commented 6 years ago

Move code unlikely to be modified by students out of the files. Right now this code determines the file location and handles retries:

def get_full_image_name(filename):
    """Returns the path to a new image with the given filename.
       You shouldn't change this function when making your own script.
    """
    scripts_dir = os.path.dirname(os.path.realpath(__file__))
    return os.path.join(
        scripts_dir, os.path.pardir, 'static', 'images', filename)

def start_camera():
    """Starts up the camera, retrying if it's currently in use.
       You shouldn't change this function when making your own script.
    """
    while True:
        try:
            return PiCamera(resolution=(600, 600))
            break
        except PiCameraMMALError:
            print('Someone else is using the camera. Retrying...')
            sleep(1)

Probably, the try-finally wrapper around the main body should be reduced to an imported with as well.

gabrielkrell commented 6 years ago

See simplify-take_picture branch. Needs to be tested.

gabrielkrell commented 5 years ago

Apparently Python doesn't much like this monkey-patching. Maybe it'd be better to make an entirely new object based off it:

Traceback (most recent call last):
  File "take_picture.py", line 35, in <module>
    click()
  File "take_picture.py", line 15, in click
    with shielded_camera() as camera:
  File "/usr/lib/python3.5/contextlib.py", line 59, in __enter__
    return next(self.gen)
  File "/opt/finsta/camera_scripts/helpful_tools.py", line 34, in shielded_camera
    camera._capture = camera.capture
AttributeError: 'PiCamera' object has no attribute '_capture'