-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
138 lines (109 loc) · 3.75 KB
/
utils.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import os
import tkinter as tk
from tkinter import filedialog
from md_file import MD_file
import pickle
"""
This file contains useful utility functions:
load_keywords()
save_keywords(keyword_list)
get_working_dir()
create_data_file_list(dir)
create_MD_file_dict(data_files, keywords)
load_processes()
save_processes(PD_handler)
"""
def load_keywords():
"""
Function which loads the metadata tags from keywords.pkl
returns:
keywords - list ... list containing the keywords as strings
"""
keywords = []
# check if keywords.txt is a file
if os.path.isfile("keywords.pkl"):
# open the file and read the lines
with open("keywords.pkl", "rb") as f:
# depickle the saved list
keywords = pickle.load(f)
return keywords
def save_keywords(keywords):
"""
Function to save the keywords to keyword.pkl
keywords - list ... list containing all keywords as strings
"""
# open the file
with open("keywords.pkl", "wb") as f:
# serialize and dump the list
pickle.dump(keywords, f)
def get_working_dir():
"""
Function which executes the open file dialog and gets the working directory
"""
# select the working_dir
root = tk.Tk()
# withdraw the standard window which is created
root.withdraw()
working_dir = filedialog.askdirectory()
return working_dir
def create_data_file_list(dir):
"""
Function to create a list of all files in a given directory
dir - string/path ... path to the working directory
returns:
file_list - list ... containg paths to all files in the working directory
"""
# initialize data file list
data_file_list = []
# create directory walk
w = os.walk(dir)
# filling the list with all file paths
for root, _, files in w:
# iterate over all files
for f in files:
# create the path
path = os.path.join(root, f)
# check if metadata is in the filename or directory above
if not "metadata" in f and not "metadata" in os.path.split(root)[1]:
data_file_list.append(os.path.normpath(path))
return data_file_list
def create_MD_file_dict(data_files, keywords):
"""
Function that creates a dict of MD_file objects
as pseudo dict wrapper around the metadata files.
This also creates empty metadata files if no files are present.
data_files - list ... containing all paths to data files
keywords - list ... list with all keywords as strings
returns:
MD_file_dict - dict ... containing one MD_file for each data file, the key is the path to the datafile
"""
# initialize empty dict
MD_file_dict = {}
# iterate over the data files
for data_file in data_files:
# fill the dict
MD_file_dict[data_file] = MD_file(data_file, keywords)
return MD_file_dict
def load_processes():
"""
Function which loads the process descriptions from processes.pkl
returns:
pd_handler - PD_handler ... object which handels the process descriptions
"""
pd_handler = None
# check if keywords.txt is a file
if os.path.isfile("processes.pkl"):
# open the file and read the lines
with open("processes.pkl", "rb") as f:
# depickle the saved list
pd_handler = pickle.load(f)
return pd_handler
def save_processes(pd_handler):
"""
Function to save the process descriptions to processes.pkl
pd_handler - PD_handler ... object which handels the process descriptions
"""
# open the file
with open("processes.pkl", "wb") as f:
# serialize and dump the list
pickle.dump(pd_handler, f)