Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small changes #376

Merged
merged 3 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions pasta_eln/inputOutput.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def importELN(backend:Backend, elnFileName:str, projID:str) -> tuple[str,dict[st
Args:
backend (backend): backend
elnFileName (str): name of file
projID (str): project to import data into. If '', create a new project (only available for PastaELN)
projID (str): project to import data into.

Returns:
str: success message, statistics
Expand All @@ -86,10 +86,10 @@ def importELN(backend:Backend, elnFileName:str, projID:str) -> tuple[str,dict[st
publisherNode = [i for i in graph if i["@id"]==rocrateNode['sdPublisher']['@id']][0]
elnName = publisherNode['name']
logging.info('Import %s', elnName)
if not projID and elnName!='PASTA ELN':
if not projID:
return "FAILURE: YOU CANNOT IMPORT AS PROJECT IF NON PASTA-ELN FILE",{}
if projID:
backend.changeHierarchy(projID)
backend.changeHierarchy(projID)
childrenStack = [0]
mainNode = [i for i in graph if i["@id"]=="./"][0]
# clean subchildren from mainNode: see https://github.com/TheELNConsortium/TheELNFileFormat/issues/98
parentNodes = {i['@id'] for i in mainNode['hasPart']}
Expand Down Expand Up @@ -235,6 +235,8 @@ def processPart(part:dict[str,str]) -> int:
docType = ''
if docType=='x0':
backend.hierStack = []
childrenStack[-1] += 1
doc['childNum'] = childrenStack[-1]
# print(f'Want to add doc:{doc} with type:{docType} and cwd:{backend.cwd}')
try:
docID = backend.addData(docType, doc)['id']
Expand All @@ -245,6 +247,7 @@ def processPart(part:dict[str,str]) -> int:
docID = None
if docID[0]=='x':
backend.changeHierarchy(docID)
childrenStack.append(0)
with open(backend.basePath/backend.cwd/'.id_pastaELN.json','w', encoding='utf-8') as f: #local path, update in any case
f.write(json.dumps(backend.db.getDoc(docID)))
# children, aka recursive part
Expand All @@ -253,6 +256,7 @@ def processPart(part:dict[str,str]) -> int:
continue
addedDocs += processPart(child)
backend.changeHierarchy(None)
childrenStack.pop()
return addedDocs

######################
Expand Down
21 changes: 14 additions & 7 deletions pasta_eln/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,19 +195,24 @@ def getDoc(self, docID:str) -> dict[str,Any]:
self.cursor.execute(f"SELECT * FROM branches WHERE id == '{docID}'")
for dataI in self.cursor.fetchall():
doc['branch'].append({'stack': dataI[2].split('/')[:-1],
'child': dataI[3],
'path': None if dataI[4] == '*' else dataI[4],
'show': [i=='T' for i in dataI[5]]})
'child': dataI[3],
'path': None if dataI[4] == '*' else dataI[4],
'show': [i=='T' for i in dataI[5]]})
cmd = "SELECT properties.key, properties.value, properties.unit, propDefinitions.long, propDefinitions.IRI, " \
"definitions.unit, definitions.query, definitions.IRI FROM properties LEFT JOIN propDefinitions USING(key) "\
"LEFT JOIN definitions ON properties.key = (definitions.class || '.' || definitions.name) "\
f"WHERE properties.id == '{docID}'"
# Big assumption in "LEFT JOIN definitions ON properties.key = (definitions.class || '.' || definitions.name)"
# with 'docType' missing
# -> each property ".status" has the same meaning for all docTypes
# -> if it has the same meaning, it is only stored once??
self.cursor.execute(cmd)
res = self.cursor.fetchall()
metadataFlat:dict[str, tuple[str,str,str,str]] = {i[0]:(i[1],
('' if i[2] is None else i[2])+('' if i[5] is None else i[5]),
('' if i[3] is None else i[3])+('' if i[6] is None else i[6]),
('' if i[4] is None else i[4])+('' if i[7] is None else i[7])) for i in res}
metadataFlat:dict[str, tuple[str,str,str,str]] = {i[0]:( i[1], #value
('' if i[2] is None else i[2]) or ('' if i[5] is None else i[5]), #unit
('' if i[3] is None else i[3]) or ('' if i[6] is None else i[6]), #long
('' if i[4] is None else i[4]) or ('' if i[7] is None else i[7]) #IRI
) for i in res}
doc |= hierarchy(metadataFlat)
return doc

Expand Down Expand Up @@ -305,6 +310,8 @@ def insertMetadata(data:dict[str,Any], parentKeys:str) -> None:
return
# properties
metaDoc = {k:v for k,v in doc.items() if k not in KEY_ORDER}
# remove the (unit, long, IRI) of the ones that are already in data hierarchy
# create a test case for this
insertMetadata(metaDoc, '')
# save changes
self.connection.commit()
Expand Down
9 changes: 7 additions & 2 deletions tests/test_90_ImportOthers.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,14 @@ def test_simple(qtbot):
}
urlBase = 'https://github.com/SteffenBrinckmann/TheELNFileFormat/raw/refs/heads/new_PastaELN/examples/'
# 'https://github.com/TheELNConsortium/TheELNFileFormat/raw/refs/heads/master/examples/'
localDir= '/home/xyz/Repositories/TheELNConsortium/TheELNFileFormat/examples/'
local = False
for eln, pathName in allELNs.items():
elnFileName = f'{tempDir}/{eln}'
request.urlretrieve(f'{urlBase}{pathName}', elnFileName)
if local:
elnFileName = f'{localDir}/{pathName}'
else:
elnFileName = f'{tempDir}/{eln}'
request.urlretrieve(f'{urlBase}{pathName}', elnFileName)
print(f'\nStart with {eln}')
projName = f'{eln[:-4]} Import'
backend.addData('x0', {'name': projName})
Expand Down
Loading