def command(config, old_name, new_name): root_folder = os.path.expanduser(config.get("folders", "root")) forbidden_char = utils.forbidden_chars(new_name) if forbidden_char is not None: print >>sys.stderr, "New paper name cannot contain %s" % ( forbidden_char) return 1 old_folder = utils.paper_folder(old_name, config) new_folder = utils.paper_folder(new_name, config) old_paper_path = utils.paper_path(old_name, config) new_paper_path = utils.paper_path(new_name, config, allow_nonexistent = True) if not os.path.exists(old_folder): return 1 if os.path.exists(new_folder): print >>sys.stderr, "Paper '%s' already exists" % (new_name) return 1 if old_paper_path is None: print >>sys.stderr, "Can't find a paper PDF within '%s'" % (old_folder) return 1 shutil.move(old_folder, new_folder) shutil.move(os.path.join(new_folder, old_name + ".pdf"), new_paper_path) return 0
def command(config, name): paper_folder = utils.paper_folder(name, config) if len(name.strip()) == 0: print >>sys.stderr, 'Must provide a paper name to remove' return 1 if not os.path.exists(paper_folder): print >>sys.stderr, "No such paper '%s'" % (name) return 1 shutil.rmtree(paper_folder) return 0
def command(config, paper_path, paper_name, doi): """ Adds a paper to the libris folder by creating a directory for the paper and adding a copy of the paper, an empty notes file, and a default metadata file to that directory """ if not os.path.exists(paper_path): print >>sys.stderr, "Can't find paper '%s'" % (paper_path) return 1 if not os.path.isfile(paper_path): print >>sys.stderr, "'%s' is not a file" % (paper_path) return 1 with open(os.path.join(SCRIPT_PATH, "templates", "metadata.yaml"), 'r') as fp: metadata = yaml.load(fp.read()) metadata['date-added'] = time.strftime('%Y-%m-%d') # Attempt to retrieve paper metadata from grobid if config.has_section('grobid'): retrieved_metadata = get_metadata_from_grobid(paper_path, doi, config) print '' print 'Retrieved the following metadata:' print '' for key, value in retrieved_metadata.items(): if type(value) == list: print key + ': ' + ', '.join(map(unicode, value)) else: print key + ': ' + unicode(value) metadata.update(retrieved_metadata) # Set paper name to the name of the file if unspecified if paper_name is None: if len(metadata['authors']) > 0 and 'year' in metadata and metadata['year'] is not None: paper_name = "%s%02d" % (metadata['authors'][0].split(' ')[-1], metadata['year'] % 100) else: paper_name = os.path.splitext(os.path.basename(paper_path))[0] # Convert spaces in paper name to underscores if paper_name.find(' ') != -1: paper_name = paper_name.replace(' ', '_') print >>sys.stderr, ("Converting spaces in paper name to underscores; " "paper name is now '%s'" % (paper_name)) forbidden_char = utils.forbidden_chars(paper_name) if forbidden_char is not None: print >>sys.stderr, "Paper name cannot contain %s" % (forbidden_char) return 1 paper_basename = os.path.basename(paper_path) if paper_name is None: paper_name = os.path.splitext(paper_basename)[0] new_folder_path = utils.paper_folder(paper_name, config) if new_folder_path is None: return 1 try: os.makedirs(new_folder_path) except os.error, e: print >>sys.stderr, "Can't create '%s': %s" % (new_folder_path, e) return 1