Clean up more docs to work with sphinx.
[python_utils.git] / base_presence.py
index 5984b416558d8959bb2bc8859bd32de9ad5dc95b..4b8791d3b0b2220c6d61b7392a3e71a905d152ce 100755 (executable)
@@ -47,10 +47,15 @@ cfg.add_argument(
 
 
 class PresenceDetection(object):
-    """See above.  This is a base class for determining a person's
-    location on networks I administer."""
+    """This is a module dealing with trying to guess a person's location
+    based on the location of certain devices (e.g. phones, laptops)
+    belonging to that person.  It works with networks I run that log
+    device MAC addresses active.
+    """
 
     def __init__(self) -> None:
+        """C'tor"""
+
         # Note: list most important devices first.
         self.devices_by_person: Dict[Person, List[str]] = {
             Person.SCOTT: [
@@ -87,6 +92,10 @@ class PresenceDetection(object):
         self.last_update: Optional[datetime.datetime] = None
 
     def maybe_update(self) -> None:
+        """Determine if our state is stale and needs to be updated and do
+        it, if so.
+        """
+
         if self.last_update is None:
             self.update()
         else:
@@ -102,29 +111,34 @@ class PresenceDetection(object):
                 self.update()
 
     def update(self) -> None:
+        """Unconditionally update our state."""
+
         self.dark_locations = set()
         if self.run_location is Location.HOUSE:
-            self.update_from_house()
+            self._update_from_house()
         elif self.run_location is Location.CABIN:
-            self.update_from_cabin()
+            self._update_from_cabin()
         else:
             raise Exception("Where the hell is this running?!")
         self.last_update = datetime.datetime.now()
 
-    def update_from_house(self) -> None:
+    def _update_from_house(self) -> None:
+        """Internal method for updating from code running on the house
+        network."""
+
         from exec_utils import cmd
 
         try:
             persisted_macs = config.config['presence_macs_file']
         except KeyError:
             persisted_macs = '/home/scott/cron/persisted_mac_addresses.txt'
-        self.read_persisted_macs_file(persisted_macs, Location.HOUSE)
+        self._read_persisted_macs_file(persisted_macs, Location.HOUSE)
         try:
             raw = cmd(
                 "ssh [email protected] 'cat /home/scott/cron/persisted_mac_addresses.txt'",
                 timeout_seconds=20.0,
             )
-            self.parse_raw_macs_file(raw, Location.CABIN)
+            self._parse_raw_macs_file(raw, Location.CABIN)
         except Exception as e:
             logger.exception(e)
             msg = "Can't see the cabin right now; presence detection impared."
@@ -132,20 +146,23 @@ class PresenceDetection(object):
             logger.warning(msg, stacklevel=2)
             self.dark_locations.add(Location.CABIN)
 
-    def update_from_cabin(self) -> None:
+    def _update_from_cabin(self) -> None:
+        """Internal method for updating from code running on the cabing
+        network."""
+
         from exec_utils import cmd
 
         try:
             persisted_macs = config.config['presence_macs_file']
         except KeyError:
             persisted_macs = '/home/scott/cron/persisted_mac_addresses.txt'
-        self.read_persisted_macs_file(persisted_macs, Location.CABIN)
+        self._read_persisted_macs_file(persisted_macs, Location.CABIN)
         try:
             raw = cmd(
-                "ssh scott@wennabe.house 'cat /home/scott/cron/persisted_mac_addresses.txt'",
+                "ssh scott@wannabe.house 'cat /home/scott/cron/persisted_mac_addresses.txt'",
                 timeout_seconds=10.0,
             )
-            self.parse_raw_macs_file(raw, Location.HOUSE)
+            self._parse_raw_macs_file(raw, Location.HOUSE)
         except Exception as e:
             logger.exception(e)
             msg = "Can't see the house right now; presence detection impared."
@@ -153,14 +170,25 @@ class PresenceDetection(object):
             warnings.warn(msg, stacklevel=2)
             self.dark_locations.add(Location.HOUSE)
 
-    def read_persisted_macs_file(self, filename: str, location: Location) -> None:
+    def _read_persisted_macs_file(self, filename: str, location: Location) -> None:
+        """Internal method that, Given a filename that contains MAC addresses
+        seen on the network recently, reads it in and calls
+        _parse_raw_macs_file with the contents.
+
+        Args:
+            filename: The name of the file to read
+            location: The location we're reading from
+
+        """
         if location is Location.UNKNOWN:
             return
         with open(filename, "r") as rf:
             lines = rf.read()
-        self.parse_raw_macs_file(lines, location)
+        self._parse_raw_macs_file(lines, location)
+
+    def _parse_raw_macs_file(self, raw: str, location: Location) -> None:
+        """Internal method that parses the contents of the MACs file."""
 
-    def parse_raw_macs_file(self, raw: str, location: Location) -> None:
         lines = raw.split("\n")
 
         # CC:F4:11:D7:FA:EE, 2240, 10.0.0.22 (side_deck_high_home), Google, 1611681990
@@ -195,6 +223,16 @@ class PresenceDetection(object):
             self.weird_mac_at_cabin = True
 
     def is_anyone_in_location_now(self, location: Location) -> bool:
+        """Determine if anyone is in a given location based on the presence of
+        MAC files seen recently on the network.
+
+        Args:
+            location: the location in question
+
+        Returns:
+            True if someone is detected or False otherwise.
+        """
+
         self.maybe_update()
         if location in self.dark_locations:
             raise Exception(f"Can't see {location} right now; answer undefined.")
@@ -208,6 +246,16 @@ class PresenceDetection(object):
         return False
 
     def where_is_person_now(self, name: Person) -> Location:
+        """Given a person, see if we can determine their location based on
+        network MAC addresses.
+
+        Args:
+            name: The person we're looking for.
+
+        Returns:
+            The Location where we think they are (including UNKNOWN).
+        """
+
         self.maybe_update()
         if len(self.dark_locations) > 0:
             msg = f"Can't see {self.dark_locations} right now; answer confidence impacted"