Skip to content

Instantly share code, notes, and snippets.

@professorjamesmoriarty
Created November 19, 2016 01:38
Show Gist options
  • Save professorjamesmoriarty/b53dac7e2e481d7e09648f74750d9ce2 to your computer and use it in GitHub Desktop.
Save professorjamesmoriarty/b53dac7e2e481d7e09648f74750d9ce2 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2016 Johnathan "Shaggytwodope" Jenkins <twodopeshaggy@gmail.com>
#
# Distributed under terms of the GPL2 license.
import argparse
import os
import urllib.request
import shutil
import configparser
import sys
localaptsfile = "/home/shaggy/.calcurse/myapts"
aptsfile = "/home/shaggy/.calcurse/apts"
aptsfilebackup = "/home/shaggy/.calcurse/apts_backup"
aptstempfile = "/home/shaggy/.calcurse/aptstemp"
parser = argparse.ArgumentParser()
parser.add_argument("-v", "--verbose",
help='verbose mode',
action='store_true')
args = parser.parse_args()
filepath = '/home/shaggy/.config/updatecalcurse.cfg'
config = configparser.ConfigParser()
config.read(filepath)
def main():
for (name, url) in config.items('calendars'):
if args.verbose:
print("downloading %s" % name.split('.')[0])
try:
urllib.request.urlretrieve(url, name)
except IOError:
print("There was a problem downloading the data for "
+ name.split('.')[0])
sys.exit(1)
if args.verbose:
print("clearing old data...")
try:
os.remove(aptsfile)
except:
print("There was a problem removing old data")
sys.exit(1)
if args.verbose:
print("importing calendar data...")
for (name, url) in config.items('calendars'):
try:
os.system("calcurse -i " + str(name) + ">/dev/null 2>&1")
except OSError:
print("There was a problem importing the data for "
+ name.split('.')[0])
try:
shutil.move(aptsfile, aptstempfile)
except:
print("There was a problem copying data to back up")
sys.exit(1)
filenames = [localaptsfile, aptstempfile]
with open(aptsfile, "w") as outfile:
for fname in filenames:
with open(fname) as infile:
outfile.write(infile.read())
if args.verbose:
print("backing up..")
try:
shutil.copy(aptsfile, aptsfilebackup)
except:
print("There was a problem backing up data")
sys.exit(1)
if args.verbose:
print("cleaning up downloaded files...")
try:
os.remove(aptstempfile)
except:
print("There was a problem cleaning up temp data")
sys.exit(1)
for (name, url) in config.items('calendars'):
try:
os.remove(name)
except OSError:
print("There was a problem removing calendar data")
sys.exit(1)
if args.verbose:
print("done")
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment