Unicode support added for export
[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
14 # set the document data
15 def set_data (self, data):
16 # document data
17 self.data = data
18
19 # export function
20 def export (self):
21 # fill education data template bit
22 str_education = self._get_education ()
23
24 # fill the professional history template bit
25 str_profession = self._get_profession ()
26
27 # fill the short term objective and long term goals template bit
28 str_shorttermobjectives, str_longtermgoals = self._get_career ()
29
30 # fill the skills template bit
31 str_skills = self._get_skills ()
32
33 # fill the language template bit
34 str_languages = self._get_languages ()
35
36 # fill the main template
37 str_main = self._get_main (
38 str_education,
39 str_profession,
40 str_shorttermobjectives,
41 str_longtermgoals,
42 str_skills,
43 str_languages)
44
45 # write to export file
46 codecs.open (self.file_output, "w", "utf-8").write (str_main)
47
48 # fill up the main template
49 def _get_main (self, education, profession, shortterm, longterm, skills, languages):
50 # main template
51 tpl_main = string.Template (codecs.open (self.fil_main, "r", "utf-8").read ())
52
53 if self.data["maritalstatus"] is True:
54 str_marital = self.lang_strings[3]
55 elif self.data["maritalstatus"] is False:
56 str_marital = self.lang_strings[4]
57 else:
58 str_marital = self.lang_strings[5]
59
60 str_main = tpl_main.safe_substitute (
61 title = self.data["title"],
62 nametitle = self.data["nametitle"],
63 firstname = self.data["firstname"],
64 lastname = self.data["lastname"],
65 dateofbirth = self.data["dateofbirth"],
66 maritalstatus = str_marital,
67 shortterm = shortterm,
68 longterm = longterm,
69 skills = skills,
70 education = education,
71 profession = profession,
72 languages = languages,
73 street = self.data["street"],
74 area = self.data["area"],
75 city = self.data["city"],
76 areacode = self.data["areacode"],
77 country = self.data["country"],
78 countrycode_landline = self.data["countrycode_landline"],
79 landline = self.data["landline"],
80 countrycode_mobile = self.data["countrycode_mobile"],
81 mobile = self.data["mobile"],
82 email = self.data["email"],
83 additionalinformation = self.data["additionalinformation"]
84 )
85
86 return str_main
87
88 # get the language list
89 def _get_languages (self):
90 tpl_language = string.Template (codecs.open (self.fil_language, "r", "utf-8").read ())
91
92 lst_languages = []
93 # loop through each item
94 for item in self.data["languagesknown"]:
95 str_canspeak = ""
96 str_canreadwrite = ""
97 str_proficient = ""
98 if item["canspeak"] is True: str_canspeak = self.lang_strings[0]
99 if item["canreadwrite"] is True: str_canreadwrite = self.lang_strings[1]
100 if item["isproficient"] is True: str_proficient = self.lang_strings[2]
101 str_lang = tpl_language.safe_substitute (
102 language = item["language"],
103 canspeak = str_canspeak,
104 canreadwrite = str_canreadwrite,
105 proficient = str_proficient
106 )
107 lst_languages.append (str_lang)
108
109 return "\n".join (lst_languages)
110
111 # get the skills
112 def _get_skills (self):
113 # load the template
114 tpl_skill = string.Template (codecs.open (self.fil_skills, "r", "utf-8").read ())
115
116 lst_skills = []
117 # loop through each item
118 for item in self.data["skillsets"]:
119 str_skill = tpl_skill.safe_substitute (
120 skilltitle = item["skilltitle"],
121 skilldesc = item["skilldesc"]
122 )
123 lst_skills.append (str_skill)
124
125 return "\n".join (lst_skills)
126
127 # get short term career objectives
128 def _get_career (self):
129 # load the template
130 tpl_career = string.Template (codecs.open (self.fil_career, "r", "utf-8").read ())
131
132 lst_shorttermcareer = []
133 lst_longtermgoals = []
134 # loop through each item
135 for item in self.data["shorttermobjectives"]:
136 str_career = tpl_career.safe_substitute (
137 careerobjective_item = item
138 )
139 lst_shorttermcareer.append (str_career)
140
141 for item in self.data["longtermgoals"]:
142 str_career = tpl_career.safe_substitute (
143 careerobjective_item = item
144 )
145 lst_longtermgoals.append (str_career)
146
147 return "\n".join (lst_shorttermcareer), "\n".join (lst_longtermgoals)
148
149 # fill the professional history template
150 def _get_profession (self):
151 # load the template
152 tpl_profession = string.Template (codecs.open (self.fil_profession, "r", "utf-8").read ())
153
154 lst_profession = []
155 # loop through each item
156 for item in self.data["professionalhistory"]:
157 if item["leavedate"] == "":
158 leavedate_str = self.lang_strings[6]
159 else:
160 leavedate_str = item["leavedate"]
161 str_profession = tpl_profession.safe_substitute (
162 joindate = item["joindate"],
163 leavedate = leavedate_str,
164 organization = item["organization"],
165 jobtitle = item["jobtitle"],
166 additionalinfo = item["additionalinfo"]
167 )
168 lst_profession.append (str_profession)
169
170 return "\n".join (lst_profession)
171
172 # fill the education template
173 def _get_education (self):
174 # load the template
175 tpl_education = string.Template (codecs.open (self.fil_education, "r", "utf-8").read ())
176
177 lst_education = []
178 # loop through each item
179 for item in self.data["educationalqualifications"]:
180 str_education = tpl_education.safe_substitute (
181 degree = item["degree"],
182 graduation = item["graduation"],
183 institution = item["institution"],
184 university = item["university"],
185 grade = item["grade"],
186 percentage = item["percentage"]
187 )
188 lst_education.append (str_education)
189
190 return "\n".join (lst_education)
191
192 # set the template directory and the files within
193 def set_template_directory (self, templatedir):
194 # set the template file names
195
196 # main document template
197 self.fil_main = os.path.join (templatedir, "main.tpl")
198 # education qualification bit
199 self.fil_education = os.path.join (templatedir, "education_bit.tpl")
200 # profession history bit
201 self.fil_profession = os.path.join (templatedir, "profession_bit.tpl")
202 # career list bit
203 self.fil_career = os.path.join (templatedir, "career_bit.tpl")
204 # skills bit
205 self.fil_skills = os.path.join (templatedir, "skill_bit.tpl")
206 # languages learned bit
207 self.fil_language = os.path.join (templatedir, "language_bit.tpl")
208
209 # load language strings
210 lang_str = codecs.open (os.path.join (templatedir, "misc_strings.txt"), "r", "utf-8").read ()
211
212 self.lang_strings = lang_str.splitlines ()
213
214 # set the output file
215 def set_output (self, output):
216 self.file_output = output