summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPierre-Yves Chibon <pingou@pingoured.fr>2012-04-25 08:10:45 +0200
committerPierre-Yves Chibon <pingou@pingoured.fr>2012-04-25 08:10:45 +0200
commite8134dbcf71924d346826a170be6bb1afefc4f07 (patch)
tree6d1c3afdab057157d36f88dc9e441baba9bef027
parent026cdfc591dfaedc7dcdeb050b9131ae425f85cd (diff)
Create the kittystore module
This module provides the interface to the different store we are supporting/testing
-rw-r--r--kittystore/mongostore.py74
-rw-r--r--kittystore/sastore.py105
2 files changed, 179 insertions, 0 deletions
diff --git a/kittystore/mongostore.py b/kittystore/mongostore.py
new file mode 100644
index 0000000..19a8790
--- /dev/null
+++ b/kittystore/mongostore.py
@@ -0,0 +1,74 @@
+# -*- coding: utf-8 -*-
+
+"""
+mmstore - an object mapper and interface to the mongo database
+ representation of emails for mailman 3.
+
+Copyright (C) 2012 Pierre-Yves Chibon
+Author: Pierre-Yves Chibon <pingou@pingoured.fr>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or (at
+your option) any later version.
+See http://www.gnu.org/copyleft/gpl.html for the full text of the
+license.
+"""
+
+from ming import Session, Document, Field, schema
+from ming.datastore import DataStore
+
+from datetime import datetime
+
+host = 'localhost'
+port = '27017'
+database = 'test'
+bind = DataStore('mongodb://%s:%s/' %(host, port),
+ database=database)
+session = Session(bind)
+
+
+class MMStore(Document):
+ """ Object representation of the information stored for every email
+ in the mongo database.
+
+ This class is the interface used to read and write to the database
+ any email it contains.
+ """
+
+ class __mongometa__:
+ """ Meta information required for the class """
+ name = 'mails'
+ session = session
+ custom_indexes = [
+ dict(fields=('MessageID',), unique=True, sparse=False),
+ dict(fields=('Date',), unique=False, sparse=True)
+ ]
+
+ # Unique identifier, specific to mongodb
+ _id = Field(schema.ObjectId)
+ # Name of the sender
+ From = Field(str)
+ # Email address of the sender
+ Email = Field(str)
+ # Email body
+ Content = Field(str)
+ # Date when the email was sent
+ Date = Field(datetime)
+ # Unique identifier of the message as present in the header
+ MessageID = Field(str)
+ # Helper to keep links consistent with pipermail
+ #LegacyID = Field(int)
+ # Assign a category to the email -- HK specific
+ #Category = Field(str)
+ # Full email (headers and body included)
+ Full = Field(str)
+
+
+if __name__ == '__main__':
+ #store = MMStore(dict(From = 'test@test', Content = 'test'))
+ #store.m.save()
+ mail = MMStore.m.find({'MessageID':'jfc06g$ci3$1@dough.gmane.org'}).one()
+ print mail, mail.keys()
+ print dir(MMStore.__mongometa__.session.db.name)
+ print MMStore.__mongometa__.session.db.name
diff --git a/kittystore/sastore.py b/kittystore/sastore.py
new file mode 100644
index 0000000..46d4499
--- /dev/null
+++ b/kittystore/sastore.py
@@ -0,0 +1,105 @@
+# -*- coding: utf-8 -*-
+
+"""
+sastore - an object mapper and interface to a SQL database
+ representation of emails for mailman 3.
+
+Copyright (C) 2012 Pierre-Yves Chibon
+Author: Pierre-Yves Chibon <pingou@pingoured.fr>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or (at
+your option) any later version.
+See http://www.gnu.org/copyleft/gpl.html for the full text of the
+license.
+"""
+
+from sqlalchemy import (
+ create_engine,
+ Column,
+ Integer,
+ DateTime,
+ String,
+ Text,
+)
+
+from sqlalchemy.ext.declarative import declarative_base
+
+Base = declarative_base()
+
+
+class Email(Base):
+ """ Email table. """
+
+ __tablename__ = 'email'
+ id = Column(Integer, primary_key=True)
+ list_name = Column(String(50), nullable=False)
+ sender = Column(String(100), nullable=False)
+ email = Column(String(75), nullable=False)
+ subject = Column(Text, nullable=False)
+ content = Column(Text, nullable=False)
+ date = Column(DateTime)
+ message_id = Column(String(150), unique=True, nullable=False)
+ stable_url_id = Column(String(250), unique=True, nullable=False)
+ thread_id = Column(String(150), nullable=False)
+ references = Column(Text)
+ full = Column(Text)
+
+ def __init__(self, list_name, sender, email, subject, content,
+ date, message_id, stable_url_id, thread_id, references, full):
+ """ Constructor instanciating the defaults values """
+ self.list_name = list_name
+ self.sender = sender
+ self.email = email
+ self.subject = subject
+ self.content = content
+ self.date = date
+ self.message_id = message_id
+ self.stable_url_id = stable_url_id
+ self.thread_id = thread_id
+ self.references = references
+ self.full = full
+
+ def __repr__(self):
+ return "<Email('%s', '%s','%s', '%s', '%s')>" % (self.list_name,
+ self.sender, self.email, self.date, self.subject)
+
+ def save(self, session):
+ """ Save the object into the database. """
+ session.add(self)
+
+
+class MMEmail(object):
+ """ Interface to query emails from the database. """
+
+ def __init__(self, url, debug=False):
+ """ Constructor.
+ Create the session using the engine defined in the url.
+
+ :arg url, URL used to connect to the database. The URL contains
+ information with regards to the database engine, the host to connect
+ to, the user and password and the database name.
+ ie: <engine>://<user>:<password>@<host>/<dbname>
+ ie: mysql://mm3_user:mm3_password@localhost/mm3
+ :kwarg debug, a boolean to set the debug mode on or off.
+ """
+ self.engine = create_engine(url, echo=True)
+
+
+def create(url):
+ """ Create the tables in the database using the information from the
+ url obtained.
+
+ :arg url, URL used to connect to the database. The URL contains
+ information with regards to the database engine, the host to connect
+ to, the user and password and the database name.
+ ie: <engine>://<user>:<password>@<host>/<dbname>
+ ie: mysql://mm3_user:mm3_password@localhost/mm3
+ """
+ engine = create_engine(url, echo=True)
+ Base.metadata.create_all(engine)
+
+
+if __name__ == '__main__':
+ create('postgresql://mm3:mm3@localhost/mm3')