コード例 #1
0
ファイル: stored_bibs.py プロジェクト: alyssaq/distil
def change_cite_key_and_rename_dir(curr_cite_key, new_cite_key):
  """Change 'curr_cite_key' to 'new_cite_key', and rename the cite-key directory
  and the files within it accordingly.
  
  Cite-keys are auto-generated to be predictable and consistent, so it's not
  recommended that you change them manually, but if you must, this function
  will enable you to do so.

  It's assumed that the cite-key directory, and the files within it, have been
  committed to the repository already.  (For example, perhaps you've just added
  a new bib+doc, and you notice that the auto-generated cite-key is terrible,
  and now you want to change it.)
  """

  # Rename the cite-key dir.
  # Rename the bib file and, if present, the doc.
  # Change the cite-key in the bib file.
  # Update the topic-tags indices appropriately.

  bibs_subdir_abspath = os.path.join(config.DOCLIB_BASE_ABSPATH, constants.BIBS_SUBDIR)
  curr_cite_key_dir_abspath = os.path.join(bibs_subdir_abspath, curr_cite_key)
  new_cite_key_dir_abspath = os.path.join(bibs_subdir_abspath, new_cite_key)

  if not os.path.exists(curr_cite_key_dir_abspath):
    raise filesystem_utils.DirectoryNotFound(curr_cite_key_dir_abspath)
  if os.path.exists(new_cite_key_dir_abspath):
    raise DirectoryAlreadyExistsInBibs(new_cite_key)
  repository.move(curr_cite_key_dir_abspath, new_cite_key_dir_abspath)
  dirs_modified_abspaths = [curr_cite_key_dir_abspath, new_cite_key_dir_abspath]

  curr_bib_fname_abspath = os.path.join(new_cite_key_dir_abspath, curr_cite_key + ".bib")
  new_bib_fname_abspath = os.path.join(new_cite_key_dir_abspath, new_cite_key + ".bib")
  repository.move(curr_bib_fname_abspath, new_bib_fname_abspath)
  bibfile_utils.replace_cite_key_in_file(new_cite_key, new_bib_fname_abspath)

  doc_attrs = get_doc_attrs(new_cite_key, curr_cite_key)
  if doc_attrs:
    # There is a doc in this cite-key dir.
    curr_doc_fname_abspath = os.path.join(new_cite_key_dir_abspath, doc_attrs["doc-name"])
    new_doc_fname_abspath = os.path.join(new_cite_key_dir_abspath, new_cite_key + doc_attrs["doc-suffix"])
    repository.move(curr_doc_fname_abspath, new_doc_fname_abspath)

  topic_tags_fname_abspath = os.path.join(new_cite_key_dir_abspath, constants.TOPIC_TAGS_FNAME)
  if os.path.exists(topic_tags_fname_abspath):
    # There are topic tags, so we need to update the indices.
    topic_tags = topic_tag_file_io.read_topic_tags(topic_tags_fname_abspath)

    # Note that, because the current cite-key is already in this topic-tag index,
    # we know that this topic-tag index will exist, so we use 'add...to_existing'
    # rather than 'add..._to_new' for the new cite-key.
    index_dir_abspath = os.path.join(config.DOCLIB_BASE_ABSPATH, constants.TOPIC_TAG_INDEX_SUBDIR)
    filesystem_utils.ensure_dir_exists(index_dir_abspath)
    topic_tag_file_io.add_cite_key_to_existing_topic_tag_index(new_cite_key, topic_tags, index_dir_abspath)
    topic_tag_file_io.remove_cite_key_from_topic_tag_index(curr_cite_key, topic_tags, index_dir_abspath)

    dirs_modified_abspaths.append(index_dir_abspath)

  repository.commit(dirs_modified_abspaths,
      "Renamed cite-key '%s' to '%s'" % (curr_cite_key, new_cite_key))
コード例 #2
0
ファイル: stored_bibs.py プロジェクト: alyssaq/distil
def store_new_bib(bib_fname, doc_fname=None, abstract_fname=None):
    """Store a new bib-file in the doclib."""

    if not os.path.exists(bib_fname):
        raise filesystem_utils.FileNotFound(bib_fname)
    if doc_fname and not os.path.exists(doc_fname):
        raise filesystem_utils.FileNotFound(doc_fname)
    if abstract_fname and not os.path.exists(abstract_fname):
        raise filesystem_utils.FileNotFound(abstract_fname)

    # Store the bib-entry in a directory named after the cite-key.
    # This will ensure an almost-unique directory-name for each bib-entry, while
    # also enabling duplicate bib-entries to be detected.
    cite_key = get_one_cite_key(bib_fname)
    bibs_subdir_abspath = os.path.join(config.DOCLIB_BASE_ABSPATH,
                                       constants.BIBS_SUBDIR)
    cite_key_dir_abspath = os.path.join(bibs_subdir_abspath, cite_key)
    try:
        os.makedirs(cite_key_dir_abspath)
    except OSError as e:
        if e.errno == errno.EEXIST:
            raise DirectoryAlreadyExistsInBibs(cite_key)
        else:
            raise

    new_bib_fname_abspath = \
        filesystem_utils.move_and_rename(bib_fname, cite_key_dir_abspath,
            cite_key + ".bib")
    bibfile_utils.replace_cite_key_in_file(cite_key, new_bib_fname_abspath)

    if doc_fname:
        filesystem_utils.move_and_rename(
            doc_fname, cite_key_dir_abspath,
            cite_key + filesystem_utils.get_suffix(doc_fname))
    if abstract_fname:
        filesystem_utils.move_and_rename(abstract_fname, cite_key_dir_abspath,
                                         constants.ABSTRACT_FNAME)

    filesystem_utils.add_datestamp(cite_key_dir_abspath)
    repository.add_and_commit_new_cite_key_dir(cite_key)

    # Do we want to merge the commit in the following function with the commit
    # in 'add_and_commit_new_cite_key_dir'?
    topic_tag_file_io.update_topic_tags_for_cite_key(cite_key, [],
                                                     "new unread")

    return (cite_key, cite_key_dir_abspath)
コード例 #3
0
ファイル: stored_bibs.py プロジェクト: alyssaq/distil
def store_new_bib(bib_fname, doc_fname=None, abstract_fname=None):
  """Store a new bib-file in the doclib."""

  if not os.path.exists(bib_fname):
    raise filesystem_utils.FileNotFound(bib_fname)
  if doc_fname and not os.path.exists(doc_fname):
    raise filesystem_utils.FileNotFound(doc_fname)
  if abstract_fname and not os.path.exists(abstract_fname):
    raise filesystem_utils.FileNotFound(abstract_fname)

  # Store the bib-entry in a directory named after the cite-key.
  # This will ensure an almost-unique directory-name for each bib-entry, while
  # also enabling duplicate bib-entries to be detected.
  cite_key = get_one_cite_key(bib_fname)
  bibs_subdir_abspath = os.path.join(config.DOCLIB_BASE_ABSPATH, constants.BIBS_SUBDIR)
  cite_key_dir_abspath = os.path.join(bibs_subdir_abspath, cite_key)
  try:
    os.makedirs(cite_key_dir_abspath)
  except OSError as e:
    if e.errno == errno.EEXIST:
      raise DirectoryAlreadyExistsInBibs(cite_key)
    else:
      raise

  new_bib_fname_abspath = \
      filesystem_utils.move_and_rename(bib_fname, cite_key_dir_abspath,
          cite_key + ".bib")
  bibfile_utils.replace_cite_key_in_file(cite_key, new_bib_fname_abspath)

  if doc_fname:
    filesystem_utils.move_and_rename(doc_fname, cite_key_dir_abspath,
        cite_key + filesystem_utils.get_suffix(doc_fname))
  if abstract_fname:
    filesystem_utils.move_and_rename(abstract_fname, cite_key_dir_abspath,
        constants.ABSTRACT_FNAME)

  filesystem_utils.add_datestamp(cite_key_dir_abspath)
  repository.add_and_commit_new_cite_key_dir(cite_key)

  # Do we want to merge the commit in the following function with the commit
  # in 'add_and_commit_new_cite_key_dir'?
  topic_tag_file_io.update_topic_tags_for_cite_key(cite_key, [], "new unread")

  return (cite_key, cite_key_dir_abspath)
コード例 #4
0
ファイル: stored_bibs.py プロジェクト: alyssaq/distil
def change_cite_key_and_rename_dir(curr_cite_key, new_cite_key):
    """Change 'curr_cite_key' to 'new_cite_key', and rename the cite-key directory
  and the files within it accordingly.
  
  Cite-keys are auto-generated to be predictable and consistent, so it's not
  recommended that you change them manually, but if you must, this function
  will enable you to do so.

  It's assumed that the cite-key directory, and the files within it, have been
  committed to the repository already.  (For example, perhaps you've just added
  a new bib+doc, and you notice that the auto-generated cite-key is terrible,
  and now you want to change it.)
  """

    # Rename the cite-key dir.
    # Rename the bib file and, if present, the doc.
    # Change the cite-key in the bib file.
    # Update the topic-tags indices appropriately.

    bibs_subdir_abspath = os.path.join(config.DOCLIB_BASE_ABSPATH,
                                       constants.BIBS_SUBDIR)
    curr_cite_key_dir_abspath = os.path.join(bibs_subdir_abspath,
                                             curr_cite_key)
    new_cite_key_dir_abspath = os.path.join(bibs_subdir_abspath, new_cite_key)

    if not os.path.exists(curr_cite_key_dir_abspath):
        raise filesystem_utils.DirectoryNotFound(curr_cite_key_dir_abspath)
    if os.path.exists(new_cite_key_dir_abspath):
        raise DirectoryAlreadyExistsInBibs(new_cite_key)
    repository.move(curr_cite_key_dir_abspath, new_cite_key_dir_abspath)
    dirs_modified_abspaths = [
        curr_cite_key_dir_abspath, new_cite_key_dir_abspath
    ]

    curr_bib_fname_abspath = os.path.join(new_cite_key_dir_abspath,
                                          curr_cite_key + ".bib")
    new_bib_fname_abspath = os.path.join(new_cite_key_dir_abspath,
                                         new_cite_key + ".bib")
    repository.move(curr_bib_fname_abspath, new_bib_fname_abspath)
    bibfile_utils.replace_cite_key_in_file(new_cite_key, new_bib_fname_abspath)

    doc_attrs = get_doc_attrs(new_cite_key, curr_cite_key)
    if doc_attrs:
        # There is a doc in this cite-key dir.
        curr_doc_fname_abspath = os.path.join(new_cite_key_dir_abspath,
                                              doc_attrs["doc-name"])
        new_doc_fname_abspath = os.path.join(
            new_cite_key_dir_abspath, new_cite_key + doc_attrs["doc-suffix"])
        repository.move(curr_doc_fname_abspath, new_doc_fname_abspath)

    topic_tags_fname_abspath = os.path.join(new_cite_key_dir_abspath,
                                            constants.TOPIC_TAGS_FNAME)
    if os.path.exists(topic_tags_fname_abspath):
        # There are topic tags, so we need to update the indices.
        topic_tags = topic_tag_file_io.read_topic_tags(
            topic_tags_fname_abspath)

        # Note that, because the current cite-key is already in this topic-tag index,
        # we know that this topic-tag index will exist, so we use 'add...to_existing'
        # rather than 'add..._to_new' for the new cite-key.
        index_dir_abspath = os.path.join(config.DOCLIB_BASE_ABSPATH,
                                         constants.TOPIC_TAG_INDEX_SUBDIR)
        filesystem_utils.ensure_dir_exists(index_dir_abspath)
        topic_tag_file_io.add_cite_key_to_existing_topic_tag_index(
            new_cite_key, topic_tags, index_dir_abspath)
        topic_tag_file_io.remove_cite_key_from_topic_tag_index(
            curr_cite_key, topic_tags, index_dir_abspath)

        dirs_modified_abspaths.append(index_dir_abspath)

    repository.commit(
        dirs_modified_abspaths,
        "Renamed cite-key '%s' to '%s'" % (curr_cite_key, new_cite_key))