koleo-cli 0.2.137.9__tar.gz → 0.2.137.10__tar.gz

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.

Potentially problematic release.


This version of koleo-cli might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: koleo-cli
3
- Version: 0.2.137.9
3
+ Version: 0.2.137.10
4
4
  Summary: Koleo CLI
5
5
  Home-page: https://github.com/lzgirlcat/koleo-cli
6
6
  Author: Zoey !
@@ -89,6 +89,37 @@ class CLI:
89
89
  self.print(station_info)
90
90
  self.get_arrivals(st["id"], date)
91
91
 
92
+ def all_trains(self, station: str, date: datetime):
93
+ st = self.get_station(station)
94
+ station_info = f"[bold blue][link=https://koleo.pl/dworzec-pkp/{st["name_slug"]}/{date.strftime("%Y-%m-%d")}]{st["name"]} at {date.strftime("%d-%m %H:%M")}[/bold blue] ID: {st["id"]}[/link]"
95
+ self.print(station_info)
96
+ arr_cache_id = f"arr-{st['id']}-{date.strftime("%Y-%m-%d")}"
97
+ dep_cache_id = f"dep-{st['id']}-{date.strftime("%Y-%m-%d")}"
98
+ arrivals = self.storage.get_cache(arr_cache_id) or self.storage.set_cache(
99
+ arr_cache_id, self.client.get_arrivals(st['id'], date)
100
+ )
101
+ departures = self.storage.get_cache(dep_cache_id) or self.storage.set_cache(
102
+ dep_cache_id, self.client.get_departures(st['id'], date)
103
+ )
104
+ trains = sorted(
105
+ [(i, 1) for i in departures] + [(i, 2) for i in arrivals],
106
+ key=lambda train: datetime.fromisoformat(train[0]["departure"] if train[1] == 1 else train[0]["arrival"]).timestamp()
107
+ )
108
+ trains = [
109
+ (i, type)
110
+ for i, type in trains
111
+ if datetime.fromisoformat(i["departure"] if type == 1 else i["arrival"]).timestamp() > date.timestamp() # type: ignore
112
+ ]
113
+ brands = self.storage.get_cache("brands") or self.storage.set_cache("brands", self.client.get_brands())
114
+ parts = []
115
+ for train, type in trains:
116
+ time = f"[bold green]{train['departure'][11:16]}[/bold green]" if type == 1 else f"[bold yellow]{train['arrival'][11:16]}[/bold yellow]"
117
+ brand = next(iter(i for i in brands if i["id"] == train["brand_id"]), {}).get("logo_text")
118
+ parts.append(
119
+ f"{time} [red]{brand}[/red] {train["train_full_name"]}[purple] {train["stations"][0]["name"]} {self.format_position(train["platform"], train["track"])}[/purple]"
120
+ )
121
+ self.print("\n".join(parts))
122
+
92
123
  def find_station(self, query: str | None):
93
124
  if query:
94
125
  stations = self.client.find_station(query)
@@ -356,6 +387,26 @@ def main():
356
387
  arrivals.add_argument("-s", "--save", help="save the station as your default one", action="store_true")
357
388
  arrivals.set_defaults(func=cli.full_arrivals, pass_=["station", "date"])
358
389
 
390
+ all_trains = subparsers.add_parser(
391
+ "all", aliases=["w", "wszystkie", "all_trains", "pociagi"], help="Allows you to list all station trains"
392
+ )
393
+ all_trains.add_argument(
394
+ "station",
395
+ help="The station name",
396
+ default=None,
397
+ nargs="*",
398
+ action=RemainderString,
399
+ )
400
+ all_trains.add_argument(
401
+ "-d",
402
+ "--date",
403
+ help="the date",
404
+ type=lambda s: parse_datetime(s),
405
+ default=datetime.now(),
406
+ )
407
+ all_trains.add_argument("-s", "--save", help="save the station as your default one", action="store_true")
408
+ all_trains.set_defaults(func=cli.all_trains, pass_=["station", "date"])
409
+
359
410
  train_route = subparsers.add_parser(
360
411
  "trainroute",
361
412
  aliases=["r", "tr", "t", "poc", "pociąg"],
@@ -445,7 +496,6 @@ def main():
445
496
  args.station = storage.favourite_station
446
497
  elif hasattr(args, "station") and args.save:
447
498
  storage.favourite_station = args.station
448
- storage.save()
449
499
  if not hasattr(args, "func"):
450
500
  if storage.favourite_station:
451
501
  cli.full_departures(storage.favourite_station, datetime.now())
@@ -454,3 +504,6 @@ def main():
454
504
  exit()
455
505
  else:
456
506
  args.func(**{k: v for k, v in args.__dict__.items() if k in getattr(args, "pass_", [])})
507
+
508
+ if storage._dirty:
509
+ storage.save()
@@ -33,6 +33,7 @@ class Storage:
33
33
 
34
34
  def __post_init__(self):
35
35
  self._path: str
36
+ self._dirty = False
36
37
 
37
38
  @classmethod
38
39
  def load(cls, *, path: str = DEFAULT_CONFIG_PATH) -> t.Self:
@@ -57,13 +58,13 @@ class Storage:
57
58
  return item
58
59
  else:
59
60
  self.cache.pop(id)
60
- self.save()
61
+ self._dirty = True
61
62
 
62
63
  def set_cache(self, id: str, item: T, ttl: int = 86400) -> T:
63
64
  if self.disable_cache:
64
65
  return item
65
66
  self.cache[id] = (int(time() + ttl), item)
66
- self.save()
67
+ self._dirty = True
67
68
  return item
68
69
 
69
70
  def save(self):
@@ -72,4 +73,12 @@ class Storage:
72
73
  if not ospath.exists(dir):
73
74
  makedirs(dir)
74
75
  with open(self._path, "w+") as f:
76
+ self.clean()
75
77
  dump(asdict(self), f, indent=True)
78
+
79
+ def clean(self):
80
+ now = time()
81
+ copy = self.cache.copy()
82
+ self.cache = {k: data for k, data in copy.items() if data[0] > now}
83
+ if copy != self.cache:
84
+ self._dirty = True
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: koleo-cli
3
- Version: 0.2.137.9
3
+ Version: 0.2.137.10
4
4
  Summary: Koleo CLI
5
5
  Home-page: https://github.com/lzgirlcat/koleo-cli
6
6
  Author: Zoey !
@@ -14,7 +14,7 @@ def parse_requirements_file(path):
14
14
 
15
15
  setuptools.setup(
16
16
  name="koleo-cli",
17
- version="0.2.137.9",
17
+ version="0.2.137.10",
18
18
  description="Koleo CLI",
19
19
  long_description=long_description(),
20
20
  long_description_content_type="text/markdown",
File without changes
File without changes
File without changes