def edit(nickname, date_string, defs): integerre = re.compile('[0-9]*') if date_string == "last": last = elist(nickname, 1, defs, print_out=False) date_string = last[0][0] elif integerre.fullmatch(date_string): last_entries = elist(nickname, -1, defs, print_out=False) nstring = int(date_string) if nstring < len(last_entries): date_string = last_entries[nstring][0] else: sys.exit("Invalid entry index {}. Number of entries is {}".format(nstring, len(last_entries))) # find the file corresponding to the date string entry_dir = "{}/journal-{}/entries/".format(defs[nickname]["working_path"], nickname) os.chdir(entry_dir) # if we got the date string from the prompt, it may have a "_" date_string = date_string.replace("_", " ") try: d, t = date_string.split(" ") except: sys.exit("invalid date string") if not os.path.isdir(d): sys.exit("entry directory does not exist") # Look for a Markdown file for the entry, # then a LaTeX file if it does not exist. file = "{}/{}_{}.md".format(d, d, t) if not os.path.isfile(file): file = "{}/{}_{}.tex".format(d, d, t) if not os.path.isfile(file): sys.exit("entry {} does not exist".format(file)) # open the file for appending try: editor = os.environ["EDITOR"] except: editor = "emacs" entry_id = get_entry_string() try: f = open(file, "a+") except: sys.exit("ERROR: unable to open {}".format(file)) f.close() if editor == "emacs": prog = "emacs -nw {}".format(file) else: prog = "{} {}".format(editor, file) stdout, stderr, rc = shell_util.run(prog) # git commit any changes stdout, stderr, rc = shell_util.run("git commit -m 'edited entry' " + file)
def add_list(list_name, defs): todo_dir = "{}/todo_list/".format(defs["working_path"]) try: os.chdir(todo_dir) except: sys.exit("ERROR: unable to cd into working directory {}".format(todo_dir)) # does it already exist? if os.path.isfile("{}.list".format(list_name)): sys.exit("ERROR: list already exists") # create the list file try: f = open("{}.list".format(list_name), "w") except: sys.exit("ERROR: unable to create list {}".format(list_name)) f.write("# list: {} managed by pytodo".format(list_name)) f.close() # commit the list stdout, stderr, rc = shell_util.run("git add {}.list".format(list_name)) stdout, stderr, rc = shell_util.run("git commit -m 'new list' {}.list".format(list_name))
def add_list(list_name, defs): todo_dir = "{}/todo_list/".format(defs["working_path"]) try: os.chdir(todo_dir) except: sys.exit("ERROR: unable to cd into working directory {}".format(todo_dir)) # does it already exist? if os.path.isfile("{}.list".format(list_name)): sys.exit("ERROR: list already exists") # create the list file try: f = open("{}.list".format(list_name), "w") except: sys.exit("ERROR: unable to create list {}".format(list_name)) f.write("# list: {} managed by pytodo".format(list_name)) f.close() # commit the list stdout, stderr, rc = shell_util.run("git add {}.list".format(list_name)) stdout, stderr, rc = shell_util.run("git commit -m 'new list' {}.list".format(list_name))
def init_todo(master_path, working_path, defs): # create the bare git repo git_master = "{}/todo_list.git".format(os.path.normpath(master_path)) try: os.mkdir(git_master) except: sys.exit( "ERROR: unable to create a directory in {}".format(master_path)) os.chdir(git_master) stdout, stderr, rc = shell_util.run("git init --bare") # create the local working copy try: os.chdir(os.path.normpath(working_path)) except: sys.exit("ERROR: unable to change to {}".format(working_path)) stdout, stderr, rc = shell_util.run("git clone " + git_master) # create (or add to) the .pytodorc file try: f = open(defs["param_file"], "a+") except: sys.exit("ERROR: unable to open {} for appending".format( defs["param_file"])) f.write("[main]\n") f.write("master_repo = {}\n".format(git_master)) f.write("working_path = {}\n".format(working_path)) f.write("\n") f.close() # create a README working_todo = "{}/todo_list".format(os.path.normpath(working_path)) try: f = open("{}/README".format(working_todo), "w") except: sys.exit("ERROR: unable to open {}/README".format(working_todo)) f.write("TODO collection managed by pytodo\n") f.close() # add README to the repo and do a git push to make it synced os.chdir(working_todo) stdout, stderr, rc = shell_util.run("git add README") stdout, stderr, rc = shell_util.run( "git commit -m 'initial README file' README") stdout, stderr, rc = shell_util.run("git push")
def md_to_tex(mdpath): """ Given a path specifying a markdown file, use pandoc to generate a tex file by the same name (modulo extension) in the same directory for building the journal. Returns the path of the new tex file. """ tbase = stripextension(mdpath) ttex = tbase + '.tex' ftex = os.path.join(os.path.dirname(mdpath), ttex) cmd = "pandoc --from=markdown --output={} {}".format(ftex, mdpath) shell_util.run(cmd) return ftex
def edit(nickname, date_string, defs): if date_string == "last": last = elist(nickname, 1, defs, print_out=False) date_string = last[0][0] # find the file corresponding to the date string entry_dir = "{}/journal-{}/entries/".format(defs[nickname]["working_path"], nickname) os.chdir(entry_dir) # if we got the date string from the prompt, it may have a "_" date_string = date_string.replace("_", " ") try: d, t = date_string.split(" ") except: sys.exit("invalid date string") if not os.path.isdir(d): sys.exit("entry directory does not exist") file = "{}/{}_{}.tex".format(d, d, t) if not os.path.isfile(file): sys.exit("entry {} does not exist".format(file)) # open the file for appending try: editor = os.environ["EDITOR"] except: editor = "emacs" entry_id = get_entry_string() try: f = open(file, "a+") except: sys.exit("ERROR: unable to open {}".format(file)) f.write("\n\n% entry edited: {}".format(entry_id)) f.close() if editor == "emacs": prog = "emacs -nw {}".format(file) else: prog = "{} {}".format(editor, file) stdout, stderr, rc = shell_util.run(prog) # git commit any changes stdout, stderr, rc = shell_util.run("git commit -m 'edited entry' " + file)
def init_todo(master_path, working_path, defs): # create the bare git repo git_master = "{}/todo_list.git".format(os.path.normpath(master_path)) try: os.mkdir(git_master) except: sys.exit("ERROR: unable to create a directory in {}".format(master_path)) os.chdir(git_master) stdout, stderr, rc = shell_util.run("git init --bare") # create the local working copy try: os.chdir(os.path.normpath(working_path)) except: sys.exit("ERROR: unable to change to {}".format(working_path)) stdout, stderr, rc = shell_util.run("git clone " + git_master) # create (or add to) the .pytodorc file try: f = open(defs["param_file"], "a+") except: sys.exit("ERROR: unable to open {} for appending".format(defs["param_file"])) f.write("[main]\n") f.write("master_repo = {}\n".format(git_master)) f.write("working_path = {}\n".format(working_path)) f.write("\n") f.close() # create a README working_todo = "{}/todo_list".format(os.path.normpath(working_path)) try: f = open("{}/README".format(working_todo), "w") except: sys.exit("ERROR: unable to open {}/README".format(working_todo)) f.write("TODO collection managed by pytodo\n") f.close() # add README to the repo and do a git push to make it synced os.chdir(working_todo) stdout, stderr, rc = shell_util.run("git add README") stdout, stderr, rc = shell_util.run("git commit -m 'initial README file' README") stdout, stderr, rc = shell_util.run("git push")
def appendix(nickname, name, defs, fmt='.tex'): # is there an appendix directory? app_dir = "{}/journal-{}/entries/appendices/".format( defs[nickname]["working_path"], nickname) if not os.path.isdir(app_dir): try: os.mkdir(app_dir) except: sys.exit("ERROR: unable to make the appendices/ directory") os.chdir(app_dir) # Look for a Markdown file for the appendix, # then a LaTeX file if it does not exist. file = "{}.md".format(name) if not os.path.isfile(file): file = "{}.tex".format(name) if not os.path.isfile(file): warning("appendix {} will be created".format(name)) # edit the file, create if it does not exist file = "{}{}".format(name, fmt) # open the file for appending try: editor = os.environ["EDITOR"] except: editor = "emacs" try: f = open(file, "a+") except: sys.exit("ERROR: unable to open {}".format(file)) entry_id = get_entry_string() f.close() if editor == "emacs": prog = "emacs -nw {}".format(file) else: prog = "{} {}".format(editor, file) stdout, stderr, rc = shell_util.run(prog) # git commit any changes stdout, stderr, rc = shell_util.run("git add " + file) stdout, stderr, rc = shell_util.run("git commit -m 'edited appendix' " + file)
def connect_todo(master_repo, working_path, defs): # if a .pytodorc file already exists, we abort -- only one # collection per machine if os.path.isfile(defs["param_file"]): sys.exit("ERROR: a pytodo collection already exists") # git clone the bare repo at master_repo into the working path try: os.chdir(working_path) except: sys.exit("ERROR: unable to switch to directory {}".format(working_path)) stdout, stderr, rc = shell_util.run("git clone " + master_repo) if not rc == 0: print(stderr) sys.exit("ERROR: something went wrong with the git clone") # create the .pytodorc file try: f = open(defs["param_file"], "w") except: sys.exit("ERROR: unable to open {}".format(defs["param_file"])) f.write("[main]\n") f.write("master_repo = {}\n".format(master_repo)) f.write("working_path = {}\n".format(working_path)) f.write("\n") f.close()
def connect_todo(master_repo, working_path, defs): # if a .pytodorc file already exists, we abort -- only one # collection per machine if os.path.isfile(defs["param_file"]): sys.exit("ERROR: a pytodo collection already exists") # git clone the bare repo at master_repo into the working path try: os.chdir(working_path) except: sys.exit( "ERROR: unable to switch to directory {}".format(working_path)) stdout, stderr, rc = shell_util.run("git clone " + master_repo) if not rc == 0: print(stderr) sys.exit("ERROR: something went wrong with the git clone") # create the .pytodorc file try: f = open(defs["param_file"], "w") except: sys.exit("ERROR: unable to open {}".format(defs["param_file"])) f.write("[main]\n") f.write("master_repo = {}\n".format(master_repo)) f.write("working_path = {}\n".format(working_path)) f.write("\n") f.close()
def connect(master_repo, working_path, defs): # get the nickname from the master repo name re_name = r"journal-(.*).git" a = re.search(re_name, master_repo) if not a == None: nickname = a.group(1) else: sys.exit("ERROR: the remote-git-repo should be of the form: ssh://machine/dir/journal-nickname.git") # make sure that a journal with this nickname doesn't already exist if nickname in defs.keys(): sys.exit("ERROR: nickname already exists") # git clone the bare repo at master_repo into the working path try: os.chdir(working_path) except: sys.exit("ERROR: unable to switch to directory {}".format(working_path)) stdout, stderr, rc = shell_util.run("git clone " + master_repo) if not rc == 0: print(stderr) sys.exit("ERROR: something went wrong with the git clone") # create (or add to) the .pyjournalrc file try: f = open(defs["param_file"], "a+") except: sys.exit("ERROR: unable to open {} for appending".format(defs["param_file"])) f.write("[{}]\n".format(nickname)) f.write("master_repo = {}\n".format(master_repo)) f.write("working_path = {}\n".format(working_path)) f.write("\n") f.close()
def appendix(nickname, name, defs): # is there an appendix directory? app_dir = "{}/journal-{}/entries/appendices/".format( defs[nickname]["working_path"], nickname) if not os.path.isdir(app_dir): try: os.mkdir(app_dir) except: sys.exit("ERROR: unable to make the appendices/ directory") os.chdir(app_dir) # edit the file, create if it does not exist file = "{}.tex".format(name) if not os.path.isfile(file): warning("appendix {} will be created".format(name)) # open the file for appending try: editor = os.environ["EDITOR"] except: editor = "emacs" try: f = open(file, "a+") except: sys.exit("ERROR: unable to open {}".format(file)) entry_id = get_entry_string() f.write("\n\n% entry edited: {}".format(entry_id)) f.close() if editor == "emacs": prog = "emacs -nw {}".format(file) else: prog = "{} {}".format(editor, file) stdout, stderr, rc = shell_util.run(prog) # git commit any changes stdout, stderr, rc = shell_util.run("git add " + file) stdout, stderr, rc = shell_util.run("git commit -m 'edited appendix' " + file)
def rename_list(old_name, new_name, defs): todo_dir = "{}/todo_list/".format(defs["working_path"]) try: os.chdir(todo_dir) except: sys.exit("ERROR: unable to cd into working directory {}".format(todo_dir)) if not os.path.isfile("{}.list".format(old_name)): sys.exit("ERROR: list does not exist") try: shutil.move("{}.list".format(old_name), "{}.list".format(new_name)) except: sys.exit("ERROR: unable to rename list") stdout, stderr, rc = shell_util.run("git add {}.list".format(new_name)) stdout, stderr, rc = \ shell_util.run("git commit -m 'renamed' {}.list {}.list".format(old_name, new_name))
def rename_list(old_name, new_name, defs): todo_dir = "{}/todo_list/".format(defs["working_path"]) try: os.chdir(todo_dir) except: sys.exit("ERROR: unable to cd into working directory {}".format(todo_dir)) if not os.path.isfile("{}.list".format(old_name)): sys.exit("ERROR: list does not exist") try: shutil.move("{}.list".format(old_name), "{}.list".format(new_name)) except: sys.exit("ERROR: unable to rename list") stdout, stderr, rc = shell_util.run("git add {}.list".format(new_name)) stdout, stderr, rc = \ shell_util.run("git commit -m 'renamed' {}.list {}.list".format(old_name, new_name))
def show(list_name, defs): todo_dir = "{}/todo_list/".format(defs["working_path"]) try: os.chdir(todo_dir) except: sys.exit("ERROR: unable to cd into working directory {}".format(todo_dir)) # does it already exist? if not os.path.isfile("{}.list".format(list_name)): sys.exit("ERROR: list does not exist") hash_orig = hashlib.md5(open("{}.list".format(list_name), 'r').read().encode('utf-8')).hexdigest() # open for editing try: editor = os.environ["EDITOR"] except: editor = "emacs" if editor == "emacs": prog = "emacs -nw {}.list".format(list_name) else: prog = "{} {}.list".format(editor, list_name) stdout, stderr, rc = shell_util.run(prog) hash_new = hashlib.md5(open("{}.list".format(list_name), 'r').read().encode('utf-8')).hexdigest() if hash_orig != hash_new: # git-store the updates stdout, stderr, rc = \ shell_util.run("git commit -m 'edited list {}.list' {}.list".format(list_name, list_name)) if rc != 0: print(stdout, stderr) sys.exit("ERROR: there were git errors commiting the list")
def show(list_name, defs): todo_dir = "{}/todo_list/".format(defs["working_path"]) try: os.chdir(todo_dir) except: sys.exit("ERROR: unable to cd into working directory {}".format(todo_dir)) # does it already exist? if not os.path.isfile("{}.list".format(list_name)): sys.exit("ERROR: list does not exist") hash_orig = hashlib.md5(open("{}.list".format(list_name), 'r').read().encode('utf-8')).hexdigest() # open for editing try: editor = os.environ["EDITOR"] except: editor = "emacs" if editor == "emacs": prog = "emacs -nw {}.list".format(list_name) else: prog = "{} {}.list".format(editor, list_name) stdout, stderr, rc = shell_util.run(prog) hash_new = hashlib.md5(open("{}.list".format(list_name), 'r').read().encode('utf-8')).hexdigest() if hash_orig != hash_new: # git-store the updates stdout, stderr, rc = \ shell_util.run("git commit -m 'edited list {}.list' {}.list".format(list_name, list_name)) if rc != 0: print(stdout, stderr) sys.exit("ERROR: there were git errors commiting the list")
def push(defs, nickname=None): # switch to the working directory and push to the master if not nickname == None: wd = "{}/journal-{}".format(defs[nickname]["working_path"], nickname) else: wd = "{}/todo_list".format(defs["working_path"]) try: os.chdir(wd) except: sys.exit("ERROR: unable to switch to working directory: {}".format(wd)) stdout, stderr, rc = shell_util.run("git push") if not rc == 0: print(stderr) sys.exit("ERROR: something went wrong with the git push") print(stderr)
def push(defs, nickname=None): # switch to the working directory and push to the master if not nickname == None: wd = "{}/journal-{}".format(defs[nickname]["working_path"], nickname) else: wd = "{}/todo_list".format(defs["working_path"]) try: os.chdir(wd) except: sys.exit("ERROR: unable to switch to working directory: {}".format(wd)) stdout, stderr, rc = shell_util.run("git push") if not rc == 0: print(stderr) sys.exit("ERROR: something went wrong with the git push") print(stderr)
def connect(master_repo, working_path, defs): # get the nickname from the master repo name re_name = r"journal-(.*).git" a = re.search(re_name, master_repo) if not a == None: nickname = a.group(1) else: sys.exit( "ERROR: the remote-git-repo should be of the form: ssh://machine/dir/journal-nickname.git" ) # make sure that a journal with this nickname doesn't already exist if nickname in defs.keys(): sys.exit("ERROR: nickname already exists") # git clone the bare repo at master_repo into the working path try: os.chdir(working_path) except: sys.exit( "ERROR: unable to switch to directory {}".format(working_path)) stdout, stderr, rc = shell_util.run("git clone " + master_repo) if not rc == 0: print(stderr) sys.exit("ERROR: something went wrong with the git clone") # create (or add to) the .pyjournalrc file try: f = open(defs["param_file"], "a+") except: sys.exit("ERROR: unable to open {} for appending".format( defs["param_file"])) f.write("[{}]\n".format(nickname)) f.write("master_repo = {}\n".format(master_repo)) f.write("working_path = {}\n".format(working_path)) f.write("\n") f.close()
def init(nickname, master_path, working_path, defs): # check the working path to make sure it is absolute, not relative working_path = os.path.normpath(working_path) if not working_path.startswith("/"): working_path = os.path.abspath(working_path) # make sure that a journal with this nickname doesn't already exist if nickname in defs.keys(): sys.exit("ERROR: nickname already exists") # we are create the directory beneath master_path/, so make sure that # exists master_path = os.path.normpath(master_path) if not master_path.startswith("/"): master_path = os.path.abspath(master_path) if not os.path.isdir(master_path): try: os.mkdir(master_path) except: sys.exit( "ERROR: you need to specify an existing path in which to create the journal repo" ) # create the bare git repo git_master = "{}/journal-{}.git".format(master_path, nickname) try: os.mkdir(git_master) except: sys.exit( "ERROR: unable to create a directory in {}".format(master_path)) os.chdir(git_master) stdout, stderr, rc = shell_util.run("git init --bare") # create the local working copy try: os.chdir(working_path) except: sys.exit("ERROR: unable to change to {}".format(working_path)) stdout, stderr, rc = shell_util.run("git clone " + git_master) # create the initial directory structure working_journal = "{}/journal-{}".format(working_path, nickname) try: os.mkdir(working_journal + "/entries/") except: sys.exit("ERROR: unable to create initial directory structure") # create (or add to) the .pyjournalrc file try: f = open(defs["param_file"], "a+") except: sys.exit("ERROR: unable to open {} for appending".format( defs["param_file"])) f.write("[{}]\n".format(nickname)) f.write("master_repo = {}\n".format(git_master)) f.write("working_path = {}\n".format(working_path)) f.write("\n") f.close() defs[nickname] = {} defs[nickname]["master_repo"] = git_master defs[nickname]["working_path"] = working_path # create an initial entry saying "journal created" images = [] entry_util.entry(nickname, images, defs, string="journal created") # copy over the journal.tex try: f = open("{}/journal.tex".format(working_journal), "w") except: sys.exit( "ERROR: unable to open {}/journal.tex".format(working_journal)) for line in master_util.journal_master.split("\n"): f.write("{}\n".format(line.rstrip())) f.close() # add journal.tex to the repo and do a git push to make it synced os.chdir(working_journal) stdout, stderr, rc = shell_util.run("git add journal.tex") stdout, stderr, rc = shell_util.run( "git commit -m 'initial journal.tex file' journal.tex") stdout, stderr, rc = shell_util.run("git push origin master")
try: notesdir = os.environ["NOTES"] except: notesdir = "." tododir = notesdir + "/todos" #################################3 # open editor and modify tmp file full_path = "{}/tmp-todo.md".format(tododir) shell_util.openFile(full_path) # create note from it # n = readNoteFile(full_path) n = readSimpleNote(full_path) #################################3 # analyze and set defaults # if date is not set, add today if n.date == "": n.setDateNow() # save to file n.save(tododir) # delete temporary shell_util.run("rm {}".format(full_path))
def entry(nickname, images, defs, fmt='.tex', string=None): try: editor = os.environ["EDITOR"] except: editor = "emacs" # determine the filename entry_id = get_entry_string() entry_dir = get_dir_string() ofile = entry_id + fmt # determine the directory we place it in -- this is the form yyyy-mm-dd/ odir = "{}/journal-{}/entries/{}/".format(defs[nickname]["working_path"], nickname, entry_dir) if not os.path.isdir(odir): try: os.mkdir(odir) except: sys.exit("ERROR: unable to make directory {}".format(odir)) # create the entry file. If we passed in a string, then write it # too. try: f = open(odir + ofile, "w") except: sys.exit("ERROR: unable to open {}".format(odir + ofile)) if string is not None: f.write(string) # if there are images, then copy them over and add the figure # headings to the entry images_copied = [] for im in images: # does an image by that name already live in the dest # directory? src = "{}/{}".format(defs["image_dir"], im) dest = odir if os.path.isfile("{}/{}".format(dest, im)): im_copy = "{}_{}".format(entry_id.replace(".", "_"), im) else: im_copy = im dest = "{}/{}".format(dest, im_copy) # copy it try: shutil.copy(src, dest) except: sys.exit("ERROR: unable to copy image {} to {}".format(src, dest)) images_copied.append(im_copy) # create a unique label for latex referencing idx = im.lower().rfind(".jpg") idx = max(idx, im.lower().rfind(".png")) idx = max(idx, im.lower().rfind(".pdf")) if idx >= 0: im0 = "{}:{}".format(entry_id, im[:idx]) fname = "entries/{}/{}".format(entry_dir, im_copy) # add the figure text for l in figure_str.split("\n"): f.write("{}\n".format( l.replace("@figname@", fname).replace("@figlabel@", im0).rstrip())) f.close() # get the hash for the file hash_orig = hashlib.md5(open(odir + ofile, 'r').read().encode('utf-8')).hexdigest() # launch the editor specified in the EDITOR environment variable if string == None: if editor == "emacs": prog = "emacs -nw {}/{}".format(odir, ofile) else: prog = "{} {}/{}".format(editor, odir, ofile) stdout, stderr, rc = shell_util.run(prog) if stderr: print(stderr) # did the user actually make edits? hash_new = hashlib.md5(open(odir + ofile, 'r').read().encode('utf-8')).hexdigest() if string == None and len(images) == 0 and (hash_new == hash_orig): # user didn't do anything interesting answer = input("no input made -- add this to the journal? (y/N) ") if answer.lower() != "y": try: os.remove(odir + ofile) except: sys.exit("ERROR: unable to remove file -- entry aborted") sys.exit("entry aborted") # any tags? #tags = find_tags(odir + ofile) # commit the entry to the working git repo os.chdir(odir) stdout, stderr, rc = shell_util.run("git add " + ofile) stdout, stderr, rc = shell_util.run("git commit -m 'new entry' " + ofile) # commit any images too for im in images_copied: stdout, stderr, rc = shell_util.run("git add " + im) stdout, stderr, rc = shell_util.run("git commit -m 'new image' " + im) # helpful edit suggestion print("entry created. Use 'pyjournal.py edit {}' to edit this entry.".format(entry_id))
# nbtmp.print() #XXX debug print print("number of notes after: {}".format(len(nbtmp.notes))) # always save inbox and tasklist nbtmp.inbox.save(directory) nbtmp.tasklist.save(directory) # compare notebooks added, modified, removed = compareNoteBooks(nb, nbtmp) # and now add accordingly for note in added: note.save(directory) for note in modified: note.save(directory) for note in removed: note.body += "\n ===DONE===" note.save(done_directory) # print("note name before removal: {}".format(note.name)) prog = "rm {}/{}".format(directory, note.name) # print(prog) shell_util.run(prog) # remove tmp file # XXX # shell_util.run("rm {}".format(fname))
def entry(nickname, images, defs, string=None): try: editor = os.environ["EDITOR"] except: editor = "emacs" # determine the filename entry_id = get_entry_string() entry_dir = get_dir_string() ofile = entry_id + ".tex" # determine the directory we place it in -- this is the form yyyy-mm-dd/ odir = "{}/journal-{}/entries/{}/".format(defs[nickname]["working_path"], nickname, entry_dir) if not os.path.isdir(odir): try: os.mkdir(odir) except: sys.exit("ERROR: unable to make directory {}".format(odir)) # create the entry file. If we passed in a string, then write it # too. try: f = open(odir + ofile, "w") except: sys.exit("ERROR: unable to open {}".format(odir + ofile)) if string is not None: f.write(string) else: f.write("% journal: {}\n".format(nickname)) # if there are images, then copy them over and add the figure # headings to the entry images_copied = [] for im in images: # does an image by that name already live in the dest # directory? src = "{}/{}".format(defs["image_dir"], im) dest = odir if os.path.isfile("{}/{}".format(dest, im)): im_copy = "{}_{}".format(entry_id.replace(".", "_"), im) else: im_copy = im dest = "{}/{}".format(dest, im_copy) # copy it try: shutil.copy(src, dest) except: sys.exit("ERROR: unable to copy image {} to {}".format(src, dest)) images_copied.append(im_copy) # create a unique label for latex referencing idx = im.lower().rfind(".jpg") idx = max(idx, im.lower().rfind(".png")) idx = max(idx, im.lower().rfind(".pdf")) if idx >= 0: im0 = "{}:{}".format(entry_id, im[:idx]) fname = "entries/{}/{}".format(entry_dir, im_copy) # add the figure text for l in figure_str.split("\n"): f.write("{}\n".format( l.replace("@figname@", fname).replace("@figlabel@", im0).rstrip())) # add the entry id as a LaTeX comment f.write("\n\n% entry: {}".format(entry_id)) f.close() # get the hash for the file hash_orig = hashlib.md5(open(odir + ofile, 'r').read().encode('utf-8')).hexdigest() # launch the editor specified in the EDITOR environment variable if string == None: if editor == "emacs": prog = "emacs -nw {}/{}".format(odir, ofile) else: prog = "{} {}/{}".format(editor, odir, ofile) stdout, stderr, rc = shell_util.run(prog) # did the user actually make edits? hash_new = hashlib.md5(open(odir + ofile, 'r').read().encode('utf-8')).hexdigest() if string == None and len(images) == 0 and (hash_new == hash_orig): # user didn't do anything interesting answer = raw_input("no input made -- add this to the journal? (y/N) ") if answer.lower() != "y": try: os.remove(odir + ofile) except: sys.exit("ERROR: unable to remove file -- entry aborted") sys.exit("entry aborted") # any tags? #tags = find_tags(odir + ofile) # commit the entry to the working git repo os.chdir(odir) stdout, stderr, rc = shell_util.run("git add " + ofile) stdout, stderr, rc = shell_util.run("git commit -m 'new entry' " + ofile) # commit any images too for im in images_copied: stdout, stderr, rc = shell_util.run("git add " + im) stdout, stderr, rc = shell_util.run("git commit -m 'new image' " + im) # helpful edit suggestion print("entry created. Use 'pyjournal.py edit {}' to edit this entry.".format(entry_id))
def init(nickname, master_path, working_path, defs): # check the working path to make sure it is absolute, not relative working_path = os.path.normpath(working_path) if not working_path.startswith("/"): working_path = os.path.abspath(working_path) # make sure that a journal with this nickname doesn't already exist if nickname in defs.keys(): sys.exit("ERROR: nickname already exists") # we are create the directory beneath master_path/, so make sure that # exists master_path = os.path.normpath(master_path) if not master_path.startswith("/"): master_path = os.path.abspath(master_path) if not os.path.isdir(master_path): try: os.mkdir(master_path) except: sys.exit("ERROR: you need to specify an existing path in which to create the journal repo") # create the bare git repo git_master = "{}/journal-{}.git".format(master_path, nickname) try: os.mkdir(git_master) except: sys.exit("ERROR: unable to create a directory in {}".format(master_path)) os.chdir(git_master) stdout, stderr, rc = shell_util.run("git init --bare") # create the local working copy try: os.chdir(working_path) except: sys.exit("ERROR: unable to change to {}".format(working_path)) stdout, stderr, rc = shell_util.run("git clone " + git_master) # create the initial directory structure working_journal = "{}/journal-{}".format(working_path, nickname) try: os.mkdir(working_journal + "/entries/") except: sys.exit("ERROR: unable to create initial directory structure") # create (or add to) the .pyjournalrc file try: f = open(defs["param_file"], "a+") except: sys.exit("ERROR: unable to open {} for appending".format(defs["param_file"])) f.write("[{}]\n".format(nickname)) f.write("master_repo = {}\n".format(git_master)) f.write("working_path = {}\n".format(working_path)) f.write("\n") f.close() defs[nickname] = {} defs[nickname]["master_repo"] = git_master defs[nickname]["working_path"] = working_path # create an initial entry saying "journal created" images = [] entry_util.entry(nickname, images, defs, string="journal created") # copy over the journal.tex try: f = open("{}/journal.tex".format(working_journal), "w") except: sys.exit("ERROR: unable to open {}/journal.tex".format(working_journal)) for line in master_util.journal_master.split("\n"): f.write("{}\n".format(line.rstrip())) f.close() # add journal.tex to the repo and do a git push to make it synced os.chdir(working_journal) stdout, stderr, rc = shell_util.run("git add journal.tex") stdout, stderr, rc = shell_util.run("git commit -m 'initial journal.tex file' journal.tex") stdout, stderr, rc = shell_util.run("git push origin master")
def build(nickname, defs, show=0): entry_dir = "{}/journal-{}/entries/".format(defs[nickname]["working_path"], nickname) entries = [] years = [] # get the list of directories in entries/ for d in os.listdir(entry_dir): if d.endswith("appendices"): continue if os.path.isdir(entry_dir + d): entries.append(d) y, m, d = d.split("-") if not y in years: years.append(y) os.chdir(entry_dir) years.sort() entries.sort() # years are chapters try: f = open("chapters.tex", "w") except: sys.exit("ERROR: unable to create chapters.tex") for y in years: f.write("\\chapter{{{}}}\n".format(y)) f.write("\\input{{entries/{}.tex}}\n\n".format(y)) # now do the appendices f.write("\\appendix\n") app_dir = "{}/journal-{}/entries/appendices/".format( defs[nickname]["working_path"], nickname) if os.path.isdir(app_dir): for t in os.listdir(app_dir): if t.endswith(".tex"): f.write("\\input{{entries/appendices/{}}}\n\n".format(t)) f.close() # within each year, months are sections for y in years: try: f = open("{}.tex".format(y), "w") except: sys.exit("ERROR: unable to create chapters.tex") current_month = None current_day = None for e in entries: ytmp, m, d = e.split("-") if not ytmp == y: continue if not m == current_month: f.write("\\section{{{}}}\n".format( calendar.month_name[int(m)])) current_month = m tex = [] for t in os.listdir(e): if t.endswith(".tex"): tex.append(t) tex.sort() for t in tex: if not d == current_day: f.write("\\subsection{{{} {}}}\n".format( calendar.month_name[int(m)], d)) current_day = d f.write("\\HRule\\\\ \n") idx = t.rfind(".tex") tout = t[:idx].replace("_", " ") f.write( "{{\\bfseries {{\sffamily {} }} }}\\\\[0.5em] \n".format( tout)) f.write("\\input{{entries/{}/{}}}\n\n".format(e, t)) f.write("\\vskip 2em\n") f.write("\n") f.close() # now do the latexing to get the PDF build_dir = "{}/journal-{}/".format(defs[nickname]["working_path"], nickname) os.chdir(build_dir) stdout, stderr, rc = shell_util.run("pdflatex --halt-on-error journal.tex") stdout, stderr, rc = shell_util.run("pdflatex --halt-on-error journal.tex") stdout, stderr, rc = shell_util.run("makeindex journal") stdout, stderr, rc = shell_util.run("pdflatex --halt-on-error journal.tex") stdout, stderr, rc = shell_util.run("pdflatex --halt-on-error journal.tex") # if we were not successful, then the PDF is not produced # note: pdflatex does not seem to use stderr at all pdf = os.path.normpath("{}/journal.pdf".format(build_dir)) if os.path.isfile(pdf): print("journal is located at {}".format(pdf)) else: print(stdout) print("There were LaTeX errors") print("Check the source in {}/entries/".format(build_dir)) print("be sure to 'git commit' to store any fixes") sys.exit() # show it in a PDF viewer if show == 1: os.system("evince {} &".format(pdf))
def build(nickname, defs, show=0): entry_dir = "{}/journal-{}/entries/".format(defs[nickname]["working_path"], nickname) entries = [] years = [] # get the list of directories in entries/ for d in os.listdir(entry_dir): if d.endswith("appendices"): continue if os.path.isdir(entry_dir + d): entries.append(d) y, m, d = d.split("-") if not y in years: years.append(y) os.chdir(entry_dir) years.sort() entries.sort() # years are chapters try: f = open("chapters.tex", "w") except: sys.exit("ERROR: unable to create chapters.tex") for y in years: f.write("\\chapter{{{}}}\n".format(y)) f.write("\\input{{entries/{}.tex}}\n\n".format(y)) # now do the appendices f.write("\\appendix\n") app_dir = "{}/journal-{}/entries/appendices/".format(defs[nickname]["working_path"], nickname) if os.path.isdir(app_dir): for t in os.listdir(app_dir): if t.endswith(".tex"): # Find out if there is a markdown file by the same base-name # and if there is, then defer to it. tmd = stripextension(t) + '.md' if not os.path.isfile(os.path.join(app_dir, tmd)): ttex = t else: continue elif t.endswith(".md"): # Process Markdown with Pandoc to LaTeX tpath = os.path.join(app_dir, t) ttex = os.path.basename(md_to_tex(tpath)) else: continue f.write("\\input{{entries/appendices/{}}}\n\n".format(ttex)) f.close() # within each year, months are sections for y in years: try: f = open("{}.tex".format(y), "w") except: sys.exit("ERROR: unable to create chapters.tex") current_month = None current_day = None for e in entries: ytmp, m, d = e.split("-") if not ytmp == y: continue if not m == current_month: f.write("\\section{{{}}}\n".format(calendar.month_name[int(m)])) current_month = m tex = [] for t in os.listdir(e): if t.endswith(".tex"): # Find out if there is a markdown file by the same base-name # and if there is, then defer to it. tmd = stripextension(t) + '.md' if not os.path.isfile(os.path.join(os.getcwd(), e, tmd)): tex.append(t) elif t.endswith(".md"): # Use Pandoc to convert Markdown to LaTeX tpath = os.path.join(os.getcwd(), e, t) ftex = md_to_tex(tpath) tex.append(os.path.basename(ftex)) tex.sort() for t in tex: if not d == current_day: f.write("\\subsection{{{} {}}}\n".format(calendar.month_name[int(m)], d)) current_day = d f.write("\\HRule\\\\ \n") idx = t.rfind(".tex") tout = t[:idx].replace("_", " ") f.write("{{\\bfseries {{\sffamily {} }} }}\\\\[0.5em] \n".format(tout)) f.write("\\input{{entries/{}/{}}}\n\n".format(e, t)) f.write("\\vskip 2em\n") f.write("\n") f.close() # now do the latexing to get the PDF build_dir = "{}/journal-{}/".format(defs[nickname]["working_path"], nickname) os.chdir(build_dir) stdout, stderr, rc = shell_util.run("pdflatex --halt-on-error journal.tex") stdout, stderr, rc = shell_util.run("pdflatex --halt-on-error journal.tex") stdout, stderr, rc = shell_util.run("makeindex journal") stdout, stderr, rc = shell_util.run("pdflatex --halt-on-error journal.tex") stdout, stderr, rc = shell_util.run("pdflatex --halt-on-error journal.tex") # if we were not successful, then the PDF is not produced # note: pdflatex does not seem to use stderr at all pdf = os.path.normpath("{}/journal.pdf".format(build_dir)) if os.path.isfile(pdf): print("journal is located at {}".format(pdf)) else: print(stdout) print("There were LaTeX errors") print("Check the source in {}/entries/".format(build_dir)) print("be sure to 'git commit' to store any fixes") sys.exit() # show it in a PDF viewer if show == 1: os.system("evince {} &".format(pdf))