Last active
December 24, 2023 23:47
-
-
Save toddlucas/220eb42796a44bac4f227024a65a8fb2 to your computer and use it in GitHub Desktop.
A simple script to edit a dated file for journalling or note taking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import time | |
import os | |
import argparse | |
import configparser | |
journal_dirname = "" | |
journal_cwd = "" | |
journal_date = "" | |
journal_filename = "" | |
journal_ext = "" | |
journal_editor = "" | |
journal_pathfmt = "" | |
journal_filefmt = "" | |
def get_args(args=None): | |
"""Get the program arguments""" | |
parser = argparse.ArgumentParser(description='Daily journal file opener') | |
parser.add_argument('-i', '--inifile', default='jour.ini', | |
help='Override the default jour.ini filename') | |
parser.add_argument('-s', '--suffix', | |
help="File suffix") | |
parser.add_argument('filename', nargs='?', | |
help="Override the specific filename to edit") | |
parser.add_argument('-p', '--pathname', | |
help="Override the path to edit") | |
return parser.parse_args(args) | |
# | |
# Example jour.ini (blog.ini, work.ini, ...) | |
# | |
# [settings] | |
# path=C:\Source\journal | |
# editor=notepad | |
# ext=.md | |
# dateformat=%%Y\%%m-%%d | |
# | |
# By default, settings are read from the location of this script, so as long | |
# as it's in the path, there's no need to specify the ini argument to get the | |
# default settings. | |
# | |
def read_settings(inifile): | |
global journal_dirname | |
global journal_cwd | |
global journal_ext | |
global journal_editor | |
global journal_pathfmt | |
global journal_filefmt | |
journal_cwd = sys.path[0] | |
config = configparser.ConfigParser(interpolation=EnvInterpolation()) | |
config.read(os.path.join(journal_cwd, inifile)) | |
try: | |
journal_dirname = config.get("settings", "path") | |
except: | |
journal_dirname = journal_cwd | |
try: | |
journal_editor = config.get("settings", "editor") | |
except: | |
journal_editor = "notepad" | |
try: | |
journal_pathfmt = config.get("settings", "pathformat") | |
except: | |
print("Unexpected error:", sys.exc_info()[0]) | |
journal_pathfmt = "" | |
try: | |
journal_filefmt = config.get("settings", "fileformat") | |
except: | |
print("Unexpected error:", sys.exc_info()[0]) | |
journal_filefmt = "%Y-%b-%d" | |
try: | |
journal_ext = config.get("settings", "ext") | |
except: | |
journal_ext = ".txt" | |
if not journal_ext: | |
journal_ext = ".txt" | |
def main(): | |
global journal_filename | |
global journal_date | |
global journal_pathfmt | |
global journal_filefmt | |
args = get_args() | |
# Read optional settings ini file name. | |
_, fileext = os.path.splitext(args.inifile) | |
if fileext: | |
inifile = args.inifile | |
else: | |
inifile = args.inifile + ".ini" | |
read_settings(inifile) | |
# Create a new filename of the format "2004-01-03". | |
journal_path = time.strftime(journal_pathfmt, time.localtime()) | |
journal_file = time.strftime(journal_filefmt, time.localtime()) | |
journal_name = journal_file | |
# Allow an optional path override. | |
if args.pathname: | |
journal_path = args.pathname | |
# Allow an optional filename override. | |
if args.filename: | |
journal_name = args.filename | |
# Allow an optional prefix for subjournals. | |
if args.suffix: | |
journal_name = journal_file + "-" + args.suffix | |
journal_filename = os.path.join(journal_dirname, journal_path, journal_name + journal_ext) | |
# Edit the journal entry | |
os.system(journal_editor + " " + journal_filename) | |
class EnvInterpolation(configparser.BasicInterpolation): | |
"""Interpolation which expands environment variables in values.""" | |
def before_get(self, parser, section, option, value, defaults): | |
value = super().before_get(parser, section, option, value, defaults) | |
return os.path.expandvars(value) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment