def retrieve_stored(document, suffix): stored_path = _stored_path()+'.'+suffix if not isfile(stored_path): # @ninjin: not sure what 'version' was supposed to be returned # here, but none was defined, so returning that # raise NoSVGError(version) raise NoSVGError('None') filename = document+'.'+suffix # sorry, quick hack to get the content-type right # TODO: send this with initial 'stored' response instead of # guessing on suffix if suffix == SVG_SUFFIX: content_type = 'image/svg+xml' elif suffix == PNG_SUFFIX: content_type = 'image/png' elif suffix == PDF_SUFFIX: content_type = 'application/pdf' elif suffix == EPS_SUFFIX: content_type = 'application/postscript' else: Messager.error('Unknown suffix "%s"; cannot determine Content-Type' % suffix) # TODO: reasonable backoff value content_type = None # Bail out with a hack since we violated the protocol hdrs = [('Content-Type', content_type), ('Content-Disposition', 'inline; filename=' + filename)] with open(stored_path, 'rb') as stored_file: data = stored_file.read() raise NoPrintJSONError(hdrs, data)
def download_collection(collection, exclude_configs=False): directory = collection real_dir = real_directory(directory) dir_name = basename(dirname(real_dir)) fname = '%s.%s' % (dir_name, 'tar.gz') tmp_file_path = None try: tmp_file_fh, tmp_file_path = mkstemp() os_close(tmp_file_fh) tar_cmd_split = ['tar', '--exclude=.stats_cache'] if exclude_configs: tar_cmd_split.extend(['--exclude=annotation.conf', '--exclude=visual.conf', '--exclude=tools.conf', '--exclude=kb_shortcuts.conf']) tar_cmd_split.extend(['-c', '-z', '-f', tmp_file_path, dir_name]) tar_p = Popen(tar_cmd_split, cwd=path_join(real_dir, '..')) tar_p.wait() hdrs = [('Content-Type', 'application/octet-stream'), #'application/x-tgz'), ('Content-Disposition', 'inline; filename=%s' % fname)] with open(tmp_file_path, 'rb') as tmp_file: tar_data = tmp_file.read() raise NoPrintJSONError(hdrs, tar_data) finally: if tmp_file_path is not None: remove(tmp_file_path)
def download_collection(collection, include_conf=False): directory = collection real_dir = real_directory(directory) dir_name = basename(dirname(real_dir)) fname = '%s.%s' % (dir_name, 'tar.gz') confs = [ 'annotation.conf', 'visual.conf', 'tools.conf', 'kb_shortcuts.conf' ] try: include_conf = int(include_conf) except ValueError: pass tmp_file_path = None try: tmp_file_fh, tmp_file_path = mkstemp() os_close(tmp_file_fh) tar_cmd_split = ['tar', '--exclude=.stats_cache'] conf_names = [] if not include_conf: tar_cmd_split.extend(['--exclude=%s' % c for c in confs]) else: # also include configs from parent directories. for cname in confs: cdir, depth = find_in_directory_tree(real_dir, cname) if depth is not None and depth > 0: relpath = path_join(dir_name, *['..' for _ in range(depth)]) conf_names.append(path_join(relpath, cname)) if conf_names: # replace pathname components ending in ".." with target # directory name so that .confs in parent directories appear # in the target directory in the tar. tar_cmd_split.extend([ '--absolute-names', '--transform', 's|.*\\.\\.|%s|' % dir_name ]) tar_cmd_split.extend(['-c', '-z', '-f', tmp_file_path, dir_name]) tar_cmd_split.extend(conf_names) tar_p = Popen(tar_cmd_split, cwd=path_join(real_dir, '..')) tar_p.wait() hdrs = [ ('Content-Type', 'application/octet-stream'), #'application/x-tgz'), ('Content-Disposition', 'inline; filename=%s' % fname) ] with open(tmp_file_path, 'rb') as tmp_file: tar_data = tmp_file.read() raise NoPrintJSONError(hdrs, tar_data) finally: if tmp_file_path is not None: remove(tmp_file_path)
def download_file(document, collection, extension): directory = collection real_dir = real_directory(directory) fname = '%s.%s' % (document, extension) fpath = path_join(real_dir, fname) hdrs = [('Content-Type', 'text/plain; charset=utf-8'), ('Content-Disposition', 'inline; filename=%s' % fname)] with open_textfile(fpath, 'r') as txt_file: data = txt_file.read().encode('utf-8') raise NoPrintJSONError(hdrs, data)
def retrieve_svg(document): svg_path = _svg_path() if not isfile(svg_path): raise NoSVGError(version) # Bail out with a hack since we violated the protocol hdrs = [('Content-Type', 'image/svg+xml'), ('Content-Disposition', 'inline; filename=' + document + '.svg')] with open(svg_path, 'r') as svg_file: data = svg_file.read() raise NoPrintJSONError(hdrs, data)
def download_file(document, collection, extension): directory = collection real_dir = real_directory(directory) fname = '%s.%s' % (document, extension) fpath = path_join(real_dir, fname) hdrs = [('Content-Type', 'text/plain; charset=utf-8'), ('Content-Disposition', 'inline; filename=%s' % fname)] #Folia conversion added by Sander Naert from brat2folia import convert if extension == 'xml': convert(real_dir, document) #convert to folia with open_textfile(fpath, 'r') as txt_file: data = txt_file.read().encode('utf-8') raise NoPrintJSONError(hdrs, data)