Changes to templates to include e-mail as a link
[biacv.git] / biacv_exporter.py
1 # class to export data in BiaCV native format to any external format as
2 # specified by a template set. The final document is produced by putting
3 # together the template "bits" into a single document and filling in the
4 # document fields.
5
6 import string
7 import codecs
8 import os.path
9
10 class BiaCVExporter:
11 def __init__ (self):
12 self.data = {}
13 self.exporter_name = ""
14 self.export_filter = ""
15
16 # set the document data
17 def set_data (self, data):
18 # document data
19 self.data = data
20
21 # export function
22 def export (self):
23 # fill education data template bit
24 str_education = self._get_education ()
25
26 # fill the profile highlight template bit
27 str_profile = self._get_profile ()
28
29 # fill the professional history template bit
30 str_profession = self._get_profession ()
31
32 # fill the short term objective and long term goals template bit
33 str_shorttermobjectives, str_longtermgoals = self._get_career ()
34
35 # fill the skills template bit
36 str_skills = self._get_skills ()
37
38 # fill the language template bit
39 str_languages = self._get_languages ()
40
41 # fill the main template
42 str_main = self._get_main (
43 str_profile,
44 str_education,
45 str_profession,
46 str_shorttermobjectives,
47 str_longtermgoals,
48 str_skills,
49 str_languages)
50
51 # write to export file
52 codecs.open (self.file_output, "w", "utf-8").write (str_main)
53
54 # fill up the main template
55 def _get_main (self, profile, education, profession, shortterm, longterm, skills, languages):
56 # main template
57 tpl_main = string.Template (codecs.open (self.fil_main, "r", "utf-8").read ())
58
59 if self.data["maritalstatus"] is True:
60 str_marital = self.lang_strings[3]
61 elif self.data["maritalstatus"] is False:
62 str_marital = self.lang_strings[4]
63 else:
64 str_marital = self.lang_strings[5]
65
66 str_main = tpl_main.safe_substitute (
67 title = self.data["title"],
68 nametitle = self.data["nametitle"],
69 firstname = self.data["firstname"],
70 lastname = self.data["lastname"],
71 dateofbirth = self.data["dateofbirth"],
72 maritalstatus = str_marital,
73 profile = profile,
74 shortterm = shortterm,
75 longterm = longterm,
76 skills = skills,
77 education = education,
78 profession = profession,
79 languages = languages,
80 street = self.data["street"],
81 area = self.data["area"],
82 city = self.data["city"],
83 areacode = self.data["areacode"],
84 country = self.data["country"],
85 countrycode_landline = self.data["countrycode_landline"],
86 landline = self.data["landline"],
87 countrycode_mobile = self.data["countrycode_mobile"],
88 mobile = self.data["mobile"],
89 email = self.data["email"],
90 additionalinformation = self.data["additionalinformation"]
91 )
92
93 return str_main
94
95 # get the profile highlights list
96 def _get_profile (self):
97 tpl_profile = string.Template (codecs.open (self.fil_profile, "r", "utf-8").read ())
98
99 lst_profile = []
100 # loop through each item
101 for item in self.data["profile"]:
102 str_profile = tpl_profile.safe_substitute (
103 profile_item = item
104 )
105 lst_profile.append (str_profile)
106
107 return u'\n'.join (lst_profile)
108
109 # get the language list
110 def _get_languages (self):
111 tpl_language = string.Template (codecs.open (self.fil_language, "r", "utf-8").read ())
112
113 lst_languages = []
114 # loop through each item
115 for item in self.data["languagesknown"]:
116 str_canspeak = ""
117 str_canreadwrite = ""
118 str_proficient = ""
119 if item["canspeak"] is True: str_canspeak = self.lang_strings[0]
120 if item["canreadwrite"] is True: str_canreadwrite = self.lang_strings[1]
121 if item["isproficient"] is True: str_proficient = self.lang_strings[2]
122 str_lang = tpl_language.safe_substitute (
123 language = item["language"],
124 canspeak = str_canspeak,
125 canreadwrite = str_canreadwrite,
126 proficient = str_proficient
127 )
128 lst_languages.append (str_lang)
129
130 return u'\n'.join (lst_languages)
131
132 # get the skills
133 def _get_skills (self):
134 # load the template
135 tpl_skill = string.Template (codecs.open (self.fil_skills, "r", "utf-8").read ())
136
137 lst_skills = []
138 # loop through each item
139 for item in self.data["skillsets"]:
140 str_skill = tpl_skill.safe_substitute (
141 skilltitle = item["skilltitle"],
142 skilldesc = item["skilldesc"]
143 )
144 lst_skills.append (str_skill)
145
146 return u'\n'.join (lst_skills)
147
148 # get short term career objectives
149 def _get_career (self):
150 # load the template
151 tpl_career = string.Template (codecs.open (self.fil_career, "r", "utf-8").read ())
152
153 lst_shorttermcareer = []
154 lst_longtermgoals = []
155 # loop through each item
156 for item in self.data["shorttermobjectives"]:
157 str_career = tpl_career.safe_substitute (
158 careerobjective_item = item
159 )
160 lst_shorttermcareer.append (str_career)
161
162 for item in self.data["longtermgoals"]:
163 str_career = tpl_career.safe_substitute (
164 careerobjective_item = item
165 )
166 lst_longtermgoals.append (str_career)
167
168 return u'\n'.join (lst_shorttermcareer), u'\n'.join (lst_longtermgoals)
169
170 # fill the professional history template
171 def _get_profession (self):
172 # load the template
173 tpl_profession = string.Template (codecs.open (self.fil_profession, "r", "utf-8").read ())
174
175 lst_profession = []
176 # loop through each item
177 for item in self.data["professionalhistory"]:
178 if item["leavedate"] == "":
179 leavedate_str = self.lang_strings[6]
180 else:
181 leavedate_str = item["leavedate"]
182 str_profession = tpl_profession.safe_substitute (
183 joindate = item["joindate"],
184 leavedate = leavedate_str,
185 organization = item["organization"],
186 jobtitle = item["jobtitle"],
187 additionalinfo = item["additionalinfo"]
188 )
189 lst_profession.append (str_profession)
190
191 return u'\n'.join (lst_profession)
192
193 # fill the education template
194 def _get_education (self):
195 # load the template
196 tpl_education = string.Template (codecs.open (self.fil_education, "r", "utf-8").read ())
197
198 lst_education = []
199 # loop through each item
200 for item in self.data["educationalqualifications"]:
201 str_education = tpl_education.safe_substitute (
202 degree = item["degree"],
203 graduation = item["graduation"],
204 institution = item["institution"],
205 university = item["university"],
206 grade = item["grade"],
207 percentage = item["percentage"]
208 )
209 lst_education.append (str_education)
210
211 return u'\n'.join (lst_education)
212
213 # set the template directory and the files within
214 def set_template_directory (self, templatedir):
215 # load the meta file for exporter title and name
216 file_meta = os.path.join (templatedir, "META-INFO")
217 lst_file_meta = (codecs.open (file_meta, 'r', "utf-8").read ()).splitlines ()
218 self.exporter_name = lst_file_meta[0]
219 self.export_filter = lst_file_meta[1]
220
221 # set the template file names
222
223 # main document template
224 self.fil_main = os.path.join (templatedir, "main.tpl")
225 # education qualification bit
226 self.fil_education = os.path.join (templatedir, "education_bit.tpl")
227 # profession history bit
228 self.fil_profession = os.path.join (templatedir, "profession_bit.tpl")
229 # career list bit
230 self.fil_career = os.path.join (templatedir, "career_bit.tpl")
231 # skills bit
232 self.fil_skills = os.path.join (templatedir, "skill_bit.tpl")
233 # languages learned bit
234 self.fil_language = os.path.join (templatedir, "language_bit.tpl")
235 # profile bit
236 self.fil_profile = os.path.join (templatedir, "profile_bit.tpl")
237
238 # load language strings
239 lang_str = codecs.open (os.path.join (templatedir, "misc_strings.txt"), "r", "utf-8").read ()
240
241 self.lang_strings = lang_str.splitlines ()
242
243
244 # set the output file
245 def set_output (self, output):
246 self.file_output = output