Skip to content

Commit

Permalink
Remove dateutil requirement
Browse files Browse the repository at this point in the history
- update clocks app to use zoneinfo module (requires python 3.9)
  • Loading branch information
ejeschke committed Feb 17, 2024
1 parent 57c11e5 commit ce3238a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 54 deletions.
78 changes: 32 additions & 46 deletions ginga/examples/gw/clocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
#
# clocks.py -- Ginga clocks
#
# [email protected]
#
# This is open-source software licensed under a BSD license.
# Please see the file LICENSE.txt for details.
#
Expand All @@ -15,13 +13,13 @@
$ clock.py --help
$ clock.py --show-timezones
$ clock.py --show-colors
NOTE: needs python >= 3.9
"""
import sys
import os
from datetime import datetime

import pytz
from dateutil import tz
from datetime import datetime, timezone, timedelta
import zoneinfo

import ginga.toolkit as ginga_toolkit
from ginga import colors
Expand All @@ -43,15 +41,13 @@ def __init__(self, app, logger, timezone_info, color='lightgreen',

if isinstance(timezone_info, Bunch):
self.timezone_name = timezone_info.location
self.tzinfo = tz.tzoffset(self.timezone_name,
timezone_info.time_offset)
self.tzinfo = timezone(timedelta(days=0,
seconds=int(timezone_info.time_offset)),
name=self.timezone_name)
else:
# assume timezone_info is a str
self.timezone_name = timezone_info
#self.tzinfo = pytz.timezone(timezone)
# NOTE: wierd construction is necessary to get a dateutil
# timezone from the better names produced by pytz
self.tzinfo = tz.gettz(str(pytz.timezone(timezone_info)))
self.tzinfo = zoneinfo.ZoneInfo(timezone_info)

self.color = color
self.font = font
Expand Down Expand Up @@ -81,7 +77,7 @@ def __init__(self, app, logger, timezone_info, color='lightgreen',

self.clock_resized_cb(self.viewer, wd, ht)

dt = datetime.now(tz=tz.UTC)
dt = datetime.now(tz=timezone.utc)
self.update_clock(dt)

def clock_resized_cb(self, viewer, width, height):
Expand Down Expand Up @@ -174,11 +170,10 @@ def __init__(self, logger, settings, options):
self.country_timezone = Widgets.ComboBox(editable=True)

# make a giant list of time zones
zones = [timezone for timezones in pytz.country_timezones.values()
for timezone in timezones]
zones = list(zoneinfo.available_timezones())
zones.sort()
for timezone in zones:
self.country_timezone.append_text(timezone)
for zonename in zones:
self.country_timezone.append_text(zonename)

# also let user set timezone by UTC offset
self.location_label = Widgets.Label('Location')
Expand Down Expand Up @@ -220,43 +215,48 @@ def __init__(self, logger, settings, options):
self.timer.start(1.0)

def more_clock_by_offset(self, w):
location = self.location.get_text()
location = self.location.get_text().strip()
time_offset = self.time_offset.get_value()
sec_hour = 3600
timezone = Bunch(location=location, time_offset=time_offset * sec_hour)
if location == "":
location = f"UTC{time_offset:+.2f}"
timezone_info = Bunch(location=location,
time_offset=time_offset * sec_hour)
color = self.colors[self.color_index % len(self.colors)]
self.color_index += 1
self.add_clock(timezone=timezone, color=color)
self.add_clock(timezone_info, color=color)

def more_clock_by_timezone(self, w):
timezone = self.country_timezone.get_text()
timezone_info = self.country_timezone.get_text()
color = self.colors[self.color_index % len(self.colors)]
self.color_index += 1

self.add_clock(timezone=timezone, color=color)
self.add_clock(timezone_info, color=color)

def add_clock(self, timezone, color='lightgreen', show_seconds=None):
"""Add a clock to the grid. `timezone` is a string representing
def add_clock(self, timezone_info, color='lightgreen', show_seconds=None):
"""Add a clock to the grid. `timezone_info` is a string representing
a valid timezone.
"""
if show_seconds is None:
show_seconds = self.options.show_seconds

clock = Clock(self.app, self.logger, timezone, color=color,
clock = Clock(self.app, self.logger, timezone_info, color=color,
font=self.options.font, show_seconds=show_seconds)
clock.widget.cfg_expand(horizontal='expanding', vertical='expanding')

num_clocks = len(self.clocks)
cols = self.settings.get('columns')
row = num_clocks // cols
col = num_clocks % cols
self.clocks[timezone] = clock
if not isinstance(timezone_info, str):
timezone_info = timezone_info.location
self.clocks[timezone_info] = clock

self.grid.add_widget(clock.widget, row, col, stretch=1)

def timer_cb(self, timer):
"""Timer callback. Update all our clocks."""
dt_now = datetime.now(tz=tz.UTC)
dt_now = datetime.now(tz=timezone.utc)
self.logger.debug("timer fired. utc time is '%s'" % (str(dt_now)))

for clock in self.clocks.values():
Expand Down Expand Up @@ -408,8 +408,10 @@ def main(options, args):
args = options.args

if options.show_timezones:
for timezone in pytz.all_timezones:
print(timezone)
zones = list(zoneinfo.available_timezones())
zones.sort()
for zonename in zones:
print(zonename)
sys.exit(0)

if options.show_colors:
Expand All @@ -421,20 +423,4 @@ def main(options, args):
if options.display:
os.environ['DISPLAY'] = options.display

# Are we debugging this?
if options.debug:
import pdb

pdb.run('main(options, args)')

# Are we profiling this?
elif options.profile:
import profile

print(("%s profile:" % sys.argv[0]))
profile.run('main(options, args)')

else:
main(options, args)

# END
main(options, args)
6 changes: 3 additions & 3 deletions ginga/rv/plugins/Drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
be saved, may be ignored or may cause errors trying to load the regions
shapes in other software.
"""
from datetime import datetime
from dateutil import tz
from datetime import datetime, timezone

from ginga import GingaPlugin
from ginga import colors
Expand Down Expand Up @@ -456,7 +455,8 @@ def create_mask(self):
# Add description to ChangeHistory
s = 'Mask created from {0} drawings ({1})'.format(
ntags, ','.join(sorted(obj_kinds)))
info = dict(time_modified=datetime.now(tz=tz.UTC), reason_modified=s)
info = dict(time_modified=datetime.now(tz=timezone.utc),
reason_modified=s)
self.fv.update_image_info(image, info)
self.logger.info(s)

Expand Down
5 changes: 2 additions & 3 deletions ginga/rv/plugins/Mosaic.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@
"""
import math
import time
from datetime import datetime
from dateutil import tz
from datetime import datetime, timezone
import threading

import numpy as np
Expand Down Expand Up @@ -356,7 +355,7 @@ def _inline(self, images):
suppress_callback=True)

# Add description for ChangeHistory
info = dict(time_modified=datetime.now(tz=tz.UTC),
info = dict(time_modified=datetime.now(tz=timezone.utc),
reason_modified='Added {0}'.format(
','.join([im.get('name') for im in images])))
self.fv.update_image_info(self.img_mosaic, info)
Expand Down
5 changes: 3 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ classifiers =
Operating System :: Microsoft :: Windows
Operating System :: POSIX
Programming Language :: C
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3.10
Programming Language :: Python :: 3.11
Programming Language :: Python :: 3.12
Programming Language :: Python :: 3
Programming Language :: Python :: Implementation :: CPython
Topic :: Scientific/Engineering :: Astronomy
Expand All @@ -53,7 +55,6 @@ install_requires =
pillow>=9.2
pyyaml>=6.0
tomli>=2.0.1; python_full_version < '3.11.0a7'
python-dateutil>=2.8.2
setup_requires = setuptools_scm

[options.extras_require]
Expand Down

0 comments on commit ce3238a

Please sign in to comment.