def get_calendar_date(self): year, month, day = self.calendar.get_date() # This sucks: GtkCalendar returns a zero-based month. month = month + 1 tmz = tz.LocalTimezone() date = datetime.datetime(year, month, day, 0, 0, 0, 0, tmz) return date
def generate(self, filename, days): fp = open(filename, "w") out = XMLGenerator(fp, 'utf-8') out.startDocument() out.startElement("consumerdb", {}) for day in days: day = days[day] time = datetime.time(0, 0, 0, 0, tz.LocalTimezone()) date = datetime.datetime.combine(day.get_date(), time) date = date.isoformat() out.startElement("day", {"date": date}) out.startElement("weight", {}) out.characters(str(day.weight)) out.endElement("weight") for food in day.get_foods(): args = {"name": food.name, "time": food.time.isoformat()} out.startElement("food", args) out.startElement("quantity", {}) out.characters(str(food.quantity)) out.endElement("quantity") out.startElement("energy", {}) out.characters(str(food.energy)) out.endElement("energy") out.endElement("food") out.endElement("day") out.endElement("consumerdb") out.endDocument() fp.close()
def runTest(self): # Generate an XML file first. days = {} for f in range(20): curtime = datetime.datetime(2005, 6, f + 1, 0, 0, 0, 0, tz.LocalTimezone()) day = Day(curtime) day.set_weight(100) for i in range(20): food = Food("Testfood " + str(i)) food.quantity = 10 + i food.energy = 100 * i food.time = curtime day.add_food(food) days[curtime.ctime()] = day gen = ConsumerDBGenerator() gen.generate(XML_FILE, days) # Now read. db = ConsumerDB() self.parser.setContentHandler(db) self.parser.parse(XML_FILE) assert len(db.getDays()) == 20 os.remove(XML_FILE)
def get_food_from_form(self): foodname = self.comboboxentry_food.get_text() if len(foodname) <= 0: return hour = int(self.spinbutton_hour.get_value()) minute = int(self.spinbutton_minute.get_value()) time = datetime.time(hour, minute, 0, 0, tz.LocalTimezone()) date = self.get_calendar_date() date = date.combine(date, time) quantity = round(self.spinbutton_quantity.get_value(), 2) food = Food(foodname) food.set_quantity(quantity) food.set_energy(self.spinbutton_energy.get_value()) food.set_time(date) return food
def startElement(self, name, attrs): self.cur_val = "" if name == "day": date = attrs.get("date", None) date = xml.utils.iso8601.parse(date) if self.search_from_time and date < self.search_from_time: return if self.search_to_time and date > self.search_to_time: return date = datetime.date.fromtimestamp(date) self.cur_day = Day(date) elif name == "food": ftime = attrs.get("time", None) ftime = xml.utils.iso8601.parse(ftime) if self.search_from_time and ftime < self.search_from_time: return if self.search_to_time and ftime > self.search_to_time: return self.cur_food = Food(attrs.get("name", None)) time = datetime.datetime.fromtimestamp(ftime, tz.LocalTimezone()) self.cur_food.set_time(time)
def __init__(self, name): assert len(name) > 0 self.name = name self.time = datetime.datetime.now(tz.LocalTimezone()) self.quantity = 1 self.energy = 100 # That's calories.