def update_images(self, collection_name, _id, replaces): data = self.get(collection_name, _id) if not data: raise MyException('can not get data from mongodb: [%s]' % (_id)) last_text_idx = 0 for idx, s in enumerate(data['sections']): if s['type'] == 'text': last_text_idx = idx new_sections = [] for idx, s in enumerate(data['sections']): if s['type'] == 'img': if s['src'] in replaces: r = replaces[s['src']] if idx > last_text_idx and r['width'] == r['height']: print 'skip image: [%s]' % r['src'] continue new_sections.append(r) else: new_sections.append(s) data['sections'] = new_sections collection_name, data_id = self.handler.save(data) if not collection_name or not data_id: raise MyException('update images failed') return True
def calc_dir(self, struct): try: inlist = struct['inlist'] direct = self.data['direct'] taglist = struct['taglist'] for _tag in taglist: if _tag['type'] == 'F': if _tag['dir'] == 'OFF': continue self.dirs = _tag['dir'] if _tag['type'] == 'X': self.dirs = _tag['dir'] if struct.has_key('F1'): if struct['F1']['dir'] != 'OFF': self.dirs = struct['F1']['dir'] if struct.has_key('M1'): if struct['M1']['dir'] != 'OFF': self.dirs = struct['M1']['dir'] struct['dir'] = direct[self.dirs] if struct['dir'] == 'NEW': struct['dir'] = '+' if struct.has_key('X1'): if struct['X1']['dir'] != 'OFF' and struct['X1']['dir'] == '!': if struct['dir'] == '+': struct['dir'] = '-' elif struct['dir'] == '-': struct['dir'] = '+' except Exception as e: raise MyException(format(e))
def wrapper(self, *args, **kwargs): try: if not self.request.body is None: logging.info(self.request.body) self.body_json = json.loads(self.request.body) except Exception, e: raise MyException(format(e))
def _del(self,data): return ; try: x1data = self.data['Z']; remove(x1data,data); except Exception as e: raise MyException(format(e));
def _add(self,data): return ; try: x1data = self.data['Z']; insert(x1data,data,'Z'); except Exception as e: raise MyException(format(e));
def encode(self, struct): try: # self._fetch_num(struct); self._fetch_eng(struct) self._fetch_num_unit(struct) except Exception: raise MyException(sys.exc_info())
def _add(self, data): try: xdata = self.data['X'] if data.has_key('dir') and data.has_key('value'): tdir = data.get('dir') value = data.get('value') if not xdata.has_key(tdir): xdata[tdir] = list() if value in xdata[tdir]: pass else: xdata[tdir].append(value) else: raise MyException('X: input data has no key [dir|value]') except Exception as e: raise MyException(format(e))
def encode(self, struct): try: self.check_input(struct) xdata = self.data['X'] self.__match_x(xdata, struct) except Exception as e: raise MyException(format(e))
def encode(self,struct): try: self.check_input(struct); f1data = self.data['F1']; match(f1data,struct,'F1'); except Exception as e: raise MyException(format(e));
def insert(d1data,data,typec): try: if data.has_key('value') and data.has_key('dir'): reg = data.get('value'); dirs = data.get('dir'); for rdata in d1data: if reg == rdata['reg']: return; tdic = dict(); tdic['type'] = typec; tdic['reg'] = reg; tdic['dir'] = dirs; d1data.append(tdic); else: raise MyException('%s data has no key [dir | value]' % typec); except Exception as e: raise MyException(format(e));
def encode(self,struct): return; try: self.check_input(struct); m1data = self.data['Z']; match(m1data,struct,'Z'); except Exception as e: raise MyException(format(e));
def encode(self, struct): try: self.text = None # self._wsvd(struct); self._esvd(struct, self.encomp) # struct['stseg'] = self.text.split(' '); except Exception: raise MyException(sys.exc_info())
def _check(self,struct): try: self.check_input(struct); inlist = struct['inlist']; taglist = struct['taglist']; reg = ''; for _tag in taglist: if type(_tag) == dict: reg = reg + _tag['type']; elif _tag in inlist: raise ValueError('the word %s is unknow' % _tag); regs = self.data['reg']; if reg in regs: struct['reg'] = reg; else: raise MyException('can`t find the reg[%s] from PM file' % reg); except Exception as e: raise MyException(format(e));
def connect(self): self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.connect((self.ip, self.port)) try: msg = self.receive() if msg.startswith('TS3'): if 'Welcome' not in msg: self.receive() self.send('use %s\n\r' % (self.sid, )) if not self.receive().endswith('msg=ok\n\r'): raise MyException("ServerID not found") self.send('login ' + self.user + ' ' + self.password) if not self.receive().endswith('msg=ok\n\r'): raise MyException("username/password wrong") else: raise MyException("this isn't a TS3-Server, right?") except (socket.error, socket.timeout): self.disconnect()
def _del(self,data): try: fdata = self.data; regs = fdata.get('reg'); invalue = data.get('value'); if invalue in regs: del regs[invalue]; except Exception as e: raise MyException(format(e));
def write_file(self, dfile): try: os.rename(dfile, dfile + '.1') data = json.dumps(self.data, indent=2, ensure_ascii=False) fd = open(dfile, 'w') fd.write(data) fd.close() except Exception as e: raise MyException(format(e))
def _add(self, data): try: word = data.get('value') mdata = self.data['M'] if word in mdata: return mdata.append(word) except Exception as e: raise MyException(format(e))
def _del(self, data): try: fdata = self.data fkeys = fdata.keys() invalue = data.get('value') if invalue in fkeys: del fdata[invalue] except Exception as e: raise MyException(format(e))
def _del(self, data): try: mdata = self.data['M'] invalue = data.get('value') if invalue in mdata: idx = mdata.index(invalue) del mdata[idx] return except Exception as e: raise MyException(format(e))
def _add(self,data): try: fdata = self.data; regs = fdata.get('reg'); if data.has_key('value'): value = data.get('value'); if value in regs: return; regs.append(value); except Exception as e: raise MyException(format(e));
def _del(self, data): try: xdata = self.data['X'] invalue = data.get('value') for key in xdata.keys(): tdata = xdata[key] if invalue in tdata: idx = tdata.index(invalue) del tdata[idx] break except Exception as e: raise MyException(format(e))
def encode(self, struct, tables): try: if not struct.has_key('stc'): struct['stc'] = dict() for key in tables: if not struct.has_key(key): continue self.fetch_cept(struct['stc'], struct[key]) del struct[key] self._fetch_dkey(struct, 'SomeNum', None) self._fetch_ckey(struct, 'SomeEngs', None) self._fetch_ckey(struct, 'SomeNunit', None) except Exception as e: raise MyException(sys.exc_info())
def save(self, doc): doc_mongo = { '_id': self.mkid(doc['url']), 'title': doc['title'], 'url': doc['url'], 'update_time': doc['publish_time'], 'sections': doc['sections'] } collection_name, data_id = self.handler.create(doc_mongo) doc['collection_name'] = collection_name doc['data_id'] = str(data_id) if not collection_name or not data_id: raise MyException('save data failed') return True
def calc_level(self, struct): try: inlist = struct['inlist'] level = self.data['value'] taglist = struct['taglist'] for _tag in taglist: if _tag['type'] == 'C': self.level = _tag['level'] if struct.has_key('M1'): m1 = struct['M1'] if m1['reg'].find('C') != -1: self.level = u'中' struct['value'] = level[self.level] except Exception as e: raise MyException(format(e))
def encode(self, struct): try: self.check_input(struct) inlist = struct['inlist'] mdata = self.data['M'] taglist = struct['taglist'] for st in inlist: if st in mdata: tdic = dict() tdic['type'] = 'M' tdic['value'] = st taglist.append(tdic) else: taglist.append(st) except Exception as e: raise MyException(format(e))
def read_json(dfile): fid = open(dfile, 'r') ondata = list() while True: line = fid.readline() if not line: break line = line.strip('\n') if len(line) <= 0 or line[0] == '#': continue ondata.append(line) all_test = ''.join(ondata) try: ojson = json.loads(all_test) return ojson except Exception as e: raise MyException('load json file failed[' + dfile + ']')
def _add(self, data): try: fdata = self.data if data.has_key('dir') and data.has_key('value'): dirs = data.get('dir') value = data.get('value') if value in fdata.keys(): return tdic = dict() tdic['name'] = value tdic['nickname'] = value if dirs == u'有' or dirs == u'无': tdic['dimension'] = u'二元' else: tdic['dimension'] = u'一维' tdic['dir'] = dirs fdata[value] = tdic except Exception as e: raise MyException(format(e))
def encode(self, struct): try: self.check_input(struct) inlist = struct['inlist'] taglist = struct['taglist'] keys = self.data.keys() for tt in inlist: if tt in keys and tt in taglist: idx = taglist.index(tt) del taglist[idx] data = self.data[tt] tdic = dict() tdic['type'] = 'F' tdic['value'] = tt tdic['dir'] = data['dir'] #tdic.update(data); taglist.insert(idx, tdic) except Exception as e: raise MyException(format(e))
def encode(self, struct): try: self.check_input(struct) inlist = struct['inlist'] keys = self.data.keys() taglist = struct['taglist'] #foreach inlist for tt in inlist: #foreach self.data.keys for key in keys: data = self.data[key] #if the dic self.data contain tt if tt in data and tt in taglist: idx = taglist.index(tt) del taglist[idx] tdic = dict() tdic['type'] = 'C' tdic['value'] = tt tdic['level'] = key taglist.insert(idx, tdic) break except Exception as e: raise MyException(format(e))
def encode(self, struct): try: self._mark_unit(struct) except Exception: raise MyException(sys.exc_info())