-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmd_file.py
145 lines (115 loc) · 4.32 KB
/
md_file.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
139
140
141
142
143
144
145
import os
class MD_file:
"""
Class to handle all the metadata
Idea:
- creating a class to use like a typical dictionary
- saving and loading data to and from the disc
"""
def __init__(self, path, keywords):
"""
Initialization of the file handler object
path - string ... path to the data file
keywords - list ... list of all metadata keywords
"""
# setting the path of the data file
self.path = path
# creating the path to the metadata
self.metadata_path = os.path.join(os.path.split(path)[0], "metadata",
os.path.split(path)[1].replace(".", "-") + "-metadata.txt")
# saving the keywords
# each md file needs its own copy
self.keywords = list(keywords)
# checking if the metadata directory is initialized
if not os.path.isdir(os.path.join(os.path.split(self.path)[0], "metadata")):
os.makedirs(os.path.join(os.path.split(self.path)[0], "metadata"))
# intializing the data dict
self.data = {}
for key in self.keywords:
self.data[key] = ""
# checking if the metadata file is initialized
if not os.path.isfile(self.metadata_path):
# writing the init data dict
self.write()
# loading data if available
self.read()
def read(self):
"""
Function to load metadata if available from the file
"""
# opening the file
with open(self.metadata_path, 'r') as tmp:
# reading all lines skipping over reference to original file
md = tmp.readlines()
# check if the second line is empty
if md[1] == "\n":
md = md[2:]
else:
md = md[1:]
# parsing the actual data
for line in md:
# getting the key and the data
key, info = line.split(":: ")
# writing the data into our data dict
self.data[key] = info.replace("\n", "")
def create_save_string(self):
"""
Function to create the save string
return:
save_string - string ... string that contains the data dict
"""
# initializing the empty string
save_string = ""
# adding the reference to the data file
save_string += "path to data file: " + self.path + "\n" + "\n"
# iterate over the data dict
for key in self.keywords:
# adding the key, info pair to the save_string
save_string += key + ":: " + self.data[key] + "\n"
# returning the created string
return save_string
def write(self):
"""
Function to write the metadata to the file
"""
# creating the save string composed of the metadata in self.data
save_string = self.create_save_string()
# opening the file
with open(self.metadata_path, "w+") as file:
# writing the save_string
file.write(save_string)
def set_keywords(self, keywords):
"""
Setter Function to update the keyword lists
keywords - list ... list of all metadata keywords
"""
self.keywords = list(keywords)
# add an empty string for new keywords
for key in self.keywords:
if not key in self.data:
self.data[key] = ""
def update_keyword(self, i, keyword):
"""
function to update a single keyword (index i)
i - index ... index of the keyword you want to update
keyword - string ... new keyword for index i
"""
self.data[keyword] = self.data[self.keywords[i]]
self.keywords[i] = keyword
def __getitem__(self, key):
"""
Operator overloading to simplify usage of the class
"""
return self.data[key]
def __setitem__(self, key, value):
"""
Operator overloading to simplify usage of the class
"""
self.data[key] = value.replace("\n", "")
# also overwrite the metadata file
self.write()
def __str__(self):
"""
Defines a string representation for the class so that its printable
"""
return str(self.data)