""" class for PhotoInfo exposing SearchInfo data such as labels """from._constantsimport_PHOTOS_4_VERSION,search_category_factory__all__=["SearchInfo"]
[docs]classSearchInfo:"""Info about search terms such as machine learning labels that Photos knows about a photo"""def__init__(self,photo,normalized=False):"""photo: PhotoInfo object normalized: if True, all properties return normalized (lower case) results"""ifphoto._db._db_version<=_PHOTOS_4_VERSION:raiseNotImplementedError("search info not implemented for this database version")self._categories=search_category_factory(photo._db._photos_ver)self._photo=photoself._normalized=normalizedself.uuid=photo.uuidtry:# get search info for this UUID# there might not be any search info data (e.g. if Photo was missing or photoanalysisd not run yet)self._db_searchinfo=photo._db._db_searchinfo_uuid[self.uuid]exceptKeyError:self._db_searchinfo=None@propertydeflabels(self):"""return list of labels associated with Photo"""returnself._get_text_for_category(self._categories.LABEL)@propertydefplace_names(self):"""returns list of place names"""returnself._get_text_for_category(self._categories.PLACE_NAME)@propertydefstreets(self):"""returns list of street names"""returnself._get_text_for_category(self._categories.STREET)@propertydefneighborhoods(self):"""returns list of neighborhoods"""returnself._get_text_for_category(self._categories.NEIGHBORHOOD)@propertydeflocality_names(self):"""returns list of other locality names"""locality=[]forcategoryinself._categories.ALL_LOCALITY:locality+=self._get_text_for_category(category)returnlocality@propertydefcity(self):"""returns city/town"""city=self._get_text_for_category(self._categories.CITY)returncity[0]ifcityelse""@propertydefstate(self):"""returns state name"""state=self._get_text_for_category(self._categories.STATE)returnstate[0]ifstateelse""@propertydefstate_abbreviation(self):"""returns state abbreviation"""abbrev=self._get_text_for_category(self._categories.STATE_ABBREVIATION)returnabbrev[0]ifabbrevelse""@propertydefcountry(self):"""returns country name"""country=self._get_text_for_category(self._categories.COUNTRY)returncountry[0]ifcountryelse""@propertydefmonth(self):"""returns month name"""month=self._get_text_for_category(self._categories.MONTH)returnmonth[0]ifmonthelse""@propertydefyear(self):"""returns year"""year=self._get_text_for_category(self._categories.YEAR)returnyear[0]ifyearelse""@propertydefbodies_of_water(self):"""returns list of body of water names"""returnself._get_text_for_category(self._categories.BODY_OF_WATER)@propertydefholidays(self):"""returns list of holiday names"""returnself._get_text_for_category(self._categories.HOLIDAY)@propertydefactivities(self):"""returns list of activity names"""returnself._get_text_for_category(self._categories.ACTIVITY)@propertydefseason(self):"""returns season name"""season=self._get_text_for_category(self._categories.SEASON)returnseason[0]ifseasonelse""@propertydefvenues(self):"""returns list of venue names"""returnself._get_text_for_category(self._categories.VENUE)@propertydefvenue_types(self):"""returns list of venue types"""returnself._get_text_for_category(self._categories.VENUE_TYPE)@propertydefmedia_types(self):"""returns list of media types (photo, video, panorama, etc)"""types=[]forcategoryinself._categories.MEDIA_TYPES:types+=self._get_text_for_category(category)returntypes@propertydefdetected_text(self):"""Returns text detected in the photo (macOS 13+ / Photos 8+ only)"""ifself._photo._db._photos_ver<8:return[]returnself._get_text_for_category(self._categories.DETECTED_TEXT)@propertydeftext_found(self):"""Returns True if photos has detected text (macOS 13+ / Photos 8+ only)"""ifself._photo._db._photos_ver<8:return[]returnself._get_text_for_category(self._categories.TEXT_FOUND)@propertydefcamera(self):"""returns camera name (macOS 13+ / Photos 8+ only)"""ifself._photo._db._photos_ver<8:return""camera=self._get_text_for_category(self._categories.CAMERA)returncamera[0]ifcameraelse""@propertydefsource(self):"""returns source of the photo (e.g. "Messages", "Safar", etc) (macOS 13+ / Photos 8+ only)"""ifself._photo._db._photos_ver<8:return""source=self._get_text_for_category(self._categories.SOURCE)returnsource[0]ifsourceelse""@propertydefall(self):"""return all search info properties in a single list"""all_=(self.labels+self.place_names+self.streets+self.neighborhoods+self.locality_names+self.bodies_of_water+self.holidays+self.activities+self.venues+self.venue_types+self.media_types+self.detected_text)ifself.city:all_+=[self.city]ifself.state:all_+=[self.state]ifself.state_abbreviation:all_+=[self.state_abbreviation]ifself.country:all_+=[self.country]ifself.month:all_+=[self.month]ifself.year:all_+=[self.year]ifself.season:all_+=[self.season]ifself.camera:all_+=[self.camera]returnall_
[docs]defasdict(self):"""return dict of search info"""return{"labels":self.labels,"place_names":self.place_names,"streets":self.streets,"neighborhoods":self.neighborhoods,"city":self.city,"locality_names":self.locality_names,"state":self.state,"state_abbreviation":self.state_abbreviation,"country":self.country,"bodies_of_water":self.bodies_of_water,"month":self.month,"year":self.year,"holidays":self.holidays,"activities":self.activities,"season":self.season,"venues":self.venues,"venue_types":self.venue_types,"media_types":self.media_types,"detected_text":self.detected_text,"camera":self.camera,"source":self.source,}
def_get_text_for_category(self,category):"""return list of text for a specified category ID"""ifself._db_searchinfo:content="normalized_string"ifself._normalizedelse"content_string"returnsorted([rec[content]forrecinself._db_searchinfoifrec["category"]==category])else:return[]