Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def clean_node(n):
n.save()
if isinstance(n, node.Node):
for c in n.children:
c.save()
assert(not n.dirty)
return n
def test_fields(self):
n = node.Node(type_=node.NodeType.Note)
TZ = node.NodeTimestamps.int_to_dt(0)
SORT = 1
TEXT = 'Text'
ITEMPLACEMENT = node.NewListItemPlacementValue.Bottom
clean_node(n)
n.timestamps.created = TZ
self.assertTrue(n.dirty)
self.assertEqual(TZ, n.timestamps.created)
clean_node(n)
n.sort = SORT
self.assertTrue(n.dirty)
self.assertEqual(SORT, n.sort)
def test_delete(self):
n = node.Node(type_=node.NodeType.Note)
clean_node(n)
n.delete()
self.assertTrue(n.timestamps.deleted)
self.assertTrue(n.dirty)
# FIXME: Node is not done
def save(self, clean=True):
ret = super(Node, self).save(clean)
ret['id'] = self.id
ret['kind'] = 'notes#node'
ret['type'] = self.type.value
ret['parentId'] = self.parent_id
ret['sortValue'] = self._sort
if not self.moved and self._version is not None:
ret['baseVersion'] = self._version
ret['text'] = self._text
if self.server_id is not None:
ret['serverId'] = self.server_id
ret['timestamps'] = self.timestamps.save(clean)
ret['nodeSettings'] = self.settings.save(clean)
ret['annotationsGroup'] = self.annotations.save(clean)
return ret
self.touch()
@property
def new(self):
"""Get whether this node has been persisted to the server.
Returns:
bool: True if node is new.
"""
return self.server_id is None
@property
def dirty(self):
return super(Node, self).dirty or self.timestamps.dirty or self.annotations.dirty or self.settings.dirty or any((node.dirty for node in self.children))
class Root(Node):
"""Internal root node."""
ID = 'root'
def __init__(self):
super(Root, self).__init__(id_=self.ID)
@property
def dirty(self):
return False
class TopLevelNode(Node):
"""Top level node base class."""
_TYPE = None
def __init__(self, **kwargs):
super(TopLevelNode, self).__init__(parent_id=Root.ID, **kwargs)
self._color = ColorValue.White
self._archived = False
Returns:
list[gkeepapi.node.ListItem]: List items.
"""
return self._items(True)
@property
def unchecked(self):
"""Get all unchecked listitems.
Returns:
list[gkeepapi.node.ListItem]: List items.
"""
return self._items(False)
class ListItem(Node):
"""Represents a Google Keep listitem.
Interestingly enough, :class:`Note`s store their content in a single
child :class:`ListItem`.
"""
def __init__(self, parent_id=None, parent_server_id=None, super_list_item_id=None, **kwargs):
super(ListItem, self).__init__(type_=NodeType.ListItem, parent_id=parent_id, **kwargs)
self.parent_item = None
self.parent_server_id = parent_server_id
self.super_list_item_id = super_list_item_id
self.prev_super_list_item_id = None
self._subitems = {}
self._checked = False
def _load(self, raw):
super(ListItem, self)._load(raw)
self.prev_super_list_item_id = self.super_list_item_id
self._snapshot_fingerprint = raw['snapshotFingerprint'] if 'snapshotFingerprint' in raw else self._snapshot_fingerprint
self._thumbnail_generated_time = NodeTimestamps.str_to_dt(raw['thumbnailGeneratedTime']) if 'thumbnailGeneratedTime' in raw else NodeTimestamps.int_to_dt(0)
self._ink_hash = raw['inkHash'] if 'inkHash' in raw else ''
self._snapshot_proto_fprint = raw['snapshotProtoFprint'] if 'snapshotProtoFprint' in raw else self._snapshot_proto_fprint
def save(self, clean=True):
ret = super(NodeDrawingInfo, self).save(clean)
ret['drawingId'] = self.drawing_id
ret['snapshotData'] = self.snapshot.save(clean)
ret['snapshotFingerprint'] = self._snapshot_fingerprint
ret['thumbnailGeneratedTime'] = NodeTimestamps.dt_to_str(self._thumbnail_generated_time)
ret['inkHash'] = self._ink_hash
ret['snapshotProtoFprint'] = self._snapshot_proto_fprint
return ret
class Blob(Node):
"""Represents a Google Keep blob."""
_blob_type_map = {
BlobType.Audio: NodeAudio,
BlobType.Image: NodeImage,
BlobType.Drawing: NodeDrawing,
}
def __init__(self, parent_id=None, **kwargs):
super(Blob, self).__init__(type_=NodeType.Blob, parent_id=parent_id, **kwargs)
self.blob = None
@classmethod
def from_json(cls, raw):
"""Helper to construct a blob from a dict.
Args:
except (KeyError, ValueError) as e:
logger.warning('Unknown node type: %s', _type)
if DEBUG:
raise_from(exception.ParseException('Parse error for %s' % (_type), raw), e)
return None
node = ncls()
node.load(raw)
return node
if DEBUG:
Node.__load = Node._load # pylint: disable=protected-access
def _load(self, raw): # pylint: disable=missing-docstring
self.__load(raw) # pylint: disable=protected-access
self._find_discrepancies(raw) # pylint: disable=protected-access
Node._load = _load # pylint: disable=protected-access
ncls = None
_type = raw.get('type')
try:
ncls = _type_map[NodeType(_type)]
except (KeyError, ValueError) as e:
logger.warning('Unknown node type: %s', _type)
if DEBUG:
raise_from(exception.ParseException('Parse error for %s' % (_type), raw), e)
return None
node = ncls()
node.load(raw)
return node
if DEBUG:
Node.__load = Node._load # pylint: disable=protected-access
def _load(self, raw): # pylint: disable=missing-docstring
self.__load(raw) # pylint: disable=protected-access
self._find_discrepancies(raw) # pylint: disable=protected-access
Node._load = _load # pylint: disable=protected-access