summaryrefslogtreecommitdiff
path: root/pnote/layout.py
blob: 8d3fe1d25517752cbc60ce139bad9af503144fbf (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
59
60
61
62
63
64
65
66
67
68
from pathlib import Path
from datetime import datetime
import os

class Layout:

    def __init__(self, conf, paths):
        self.conf=conf
        self.paths=paths
        self.today=datetime.today()
        self.today_backup=self.today

    def settoday(self,timestamp):
        self.today=datetime.fromtimestamp(timestamp)

    def restoretoday(self):
        self.today=self.today_backup

    def gettoday(self):
        return self.today

    def flatten(self):
        """
        List all subpath present on disk.
        """
        paths=list(Path(self.paths["files"]).rglob("*"))
        result=list()
        for p in paths:
            if os.path.isfile(p):
                result.append(p.relative_to(self.paths["files"]))
        return result

    def create(self):
        """
        Create today's note file.
        """
        file=self.todaypath()
        if not os.path.exists(file):
            open(file, 'a').close()
        return self.todaysubpath()
        
    def todayname(self):
        """
        Get today's note file name.
        """
        return self.today.strftime(self.conf["filename"])

    def todaysubdir(self):
        """
        Must be overriden by child classes
        """
        subdir=self.today.strftime(self.conf["layout"])
        if not os.path.exists(subdir):
            Path(os.path.join(self.paths["files"],subdir)).mkdir(parents=True, exist_ok=True)
        return subdir

    def todaysubpath(self):
        """
        Get the subpath of today's note file.
        """
        subdir=self.todaysubdir()
        return os.path.join(self.todaysubdir(), self.todayname())
    
    def todaypath(self):
        """
        Get the path of today's note file.
        """
        return os.path.join(self.paths["files"],self.todaysubpath())