koleo-cli 0.2.137.9__tar.gz → 0.2.137.11__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.11
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)
@@ -210,11 +241,8 @@ class CLI:
210
241
  dep = arr_dep_to_dt(i["departure"])
211
242
  travel_time = (arr - dep).seconds
212
243
  parts.append(
213
- f"[bold green][link=https://koleo.pl/travel-options/{i["id"]}]{dep.strftime("%H:%M")} - {arr.strftime("%H:%M")}[/bold green] {travel_time//3600}h{(travel_time % 3600)/60:.0f}m {i['distance']}km: [/link]"
244
+ f"[bold green][link=https://koleo.pl/travel-options/{i["id"]}]{dep.strftime("%H:%M")} - {arr.strftime("%H:%M")}[/bold green] {travel_time//3600}h{(travel_time % 3600)/60:.0f}m {i['distance']}km:[/link]"
214
245
  )
215
- if i["constriction_info"]:
216
- for constriction in i["constriction_info"]:
217
- parts.append(f" [bold red]- {constriction} [/bold red]")
218
246
  if len(i["trains"]) == 1:
219
247
  train = i["trains"][0]
220
248
  stop = next(iter(i for i in train["stops"] if i["station_id"] == train["start_station_id"]), {})
@@ -227,10 +255,15 @@ class CLI:
227
255
  )
228
256
  )
229
257
  brand = next(iter(i for i in api_brands if i["id"] == train["brand_id"]), {}).get("logo_text")
258
+ s = f" [red]{brand}[/red] {train["train_full_name"]}[purple] {stop_station['name']} {self.format_position(stop["platform"], stop["track"])}[/purple]"
230
259
  parts[-1] += (
231
260
  f" [red]{brand}[/red] {train["train_full_name"]}[purple] {stop_station['name']} {self.format_position(stop["platform"], stop["track"])}[/purple]"
232
261
  )
262
+ for constriction in i["constriction_info"]:
263
+ parts.append(f" [bold red]- {constriction}[/bold red]")
233
264
  else:
265
+ for constriction in i["constriction_info"]:
266
+ parts.append(f" [bold red]- {constriction}[/bold red]")
234
267
  previous_arrival: datetime | None = None
235
268
  for train in i["trains"]:
236
269
  brand = next(iter(i for i in api_brands if i["id"] == train["brand_id"]), {}).get("logo_text")
@@ -356,6 +389,26 @@ def main():
356
389
  arrivals.add_argument("-s", "--save", help="save the station as your default one", action="store_true")
357
390
  arrivals.set_defaults(func=cli.full_arrivals, pass_=["station", "date"])
358
391
 
392
+ all_trains = subparsers.add_parser(
393
+ "all", aliases=["w", "wszystkie", "all_trains", "pociagi"], help="Allows you to list all station trains"
394
+ )
395
+ all_trains.add_argument(
396
+ "station",
397
+ help="The station name",
398
+ default=None,
399
+ nargs="*",
400
+ action=RemainderString,
401
+ )
402
+ all_trains.add_argument(
403
+ "-d",
404
+ "--date",
405
+ help="the date",
406
+ type=lambda s: parse_datetime(s),
407
+ default=datetime.now(),
408
+ )
409
+ all_trains.add_argument("-s", "--save", help="save the station as your default one", action="store_true")
410
+ all_trains.set_defaults(func=cli.all_trains, pass_=["station", "date"])
411
+
359
412
  train_route = subparsers.add_parser(
360
413
  "trainroute",
361
414
  aliases=["r", "tr", "t", "poc", "pociąg"],
@@ -445,7 +498,6 @@ def main():
445
498
  args.station = storage.favourite_station
446
499
  elif hasattr(args, "station") and args.save:
447
500
  storage.favourite_station = args.station
448
- storage.save()
449
501
  if not hasattr(args, "func"):
450
502
  if storage.favourite_station:
451
503
  cli.full_departures(storage.favourite_station, datetime.now())
@@ -454,3 +506,6 @@ def main():
454
506
  exit()
455
507
  else:
456
508
  args.func(**{k: v for k, v in args.__dict__.items() if k in getattr(args, "pass_", [])})
509
+
510
+ if storage._dirty:
511
+ 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.11
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.11",
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