Skip to content

Commit

Permalink
run black
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasCARPi committed Mar 11, 2024
1 parent 11961d0 commit 7d6a3bd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
3 changes: 2 additions & 1 deletion any2eln/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ def main():
parser.add_argument('--out_dir', required=False, help='output directory', default='.')
args = parser.parse_args()

if (args.src == 'labfolder'):
if args.src == 'labfolder':
username = env_or_ask('LABFOLDER_USERNAME', 'Your Labfolder username or email: ')
password = env_or_ask('LABFOLDER_PASSWORD', 'Your Labfolder password: ')
lf = Labfolder(username, password, out_dir=args.out_dir)
lf.extract()
else:
print('Not implemented.')


if __name__ == "__main__":
main()
23 changes: 14 additions & 9 deletions any2eln/labfolder/labfolder.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@


class Labfolder:
def __init__(self, username: str, password: str, out_dir = '.'):
def __init__(self, username: str, password: str, out_dir='.'):
self.username = username
self.password = password
self.token = self.__get_token()
Expand Down Expand Up @@ -205,7 +205,9 @@ def extract(self) -> Path:
# save the full json
with entry_folder.joinpath(f"{json_metadata['id']}").open('w') as json_file:
json.dump(json_metadata, json_file, indent=2)
node['sha256'] = hashlib.sha256(json.dumps(json_metadata, indent=2).encode()).hexdigest()
node['sha256'] = hashlib.sha256(
json.dumps(json_metadata, indent=2).encode()
).hexdigest()
crate_metadata['@graph'].append(node)
files.append(node['@id'])

Expand All @@ -230,7 +232,9 @@ def extract(self) -> Path:
node = self.__get_node_from_metadata(json_metadata, entry_folder)
with entry_folder.joinpath(f"{json_metadata['id']}").open('w') as json_file:
json.dump(json_metadata, json_file, indent=2)
node['sha256'] = hashlib.sha256(json.dumps(json_metadata, indent=2).encode()).hexdigest()
node['sha256'] = hashlib.sha256(
json.dumps(json_metadata, indent=2).encode()
).hexdigest()
crate_metadata['@graph'].append(node)
files.append(node['@id'])

Expand Down Expand Up @@ -290,18 +294,19 @@ def extract(self) -> Path:
def __get_links_script(self) -> str:
lines = []
for category in self.categories:
lines.append(f"""
lines.append(
f"""
INSERT INTO experiments_links (item_id, link_id)
SELECT
experiments.id,
(SELECT items.id FROM items WHERE title = "{category}" LIMIT 1)
FROM experiments
LEFT JOIN tags2entity ON tags2entity.item_id = experiments.id AND tags2entity.item_type = 'experiments'
LEFT JOIN tags ON tags2entity.tag_id = tags.id
WHERE tags.tag = "{category}";""")
WHERE tags.tag = "{category}";"""
)
return '\n'.join(lines)


def __get_project_script(self) -> str:
header = """#!/usr/bin/env python
import elabapi_python
Expand Down Expand Up @@ -348,7 +353,7 @@ def __get_dataset_node(self, entry: dict[str, Any], content: list[str], files: l
self.categories.append(project_title)
node['keywords'] = ','.join(entry.get('tags', []))
# use this to create a Category with the Project title
#node['category'] = entry.get('project', {}).get('title', {})
# node['category'] = entry.get('project', {}).get('title', {})
node['dateCreated'] = entry['creation_date']
node['dateModified'] = entry['version_date']
node['hasPart'] = [{'@id': file} for file in files]
Expand Down Expand Up @@ -402,7 +407,7 @@ def __get_csvs_from_json(self, json_metadata: dict[str, Any]) -> list[tuple[str,
debug('Skipping csv that has no dataTable in data:')
debug(json.dumps(json_metadata, indent=2))
continue
#column_names = [str(table_data['0'][str(i)].get('value', '')) for i in range(len(table_data['0']))]
# column_names = [str(table_data['0'][str(i)].get('value', '')) for i in range(len(table_data['0']))]
rows = []
for key, values in table_data.items():
row = {}
Expand All @@ -413,7 +418,7 @@ def __get_csvs_from_json(self, json_metadata: dict[str, Any]) -> list[tuple[str,
# Create DataFrame from the list of dictionaries
df = pd.DataFrame(rows)
# drop first row
#df = df.drop(0)
# df = df.drop(0)
# None as first arg will return the csv as string instead of writing a file
csvs.append((sheet_name + '.csv', df.to_csv(None, index=False)))
return csvs
1 change: 1 addition & 0 deletions any2eln/utils/rocrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from datetime import datetime
from typing import Any


def get_crate_metadata() -> dict[str, Any]:
crate_metadata: dict[str, Any] = {}
crate_metadata['@context'] = 'https://w3id.org/ro/crate/1.1/context'
Expand Down

0 comments on commit 7d6a3bd

Please sign in to comment.