Exemple #1
0
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))
  def render_page(self, order_by="cite-key"):
    filesystem_utils.ensure_dir_exists(self.bibs_subdir_abspath)
    cite_keys = os.listdir(self.bibs_subdir_abspath)
    cite_keys_and_attrs = map(self.get_cite_keys_and_attrs, cite_keys)
    self.sort_cite_keys_and_attrs(cite_keys_and_attrs, order_by)

    self.render("cite-keys.html", title="Cite Keys", items=cite_keys_and_attrs,
        choices_and_text=self.order_by_choices_and_text, order_by_choice=order_by)
Exemple #3
0
def get_all_topic_tags(sort_tags=True):
  """Return a list of all the topic tags in the topic tag index."""

  index_dir_abspath = os.path.join(config.DOCLIB_BASE_ABSPATH, constants.TOPIC_TAG_INDEX_SUBDIR)
  filesystem_utils.ensure_dir_exists(index_dir_abspath)
  all_topic_tags = os.listdir(index_dir_abspath)
  if sort_tags:
    all_topic_tags.sort()
  return all_topic_tags
Exemple #4
0
def get_all_topic_tags(sort_tags=True):
    """Return a list of all the topic tags in the topic tag index."""

    index_dir_abspath = os.path.join(config.DOCLIB_BASE_ABSPATH,
                                     constants.TOPIC_TAG_INDEX_SUBDIR)
    filesystem_utils.ensure_dir_exists(index_dir_abspath)
    all_topic_tags = os.listdir(index_dir_abspath)
    if sort_tags:
        all_topic_tags.sort()
    return all_topic_tags
    def render_page(self, order_by="cite-key"):
        filesystem_utils.ensure_dir_exists(self.bibs_subdir_abspath)
        cite_keys = os.listdir(self.bibs_subdir_abspath)
        cite_keys_and_attrs = map(self.get_cite_keys_and_attrs, cite_keys)
        self.sort_cite_keys_and_attrs(cite_keys_and_attrs, order_by)

        self.render("cite-keys.html",
                    title="Cite Keys",
                    items=cite_keys_and_attrs,
                    choices_and_text=self.order_by_choices_and_text,
                    order_by_choice=order_by)
  def get_attachments_with_attrs(self):
    filesystem_utils.ensure_dir_exists(self.attachments_subdir_abspath)
    all_attachment_dirnames = os.listdir(self.attachments_subdir_abspath)
    attachments_with_attrs = [attachments.get_attachment_attrs(dirname)
        for dirname in all_attachment_dirnames]

    # We want the attachment index to be sorted primarily by the
    # human-readable filename (compared case-INSENSITIVELY), and
    # secondarily by the unique attachment ID (to ensure that if there
    # are duplicate human-readable filenames, their relative ordering
    # will be stable).
    return sorted(
        sorted(attachments_with_attrs, key=lambda t: t[1]),
            key=lambda t: t[0].lower())
    def get_attachments_with_attrs(self):
        filesystem_utils.ensure_dir_exists(self.attachments_subdir_abspath)
        all_attachment_dirnames = os.listdir(self.attachments_subdir_abspath)
        attachments_with_attrs = [
            attachments.get_attachment_attrs(dirname)
            for dirname in all_attachment_dirnames
        ]

        # We want the attachment index to be sorted primarily by the
        # human-readable filename (compared case-INSENSITIVELY), and
        # secondarily by the unique attachment ID (to ensure that if there
        # are duplicate human-readable filenames, their relative ordering
        # will be stable).
        return sorted(sorted(attachments_with_attrs, key=lambda t: t[1]),
                      key=lambda t: t[0].lower())
Exemple #8
0
def update_topic_tags_for_cite_key(cite_key, chosen_tags, new_tags_str):
    """Update the topic tags for 'cite_key' to 'chosen_tags' (a collection of existing tags)
  and 'new_tags_str' (a string of whitespace-or-comma-delimited non-pre-existing tags).

  This function will sanitise all the tags.

  It will also ensure that all the "new tags" are actually new, moving them into the
  collection of "chosen tags" if they're not.
  """
    index_dir_abspath = os.path.join(config.DOCLIB_BASE_ABSPATH,
                                     constants.TOPIC_TAG_INDEX_SUBDIR)
    filesystem_utils.ensure_dir_exists(index_dir_abspath)

    chosen_tags = set(map(sanitize_tag, chosen_tags))
    new_tags = set(
        map(sanitize_tag, split_at_whitespace_and_commas(new_tags_str)))
    ensure_new_tags_are_actually_new(new_tags, chosen_tags)

    prev_tags = set(get_topic_tags_for_cite_key(cite_key, sort_tags=False))
    if (chosen_tags == prev_tags) and not new_tags:
        # There were no changes to the tags for this cite-key.
        return

    added_tags = chosen_tags - prev_tags
    removed_tags = prev_tags - chosen_tags

    topic_tags_fname_abspath = \
        os.path.join(config.DOCLIB_BASE_ABSPATH, constants.BIBS_SUBDIR, cite_key, constants.TOPIC_TAGS_FNAME)
    write_topic_tags(topic_tags_fname_abspath,
                     list(chosen_tags) + list(new_tags))

    remove_cite_key_from_topic_tag_index(cite_key, removed_tags,
                                         index_dir_abspath)
    add_cite_key_to_existing_topic_tag_index(cite_key, added_tags,
                                             index_dir_abspath)
    add_cite_key_to_new_topic_tag_index(cite_key, new_tags, index_dir_abspath)

    # If this function was called, then something must have been changed...
    # so commit something.
    repository.commit([topic_tags_fname_abspath, index_dir_abspath],
                      "updated topic tags for cite-key %s" % cite_key)
Exemple #9
0
def update_topic_tags_for_cite_key(cite_key, chosen_tags, new_tags_str):
  """Update the topic tags for 'cite_key' to 'chosen_tags' (a collection of existing tags)
  and 'new_tags_str' (a string of whitespace-or-comma-delimited non-pre-existing tags).

  This function will sanitise all the tags.

  It will also ensure that all the "new tags" are actually new, moving them into the
  collection of "chosen tags" if they're not.
  """
  index_dir_abspath = os.path.join(config.DOCLIB_BASE_ABSPATH, constants.TOPIC_TAG_INDEX_SUBDIR)
  filesystem_utils.ensure_dir_exists(index_dir_abspath)

  chosen_tags = set(map(sanitize_tag, chosen_tags))
  new_tags = set(map(sanitize_tag, split_at_whitespace_and_commas(new_tags_str)))
  ensure_new_tags_are_actually_new(new_tags, chosen_tags)

  prev_tags = set(get_topic_tags_for_cite_key(cite_key, sort_tags=False))
  if (chosen_tags == prev_tags) and not new_tags:
    # There were no changes to the tags for this cite-key.
    return

  added_tags = chosen_tags - prev_tags
  removed_tags = prev_tags - chosen_tags

  topic_tags_fname_abspath = \
      os.path.join(config.DOCLIB_BASE_ABSPATH, constants.BIBS_SUBDIR, cite_key, constants.TOPIC_TAGS_FNAME)
  write_topic_tags(topic_tags_fname_abspath, list(chosen_tags) + list(new_tags))

  remove_cite_key_from_topic_tag_index(cite_key, removed_tags, index_dir_abspath)
  add_cite_key_to_existing_topic_tag_index(cite_key, added_tags, index_dir_abspath)
  add_cite_key_to_new_topic_tag_index(cite_key, new_tags, index_dir_abspath)

  # If this function was called, then something must have been changed...
  # so commit something.
  repository.commit([topic_tags_fname_abspath, index_dir_abspath],
      "updated topic tags for cite-key %s" % cite_key)
    def get(self):
        filesystem_utils.ensure_dir_exists(self.wiki_subdir_abspath)
        titles = os.listdir(self.wiki_subdir_abspath)
        titles.sort()

        self.render("wiki-words.html", title="Wiki Words", items=titles)
  def get(self):
    filesystem_utils.ensure_dir_exists(self.wiki_subdir_abspath)
    titles = os.listdir(self.wiki_subdir_abspath)
    titles.sort()

    self.render("wiki-words.html", title="Wiki Words", items=titles)
Exemple #12
0
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))