From 582bf171abb8d3dc70a010cb570eb0a3dc9a9b3b Mon Sep 17 00:00:00 2001 From: Ivan Reshetnikov Date: Fri, 19 Aug 2022 17:31:51 +0500 Subject: [PATCH] Initial commit --- histd.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ license.md | 15 +++++++++++++++ readme.md | 25 +++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100755 histd.py create mode 100644 license.md create mode 100644 readme.md diff --git a/histd.py b/histd.py new file mode 100755 index 0000000..8c83e49 --- /dev/null +++ b/histd.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 +from datetime import date +import os +import subprocess + + +def main(): + base_dir = get_base_dir() + today = date.today() + edit_note(base_dir, today) + + +def get_base_dir() -> str: + """ + Creates the directories necessary for the program + to work, if they are not present. + + Returns the path to the directory where data can be stored. + """ + local_dir = os.path.expanduser('~/.local') + if not os.path.exists(local_dir): + os.mkdir(local_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): + """ + Creates the required directories and opens a text editor + so that the user can describe the day. + """ + # Create dirs (base_dir/year/month) + year_dir = os.path.join(base_dir, str(note_date.year)) + if not os.path.exists(year_dir): + os.mkdir(year_dir) + + 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 + path_to_file = os.path.join(month_dir, f'{note_date.day:02}.txt') + editor = os.environ.get('EDITOR', 'nano') + subprocess.run([editor, path_to_file]) + + +if __name__ == '__main__': + main() diff --git a/license.md b/license.md new file mode 100644 index 0000000..da4086f --- /dev/null +++ b/license.md @@ -0,0 +1,15 @@ +Copyright © 2022 Ivan Reshetnikov + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation files (the “Software”), +to deal in the Software without restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, +and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE +OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..0da425f --- /dev/null +++ b/readme.md @@ -0,0 +1,25 @@ +# How I spent this day + +A simple but useful personal diary application. + +The sole purpose of this application is to quickly create a file +and open it in a text editor so that I can take a note before I lose my desire. +Notes can be found in the `~/.local/histd` directory. + +```sh +tree ~/.local/histd +# /home/user/.local/histd +# └── 2022 +# └── 08 +# ├── 18.txt +# └── 19.txt +``` + +## Usage +```sh +./histd.py +``` +or +```sh +python3 histd.py +```