def to_jsonschema(obj): """Schema are stored in OpenAPI spec and might need some massaging to make for valid JSON Schema.""" if is_mapping(obj): # Re-write nullable fields: type_ = obj.get("type") if obj.get("nullable", False): type_ = obj.pop("type", None) format_ = obj.pop("format", None) obj["oneOf"] = [ { "type": "null" }, { "type": type_, "format": format_ }, ] obj.pop("nullable", None) out = {} for key, value in obj.items(): out[key] = to_jsonschema(value) return out if is_listish(obj): return [to_jsonschema(o) for o in obj] return obj
def apply_filters(self, q): for col, val in self.filters: if is_listish(val): q = q.where(self.get_column(col).in_(val)) else: q = q.where(self.get_column(col) == val) for col, val in self.filters_not: if is_listish(val): q = q.where(self.get_column(col).notin_(val)) else: q = q.where(self.get_column(col) != val) # not sure this is a great idea: # if self.data.get('where'): # q = q.where(sql_text(self.data.get('where'))) for join in self.joins: left = self.get_column(join.get('left')) right = self.get_column(join.get('right')) q = q.where(left == right) return q
def clean_note(text: Optional[str]) -> List[str]: out: List[str] = [] if text is None: return out if is_listish(text): for t in text: out.extend(clean_note(t)) return out text = PREFIX.sub(" ", text) text = INTERPOL_URL.sub(" ", text) text = collapse_spaces(text) if text is None: return out return [text]
def _clean_response(self, data): """Remove unset values from the response to save some bandwidth.""" if is_mapping(data): out = {} for k, v in data.items(): v = self._clean_response(v) if v is not None: out[k] = v return out if len(out) else None elif is_listish(data): data = [self._clean_response(d) for d in data] data = [d for d in data if d is not None] return data if len(data) else None elif isinstance(data, str): return data if len(data) else None return data
def resolve_includes(file_path: PathLike, data: Any) -> Any: """Handle include statements in the graph configuration file. This allows the YAML graph configuration to be broken into multiple smaller fragments that are easier to maintain.""" if is_listish(data): return [resolve_includes(file_path, i) for i in data] if is_mapping(data): include_paths = ensure_list(data.pop("include", [])) for include_path in include_paths: dir_prefix = os.path.dirname(file_path) include_path = os.path.join(dir_prefix, include_path) data.update(load_mapping_file(include_path)) for key, value in data.items(): data[key] = resolve_includes(file_path, value) return data
def clean_query(query): # XXX - do these premises hold? if is_mapping(query): data = {} for key, value in query.items(): if key not in ['match_all', 'match_none']: value = clean_query(value) if value is not None: data[key] = value if not len(data): return None return data if is_listish(query): values = [clean_query(v) for v in query] values = [v for v in values if v is not None] if not len(values): return None return values return query
def to_jsonschema(obj): """Schema are stored in OpenAPI spec and might need some massaging to make for valid JSON Schema.""" if is_mapping(obj): # Re-write nullable fields: type_ = obj.get('type') if obj.get('nullable', False): type_ = obj.pop('type', None) format_ = obj.pop('format', None) obj['oneOf'] = [ {'type': 'null'}, {'type': type_, 'format': format_}, ] obj.pop('nullable', None) out = {} for key, value in obj.items(): out[key] = to_jsonschema(value) return out if is_listish(obj): return [to_jsonschema(o) for o in obj] return obj
def apply_filter(self, data, key, pred): value = data.get(key) if is_listish(pred): return value in pred return value == pred