Skip to content

Commit

Permalink
Update with new URL
Browse files Browse the repository at this point in the history
  • Loading branch information
n8henrie committed Oct 8, 2019
1 parent 0389ea9 commit 7bb0369
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
6 changes: 5 additions & 1 deletion README.md
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ If you're choosing to run it as a script, you probably know a lot more about Pyt
Hmmm. Unclear what it would take to port to windows. You may give a shot to the webapp.

#### WebApp
[icsconverterwebapp.n8henrie.com](http://icsconverterwebapp.n8henrie.com)

~~icsConverterWebapp~~ used to be hosted at
http://icsconverterwebapp.n8henrie.com, but not anymore. The updated version is
[live at icw.n8henrie.com](http://icw.n8henrie.com), source code at
<https://github.com/n8henrie/icw>.

## Usage
Not going to write this over again. See my post [here](http://n8henrie.com/2013/05/spreadsheet-to-calendar/).
Expand Down
39 changes: 20 additions & 19 deletions icsconverter.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,25 @@ def clean_spaces(csv_dict):
clean_row.update({ k: v.strip() })
else:
clean_row.update({ k: None })

yield clean_row

def check_dates_and_times(
start_date = None, start_time = None,
end_date = None, end_time = None, all_day = None, subject = None
):
'''Checks the dates and times to make sure everything is kosher.'''

logger.debug('Date checker started.')

# Gots to have a start date, no matter what.
if start_date in ['', None]:
logger.error('Missing a start date')
easygui.msgbox('''You're missing a start date. All events need start dates! '''
'''The Subject for the event was: {}'''.format(subject))
raise DateTimeError('Missing a start date')
return False

for date in [start_date, end_date]:
if date not in ['', None]:
try:
Expand All @@ -97,7 +97,7 @@ def check_dates_and_times(

for time in [start_time, end_time]:
if time not in ['', None]:
try:
try:
time = time.replace(' ', '')
if time[-2:].lower() in ['am','pm']:
datetime.strptime(time, '%I:%M%p')
Expand All @@ -109,7 +109,7 @@ def check_dates_and_times(
logger.error('Problem with time formatting. Time: {}'.format(time))
raise DateTimeError('Something isn\'t right with the times.')
return False

if all_day == None or all_day.lower() != 'true':
if not (start_time and end_time):
easygui.msgbox('''Missing a required time field in a non-all_day event on date: {}.\n'''
Expand All @@ -121,25 +121,26 @@ def check_dates_and_times(
logger.debug('Date checker ended.')
return True

def main(open_gui = None):
def main(infile=None):
# The skipinitialspace option eliminates leading
# spaces and reduces blank cells to '' while the clean_spaces
# function gets rid of trailing spaces.
try:
if open_gui == None:
if infile == None:
start_dir = '~/'
if isdir(expanduser("~/Desktop")):
start_dir = '~/Desktop/'
msg = 'Please select the .csv file to be converted to .ics'
open_gui = easygui.fileopenbox(msg=msg, title="", default=expanduser(start_dir), filetypes=["*.csv"])
infile = easygui.fileopenbox(msg=msg, title="", default=expanduser(start_dir), filetypes=["*.csv"])

reader_builder = list(csv.DictReader(open(open_gui, 'Ub'), skipinitialspace = True))
reader_builder = list(csv.DictReader(open(infile, 'U'), skipinitialspace = True))

# For testing comment 4 lines above (2 x if / else) and use this:
# reader_builder = list(csv.DictReader(open('path_to_tester.csv', 'rb'), skipinitialspace = True))

except:
easygui.msgbox('Looks like there was an error opening the file, didn\'t even make it to the conversion part. Sorry!')

except Exception as e:
logger.exception(e)
easygui.msgbox("Looks like there was an error opening the file, didn't even make it to the conversion part. Sorry!")
sys.exit(1)

# Filter out events with empty subjects, a required element
Expand All @@ -160,11 +161,11 @@ def main(open_gui = None):

# Write the clean list of dictionaries to events.
rownum = 0
try:
try:
for row in reader:
event = Event()
event.add('summary', row['Subject'])

try:
check_dates_and_times(
start_date = row.get('Start Date'),
Expand All @@ -176,15 +177,15 @@ def main(open_gui = None):
)
except DateTimeError as e:
sys.exit(e)

# If marked as an "all day event," ignore times.
# If start and end date are the same
# or if end date is blank default to a single 24-hour event.
if row.get('All Day Event') != None and row['All Day Event'].lower() == 'true':

# All-day events will not be marked as 'busy'
event.add('transp', 'TRANSPARENT')

event.add('dtstart', datetime.strptime(row['Start Date'], '%m/%d/%Y' ).date())

if row.get('End Date') in ['', None]:
Expand All @@ -197,7 +198,7 @@ def main(open_gui = None):

# Events with times should be 'busy' by default
event.add('transp', 'OPAQUE')

# Get rid of spaces
# Note: Must have both start and end times if not all_day, already checked
row['Start Time'] = row['Start Time'].replace(' ', '')
Expand Down Expand Up @@ -262,7 +263,7 @@ def main(open_gui = None):
if __name__ == "__main__":
try:
if len(sys.argv) > 1:
main(open_gui = sys.argv[1])
main(infile=sys.argv[1])
else:
main()
except (HeadersError, DateTimeError) as e:
Expand Down

0 comments on commit 7bb0369

Please sign in to comment.