Permalink
Browse files

add sms name and type regex

  • Loading branch information...
1 parent 937bf78 commit 2108761faa379f975e25fd9fc36f8d6b9f0a218f @alswl committed Jan 8, 2012
Showing with 74 additions and 90 deletions.
  1. +1 −1 config.py
  2. +20 −16 config.yml
  3. 0 data/__init__.py
  4. +6 −4 model/contact.py
  5. +0 −19 model/meta.py
  6. +15 −8 model/sms.py
  7. +15 −2 parser/best_message_storer_parser.py
  8. +7 −7 parser/parser.py
  9. +0 −16 parser/textparser.py
  10. +10 −17 smsir.py
View
@@ -10,7 +10,7 @@
config = yaml.load(open('config.yml', 'r'))
-engine = create_engine(config['data'], echo=True) # set data connect string
+engine = create_engine(config['main']['data'], echo=True) # set data connect string
# SQLAlchemy session manager. Updated by model.init_model()
Session = scoped_session(sessionmaker())
View
@@ -1,18 +1,22 @@
-version: 1
-formatters:
- simple:
- format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
-handlers:
- console:
- class: logging.StreamHandler
- level: DEBUG
- formatter: simple
- stream: ext://sys.stdout
-loggers:
- simple:
- level: DEBUG
-root:
- level: DEBUG
- handlers: [console]
+main:
+ data: 'sqlite:///data/data.db'
+
+logging:
+ version: 1
+ formatters:
+ simple:
+ format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
+ handlers:
+ console:
+ class: logging.StreamHandler
+ level: DEBUG
+ formatter: simple
+ stream: ext://sys.stdout
+ loggers:
+ simple:
+ level: DEBUG
+ root:
+ level: DEBUG
+ handlers: [console]
# vim: set ft=yaml:
View
No changes.
View
@@ -1,18 +1,20 @@
#!/usr/bin/env python
# coding=utf8
-from sqlalchemy import Column, Integer, String, Unicode
+from sqlalchemy import Column, Integer, String, Unicode, TIMESTAMP
from sqlalchemy.orm import relationship, backref
-from meta import Base, session
+from config import Base, session
class Contact(Base):
"""contact model"""
__tablename__ = 'contact'
id = Column(Integer, primary_key=True)
name = Column(Unicode(50), nullable=False)
- phone = Column(Integer(20), nullable=False)
+ phone = Column(String(20), nullable=False)
+ create_at = Column(TIMESTAMP)
- def __init__(self, name):
+ def __init__(self, name=None, phone=None):
self.name = name
+ self.phone = phone
View
@@ -1,19 +0,0 @@
-# coding=utf8
-
-import logging
-import logging.config
-import yaml
-
-from sqlalchemy.ext.declarative import declarative_base
-from sqlalchemy.orm import scoped_session, sessionmaker
-from sqlalchemy import create_engine
-
-engine = create_engine('sqlite:///data.db', echo=True)
-
-# SQLAlchemy session manager. Updated by model.init_model()
-Session = scoped_session(sessionmaker())
-Session.configure(bind=engine)
-session = Session()
-
-# The declarative Base
-Base = declarative_base()
View
@@ -1,27 +1,34 @@
#!/usr/bin/env python
# coding=utf8
-from sqlalchemy import Column, Integer, String
+from sqlalchemy import Column, Integer, String, TIMESTAMP
from sqlalchemy import ForeignKey
from sqlalchemy.orm import relationship, backref
-from meta import Base, session
+from config import Base, session
class Sms(Base):
"""sms model"""
__tablename__ = 'sms'
id = Column(Integer, primary_key=True)
from_contact_id = Column(Integer, ForeignKey('contact.id'))
- from_contact = relationship('Contact', backref='sms')
- #to_contact_id = Column(Integer, ForeignKey('contact.id'))
- #to_contact = relationship('Contact', backref='sms')
+ to_contact_id = Column(Integer, ForeignKey('contact.id'))
+ from_contact = relationship(
+ 'Contact',
+ primaryjoin='Contact.id == Sms.from_contact_id',
+ )
+ to_contact = relationship(
+ 'Contact',
+ primaryjoin='Contact.id == Sms.to_contact_id',
+ )
content = Column(String(500))
- # TODO create_at
+ create_at = Column(TIMESTAMP)
- def __init__(self):
- pass
+ def __init__(self, from_contact=None, to_contact=None):
+ self.from_contact = from_contact
+ self.to_contact = to_contact
def add(self):
session.add(self)
@@ -6,13 +6,17 @@
# date: 2011-01-07
import logging
+import re
from model.sms import Sms
from parser import Parser, IllegalFormatError
logger = logging.getLogger(__name__)
class Best_message_storer_parser(Parser):
+ name_re = re.compile(ur'[发件人|收件人]: (\+?[\u2e80-\uffff|\s|\w|\d]+)\n')
+ type_re = re.compile(ur'文件夹: ([\u2e80-\uffff|\s]+)\n')
+
def __init__(self, text):
self.text = text
self.smses = []
@@ -22,7 +26,7 @@ def parse(self):
try:
self.process_block(block)
except IllegalFormatError, e:
- logger.info('%s is not illegal format' %block)
+ #logger.info('%s is not illegal format' %block) # FIXME
continue
return self.smses
@@ -33,5 +37,14 @@ def process_block(self, text):
raise IllegalFormatError()
meta = splits[0]
content = splits[1].strip()
- logger.debug(content)
+
+ type = self.type_re.search(meta).group(1)
+ name = self.name_re.search(meta).group(1) # TODO
+ logger.debug(name)
+ if type == u'发出的信息':
+ pass
+ elif type == u'收件箱':
+ pass
+ else:
+ raise IllegalFormatError()
# self.smses.append(sms)
View
@@ -5,16 +5,16 @@
# author: alswl
# date: 2011-01-07
-from sms import Sms
+from model.sms import Sms
-class TextParser(object):
+class Parser(object):
def __init__(self):
pass
def parse(self, text):
- sms = Sms()
- for block in text.split('================================' \
- '======================================'):
- print line
-
pass
+
+class IllegalFormatError(ValueError):
+ """非法格式"""
+ pass
+
View
@@ -1,16 +0,0 @@
-#!/usr/bin/env python
-#coding=utf-8
-
-# desc: Parser
-# author: alswl
-# date: 2011-01-07
-
-from model.sms import Sms
-
-def parse(text):
- sms = Sms()
- for block in text.split('================================' \
- '======================================'):
- print block
-
- pass
View
@@ -7,10 +7,10 @@
from sqlalchemy import create_engine
import yaml
-from model.meta import Base, session, engine
+from config import Base, session, engine
from model.sms import Sms
from model.contact import Contact
-import parser.textparser
+from parser.best_message_storer_parser import Best_message_storer_parser
logger = logging.getLogger(__name__)
@@ -22,10 +22,6 @@ def __init__(self):
self.session = session
self.engine = engine
- # Loging
- config = yaml.load(open('config.yml', 'r'))
- logging.config.dictConfig(config)
-
def run(self):
parser = argparse.ArgumentParser(
description='a sms console helper to manage sms'
@@ -51,21 +47,20 @@ def run(self):
elif args.list_messages:
self.list_messages()
elif args.import_sms_backup_restore != None:
- self.import_sms_backup_restore(args.import_sms_backup_restore.read())
+ pass
elif args.import_best_message_storer != None:
- self.import_best_message_storer(args.import_best_message_storer.read())
+ best_message_storer_parser = Best_message_storer_parser(
+ args.import_best_message_storer.read().decode('utf-16le')
+ )
+ smses = best_message_storer_parser.parse()
+ session.add_all(smses)
+ session.commit()
else:
parser.print_help()
def create_all(self):
self.Base.metadata.create_all(self.engine)
- def import_from_xml(self):
- pass
-
- def import_from_txt(self):
- pass
-
def list_messages(self):
print 'messages'
pass
@@ -77,9 +72,7 @@ def import_best_message_storer(self, texts):
textparser.parse(texts)
def test(self):
- sms = Sms()
- sms.from_contact = Contact(u'路飞')
- sms.add()
+ pass
def main():
smsir = Smsir()

0 comments on commit 2108761

Please sign in to comment.