def __init__(self, id=None, sid=None, name=None, networks=None, vertices=None, edges=None, tags=None, attributes=None, rest=None): self._conn = None self.id = id self.sid = sid self.name = name self.vertices = vertices self.edges = edges self.tags = tags self.attributes = attributes if networks is None: self.networks = [] elif not isinstance(networks, str): self.networks = list(networks) else: self.networks = [networks] if rest: self.rest = multidict(rest) else: self.rest = None
def _parse_text_response(response): """Parses a plain text formatted response from Nexus. Plain text formatted responses consist of key-value pairs, separated by C{":"}. Values may span multiple lines; in this case, the key is omitted after the first line and the extra lines start with whitespace. The response is assumed to be in UTF-8 encoding. Examples: >>> d = Nexus._parse_text_response("Id: 17\\nName: foo") >>> for key, value in sorted(d.items()): ... print("{0}={1}".format(key, value)) ... Id=17 Name=foo >>> d = Nexus._parse_text_response("Id: 42\\nName: foo\\n .\\n bar") >>> for key, value in sorted(d.items()): ... print("{0}={1}".format(key, value)) ... Id=42 Name=foo <BLANKLINE> bar """ if hasattr(response, "decode"): response = response.decode("utf-8") if isinstance(response, basestring): response = response.split("\n") result = multidict() key, value = None, [] for line in response: if hasattr(line, "decode"): line = line.decode("utf-8") line = line.rstrip() if not line: continue if key is not None and line[0] in ' \t': # Line continuation line = line.lstrip() if line == '.': line = '' value.append(line) else: # Key-value pair if key is not None: result.add(key, "\n".join(value)) key, value = line.split(":", 1) value = [value.strip()] if key is not None: result.add(key, "\n".join(value)) return result
def _parse_text_response(response): """Parses a plain text formatted response from Nexus. Plain text formatted responses consist of key-value pairs, separated by C{":"}. Values may span multiple lines; in this case, the key is omitted after the first line and the extra lines start with whitespace. Examples: >>> d = Nexus._parse_text_response("Id: 17\\nName: foo") >>> sorted(d.items()) [('Id', '17'), ('Name', 'foo')] >>> d = Nexus._parse_text_response("Id: 42\\nName: foo\\n .\\n bar") >>> sorted(d.items()) [('Id', '42'), ('Name', 'foo\\n\\nbar')] """ if isinstance(response, basestring): response = response.split("\n") result = multidict() key, value = None, [] for line in response: line = line.rstrip() if not line: continue if key is not None and line[0] in ' \t': # Line continuation line = line.lstrip() if line == '.': line = '' value.append(line) else: # Key-value pair if key is not None: result.add(key, "\n".join(value)) key, value = line.split(":", 1) value = [value.strip()] if key is not None: result.add(key, "\n".join(value)) return result
def _parse_text_response(response): """Parses a plain text formatted response from Nexus. Plain text formatted responses consist of key-value pairs, separated by C{":"}. Values may span multiple lines; in this case, the key is omitted after the first line and the extra lines start with whitespace. Examples: >>> d = Nexus._parse_text_response("Id: 17\\nName: foo") >>> sorted(d.items()) [('Id', '17'), ('Name', 'foo')] >>> d = Nexus._parse_text_response("Id: 42\\nName: foo\\n .\\n bar") >>> sorted(d.items()) [('Id', '42'), ('Name', 'foo\\n\\nbar')] """ if isinstance(response, str): response = response.split("\n") result = multidict() key, value = None, [] for line in response: line = line.rstrip() if not line: continue if key is not None and line[0] in ' \t': # Line continuation line = line.lstrip() if line == '.': line = '' value.append(line) else: # Key-value pair if key is not None: result.add(key, "\n".join(value)) key, value = line.split(":", 1) value = [value.strip()] if key is not None: result.add(key, "\n".join(value)) return result
def __init__(self, id=None, sid=None, name=None, networks=None, vertices=None, edges=None, tags=None, attributes=None, rest=None): self._conn = None self.id = id self.sid = sid self.name = name self.vertices = vertices self.edges = edges self.tags = tags self.attributes = attributes if networks is None: self.networks = [] elif not isinstance(networks, (str, unicode)): self.networks = list(networks) else: self.networks = [networks] if rest: self.rest = multidict(rest) else: self.rest = None
def _update_from_multidict(self, params): """Updates the dataset object from a multidict representation of key-value pairs, similar to the ones provided by the Nexus API in plain text response.""" self.id = params.get("id") self.sid = params.get("sid") self.name = params.get("name") self.vertices = params.get("vertices") self.edges = params.get("edges") self.tags = params.get("tags") networks = params.get("networks") if networks: self.networks = networks.split() keys_to_ignore = set( "id sid name vertices edges tags networks".split()) if self.vertices is None and self.edges is None: # Try "vertices/edges" self.vertices_edges = params.get("vertices/edges") keys_to_ignore.add("vertices/edges") if self.rest is None: self.rest = multidict() for k in set(params.keys()) - keys_to_ignore: for v in params.getlist(k): self.rest.add(k, v) if self.id: self.id = int(self.id) if self.vertices and not isinstance(self.vertices, (list, tuple)): self.vertices = int(self.vertices) if self.edges and not isinstance(self.edges, (list, tuple)): self.edges = int(self.edges) if self.tags is not None: self.tags = self.tags.split(";")
def _update_from_multidict(self, params): """Updates the dataset object from a multidict representation of key-value pairs, similar to the ones provided by the Nexus API in plain text response.""" self.id = params.get("id") self.sid = params.get("sid") self.name = params.get("name") self.vertices = params.get("vertices") self.edges = params.get("edges") self.tags = params.get("tags") networks = params.get("networks") if networks: self.networks = networks.split() keys_to_ignore = set("id sid name vertices edges tags networks".split()) if self.vertices is None and self.edges is None: # Try "vertices/edges" self.vertices_edges = params.get("vertices/edges") keys_to_ignore.add("vertices/edges") if self.rest is None: self.rest = multidict() for k in set(params.iterkeys()) - keys_to_ignore: for v in params.getlist(k): self.rest.add(k, v) if self.id: self.id = int(self.id) if self.vertices and not isinstance(self.vertices, (list, tuple)): self.vertices = int(self.vertices) if self.edges and not isinstance(self.edges, (list, tuple)): self.edges = int(self.edges) if self.tags is not None: self.tags = self.tags.split(";")