def json_export_type_converter(obj): if isinstance(obj, deque): return list(obj) elif isinstance(obj, AJ): return obj.asJson() elif isinstance(obj, ProtcurExpression): return obj.json() elif isinstance(obj, PurePath): return obj.as_posix() elif isinstance(obj, Quantity): return obj.json() elif isinstance(obj, Measurement): return obj.json() elif isinstance(obj, oq.OntTerm): return obj.iri #return obj.asDict() # FIXME need a no network/scigraph version elif isinstance(obj, idlib.Stream) and hasattr(obj, '_id_class'): if obj._id_class is str: return obj.identifier else: return json_export_type_converter(obj.identifier) #return obj.asDict() # FIXME need a no network/scigraph version elif isinstance(obj, datetime): return isoformat(obj) elif isinstance(obj, time): return isoformat(obj) elif isinstance(obj, BaseException): # FIXME hunt down where these are sneeking in from return repr(obj)
def triples_header(self): ontid = self.ontid nowish = utcnowtz() epoch = nowish.timestamp() iso = isoformat(nowish) ver_ontid = rdflib.URIRef(ontid + f'/version/{epoch}/{self.id}') sparc_methods = rdflib.URIRef('https://raw.githubusercontent.com/SciCrunch/' 'NIF-Ontology/sparc/ttl/sparc-methods.ttl') sparc_mis_helper = rdflib.URIRef('https://raw.githubusercontent.com/SciCrunch/' 'NIF-Ontology/sparc/ttl/sparc-mis-helper.ttl') pos = ( (rdf.type, owl.Ontology), (owl.versionIRI, ver_ontid), (owl.versionInfo, rdflib.Literal(iso)), (isAbout, rdflib.URIRef(self.uri_api)), (TEMP.hasUriHuman, rdflib.URIRef(self.uri_human)), (rdfs.label, rdflib.Literal(f'{self.folder_name} curation export graph')), (rdfs.comment, self.header_graph_description), (owl.imports, sparc_methods), (owl.imports, sparc_mis_helper), (TEMP.TimestampExportStart, rdflib.Literal(self.timestamp_export_start)), ) for p, o in pos: yield ontid, p, o
def render_manifest(rows): # FIXME checksum cypher return [[ filename.as_posix(), isoformat(timestamp), desc, ft, at, checksum.hex() if checksum else checksum, ] for filename, timestamp, desc, ft, at, checksum in rows]
def _datetime(self, value): if not isinstance(value, datetime): raise TypeError(f'{type(value)} is not a datetime for {value}') has_tz = value.tzinfo is not None and value.tzinfo.utcoffset( None) is not None value = isoformat(value) if not has_tz: log.warning('why do you have a timestamp without a timezone ;_;') return value
def subject_id(self, v, species=None): # TODO species for human/animal if not isinstance(v, str): #loge.critical('darn it max normlize your ids!') # now caught by the schemas if isinstance(v, datetime): # XXX datetime is a REALLY bad case because there is not a # canonical representation v = isoformat(v) else: v = str(v) v = quote(v, safe=tuple()) s = rdflib.URIRef(self.dsid + '/subjects/' + v) return s
def triples_header(self): ontid = self.ontid nowish = utcnowtz() epoch = nowish.timestamp() iso = isoformat(nowish) ver_ontid = rdflib.URIRef(ontid + f'/version/{epoch}/{self.id}') pos = ( (rdf.type, owl.Ontology), (owl.versionIRI, ver_ontid), (owl.versionInfo, rdflib.Literal(iso)), (rdfs.comment, self.header_graph_description), (TEMP.TimestampExportStart, rdflib.Literal(self.timestamp_export_start)), ) for p, o in pos: yield ontid, p, o
def _rowcellify(cell_value): if isinstance(cell_value, idlib.Stream): try: return cell_value.asUri() except AttributeError as e: breakpoint() elif (isinstance(cell_value, date) or isinstance(cell_value, datetime)): # the default json encoder doesn't know what to do with these return isoformat(cell_value) elif isinstance(cell_value, str): if cell_value.lower() == 'x': return True return cell_value
def default(self, obj): if isinstance(obj, deque): return list(obj) elif isinstance(obj, ProtcurExpression): return obj.json() elif isinstance(obj, Path): return obj.as_posix() elif isinstance(obj, Quantity): return obj.json() elif isinstance(obj, idlib.Stream): return obj.identifier elif isinstance(obj, datetime): return isoformat(obj) #else: #log.critical(f'{type(obj)} -> {obj}') # Let the base class default method raise the TypeError return json.JSONEncoder.default(self, obj)
def makeKwargs(repo_path, filepath): path = repo_path / filepath kwargs = {} kwargs['title'] = filepath kwargs['authors'] = sorted(name.strip() for name in set( repo_path.repo.git.log( ['--follow', '--pretty=format:%an%x09', filepath]).split('\n'))) kwargs['date'] = isoformat(path.latest_commit().authored_datetime) repo_url = Path( next( next(r for r in repo_path.repo.remotes if r.name == 'origin').urls)) kwargs['org'] = repo_url.parent.name kwargs['repo'] = repo_url.stem kwargs[ 'branch'] = 'master' # TODO figure out how to get the default branch on the remote kwargs['crumbs'] = _crumbs return kwargs
def triples_header(self): # TODO TODO ontid = self.ontid nowish = utcnowtz() # FIXME pass in so we can align all times per export?? epoch = nowish.timestamp() iso = isoformat(nowish) ver_ontid = rdflib.URIRef(ontid + f'/version/{epoch}/{self.id}') #sparc_methods = rdflib.URIRef('https://raw.githubusercontent.com/SciCrunch/' #'NIF-Ontology/sparc/ttl/sparc-methods.ttl') pos = ( (a, owl.Ontology), (owl.versionIRI, ver_ontid), (owl.versionInfo, rdflib.Literal(iso)), #(isAbout, rdflib.URIRef(self.uri_api)), #(TEMP.hasHumanUri, rdflib.URIRef(self.uri_human)), (rdfs.label, rdflib.Literal(f'TODO export graph')), #(rdfs.comment, self.header_graph_description), #(owl.imports, sparc_methods), ) for p, o in pos: yield ontid, p, o
def START_TIMESTAMP_LOCAL(self): return isoformat(self._start_time_local)
def START_TIMESTAMP(self): return isoformat(self._start_time)
def test_isoformat(self): now = datetime.now() n = utils.isoformat(now) today = date.today() t = utils.isoformat(today)