| 1 |  |  | 
 | 
            
                                                        
            
                                    
            
            
                | 2 |  |  | class ConnectorObject(object):
 | 
            
                                                        
            
                                    
            
            
                | 3 |  |  |     """Users, devices and streams are all built upon the base `ConnectorObject`.
 | 
            
                                                        
            
                                    
            
            
                | 4 |  |  |     The methods from ConnectorObject can be accessed from any user, device or stream.
 | 
            
                                                        
            
                                    
            
            
                | 5 |  |  | 
 | 
            
                                                        
            
                                    
            
            
                | 6 |  |  |     Do not use this object directly. The API is accessed using the ConnectorDB class (below).
 | 
            
                                                        
            
                                    
            
            
                | 7 |  |  |     """
 | 
            
                                                        
            
                                    
            
            
                | 8 |  |  | 
 | 
            
                                                        
            
                                    
            
            
                | 9 |  |  |     def __init__(self, database_connection, object_path):
 | 
            
                                                        
            
                                    
            
            
                | 10 |  |  |         self.db = database_connection
 | 
            
                                                        
            
                                    
            
            
                | 11 |  |  |         self.path = object_path
 | 
            
                                                        
            
                                    
            
            
                | 12 |  |  | 
 | 
            
                                                        
            
                                    
            
            
                | 13 |  |  |         # Metadata represents the object's json representation
 | 
            
                                                        
            
                                    
            
            
                | 14 |  |  |         self.metadata = None
 | 
            
                                                        
            
                                    
            
            
                | 15 |  |  | 
 | 
            
                                                        
            
                                    
            
            
                | 16 |  |  |     def refresh(self):
 | 
            
                                                        
            
                                    
            
            
                | 17 |  |  |         """Refresh reloads data from the server. It raises an error if it fails to get the object's metadata"""
 | 
            
                                                        
            
                                    
            
            
                | 18 |  |  |         self.metadata = self.db.read(self.path).json()
 | 
            
                                                        
            
                                    
            
            
                | 19 |  |  | 
 | 
            
                                                        
            
                                    
            
            
                | 20 |  |  |     @property
 | 
            
                                                        
            
                                    
            
            
                | 21 |  |  |     def data(self):
 | 
            
                                                        
            
                                    
            
            
                | 22 |  |  |         """Returns the raw dict representing metadata"""
 | 
            
                                                        
            
                                    
            
            
                | 23 |  |  |         if self.metadata is None:
 | 
            
                                                        
            
                                    
            
            
                | 24 |  |  |             self.refresh()
 | 
            
                                                        
            
                                    
            
            
                | 25 |  |  |         return self.metadata
 | 
            
                                                        
            
                                    
            
            
                | 26 |  |  | 
 | 
            
                                                        
            
                                    
            
            
                | 27 |  |  |     def delete(self):
 | 
            
                                                        
            
                                    
            
            
                | 28 |  |  |         """Deletes the user/device/stream"""
 | 
            
                                                        
            
                                    
            
            
                | 29 |  |  |         self.db.delete(self.path)
 | 
            
                                                        
            
                                    
            
            
                | 30 |  |  | 
 | 
            
                                                        
            
                                    
            
            
                | 31 |  |  |     def exists(self):
 | 
            
                                                        
            
                                    
            
            
                | 32 |  |  |         """returns true if the object exists, and false otherwise. This is useful for creating streams
 | 
            
                                                        
            
                                    
            
            
                | 33 |  |  |         if they exist::
 | 
            
                                                        
            
                                    
            
            
                | 34 |  |  | 
 | 
            
                                                        
            
                                    
            
            
                | 35 |  |  |             cdb = connectordb.ConnectorDB("myapikey")
 | 
            
                                                        
            
                                    
            
            
                | 36 |  |  | 
 | 
            
                                                        
            
                                    
            
            
                | 37 |  |  |             mystream = cdb["mystream"]
 | 
            
                                                        
            
                                    
            
            
                | 38 |  |  | 
 | 
            
                                                        
            
                                    
            
            
                | 39 |  |  |             if not mystream.exists():
 | 
            
                                                        
            
                                    
            
            
                | 40 |  |  |                 mystream.create({"type":"string"})
 | 
            
                                                        
            
                                    
            
            
                | 41 |  |  | 
 | 
            
                                                        
            
                                    
            
            
                | 42 |  |  |         """
 | 
            
                                                        
            
                                    
            
            
                | 43 |  |  |         try:
 | 
            
                                                        
            
                                    
            
            
                | 44 |  |  |             self.refresh()
 | 
            
                                                        
            
                                    
            
            
                | 45 |  |  |         except:
 | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |             return False
 | 
            
                                                        
            
                                    
            
            
                | 47 |  |  |         return True
 | 
            
                                                        
            
                                    
            
            
                | 48 |  |  | 
 | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |     def set(self, property_dict):
 | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |         """Attempts to set the given properties of the object.
 | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |         An example of this is setting the nickname of the object::
 | 
            
                                                        
            
                                    
            
            
                | 52 |  |  | 
 | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |             cdb.set({"nickname": "My new nickname"})
 | 
            
                                                        
            
                                    
            
            
                | 54 |  |  | 
 | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |         note that there is a convenience property `cdb.nickname` that allows you to get/set the nickname directly.
 | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |         """
 | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |         self.metadata = self.db.update(self.path, property_dict).json()
 | 
            
                                                        
            
                                    
            
            
                | 58 |  |  | 
 | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |     @property
 | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |     def name(self):
 | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |         """Returns the object's name. Object names are immutable (unless logged in is a database admin)"""
 | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |         return self.data["name"]
 | 
            
                                                        
            
                                    
            
            
                | 63 |  |  | 
 | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |     @property
 | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |     def nickname(self):
 | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |         """Allows to directly set the object's user-friendly nickname.
 | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |         Usage is as a property::
 | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |             cdb.nickname = "My Nickname!"
 | 
            
                                                        
            
                                    
            
            
                | 69 |  |  | 
 | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |             print cdb.nickname
 | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |         """
 | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |         if "nickname" in self.data:
 | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |             return self.data["nickname"]
 | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |         return None
 | 
            
                                                        
            
                                    
            
            
                | 75 |  |  | 
 | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |     @nickname.setter
 | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |     def nickname(self, new_nickname):
 | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |         """Sets the object's user-friendly nickname"""
 | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |         self.set({"nickname": new_nickname})
 | 
            
                                                        
            
                                    
            
            
                | 80 |  |  | 
 | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |     @property
 | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |     def description(self):
 | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |         """Allows to directly set the object's description. Use as a property"""
 | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |         if "description" in self.data:
 | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |             return self.data["description"]
 | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |         return None
 | 
            
                                                        
            
                                    
            
            
                | 87 |  |  | 
 | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |     @description.setter
 | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |     def description(self, new_description):
 | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |         """Sets the object's description"""
 | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |         self.set({"description": new_description})
 | 
            
                                                        
            
                                    
            
            
                | 92 |  |  | 
 | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |     @property
 | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |     def icon(self):
 | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |         """Allows to directly get and set the icon. An icon can be URLencoded (data:image/)
 | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |         or use an icon from the material design set (https://material.io/icons/), 
 | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |         prepended with "material:", and with spaces replaced by underscores.
 | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |         """
 | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |         if "icon" in self.data:
 | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |             return self.data["icon"]
 | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |         return None
 | 
            
                                                        
            
                                    
            
            
                | 102 |  |  | 
 | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |     @icon.setter
 | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |     def icon(self, new_icon):
 | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |         """Sets the object's icon"""
 | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |         self.set({"icon": new_icon})
 | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |  |