#
# Convert DayNotez to pyds entries.
# 
import re
import time
import sys
import os
import urllib
import win32com
import win32com.client
import md5
import win32api
import KLog
import xmlrpclib

KLog.HookError()	# Hook errors
#KLog.SendEmail()	# email them to me

if win32api.GetComputerName() == "PCW-PC":
	strDayNotezPath = "C:\\Program Files\\Natara\\DayNotez\\"
	strTarget = "c:\\inetpub\\wwwroot\\"
	strSource = "c:\\Play\\Website\\"
	strCategories = [ '370', 'Tech', 'Business']
	g_strLinks = []
else:
	strDayNotezPath = "C:\\Program Files\\Natara\\DayNotez\\"
	strTarget = "c:\\Play\\Website\\Blog\\"
	strSource = "c:\\Play\\Website\\"
	strCategories = [ 'Tech', 'Personal']
	g_strLinks = [ 'FAQ']

oEngine = win32com.client.dynamic.Dispatch("DAO.DBEngine.36")
oEngine.SystemDB = strDayNotezPath + "Peter Daynotez.mdw"
oWorkspace = oEngine.CreateWorkspace( "WorkSpace", "DayNotez", "")
oDatabase = oWorkspace.OpenDatabase( strDayNotezPath + "Peter Daynotez.dnz", ReadOnly = -1)

#
# Convert unicode to good old ascii
#
def DeUnicode( strString, bStripLF = 0):
	strOut = strString.encode( "iso8859_15", "backslashreplace")
	strOut = strOut.replace( '\\u2022', '*')
	strOut = strOut.replace( '\r', '')

	return strOut

#
# Convert OLE format date to C library date.
#
def OLEDate2CDate( nDate):
	nDays = int(nDate) - 25569 # Days till 1 Jan 1970 i.e. C epoch
	nSeconds = (float(nDate) - int(nDate)) * 3600 * 24
	nSeconds += nDays * 3600 * 24
	return nSeconds

#
# Dump a table
#
def DumpTable( oDatabase, strName):
	oRecordset = oDatabase.OpenRecordset( "SELECT * FROM %s" % strName)

	while oRecordset.EOF == 0:
		for oField in oRecordset.Fields:
			strValue = oField.Value

			if strValue == None:
				strValue = "<NULL>"
			elif type(strValue) == type( u''):
				strValue = DeUnicode(strValue)
			else:
				strValue = str(strValue)

			print oField.Name + "=" + strValue

		oRecordset.MoveNext()

#
# Gather definitions of categories.
# This is quicker for me right now than figuring out how to do a join.
#
oCategories = {}

oRecordset = oDatabase.OpenRecordset( "SELECT * FROM Categories")

while oRecordset.EOF == 0:
	nID = oRecordset.Fields( "CategoryId").Value
	strName = DeUnicode( oRecordset.Fields("CategoryName").Value)
	
	if strName != "":
		oCategories[nID] = strName

	oRecordset.MoveNext()

#
# Fixup pages to please rxstructuredtext
#
def FixupPage( strPage):
	#
	# Nice Daynotez bullet mark syntax upsets docutils because of the leading tab
	# It also likes a blank line before the first item
	#
	strNoteLines = strPage.split( "\n")
	bInTable = False
	bWasBullet = False

	for i in range( len( strNoteLines)):
		strText = strNoteLines[i]

		if not bInTable:
			oMatch = re.match( r'^(\s+)(\*\s|\d+\.)', strText)

			if oMatch:
				if bWasBullet == False:
					strNoteLines[i] = "\n" + strText[1:]
				else:
					strNoteLines[i] = strText[1:]
				bWasBullet = True
			else:
				bWasBullet = False

				if re.match( r'=+\s+=+', strText):
					#
					# Start of a table.
					# Do not fiddle with formatting within tables.
					#
					bInTable = True
				else:
					#
					# If line looks like the start of a sentence then insert a blank line
					# to create a new paragraph.
					#
					if len(strText) > 0 and not strText[0] in ' \t':
						strNoteLines[i] = "\n" + strText
		else:
			#
			# In a table. A blank line signifies the end.
			#
			if strText.strip() == "":
				bInTable = False

	return "\n".join( strNoteLines).replace( "\n\n\n", "\n\n")

#
# Read existing weblog posts
#
oPyDS = xmlrpclib.ServerProxy( 'http://192.168.0.202:4334/RPC2')
oPosts = oPyDS.metaWeblog.getRecentPosts( '', 'peter', 'wibble', 1000)

#
# build a map of the existing entries
#
oExistingPosts = {}

for oPost in oPosts:
	m = md5.md5()
	m.update( DeUnicode(oPost['description'].strip()))
	oExistingPosts[m.digest()] = 1

def DoIt():
	#
	# Dump out all DayNotez entries.
	#
	# There are two outputs:
	#   All notes for a particular date
	#   The last few entries for the main page
	#
	oRecordset = oDatabase.OpenRecordset( "SELECT * FROM Notes ORDER BY Created DESC")

	while oRecordset.EOF == 0:
		nCreated = oRecordset.Fields("Created").Value
		strCreated = time.strftime( "%a %d %b %Y, %H:%M:%S", time.localtime( OLEDate2CDate( nCreated)))
		strCreatedDate = time.strftime( "%a %d %b %Y", time.localtime( OLEDate2CDate( nCreated)))
		strNote = DeUnicode( oRecordset.Fields("NoteText").Value)
		nCategory = oRecordset.Fields("Category").Value
		strCategory = oCategories[nCategory]
		
		if strCategory in strCategories:
			strRtxNote = FixupPage( strNote)
			m = md5.md5()
			m.update( strRtxNote.strip())
			if not oExistingPosts.has_key( m.digest()):
				#
				# Create a title from the start of the posting.
				#
				strTitle = ""
				for strWord in strRtxNote.strip().replace( '\n', ' ').replace('\t', ' ').split( ' '):
					if len(strTitle + strWord) > 50:
						strTitle += '..'
						break

					if strTitle:
						strTitle += ' '

					strTitle += strWord

					if strTitle[-1] in '.:;?!':
						break

				#
				# Bold markup does not work in the title.
				#
				strTitle = strTitle.replace( '**', '')
				oPost = {}
				#
				# Send back in unicode format: this allows £ characters through.
				#
				oPost['title'] = unicode( strTitle, 'ISO-8859-1')
				oPost['description'] = unicode( strRtxNote, 'ISO-8859-1')
				oPost['pubtime'] = str(int(OLEDate2CDate( nCreated)))

				print strTitle
				oPyDS.metaWeblog.newPost( '', 'peter', 'wibble', oPost, 1)

		oRecordset.MoveNext()

DoIt()
