twd-m4sc0 3.1.1__py3-none-any.whl → 3.1.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
twd/__init__.py CHANGED
@@ -1 +1 @@
1
- __version__ = "3.1.1"
1
+ __version__ = "3.1.2"
twd/cli.py CHANGED
@@ -16,7 +16,7 @@ def cli(ctx):
16
16
  ctx.ensure_object(dict)
17
17
 
18
18
  ctx.obj['config'] = Config.load() # load config
19
- ctx.obj['manager'] = TwdManager(ctx.obj['config'].data_path)
19
+ ctx.obj['manager'] = TwdManager(ctx.obj['config']) # pass config
20
20
 
21
21
  # if no subcommand was provided, launch TUI
22
22
  if ctx.invoked_subcommand is None:
twd/config.py CHANGED
@@ -1,7 +1,7 @@
1
1
 
2
-
3
2
  from pathlib import Path
4
3
  from pydantic import BaseModel, Field, validator
4
+ from enum import Enum
5
5
  import json
6
6
  import os
7
7
  import sys
@@ -32,10 +32,28 @@ def get_data_path() -> Path:
32
32
 
33
33
  return data_dir / "data.csv"
34
34
 
35
+ class SortField(str, Enum):
36
+ ALIAS = "alias"
37
+ PATH = "path"
38
+ NAME = "name"
39
+ CREATED_AT = "created_at"
40
+
41
+ class SortDirection(str, Enum):
42
+ ASC = "asc"
43
+ DESC = "desc"
44
+
45
+ class SortOrderSetting(BaseModel):
46
+ field: SortField = SortField.CREATED_AT
47
+ direction: SortDirection = SortDirection.ASC
48
+
49
+ class Settings(BaseModel):
50
+ sort_order: SortOrderSetting = Field(default_factory=SortOrderSetting)
51
+
35
52
  class Config(BaseModel):
36
53
  """App configuration"""
37
54
 
38
55
  data_path: Path = Field(default_factory=get_data_path)
56
+ settings: Settings = Field(default_factory=Settings)
39
57
 
40
58
  @validator("data_path")
41
59
  def validate_path(cls, v):
twd/data.py CHANGED
@@ -5,7 +5,7 @@ from pydantic import BaseModel, Field, validator
5
5
  from typing import List, Optional
6
6
  from datetime import datetime
7
7
 
8
- from .config import Config
8
+ from .config import Config, SortDirection
9
9
 
10
10
  class Entry(BaseModel):
11
11
  """Data class for a signle TWD Entry"""
@@ -74,17 +74,17 @@ class TwdManager:
74
74
  CSV_HEADERS = ["alias", "path", "name", "created_at"]
75
75
  CSV_HEADERS_FANCY = ["Alias", "Path", "Description", "Created at"]
76
76
 
77
- def __init__(self, csv_path: Path):
78
- self.csv_path = csv_path
77
+ def __init__(self, config: Config): # accept config as argument
78
+ self.config = config
79
79
  self._ensure_csv_exists()
80
80
  self.cwd = str(Path.cwd())
81
81
 
82
82
  def _ensure_csv_exists(self) -> None:
83
83
  """create csv headers"""
84
- if not self.csv_path.exists():
85
- self.csv_path.parent.mkdir(parents=True, exist_ok=True)
84
+ if not self.config.data_path.exists():
85
+ self.config.data_path.parent.mkdir(parents=True, exist_ok=True)
86
86
 
87
- with open(self.csv_path, 'w', newline='') as f:
87
+ with open(self.config.data_path, 'w', newline='') as f:
88
88
  writer = csv.writer(f)
89
89
  writer.writerow(self.CSV_HEADERS)
90
90
 
@@ -92,7 +92,7 @@ class TwdManager:
92
92
  """read all entries"""
93
93
  entries = []
94
94
 
95
- with open(self.csv_path, 'r', newline='') as f:
95
+ with open(self.config.data_path, 'r', newline='') as f:
96
96
  reader = csv.reader(f)
97
97
 
98
98
  next(reader) # skip headers
@@ -103,7 +103,7 @@ class TwdManager:
103
103
  return entries
104
104
 
105
105
  def _write_all(self, entries: List[Entry]) -> None:
106
- with open(self.csv_path, 'w', newline='') as f:
106
+ with open(self.config.data_path, 'w', newline='') as f:
107
107
  writer = csv.writer(f)
108
108
  writer.writerow(self.CSV_HEADERS)
109
109
 
@@ -141,10 +141,16 @@ class TwdManager:
141
141
  if not self.exists(alias):
142
142
  return False
143
143
 
144
- # simplest form of update is remove and add
145
- self.remove(alias)
144
+ entries = self._read_all()
145
+
146
+ # find and upate
147
+ for i, e in enumerate(entries):
148
+ if e.alias == alias.lower():
149
+ entries[i] = entry
150
+ break
146
151
 
147
- self.add(entry.alias, entry.path, entry.name)
152
+ self._write_all(entries)
153
+ return True
148
154
 
149
155
  def remove(self, alias: str) -> None:
150
156
  """remove entry by alias"""
@@ -161,7 +167,9 @@ class TwdManager:
161
167
  def list_all(self) -> List[Entry]:
162
168
  entries = self._read_all()
163
169
 
164
- return sorted(entries, key=lambda e: e.created_at)
170
+ sort_config = self.config.settings.sort_order
171
+ reverse = (sort_config.direction == SortDirection.DESC)
172
+ return sorted(entries, key=lambda e: getattr(e, sort_config.field.value), reverse=reverse)
165
173
 
166
174
  def exists(self, alias: str) -> bool:
167
175
  return self.get(alias) is not None
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: twd-m4sc0
3
- Version: 3.1.1
3
+ Version: 3.1.2
4
4
  Summary: TWD by m4sc0
5
5
  Author: m4sc0
6
6
  License: MIT
@@ -0,0 +1,15 @@
1
+ twd/__init__.py,sha256=EkxTxShULe9D7KtZB_iICEO13IyzloAxfYgXHGwpMhQ,22
2
+ twd/cli.py,sha256=Xxm5Oq_k2ChP_lzEh-_Rc8KKDDIJirBkejaD_Wgybn0,4828
3
+ twd/config.py,sha256=5X2rWTw26u1yMjwNtmVTBjEPT8s89f-ypYPQKYG7xa8,2488
4
+ twd/data.py,sha256=oIBoAIjfozysQtMkLFlZMcMqvktzxXdMlOQCF1Q1Db4,5112
5
+ twd/tui.py,sha256=X8HWrtvru8nigKx61aMemQ-elJM8UrUZdhjTXMZBdF0,8253
6
+ twd/tui.tcss,sha256=GVEtk-fUcAaaGP3I-VjTFUzZocPAEcASDCBNGVoUBWw,778
7
+ twd/utils.py,sha256=xsMa69vAotpNMd3Vp5_F7ELwZCwT-sd45taN7PRI9pE,1201
8
+ twd/modals/__init__.py,sha256=PE3zZ9RrWABlvjoBRnpbbNj8t7xXy24mqLeW3uVv7Yg,171
9
+ twd/modals/confirm.py,sha256=QOEnV6OYzt95ZWrTd4lTULfWd1bl52aotZWu4uBovsk,2814
10
+ twd/modals/edit.py,sha256=c7Mr5cNeq5XR6EdHCzbWi32ruCPHXDNIZPgEv8cYtj4,1475
11
+ twd_m4sc0-3.1.2.dist-info/METADATA,sha256=NgbdylASESFqN7Dtm4a11-mVe5ycrFt3PGDTbOqYWyg,437
12
+ twd_m4sc0-3.1.2.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
13
+ twd_m4sc0-3.1.2.dist-info/entry_points.txt,sha256=KeIDpaQx0GSqgJFYql14wmT3yj_JHTwq7rRsm-mKckc,36
14
+ twd_m4sc0-3.1.2.dist-info/top_level.txt,sha256=B93Li7fIZ4uWoKq8rKVuGPU_MtzyjxxDb0NF96oGZ0o,4
15
+ twd_m4sc0-3.1.2.dist-info/RECORD,,
@@ -1,15 +0,0 @@
1
- twd/__init__.py,sha256=14eImCCNxRh4pWMIfkKe4h5OCS1ICfRjHSj2AfgEXa0,22
2
- twd/cli.py,sha256=jRdFB741czlkGn9bUrUy4cV9B6iae-87vJpTCcKp04Y,4824
3
- twd/config.py,sha256=pGAaurguz1Bv6NxjwKbLBbvJ9tuAVJx_l7tcvd0B84I,1996
4
- twd/data.py,sha256=_Wn4aoJsYyFmrjtuzOou2x70YH4ZuJrGnIdflzjIttc,4756
5
- twd/tui.py,sha256=X8HWrtvru8nigKx61aMemQ-elJM8UrUZdhjTXMZBdF0,8253
6
- twd/tui.tcss,sha256=GVEtk-fUcAaaGP3I-VjTFUzZocPAEcASDCBNGVoUBWw,778
7
- twd/utils.py,sha256=xsMa69vAotpNMd3Vp5_F7ELwZCwT-sd45taN7PRI9pE,1201
8
- twd/modals/__init__.py,sha256=PE3zZ9RrWABlvjoBRnpbbNj8t7xXy24mqLeW3uVv7Yg,171
9
- twd/modals/confirm.py,sha256=QOEnV6OYzt95ZWrTd4lTULfWd1bl52aotZWu4uBovsk,2814
10
- twd/modals/edit.py,sha256=c7Mr5cNeq5XR6EdHCzbWi32ruCPHXDNIZPgEv8cYtj4,1475
11
- twd_m4sc0-3.1.1.dist-info/METADATA,sha256=BYD3Mv8S298thP3dzitSMEr2FdPtFHw5PkRLsjQapI0,437
12
- twd_m4sc0-3.1.1.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
13
- twd_m4sc0-3.1.1.dist-info/entry_points.txt,sha256=KeIDpaQx0GSqgJFYql14wmT3yj_JHTwq7rRsm-mKckc,36
14
- twd_m4sc0-3.1.1.dist-info/top_level.txt,sha256=B93Li7fIZ4uWoKq8rKVuGPU_MtzyjxxDb0NF96oGZ0o,4
15
- twd_m4sc0-3.1.1.dist-info/RECORD,,