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