예제 #1
0
파일: ipc.py 프로젝트: stanta/ipc-parser-1
    def get_indexes(self, host, db, login, password, level=None):
        res = None
        db = SimpleMysql(
            host=host,
            db=db,
            user=login,
            passwd=password
        )
        if level is None:
            res = db.getAll('ipc_indexes')
        else:
            res = db.getAll('ipc_indexes', '*', ("level=%s", [level]))

        ind = []

        if res is None:
            print "No indexes! at level " +str(level)
            sys.exit()
            return ind

        for r in res:
            if r.level == 4:
                continue
            item = IpcIndex()
            item.id = r.id
            item.level = r.level
            item.word = r.word
            item.code = r.code
            item.category = r.category
            ind.append(item)

        db.conn.commit()
        db.conn.close()
        return ind
예제 #2
0
파일: ipc.py 프로젝트: stanta/ipc-parser-1
    def build_indexes(self, host, db, login, password):
        db = SimpleMysql(
            host=host,
            db=db,
            user=login,
            passwd=password
        )
        #Check for table
        res = db.query('SHOW TABLES LIKE "ipc_indexes"')
        tableExists = res.rowcount
        if tableExists == 0:
            print "Creating table.."
            tableCode = '''
CREATE TABLE ipc_indexes (
  id int(11) NOT NULL AUTO_INCREMENT,
  level int(11) DEFAULT NULL,
  word varchar(255) DEFAULT NULL,
  category varchar(512) DEFAULT NULL,
  code varchar(255) DEFAULT NULL,
  PRIMARY KEY (id),
  INDEX UK_ipc_indexes_code (code),
  INDEX UK_ipc_indexes_word (word),
  INDEX IDX_ipc_indexes_level (level)
)
ENGINE = INNODB
AUTO_INCREMENT = 2142
AVG_ROW_LENGTH = 224
CHARACTER SET utf8
COLLATE utf8_general_ci;
'''
            db.query(tableCode)
        else:
            db.query('TRUNCATE TABLE ipc_indexes')
        db.conn.commit()
        #Build indexes
        #LEVEL 2
        # :type entry: ipc.IpcEntry
        for rootItem in self.entries:
            #Skip level 1
            for entry in rootItem.children:
                for titlePart in entry.titleParts:
                    if not len(titlePart.replace("\n", "").replace(u'\xa0', u'')):
                        continue
                    db.insert('ipc_indexes', {
                        "level": 2,
                        "word": titlePart[:255],
                        "code": entry.symbol,
                        "category": entry.title[:235]
                    })
                #level 3
                for entry3 in entry.children:
                    for titlePart in entry3.titleParts:
                        if not len(titlePart.replace("\n", "").replace(u'\xa0', u'')):
                            continue
                        db.insert('ipc_indexes', {
                        "level": 3,
                        "word": titlePart[:255],
                        "code": entry3.symbol,
                        "category": entry3.title[:235]
                        })
                    #level 4
                    for entry4 in entry3.children:
                        for titlePart in entry4.titleParts:
                            if not len(titlePart.replace("\n", "").replace(u'\xa0', u'')):
                                continue
                            db.insert('ipc_indexes', {
                            "level": 5 if entry4.kind == "m" else 4,
                            "word": titlePart[:255],
                            "code": entry4.symbol,
                            "category": entry4.title[:235] if entry4.kind == "m" else entry3.title[:235]
                            })
                        #level 5 wtfwtf
                        '''for entry5 in entry4.children:
                            for titlePart in entry5.titleParts:
                                if not len(titlePart.replace("\n","").replace('\xa0', '')):
                                    continue
                                db.insert('ipc_indexes', {
                                "level": 5,
                                "word": titlePart[:255],
                                "code": entry5.symbol,
                                "category": entry5.title[:235]
                                })'''
        db.conn.commit()
        db.conn.close()
        return True