summaryrefslogtreecommitdiff
path: root/note_tools/__init__.py
blob: 0b4fcbd3ac41a17170515596eedc688264b3ea64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from datetime import datetime
import os
from typing import Optional, Tuple

class NoteBaseCommand(object):    
  date = datetime.now()

  def file_name(self):
    return f"{self.date.strftime('%Y%m%d')}.md"

  def get_formatted_dates(self) -> Tuple[str, str]:
    return (self.date.strftime("%Y%m%d"), self.date.strftime("%Y-%m-%d"))

  def get_date(self) -> datetime:
    return datetime.now()

  def get_date_parts(self) -> Tuple[str]:
    return (self.date.strftime('%Y'), self.date.strftime('%m'))
      
  def write_file_heading(self, file_path):      
    date_yyyymmdd = self.date.strftime('%Y-%m-%d') 
    underline = '=' * len(date_yyyymmdd)
    with open(file_path, 'w') as file:
      file.write(f"{date_yyyymmdd}\n{underline}\n\n\n")

  def parse_file_date (self, file_name: str) -> Tuple[str, str]:
    year = file_name[:4]
    month = file_name[4:6]
    return year, month

  def get_notes_path(self, project_data) -> Optional[str]:
    if project_data and 'folders' in project_data:
      for folder in project_data['folders']:
        folder_path = folder['path']
        if os.path.basename(folder_path).lower() == "notes":
          return folder_path
    return None

  def create_note(self, project_data, callback):
    # Get the current date formatted as YYYYMMDD and YYYY-MM-DD
    notes_path = self.get_notes_path(project_data)

    if notes_path:
        year, month = self.get_date_parts()
        directory = os.path.join(notes_path, year, month)
        os.makedirs(directory, exist_ok=True)
        file_path = os.path.join(directory, self.file_name())

        # Check if file already exists
        if os.path.exists(file_path):
          callback(False, f"The file '{self.file_name()}' already exists.")
        else:
          self.write_file_heading(file_path)
          callback(True, None, file_path)

    else:
      callback(False, "No 'Notes' directory found in the project. Please add a 'Notes' folder.")