Simplify the process of creating dirs and files

This commit is contained in:
Ivan R. 2022-08-24 22:43:57 +05:00
parent e6639cda58
commit ff117453a9
No known key found for this signature in database
GPG key ID: 67AA0CE744B974B8

View file

@ -17,15 +17,9 @@ def get_base_dir() -> str:
Returns the path to the directory where data can be stored. Returns the path to the directory where data can be stored.
""" """
local_dir = os.path.expanduser('~/.local') base_dir = os.path.expanduser('~/.local/histd')
if not os.path.exists(local_dir): os.makedirs(base_dir, exist_ok=True)
os.mkdir(local_dir) return base_dir
histd_dir = os.path.join(local_dir, 'histd')
if not os.path.exists(histd_dir):
os.mkdir(histd_dir)
return histd_dir
def edit_note(base_dir: str, note_date: date): def edit_note(base_dir: str, note_date: date):
@ -34,16 +28,14 @@ def edit_note(base_dir: str, note_date: date):
so that the user can describe the day. so that the user can describe the day.
""" """
# Create dirs (base_dir/year/month) # Create dirs (base_dir/year/month)
year_dir = os.path.join(base_dir, str(note_date.year)) year = str(note_date.year)
if not os.path.exists(year_dir): month = f'{note_date.month:02}'
os.mkdir(year_dir) workdir = os.path.join(base_dir, year, month)
os.makedirs(workdir, exist_ok=True)
month_dir = os.path.join(year_dir, f'{note_date.month:02}')
if not os.path.exists(month_dir):
os.mkdir(month_dir)
# Open file (base_dir/year/month/day.txt) with default editor # Open file (base_dir/year/month/day.txt) with default editor
path_to_file = os.path.join(month_dir, f'{note_date.day:02}.txt') filename = f'{note_date.day:02}.txt'
path_to_file = os.path.join(workdir, filename)
editor = os.environ.get('EDITOR', 'nano') editor = os.environ.get('EDITOR', 'nano')
subprocess.run([editor, path_to_file]) subprocess.run([editor, path_to_file])