Skip to content

Commit

Permalink
Merge branch 'master' into dev-version
Browse files Browse the repository at this point in the history
  • Loading branch information
da4089 committed Apr 9, 2024
2 parents 8466be8 + 37b0a73 commit 5109fbc
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ VObject is licensed under the [Apache 2.0 license](http://www.apache.org/license
Useful scripts included with VObject:

* [ics_diff](https://github.com/py-vobject/vobject/blob/master/vobject/ics_diff.py): order is irrelevant in iCalendar files, return a diff of meaningful changes between icalendar files
* [change_tz](https://github.com/py-vobject/vobject/blob/master/vobject/change_tz.py): Take an iCalendar file with events in the wrong timezone, change all events or just UTC events into one of the timezones PyICU supports. Requires [PyICU](https://pypi.python.org/pypi/PyICU/).
* [change_tz](https://github.com/py-vobject/vobject/blob/master/vobject/change_tz.py): Take an iCalendar file with events in the wrong timezone, change all events or just UTC events into one of the timezones **pytz** supports. Requires [pytz](https://pypi.python.org/pypi/pytz/).

# History
VObject was originally developed in concert with the Open Source Application
Expand Down
3 changes: 2 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
-r requirements.txt

coverage
python-dateutil >= 2.4.0
setuptools
wheel
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
python-dateutil >= 2.4.0
python-dateutil >= 2.5.0; python_version < '3.10'
python-dateutil >= 2.7.0; python_version >= '3.10'
pytz>=2019.1
4 changes: 3 additions & 1 deletion setup.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
]
},
include_package_data = True,
install_requires = ['python-dateutil >= 2.4.0', 'six'],
install_requires=["python-dateutil >= 2.5.0; python_version < '3.10'",
"python-dateutil >= 2.7.0; python_version >= '3.10'",
"pytz", 'six'],
platforms = ["any"],
packages = find_packages(),
description = "A full-featured Python package for parsing and creating "
Expand Down
61 changes: 46 additions & 15 deletions vobject/change_tz.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Translate an ics file's events to a different timezone."""

<<<<<<< HEAD
from optparse import OptionParser
import vobject

Expand All @@ -8,7 +9,17 @@
except:
PyICU = None

=======
>>>>>>> master
from datetime import datetime
from optparse import OptionParser

import pytz
from dateutil import tz

from vobject import base, icalendar

version = "0.1"


def change_tz(cal, new_timezone, default, utc_only=False, utc_tz=vobject.icalendar.utc):
Expand All @@ -18,11 +29,9 @@ def change_tz(cal, new_timezone, default, utc_only=False, utc_tz=vobject.icalend
Args:
cal (Component): the component to change
new_timezone (tzinfo): the timezone to change to
default (tzinfo): a timezone to assume if the dtstart or dtend in cal
doesn't have an existing timezone
default (tzinfo): a timezone to assume if the dtstart or dtend in cal doesn't have an existing timezone
utc_only (bool): only convert dates that are in utc
utc_tz (tzinfo): the tzinfo to compare to for UTC when processing
utc_only=True
utc_tz (tzinfo): the tzinfo to compare to for UTC when processing utc_only=True
"""

for vevent in getattr(cal, 'vevent_list', []):
Expand All @@ -31,21 +40,41 @@ def change_tz(cal, new_timezone, default, utc_only=False, utc_tz=vobject.icalend
for node in (start, end):
if node:
dt = node.value
if (isinstance(dt, datetime) and
(not utc_only or dt.tzinfo == utc_tz)):
if isinstance(dt, datetime) and (not utc_only or dt.tzinfo == utc_tz):
if dt.tzinfo is None:
dt = dt.replace(tzinfo=default)
node.value = dt.astimezone(new_timezone)


def show_timezones():
for tz_string in pytz.all_timezones:
print(tz_string)


def convert_events(utc_only, args):
print("Converting {} events".format('only UTC' if utc_only else 'all'))
ics_file = args[0]
_tzone = args[1] if len(args) > 1 else 'UTC'

print("... Reading {}".format(ics_file))
cal = base.readOne(open(ics_file))
change_tz(cal, new_timezone=tz.gettz(_tzone), default=tz.gettz('UTC'), utc_only=utc_only)

out_name = "{}.converted".format(ics_file)
print("... Writing {}".format(out_name))
with open(out_name, 'wb') as out:
cal.serialize(out)

print("Done")


def main():
options, args = get_options()
if PyICU is None:
print("Failure. change_tz requires PyICU, exiting")
elif options.list:
for tz_string in PyICU.TimeZone.createEnumeration():
print(tz_string)

if options.list:
show_timezones()
elif args:
<<<<<<< HEAD
utc_only = options.utc
if utc_only:
which = "only UTC"
Expand All @@ -68,11 +97,13 @@ def main():
cal.serialize(out)

print("Done")
=======
convert_events(utc_only=options.utc, args=args)
>>>>>>> master


def get_options():
# Configuration options

usage = """usage: %prog [options] ics_file [timezone]"""
parser = OptionParser(usage=usage, version=vobject.VERSION)
parser.set_description("change_tz will convert the timezones in an ics file. ")
Expand All @@ -83,14 +114,14 @@ def get_options():
default=False, help="List available timezones")

(cmdline_options, args) = parser.parse_args()
if not args and not cmdline_options.list:
if not (args or cmdline_options.list):
print("error: too few arguments given")
print
print(parser.format_help())
return False, False
return cmdline_options, False

return cmdline_options, args


if __name__ == "__main__":
try:
main()
Expand Down
2 changes: 1 addition & 1 deletion vobject/icalendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ def pickTzid(tzinfo, allowUTC=False):
if tzinfo is None or (not allowUTC and tzinfo_eq(tzinfo, utc)):
# If tzinfo is UTC, we don't need a TZID
return None
# try PyICU's tzid key
# try Pytz's tzid key
if hasattr(tzinfo, 'tzid'):
return toUnicode(tzinfo.tzid)

Expand Down

0 comments on commit 5109fbc

Please sign in to comment.