def extract(download): """ Extract either a .zip or a .tar.gz file into the user's home folder, backs up the current program directory on Windows that contains the latest Whyteboard source code and resources """ extracted_location = os.path.join(get_home_dir(), "whyteboard-" + download.version) backup_direcctory = os.path.join(get_home_dir(), "backup", meta.version) program_directory = get_path() archive = download.filesystem_path() logger.debug("Extracting archive file") logger.debug("Backing up current program [%s] to [%s]", program_directory, backup_direcctory) distutils.dir_util.copy_tree(program_directory, backup_direcctory) if is_exe(): os.rename(sys.argv[0], "whyteboard-tmp.exe") _file = zipfile.ZipFile(archive) else: _file = tarfile.open(archive) _file.extractall(get_home_dir()) _file.close() logger.debug("Moving extracted file directory [%s] into running program directory [%s]", extracted_location, program_directory) distutils.dir_util.copy_tree(extracted_location, program_directory) logger.debug("Cleaning up.") distutils.dir_util.remove_tree(extracted_location) os.remove(archive)
def on_import_preferences(self, event=None): """ Imports a preference file. Backs up the user's current preference file into a directory, with a timestamp on the filename """ logger.debug("Prompting to import preferences") wildcard = _("Whyteboard Preference Files") + u" (*.pref)|*.pref" filename = file_dialog(self, _("Import Preferences From..."), wx.OPEN, wildcard, get_home_dir()) if filename: Config().init(filename) _dir = os.path.join(get_home_dir(), u"pref-bkup") if not os.path.isdir(_dir): os.makedirs(_dir) home = os.path.join(get_home_dir(), u"user.pref") if os.path.exists(home): stamp = time.strftime(u"%d-%m-%Y_%H-%M_%S") logger.debug("Renaming old preferences file to [%s]", stamp) os.rename(home, os.path.join(_dir, stamp + u".user.pref")) config = Config().clone() config.init(filename) self.update_config(config.config)
def init(self, preferences_file=None): if not preferences_file: preferences_file = os.path.join(get_home_dir(), u"user.pref") logger.debug("Setting up configuration from preferences file [%s]", preferences_file) self.config = ConfigObj(preferences_file, configspec=meta.config_scheme, encoding=u"utf-8", default_encoding=u'utf-8') self.config.validate(Validator())
def setup_logging(self, debug): logfile = os.path.join(get_home_dir(), u"whyteboard.log") fh = logging.FileHandler(logfile) ch = logging.StreamHandler() if debug: logger.setLevel(logging.DEBUG) formatter = logging.Formatter('%(levelname)s %(asctime)s %(message)s') fh.setFormatter(formatter) ch.setFormatter(formatter) logger.addHandler(fh) logger.addHandler(ch)
def on_export_preferences(self, event=None): """ Copies the user's preferences file to another file. """ if not os.path.exists(Config().filename()): wx.MessageBox(_("You have not set any preferences"), _("Export Error")) return wildcard = _("Whyteboard Preference Files") + u" (*.pref)|*.pref" filename = file_dialog(self, _("Export preferences to..."), wx.SAVE | wx.OVERWRITE_PROMPT, wildcard) if filename: if not os.path.splitext(filename)[1]: filename += u".pref" shutil.copy(os.path.join(get_home_dir(), u"user.pref"), filename)
def GetEnvironmentInfo(self): """ Need to stick in extra information: preferences, helps with debugging """ info = super(ErrorDialog, self).GetEnvironmentInfo() path = os.path.join(get_home_dir(), u"user.pref") if os.path.exists(path): info.append(u"#---- Preferences ----#") with open(path) as f: for preference in f: preference = preference.rstrip() info.append(unicode(preference, "utf-8")) info.append(u"") info.append(u"") return os.linesep.join(info)
def write_save_file(self, save, version): """ An existing .wtbd zip must be re-created by copying all files except the pickled file, otherwise it gets added twice """ path = os.path.join(os.path.dirname(self.filename), u"whyteboard_temp_new.wtbd") tmp_file = os.path.abspath(path) errored = False logger.debug("Creating temporary zip file [%s].", tmp_file) _zip = zipfile.ZipFile(tmp_file, "w") self.save_bitmap_data(_zip) save.save_items() data = save.create_save_list(self.gui.current_tab, version) data_file = os.path.join(get_home_dir(), "save.data") logger.debug("Writing save data to [%s]", data_file) with open(data_file, "wb") as f: try: pickle.dump(data, f) except pickle.PickleError: wx.MessageBox(_("Error saving file data"), u"Whyteboard") logger.exception("Error pickling file data") self.saved = False self.filename = None errored = True if not errored: logger.debug("Writing data file to zip and cleaning up") _zip.write(data_file, "save.data") _zip.close() os.remove(data_file) if errored: os.remove(tmp_file) return False logger.debug("Removing old file and renaming temporary file") if os.path.exists(self.filename): os.remove(self.filename) shutil.move(tmp_file, self.filename)
def filesystem_path(self): filename = '~tmp-wb-%s.%s' % (self.version, self.filetype()) return os.path.join(get_home_dir(), filename)
def __init__(self, filename): self.path = os.path.join(get_home_dir(), filename) logger.debug("Using PDF cache at [%s]", self.path) if not os.path.exists(self.path): logger.debug("Cache file does not exist - creating it") self.write_dict(dict())
def convert(self): """ If the filetype is PDF/PS, convert to a (temporary) series of images and loads them. Find out the directory length before/after the conversion to know how many 'pages' were converted - used then to create a new Whyteboard tabs for each page. The PDF's file location, convert quality and converted images are written into a "library" file, effectively caching the conversion. An attempt at randomising the temp. file name is made using alphanumeric characters to help minimise conflict. """ if not self.im_location: self.prompt_for_im() if not self.im_location: # above will have changed this if IM exists return _file = self.temp_file logger.info("Converting [%s]", os.path.basename(_file)) quality = Config().convert_quality() cached = self.library.lookup(_file, quality) if cached: logger.debug("PDF is cached") self.display_converted(_file, cached) else: path = get_home_dir(u"wtbd-tmp") # directory to store the images tmp_file = make_filename() before = os.walk(path).next()[2] # file count before convert full_path = path + tmp_file + u".png" logger.debug("Writing PDF images as [%s]", full_path) cmd = convert_quality(quality, self.im_location, _file, full_path) logger.info("Starting to convert PDF") self.gui.convert_dialog(cmd) # show progress bar, kick off convert if self.gui.convert_cancelled: logger.info("Convert process cancelled by user") return after = os.walk(path).next()[2] count = len(after) - len(before) images = [] ignore = False logger.info("Convert process complete. %i images were created", count) if not count: logger.warning("Failed to convert.") wx.MessageBox( _("Failed to convert file. Ensure GhostScript is installed\nhttp://pages.cs.wisc.edu/~ghost/"), _("Conversion Failed"), ) open_url(u"http://pages.cs.wisc.edu/~ghost/") return if count == 1: images.append(path + tmp_file + u".png") ignore = True else: for x in range(count): # store the temp file path for this file in the dictionary images.append(u"%s%s-%i.png" % (path, tmp_file, x)) self.display_converted(_file, images, ignore) self.library.write(_file, images, quality) # Just in case it's a file with many pages self.gui.show_progress_dialog(_("Loading...")) self.gui.on_done_load()