Skip to content

Commit 25763cd

Browse files
committed
Enchanced formatter; additional TOML config
1 parent bc7a7f0 commit 25763cd

13 files changed

Lines changed: 457 additions & 228 deletions

File tree

README.rst

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ a GeoNames ID of 5128581. Pass that to the ``--alias`` option along with another
5151
you would like to use::
5252

5353
$ when --db --alias 5128581 NYC
54-
$ when NYC
55-
2023-03-26 05:24:02-0400 (America/New_York) 085d12w (New York City, US, New York) [🌒 Waxing Crescent]
54+
$ when --source NYC
55+
2023-07-06 07:58:33-0400 (EDT, America/New_York) 187d27w (New York City, New York, US)[🌕 Full Moon]
5656

5757

5858
Example
@@ -61,24 +61,24 @@ Example
6161
.. code:: bash
6262
6363
$ when
64-
2023-02-11 17:43:44+0900 (KST) 042d06w [🌖 Waning Gibbous]
64+
2023-07-06 20:58:02+0900 (KST, Asia/Seoul) 187d27w [🌕 Full Moon]
6565
6666
$ when --source CST
67-
2023-02-11 02:44:22-0600 (Central Standard Time) 042d06w [🌖 Waning Gibbous]
68-
2023-02-11 12:44:22+0400 (Caucasus Standard Time) 042d06w [🌖 Waning Gibbous]
69-
2023-02-11 16:44:22+0800 (China Standard Time) 042d06w [🌖 Waning Gibbous]
70-
2023-02-11 03:44:22-0500 (Cuba Standard Time) 042d06w [🌖 Waning Gibbous]
67+
2023-07-06 06:58:54-0500 (CDT, Central Standard Time) 187d27w [🌕 Full Moon]
68+
2023-07-06 15:58:54+0400 (+04, Caucasus Standard Time) 187d27w [🌕 Full Moon]
69+
2023-07-06 19:58:54+0800 (CST, China Standard Time) 187d27w [🌕 Full Moon]
70+
2023-07-06 07:58:54-0400 (CDT, Cuba Standard Time) 187d27w [🌕 Full Moon]
7171
7272
$ when --source Paris
73-
2023-02-11 09:45:11+0100 (Europe/Paris) 042d06w (Villeparisis, FR, Île-de-France) [🌖 Waning Gibbous]
74-
2023-02-11 09:45:11+0100 (Europe/Paris) 042d06w (Paris, FR, Île-de-France) [🌖 Waning Gibbous]
75-
2023-02-11 09:45:11+0100 (Europe/Paris) 042d06w (Cormeilles-en-Parisis, FR, Île-de-France) [🌖 Waning Gibbous]
76-
2023-02-11 03:45:11-0500 (America/Port-au-Prince) 042d06w (Fond Parisien, HT, Ouest) [🌖 Waning Gibbous]
77-
2023-02-11 02:45:11-0600 (America/Chicago) 042d06w (Paris, US, Texas) [🌖 Waning Gibbous]
73+
2023-07-06 13:59:25+0200 (CEST, Europe/Paris) 187d27w (Villeparisis, Île-de-France, FR)[🌕 Full Moon]
74+
2023-07-06 13:59:25+0200 (CEST, Europe/Paris) 187d27w (Paris, Île-de-France, FR)[🌕 Full Moon]
75+
2023-07-06 13:59:25+0200 (CEST, Europe/Paris) 187d27w (Cormeilles-en-Parisis, Île-de-France, FR)[🌕 Full Moon]
76+
2023-07-06 07:59:25-0400 (EDT, America/Port-au-Prince) 187d27w (Fond Parisien, Ouest, HT)[🌕 Full Moon]
77+
2023-07-06 06:59:25-0500 (CDT, America/Chicago) 187d27w (Paris, Texas, US)[🌕 Full Moon]
7878
7979
$ when --source "San Francisco,US" --target America/New_York Mar 7 1945 7:00pm
80-
1945-03-07 22:00:00-0400 (America/New_York) 066d10w [🌘 Waning Crescent]
81-
1945-03-07 22:00:00-0400 (America/New_York) 066d10w [🌘 Waning Crescent]
80+
1945-03-07 22:00:00-0400 (EWT, America/New_York) 066d10w [🌘 Waning Crescent]
81+
1945-03-07 22:00:00-0400 (EWT, America/New_York) 066d10w [🌘 Waning Crescent]
8282
8383
8484
Develop

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[tool.black]
2+
line-length = 100

setup.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727
package_dir={"": "src"},
2828
include_package_data=True,
2929
entry_points={"console_scripts": ["when = when.__main__:main",]},
30-
install_requires=["python-dateutil>=2.8.0", "toml>=0.10.2", "requests"],
30+
install_requires=[
31+
"python-dateutil>=2.8.0",
32+
"toml>=0.10.2",
33+
"requests",
34+
],
3135
classifiers=[
3236
"Environment :: Console",
3337
"Intended Audience :: Developers",

src/when/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""
2-
Calculate and convert times across time zones.
2+
Calculate and convert times across time zones and cities of significant population.
33
"""
44

5-
__version__ = (2, 1, 0)
5+
__version__ = (3, 0, 0)
66
VERSION = ".".join(str(i) for i in __version__)
77

88

src/when/cli.py

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#!/usr/bin/env python
2+
import sys
23
import logging
34
import argparse
45

56
from . import core
67
from . import VERSION
78
from . import utils
89
from .db import make
10+
from .config import settings, __doc__ as FORMAT_HELP
911

1012
logger = logging.getLogger(__name__)
1113

@@ -29,14 +31,15 @@ def db_main(args, db):
2931

3032

3133
def config_main(args):
32-
print(core.settings.write_text())
34+
print(settings.write_text())
3335
return 0
3436

3537

3638
def get_parser():
3739
parser = argparse.ArgumentParser(
3840
description="Convert times to and from time zones or cities",
3941
formatter_class=argparse.RawDescriptionHelpFormatter,
42+
add_help=False,
4043
)
4144

4245
parser.add_argument(
@@ -46,6 +49,14 @@ def get_parser():
4649
help="Timestamp to parse, defaults to local time",
4750
)
4851

52+
parser.add_argument(
53+
"-h",
54+
"--help",
55+
action="store_true",
56+
default=False,
57+
help="Show helpful usage information",
58+
)
59+
4960
parser.add_argument(
5061
"-s",
5162
"--source",
@@ -65,17 +76,12 @@ def get_parser():
6576
""",
6677
)
6778

79+
default_format = settings["formats"]["named"]["default"]
6880
parser.add_argument(
6981
"-f",
7082
"--format",
71-
default=core.DEFAULT_FORMAT,
72-
help="""
73-
Output formatting. Additionaly predefined formats by name are {}.
74-
Default: {}, where %%K is timezone long name
75-
""".format(
76-
", ".join(["rfc2822, iso, "]),
77-
core.DEFAULT_FORMAT.replace("%", "%%"),
78-
),
83+
default=default_format,
84+
help="Output formatting. Additional formats can be shown using the -v option with -h",
7985
)
8086

8187
parser.add_argument(
@@ -88,7 +94,11 @@ def get_parser():
8894
parser.add_argument("--holidays", help="Show holidays for given country code.")
8995

9096
parser.add_argument(
91-
"-v", "--verbosity", action="count", default=0, help="Verbosity (-v, -vv, etc)"
97+
"-v",
98+
"--verbosity",
99+
action="count",
100+
default=0,
101+
help="Verbosity (-v, -vv, etc). Use -v to show `when` extension detailed help",
92102
)
93103

94104
parser.add_argument(
@@ -152,15 +162,16 @@ def log_config(verbosity):
152162
log_level = logging.DEBUG if verbosity > 1 else logging.INFO
153163

154164
logging.basicConfig(level=log_level, format=log_format, force=True)
155-
logger.debug("Configuration files read: %s", ", ".join(core.settings.read_from))
165+
logger.debug("Configuration files read: %s", ", ".join(settings.read_from))
156166

157167

158168
def main(sys_args, when=None):
159169
debug = "--pdb" in sys_args
160170
if debug:
161171
sys_args.remove("--pdb")
162172

163-
args = get_parser().parse_args(sys_args)
173+
parser = get_parser()
174+
args = parser.parse_args(sys_args)
164175

165176
if debug:
166177
try:
@@ -170,15 +181,21 @@ def main(sys_args, when=None):
170181
pdb.set_trace()
171182

172183
log_config(args.verbosity)
184+
if args.help:
185+
parser.print_help()
186+
if args.verbosity:
187+
print(FORMAT_HELP)
188+
else:
189+
print("\nUse -v option for details\n")
190+
sys.exit(0)
191+
173192
when = when or core.When()
174193
if args.db:
175194
return db_main(args, when.db)
176195
elif args.config:
177196
return config_main(args)
178197
elif args.holidays:
179-
return core.holidays(
180-
args.holidays, args.timestamp[0] if args.timestamp else None
181-
)
198+
return core.holidays(args.holidays, args.timestamp[0] if args.timestamp else None)
182199

183200
ts = utils.parse_source_input(args.timestamp)
184201
targets = args.target

0 commit comments

Comments
 (0)