def import_file(self, session, filename): """ Import the data in the given file. """ if not os.path.exists(filename): raise Exception("No such file: %s" % filename) base_filename = os.path.basename(filename) # Check if we've imported this file before. Assume # if an activity exists with a start time equal to the # file name, that way we dont waste time parsing # the XML. if session.query(Import).filter(Import.identifier == base_filename).first(): log.info("Skipping: %s" % base_filename) return log.info("Importing: %s" % filename) tree = ElementTree() tree.parse(filename) root = tree.getroot() activities_elem = root.find(self._get_tag("Activities")) if activities_elem is None: raise Exception("Unable to parse %s: No activities found." % filename) for activity_elem in activities_elem.findall(self._get_tag("Activity")): self._parse_activity(session, activity_elem) # Store that we've imported this file in the past, allowing us to # delete in the UI without re-importing it if the file is still laying # around in the directory. Manual file import should be made available # at some point to correct any delete mistakes. imp = Import(1, base_filename) session.add(imp)
def initialize_db(): """ Open the database, presumably for the first time, and populate the schema. Returns the database engine. """ log.info("Creating the granola database.") metadata = Base.metadata metadata.bind = DB metadata.create_all() session = Session() # Populate the schema: session.add_all([ Sport(SPORTNAME_RUNNING), Sport(SPORTNAME_BIKING), Sport(SPORTNAME_WALKING), Sport(SPORTNAME_OTHER), ]) session.add_all([ Constant("schema_version", VERSION), ]) session.commit()
def initialize_granola(): """ Create the data directory, database, config files, and anything else required on the first run of the software. """ log.info("Creating %s." % DATA_DIR) try: os.mkdir(DATA_DIR) except OSError: pass initialize_db()
def show_activity(self, activity): """ Display the given activity on the map. """ # If we're already displaying this activity, don't do anything. # Useful check for some scenarios where the use changes a combo. if activity == self.current_activity: log.debug("Already displaying activity: %s" % activity) return if self.temp_file: log.info("Removing: %s" % self.temp_file) commands.getstatusoutput("rm %s" % self.temp_file) self.current_activity = activity generator = HtmlGenerator(activity, self.map_width, self.map_height) self.temp_file = generator.generate_html() log.debug("Wrote activity HTML to: %s" % self.temp_file) self._browser.open("file://%s" % self.temp_file)
def close_window(self, widget): log.info("Removing: %s" % self.temp_file) commands.getstatusoutput("rm %s" % self.temp_file) self.destroy()