#! /usr/bin/env python

import sys
import json
import glob
import codecs
import sqlite3
import os.path

storage = {}

# load unicode to file
def load_unicode_file(filename):
	try:
		fn = os.path.expanduser(filename)
		with codecs.open(fn,"rt",encoding="utf8") as f:
			return f.read()
	except IOError as e:
		print "IOError: %s" % e
		raise Exception("Failed to load unicode file %s" % filename)

# load json from content
def convert_to_json(content):
	try:
		return json.loads(content)
	except Exception as e:
		print "Exception: %s" % e
		raise Exception("Failed to convert content to json")

# list available files to import
def list_files(path, ext):
	full_path = os.path.expanduser(path)
	file_list = glob.glob("%s/*.%s" % (full_path, ext))
	return sorted(file_list)

# open sqlite3 database
def open_database(contract_name, station_number):
	# open or create db file
	db_path = "~/.jcd/databases/%s_%s.sqlite3" % (contract_name, station_number)
	full_db_path = os.path.expanduser(db_path)
	try:
		return sqlite3.connect(full_db_path)
	except sqlite3.OperationalError as e:
		print "OperationalError: %s" % e
		raise Exception("Failed to open database %s" % full_db_path)

# import contract into database
def import_contract_station(contract_name, station_number, station):
	# open/create/init database
	db = open_database(contract_name, station_number)
	# create table if necessary
	pass
	# import updated stations
	print "contract %s number %i station %s" % (contract_name, station_number, station)
	# commit changes
	pass

# import update file into databases
def import_update(filename):
	# loading text content
	content = load_unicode_file(filename)
	# skip on error
	if content is None:
		print "Cannot load content of %s, skipping" % filename
		return
	# load json from text
	json = convert_to_json(content)
	# skip on error
	if json is None:
		print "Cannot convert content of %s to json" % filename
		return
	# import a contract's updates
	for contract_name in json:
		contract = json[contract_name]
		for station_key in contract:
			station = contract[station_key]
			station_number = int(station_key)
			import_contract_station(contract_name,station_number,station)

# work
def work():
	files = list_files("~/.jcd/updates", "json")
	for f in files:
		try:
			import_update(f)
		except Exception as e:
			print "Exception %s" % e
			print "Skipping file %s" % f
		return
			
# main
work()

@@ -100,21 +100,152 @@
 ath%0A
-%0Astorage = %7B%7D
+import logging%0A%0Astorage = %7B%7D%0A%0Aclass JcdImportException(Exception):%0A%09def __init__(self,*args,**kwargs):%0A%09%09Exception.__init__(self,*args,**kwargs)
 %0A%0A# 
@@ -437,40 +437,42 @@
 :%0A%09%09
-print %22IOError: %25s%22 %25 e%0A%09%09raise 
+logging.error(e)%0A%09%09raise JcdImport
 Exce
@@ -625,60 +625,97 @@
 ept 
-Exception as e:%0A%09%09print %22Exception: %25s%22 %25 e%0A%09%09raise 
+(TypeError,OverflowError,ValueError,TypeError) as e:%0A%09%09logging.error(e)%0A%09%09raise JcdImport
 Exce
@@ -1248,49 +1248,42 @@
 :%0A%09%09
-print %22OperationalError: %25s%22 %25 e%0A%09%09raise 
+logging.error(e)%0A%09%09raise JcdImport
 Exce
@@ -1822,266 +1822,60 @@
 %0A%09# 
-skip on error%0A%09if content is None:%0A%09%09print %22Cannot load content of %25s, skipping%22 %25 filename%0A%09%09return%0A%09# load json from text%0A%09json = convert_to_json(content)%0A%09# skip on error%0A%09if json is None:%0A%09%09print %22Cannot convert content of %25s to json%22 %25 filename%0A%09%09return
+load json from text%0A%09json = convert_to_json(content)
 %0A%09# 
@@ -2250,16 +2250,25 @@
 %09except 
+JcdImport
 Exceptio
@@ -2282,42 +2282,44 @@
 %0A%09%09%09
-print %22Exception %25s%22 %25 e%0A%09%09%09print 
+logging.error(e)%0A%09%09%09logging.warning(
 %22Ski
@@ -2336,16 +2336,17 @@
  %25s%22 %25 f
+)
 %0A%09%09retur
@@ -2351,19 +2351,16 @@
 urn%0A
-%09%09%09
 %0A# main%0A
 work
@@ -2355,15 +2355,133 @@
 %0A# main%0A
-work
+def main():%0A%09# setup logging%0A%09logging.basicConfig(format=%22%25(levelname)s:%25(asctime)s %25(message)s%22)%0A%09# do work%0A%09work()%0A%0Amain
 ()%0A

add logging