def parse_year_file(self, file_name: str) -> Year: year = os.path.splitext(os.path.basename(file_name))[0] if self.user is not None: if self.user.employed_date.year == int(year): parsed_year = Year( year, start_date=self.user.employed_date.isoformat()) elif len(self.user.years) == 0: raise Exception else: parsed_year = Year(year) else: parsed_year = Year(year) with open(file_name, "r", encoding='utf-8') as year_file: year_string = year_file.read() holiday_pattern = re.compile( "^\s*(\d{4}-\d{2}-\d{2})\s*:\s+([\"\'].*[\"\'])\s*$", flags=re.MULTILINE) comment_pattern = re.compile("#.*$", flags=re.MULTILINE) year_string = comment_pattern.sub("", year_string) holidays = holiday_pattern.findall(year_string) for date, name in holidays: name = name.strip("\"\'") parsed_year.add_holiday(date, name) if self.user is not None: self.user.add_year(parsed_year) return parsed_year
def add_year(self, year_object: year.Year): if len(year_object.months) != 0: raise errors.YearError( "Added year can't contain any reported days.") elif (len(self.years) > 0 and year_object.year != self.current_year().year and not self.current_year().complete()): raise errors.YearError( "Previous year ({}) must be completed first.".format( self.current_year().year)) if (len(self.years) > 0 and year_object.year == self.current_year().year): self.years[-1] = year_object else: year_object.start_date = self.employed_date self.years.append(year_object)