-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_rocrate.py
More file actions
64 lines (53 loc) · 1.93 KB
/
create_rocrate.py
File metadata and controls
64 lines (53 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
from rocrate.rocrate import ROCrate
from rocrate.model.person import Person
crate = ROCrate()
# authors
author1 = crate.add(Person(crate, '#Marco', {"name": "Marco Ciccalè Baztán"}))
author2 = crate.add(Person(crate, '#Jorge', {"name": "Jorge Martín Izquierdo"}))
author3 = crate.add(Person(crate, '#Gloria', {"name": "María Gloria Cumia Espinosa de los Monteros"}))
# files
# function for extension format
def get_format(filename):
formats = {
'.pdf': 'application/pdf',
'.ipynb': 'application/x-ipynb+json',
'.py': 'text/x-python',
'.md': 'text/markdown',
'.json': 'application/json',
'.yaml': 'application/x-yaml',
'.jpg': 'image/jpeg',
'.xml': 'application/xml',
'.cff': 'text/x-citation',
'.ttl': 'text/turtle'
}
ext = os.path.splitext(filename)[1]
if ext in formats:
return formats[ext]
# 'text/plain' if extension not found
return 'text/plain'
# add files
def add_files_to_crate(directory):
for root, dirs, files in os.walk(directory):
if 'docs' in dirs:
dirs.remove('docs')
if '.git' in dirs:
dirs.remove('.git')
if '__pycache__' in dirs:
dirs.remove('__pycache__')
for file in files:
file_path = os.path.join(root, file)
rel_path = os.path.relpath(file_path, directory)
encoding_format = get_format(file)
# Añadir archivo al crate con su ruta relativa
crate.add_file(file_path, dest_path=rel_path, properties={
"name": file,
"encodingFormat": encoding_format
})
# Directorio raíz
root_dir = os.getcwd()
# Añadir archivos al crate
add_files_to_crate(root_dir)
# Guardar el RO-Crate
crate.write("RO-Crate")
print(f"RO-Crate guardado en {os.path.join(root_dir, 'ro-crate-metadata.json')}")