diff options
| author | Simeon Simeonov | 2012-11-12 15:09:47 +0100 |
|---|---|---|
| committer | Simeon Simeonov | 2012-11-12 15:09:47 +0100 |
| commit | b1b484a0b93a5651da7d1ca20e77f70089b215bb (patch) | |
| tree | e254c380eff988b97062c6e196f29b61f42eb705 | |
bpynotify - started
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | bpynotify_orm.py | 32 | ||||
| -rwxr-xr-x | socket_server.py | 67 |
3 files changed, 101 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..184ce68 --- /dev/null +++ b/.gitignore | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | *.pyc | ||
| 2 | *.db \ No newline at end of file | ||
diff --git a/bpynotify_orm.py b/bpynotify_orm.py new file mode 100644 index 0000000..0540d0a --- /dev/null +++ b/bpynotify_orm.py | |||
| @@ -0,0 +1,32 @@ | |||
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | |||
| 3 | import datetime | ||
| 4 | |||
| 5 | from sqlalchemy import schema, types | ||
| 6 | from sqlalchemy.orm import mapper | ||
| 7 | |||
| 8 | metadata = schema.MetaData() | ||
| 9 | |||
| 10 | notification_entry_table = schema.Table('notification_entries', metadata, | ||
| 11 | schema.Column('id', types.Integer, primary_key=True), | ||
| 12 | schema.Column('timestamp', types.DateTime), | ||
| 13 | schema.Column('message', types.Unicode(), default = u'Empty message'), | ||
| 14 | schema.Column('viewed', types.Boolean, default=False) | ||
| 15 | ) | ||
| 16 | |||
| 17 | |||
| 18 | |||
| 19 | class NotificationEntry(object): | ||
| 20 | |||
| 21 | def __init__(self, message): | ||
| 22 | |||
| 23 | self.timestamp = datetime.datetime.now() | ||
| 24 | self.message = message | ||
| 25 | |||
| 26 | |||
| 27 | def __repr__(self): | ||
| 28 | |||
| 29 | return message | ||
| 30 | |||
| 31 | |||
| 32 | mapper(NotificationEntry, notification_entry_table) | ||
diff --git a/socket_server.py b/socket_server.py new file mode 100755 index 0000000..7297d24 --- /dev/null +++ b/socket_server.py | |||
| @@ -0,0 +1,67 @@ | |||
| 1 | #!/usr/bin/env python | ||
| 2 | # -*- coding: utf-8 -*- | ||
| 3 | |||
| 4 | import SocketServer | ||
| 5 | import sys | ||
| 6 | |||
| 7 | from sqlalchemy import create_engine | ||
| 8 | from sqlalchemy.orm import sessionmaker | ||
| 9 | |||
| 10 | from bpynotify_orm import NotificationEntry | ||
| 11 | |||
| 12 | |||
| 13 | class CustomSocketServer(SocketServer.TCPServer): | ||
| 14 | |||
| 15 | def __init__(self, server_address, RequestHandlerClass, session): | ||
| 16 | print "Done" | ||
| 17 | SocketServer.TCPServer.__init__(self, | ||
| 18 | server_address, | ||
| 19 | RequestHandlerClass) | ||
| 20 | self.session = session | ||
| 21 | |||
| 22 | |||
| 23 | class MyTCPHandler(SocketServer.StreamRequestHandler): | ||
| 24 | """ | ||
| 25 | The RequestHandler class for our server. | ||
| 26 | |||
| 27 | It is instantiated once per connection to the server, and must | ||
| 28 | override the handle() method to implement communication to the | ||
| 29 | client. | ||
| 30 | """ | ||
| 31 | |||
| 32 | def handle(self): | ||
| 33 | # self.request is the TCP socket connected to the client | ||
| 34 | ##self.data = self.request.recv(1024).strip() | ||
| 35 | ##print "{} wrote:".format(self.client_address[0]) | ||
| 36 | ##print self.data | ||
| 37 | |||
| 38 | self.data = self.rfile.readline().strip() | ||
| 39 | |||
| 40 | entry = NotificationEntry(unicode(self.data)) | ||
| 41 | self.server.session.add(entry) | ||
| 42 | self.server.session.commit() | ||
| 43 | |||
| 44 | |||
| 45 | def main(): | ||
| 46 | |||
| 47 | HOST, PORT = "localhost", 9999 | ||
| 48 | |||
| 49 | try: | ||
| 50 | engine = create_engine('sqlite:///notifications.db') | ||
| 51 | Session = sessionmaker(bind=engine) # bound session | ||
| 52 | session = Session() | ||
| 53 | |||
| 54 | server = CustomSocketServer((HOST, PORT), MyTCPHandler, session) | ||
| 55 | # Activate the server; this will keep running until you | ||
| 56 | # interrupt the program with Ctrl-C | ||
| 57 | server.serve_forever() | ||
| 58 | |||
| 59 | except Exception as e: | ||
| 60 | sys.stderr.write("Server error: {0}".format(e)) | ||
| 61 | sys.exit(1) | ||
| 62 | |||
| 63 | sys.exit(0) | ||
| 64 | |||
| 65 | |||
| 66 | if __name__ == "__main__": | ||
| 67 | main() | ||
