Adding type annotations and fixing up formatting.
[kiosk.git] / local_photos_mirror_renderer.py
index 2e5499dcc4a472559633ac88fc75aa64fdfbc0f2..da3b9e75c3abbc2f2e81b6a63c6226045436fba0 100644 (file)
@@ -1,8 +1,12 @@
+#!/usr/bin/env python3
+
 import os
-import file_writer
-import renderer
 import random
 import re
+from typing import List, Dict
+
+import file_writer
+import renderer
 
 
 class local_photos_mirror_renderer(renderer.debuggable_abstaining_renderer):
@@ -57,14 +61,14 @@ class local_photos_mirror_renderer(renderer.debuggable_abstaining_renderer):
         ]
     )
 
-    def __init__(self, name_to_timeout_dict):
+    def __init__(self, name_to_timeout_dict: Dict[str, int]) -> None:
         super(local_photos_mirror_renderer, self).__init__(name_to_timeout_dict, False)
         self.candidate_photos = set()
 
-    def debug_prefix(self):
+    def debug_prefix(self) -> str:
         return "local_photos_mirror"
 
-    def periodic_render(self, key):
+    def periodic_render(self, key: str) -> bool:
         if key == "Index Photos":
             return self.index_photos()
         elif key == "Choose Photo":
@@ -72,50 +76,49 @@ class local_photos_mirror_renderer(renderer.debuggable_abstaining_renderer):
         else:
             raise error("Unexpected operation")
 
-    def album_is_in_whitelist(self, name):
+    def album_is_in_whitelist(self, name: str) -> bool:
         for wlalbum in self.album_whitelist:
             if re.search("\d+ %s" % wlalbum, name) != None:
                 return True
         return False
 
-    # Walk the filesystem looking for photos in whitelisted albums and
-    # keep their paths in memory.
-    def index_photos(self):
+    def index_photos(self) -> bool:
+        """Walk the filesystem looking for photos in whitelisted albums and
+        keep their paths in memory.
+        """
         for root, subdirs, files in os.walk(self.album_root_directory):
             last_dir = root.rsplit("/", 1)[1]
             if self.album_is_in_whitelist(last_dir):
-                for x in files:
-                    extension = x.rsplit(".", 1)[1]
+                for filename in files:
+                    extension = filename.rsplit(".", 1)[1]
                     if extension in self.extension_whitelist:
-                        photo_path = os.path.join(root, x)
+                        photo_path = os.path.join(root, filename)
                         photo_url = photo_path.replace(
                             "/usr/local/export/www/", "http://10.0.0.18/", 1
                         )
                         self.candidate_photos.add(photo_url)
         return True
 
-    # Pick one of the cached URLs and build a page.
     def choose_photo(self):
+        """Pick one of the cached URLs and build a page."""
         if len(self.candidate_photos) == 0:
             print("No photos!")
             return False
         path = random.sample(self.candidate_photos, 1)[0]
-        f = file_writer.file_writer("photo_23_3600.html")
-        f.write(
-            """
+        with file_writer.file_writer("photo_23_3600.html") as f:
+            f.write(
+                """
 <style>
 body{background-color:#303030;}
 div#time{color:#dddddd;}
 div#date{color:#dddddd;}
 </style>
 <center>"""
-        )
-        f.write(
-            '<img src="%s" style="display:block;max-width=800;max-height:600;width:auto;height:auto">'
-            % path
-        )
-        f.write("</center>")
-        f.close()
+            )
+            f.write(
+                f'<img src="{path}" style="display:block;max-width=800;max-height:600;width:auto;height:auto">'
+            )
+            f.write("</center>")
         return True