~marcosvanetta/+junk/finop

« back to all changes in this revision

Viewing changes to core/logging.py

  • Committer: Marcos Vanetta
  • Date: 2010-04-21 21:57:06 UTC
  • Revision ID: marcosvanetta@gmail.com-20100421215706-046jx16am69j802e
starting refactoring

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
#-*- coding:utf-8 -*-
 
3
 
 
4
"""
 
5
finop.py - A Utility IRC Bot for #ubuntu-ar
 
6
Copyright 2010, Marcos Vanetta, malev.com.ar
 
7
Licensed under the GLP3.
 
8
 
 
9
https://code.launchpad.net/~marcosvanetta/+junk/finop
 
10
http://blog.malev.com.ar
 
11
        *----*
 
12
do not execute this file only.
 
13
Call for main.py first!
 
14
"""
 
15
 
 
16
__version__ = '0.0.1'
 
17
 
 
18
import os
 
19
import datetime
 
20
import logging
 
21
import config
 
22
from storm.locals import *
 
23
 
 
24
 
 
25
the_db_file = config.DATABASE
 
26
connected = False
 
27
 
 
28
class Message (object):
 
29
    """
 
30
    In some how it must store:
 
31
    time, nickname, message
 
32
    it also has to store the people who logs in and other message.
 
33
    with css I can manage it to not interrupt the readding.
 
34
    example:
 
35
    http://chatlogs.musicbrainz.org/musicbrainz/2010/2010-04/2010-04-15.html
 
36
    id: INTEGER
 
37
    user_id: INTEGER
 
38
    date: VARCHAR
 
39
    message: VARCHAR
 
40
    """
 
41
    __storm_table__ = "message"
 
42
    id = Int(primary=True)
 
43
    date = DateTime()
 
44
    user = Unicode()
 
45
    message = Unicode()
 
46
    
 
47
    connectedDB = False
 
48
 
 
49
    def __init__(self, message, user):
 
50
        self.date = datetime.datetime.now()
 
51
        self.user = unicode(user, 'utf-8')
 
52
        self.message = unicode(message, 'utf-8')
 
53
        self.store.add(self)
 
54
        self.save()
 
55
 
 
56
    def save(self):
 
57
        Message.store.flush()
 
58
        Message.store.commit()
 
59
 
 
60
    @classmethod
 
61
    def initDB(cls):
 
62
        connectedDB = True
 
63
        if not os.path.exists(the_db_file):
 
64
            cls.database = create_database("sqlite:///" + the_db_file)
 
65
            cls.store = Store(cls.database)
 
66
            try:
 
67
                cls.store.execute('''
 
68
                    CREATE TABLE message (
 
69
                    id INTEGER PRIMARY KEY,
 
70
                    date VARCHAR
 
71
                    user VARCAHR,
 
72
                    message VARCHAR,
 
73
                    )''', noresult=True)
 
74
                cls.store.flush()
 
75
                cls.store.commit()
 
76
            except:
 
77
                logging.error('when trying to create table')
 
78
        else:
 
79
            cls.database = create_database("sqlite:///" + the_db_file)
 
80
            cls.store = Store(cls.database)
 
81
 
 
82
def write_db(others_nickname, message):
 
83
    if not Message.connectedDB:
 
84
        Message.initDB()
 
85
    print "%s: %s" % (others_nickname, message)
 
86
    a = Message(message, others_nickname)
 
87
 
 
88
if __name__ == '__main__':
 
89
    print __doc__
 
90
#    User.initDB()
 
91
#    Message.initDB()
 
92
#    m = User("malev")
 
93
#    a = Message("un dia muy largo", "malev")
 
94
 
 
95
#logging.debug('This is a debug message')
 
96
#logging.info('This is an info message')
 
97
#logging.warning('This is a warning message')
 
98
#logging.error('This is an error message')
 
99
#logging.critical('This is a critical error message')
 
100