def update_wiki_text(wiki_text, wiki_text_fname_abspath, change_descr, change_descrs_fname_abspath, what_was_changed): ensure_file_added_to_repo_if_created(open_file_write_string, wiki_text_fname_abspath, wiki_text) # If this function was called, then the wiki-text was changed, which means # that we should also append *something* to the notes-change descriptions # (even if it's just a datestamp and an empty-string). # We'll print the current time in seconds since the epoch (for easy parsing), # followed by the human-readable version. datestamp = "%s %s" % (int(time.time()), time.ctime()) # Convert every whitespace sequence in 'change_descr' to a single space character, # to ensure there are not somehow newlines written to the file (which would mess up # our assumption that odd lines are datestamps and even lines are descriptions). change_descr = " ".join(change_descr.split()) ensure_file_added_to_repo_if_created(open_file_append_strings, change_descrs_fname_abspath, [datestamp, change_descr]) repository.commit([wiki_text_fname_abspath, change_descrs_fname_abspath], "updated notes for %s: %s" % (what_was_changed, change_descr))
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 create_wiki_page(wiki_subdir_abspath, wiki_word): """Ensure the wiki-word directory and wiki-word file exist. Will also create the wiki-subdir if it doesn't already exist. """ wiki_word_dir_abspath = os.path.join(wiki_subdir_abspath, wiki_word) os.makedirs(wiki_word_dir_abspath) wiki_fname_abspath = os.path.join(wiki_word_dir_abspath, wiki_word + constants.WIKI_FNAME_SUFFIX) filesystem_utils.create_empty_file(wiki_fname_abspath) repository.add(wiki_fname_abspath) repository.commit([wiki_fname_abspath], "created wiki page '%s'" % wiki_word)
def create_wiki_page(wiki_subdir_abspath, wiki_word): """Ensure the wiki-word directory and wiki-word file exist. Will also create the wiki-subdir if it doesn't already exist. """ wiki_word_dir_abspath = os.path.join(wiki_subdir_abspath, wiki_word) os.makedirs(wiki_word_dir_abspath) wiki_fname_abspath = os.path.join(wiki_word_dir_abspath, wiki_word + constants.WIKI_FNAME_SUFFIX) filesystem_utils.create_empty_file(wiki_fname_abspath) repository.add(wiki_fname_abspath) repository.commit([wiki_fname_abspath], "created wiki page '%s'" % wiki_word)
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 update_wiki_text(wiki_text, wiki_text_fname_abspath, change_descr, change_descrs_fname_abspath, what_was_changed): ensure_file_added_to_repo_if_created(open_file_write_string, wiki_text_fname_abspath, wiki_text) # If this function was called, then the wiki-text was changed, which means # that we should also append *something* to the notes-change descriptions # (even if it's just a datestamp and an empty-string). # We'll print the current time in seconds since the epoch (for easy parsing), # followed by the human-readable version. datestamp = "%s %s" % (int(time.time()), time.ctime()) # Convert every whitespace sequence in 'change_descr' to a single space character, # to ensure there are not somehow newlines written to the file (which would mess up # our assumption that odd lines are datestamps and even lines are descriptions). change_descr = " ".join(change_descr.split()) ensure_file_added_to_repo_if_created(open_file_append_strings, change_descrs_fname_abspath, [datestamp, change_descr]) repository.commit([wiki_text_fname_abspath, change_descrs_fname_abspath], "updated notes for %s: %s" % (what_was_changed, change_descr))
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 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))