def write(self): output_dir = os.path.join(self.output_dir, self.get_csvdirname()) data = self.get_data() if data is None: print 'data not found..' else: # csv作成. csvfilename = self.make_csvfilename() def makeRow(row): arr = [] for v in row: s = u'%s' % v s = s.replace('"', '""') arr.append(u'"%s"' % s) return u','.join(arr) rows = [makeRow(row) for row in data] csv_data = StrUtil.to_s(u'\n'.join(rows), dest_enc='shift-jis') if not os.path.exists(output_dir): os.mkdir(output_dir) filepath = os.path.join(output_dir, csvfilename) self.write_file(filepath, csv_data) print 'Output:%s' % filepath strnow = OSAUtil.get_now().strftime("%Y%m%d") if strnow != self.date.strftime("%Y%m%d"): self.delete()
def __write_json_obj(self, dict_data): """jsonレスポンス. """ close_connection() json_data = StrUtil.to_s(Json.encode(dict_data)) json_data = json_data.replace(': ', ':').replace(', ', ',') json_data = json_data.replace('\\r', '') self.response.headers['Content-Type'] = 'application/json;' self.response.send(json_data)
def processAppError(self, err): if err.code == CabaretError.Code.NOT_AUTH: # ログインして下さい. url = self.makeAppLinkUrlAdmin(UrlMaker.login()) self.appRedirect(url) elif err.code == CabaretError.Code.NOT_ALLOWED_IP: # 許可されていないIP. self.html_param['permission_error_text'] = err.value self.writeAppHtml('permission_error') else: self.processError( StrUtil.to_s(err.getHtml(self.osa_util.is_dbg_user)))
def to_python(self, value): """ loadsしてみて成功したら結果を返す。 失敗したらそのまま返す。 """ if isinstance(value, unicode): value = StrUtil.to_s(value) if isinstance(value, str): try: value = Json.decode(value) except: pass return value
def to_python(self, value): """ loadsしてみて成功したら結果を返す。 失敗したらそのまま返す。 つまりload可能な文字列オブジェクトはloadされてしまうためそのまま保存できない。 """ if isinstance(value, unicode): value = StrUtil.to_s(value) if isinstance(value, str): try: return cPickle.loads(value) except (cPickle.UnpicklingError, EOFError): return value return value
def output_json(self, output_dir, model_param_list): """jsonを出力. """ json_data = {} for model_param in model_param_list: fields = [field._asdict() for field in model_param.fields] model_param_dict = model_param._asdict() model_param_dict['fields'] = fields json_data[model_param.name] = model_param_dict json = Json.encode(json_data) filepath = os.path.join(output_dir, 'tables.json') self.write_file(filepath, StrUtil.to_s(json)) print 'Output json...{}'.format(output_dir)
def output(self, overwrite=False): if self._data: self._data.append('\n') data_str = StrUtil.to_s('\n'.join(self._data), 'shift-jis') if overwrite: mode = 'w' else: mode = 'a' f = None try: f = open(self._path, mode) f.write(data_str) f.close() except: if f: f.close() raise
def __proc_csv(self): """スプレッドシートで使える形式でcsv吐き出し. """ def makeRow(sequence): return ','.join(sequence) key_table = {} for column in self.model_cls.get_column_names(): key_table[column.replace('_', '')] = column for column in self.model_form_cls.get_extra_fields(): key_table[column.replace('_', '')] = column csv_row_list = [] model_all = self.getModelAll() columns = AdminModelEditHandler.__make_sheet_columns( self.model_form_cls) csv_row_list.append(makeRow(['"%s"' % column for column in columns])) for model in model_all: data_list = [] for column in columns: attrname = key_table[column] if hasattr(model, attrname): v = getattr(model, attrname) elif hasattr(model, 'get_%s' % attrname): v = getattr(model, 'get_%s' % attrname)() else: v = 'Unknown' if type(v) in (list, tuple, dict): t = Json.encode(v) else: t = str(v) t = t.replace('"', '""') #escape. data_list.append('"%s"' % t) csv_row_list.append(makeRow(data_list)) # 書き込む. csv_data = '' for row in csv_row_list: csv_data += '%s\n' % row csv_data = StrUtil.to_s( csv_data, dest_enc='shift-jis') #Excelで開くと文字化けするのでshift-jisに. self.osa_util.write_csv_data(csv_data, self.__get_csv_name())
def sendmessage_to_chatwork(self, message): try: SERVER_URL = "https://api.chatwork.com/v1" url = '{}/rooms/21193248/messages'.format(SERVER_URL) data = urllib.urlencode({ "body": u"[To:1984418] 藤竿 高志(Takashi FUJISAO)さん\n[To:1980432] 山田 健太(YAMADA Kenta)さん\n[To:1973173] 川合 佑輔(Yusuke Kawai)さん\n[To:2004847] 酒井 崇(Takashi SAKAI)さん\n[To:1973150] 秋谷 亮(RYO Akitani)さん\n{}" .format(message) }) req = urllib2.Request(url) req.get_method = lambda: 'POST' req.add_header('X-ChatWorkToken', '2ab7238c4d2920a9714742389a0a8eb4') req.add_data(StrUtil.to_s(data)) urllib2.urlopen(req) except: if settings_sub.IS_DEV: raise
def __add_model(self, model): """モデルをCSV文字列に変換. """ model_cls = model.__class__ cls_name = model_cls.__name__ tar_model_table = self.__model_table.get(cls_name, None) if tar_model_table is None: # 新規登録. self.__model_table[cls_name] = '' # モデルのクラスを登録. if self.__cls_table.get(cls_name) is None: self.__cls_table[cls_name] = model_cls self.__cls_name.append(cls_name) csv_data_list = [] for field in model_cls._meta.fields: # フィールドに設定されている値. value = getattr(model, field.attname) if isinstance(field, ObjectField): value = field.get_db_prep_save(value, None) if value is None: value = 'NULL' elif isinstance(value, datetime.datetime): value = "%04d-%02d-%02d %02d:%02d:%02d" % ( value.year, value.month, value.day, value.hour, value.minute, value.second) elif isinstance(value, bool): value = int(value) elif isbasestring(value): # 無理やりだけどバイナリデータをエスケープ. value = MySQLdb.escape_string(StrUtil.to_s(value)) csv_data_list.append('"%s"' % value) # 書き込む. csv_text = ModelCSVManager.STR_KUGIRI.join(csv_data_list) + '\r\n' self.__model_table[cls_name] += csv_text if ModelCSVManager.SIZE_MAX <= len(self.__model_table[cls_name]): return True else: return False
def process(self): args = self.getUrlArgs('/promotionconditionget/') try: appname = args.get(0, '') requirement_idlist = [ int(s) for s in args.get(1, '').split(',') if s.isdigit() ] except: self.__sendErrorResponse(404) return model_mgr = self.getModelMgr() config = BackendApi.get_promotionconfig(model_mgr, appname, using=settings.DB_READONLY) if config is None: # アプリが存在しない. self.__sendErrorResponse(404) return if requirement_idlist: requirementlist = BackendApi.get_promotionrequirementmaster_list( model_mgr, appname, requirement_idlist, using=settings.DB_READONLY) else: # 全て検索. requirementlist = BackendApi.get_promotionrequirementmaster_all( model_mgr, appname, using=settings.DB_READONLY) json_obj = {} for requirement in requirementlist: text = requirement.text json_obj[str(requirement.id)] = text json_data = StrUtil.to_s(Json.encode(json_obj, ensure_ascii=False)) self.response.set_status(200) self.osa_util.write_json_data(json_data)
def handle(self, *args, **options): print '================================' print 'copy_img' print '================================' out = args[0] # ガチャのマスターを取得. gachalist = GachaMaster.fetchValues(filters={ 'consumetype__in': Defines.GachaConsumeType.PAYMENT_TYPES, 'schedule__gt': 0 }, using=backup_db) # ガチャのボックスデータを作成. read_boxids = [] card_dict = {} for gacha in gachalist: if gacha.boxid in read_boxids: continue schedule = ScheduleMaster.getByKey(gacha.schedule, using=backup_db) name = u'%s(%s-%s)' % (gacha.name, schedule.stime.strftime('%m/%d'), schedule.stime.strftime('%m/%d')) boxmaster = GachaBoxMaster.getByKey(gacha.boxid, using=backup_db) gachamasterset = GachaMasterSet(gacha, boxmaster, schedule) gachabox = GachaBox(gachamasterset, GachaPlayData.makeInstance( GachaPlayData.makeID(0, gacha.boxid)), blank=True) grouplist = GachaGroupMaster.getByKey(gachabox.get_group_id_list(), using=backup_db) # カードIDとガチャのIDをひもづける. for group in grouplist: if 1 < len(group.table): continue cardid = group.table[0]['id'] arr = card_dict[cardid] = card_dict.get(cardid) or [] arr.append(name) # カードマスターを取得. cardmasterlist = CardMaster.getByKey(card_dict.keys(), order_by='id', using=backup_db) # CSVを作成. rows = [] def makeRow(row): arr = [] for v in row: s = u'%s' % v s = s.replace('"', '""') arr.append(u'"%s"' % s) return u','.join(arr) for cardmaster in cardmasterlist: cardsortmaster = CardSortMaster.getByKey(cardmaster.id, using=backup_db) row = [ cardmaster.id, cardmaster.name, Defines.Rarity.NAMES[cardsortmaster.rare], Defines.CharacterType.NAMES[cardsortmaster.ctype] ] row.extend(card_dict[cardmaster.id]) str_row = makeRow(row) print str_row rows.append(str_row) csv_data = StrUtil.to_s(u'\n'.join(rows), dest_enc='shift-jis') f = None try: f = open(out, "w") f.write(csv_data) f.close() except: if f: f.close() f = None raise print '================================' print 'output:%s' % out print 'all done..'
def __getResponseApi(self, funcMakeRequest): ope_url = self.get_url() post_data = self.get_postdata() queryparams = self.get_queryparams() http_method = self.get_http_method() if type(post_data) == unicode: post_data = StrUtil.to_s(post_data, 'utf-8') self.osa_util.logger.trace('ope_url:' + ope_url) request = funcMakeRequest(queryparams) headers = request.to_header() xoauth_requestor_id = queryparams.get('xoauth_requestor_id') if xoauth_requestor_id: headers['Authorization'] = '%s, xoauth_requestor_id="%s"' % ( headers['Authorization'], xoauth_requestor_id) # del queryparams['xoauth_requestor_id'] headers['Content-Type'] = self.get_content_type() # Query the user's personal info and print them if queryparams: uri = '%s?%s' % (request.get_normalized_http_url(), urllib.urlencode(queryparams)) else: uri = request.get_normalized_http_url() body = self.get_body() logs = [ "HTTP Method:%s" % http_method, "API_URI:%s" % uri, ] logs.append('request headers:') logs.extend(['\t%s: %s' % (k, v) for k, v in headers.items()]) logs.append('request body:') logs.append(body) log = '\n'.join(logs) self.osa_util.logger.trace(log) if self._is_batch_mode: timeout = None else: timeout = 3 tmp = None try: tmp = self.osa_util.httpopen(uri, body, http_method, headers, self.get_certificate(), timeout=timeout) response = tmp.read() self.osa_util.logger.info('getResponseApi response:%s' % response) return response except urllib2.HTTPError, er: if tmp is not None and tmp.fp: tmp.fp._sock.recv = None if self.osa_util.is_dbg: if er.fp is None: error_message = str(er) else: error_message = er.read() self.osa_util.logger.error(self.osa_util.logger.to_string()) self.osa_util.logger.error('getResponseApi er_code:%s %s' % (er.code, error_message)) if er.code == 401: info = str(er) now = OSAUtil.get_now(timezone.TZ_DEFAULT) delta = self.osa_util.getElpsedTime() str_body = None try: str_body = '%s' % post_data except: self.osa_util.logger.error( 'str_body can not translate... %s' % ErrorUtil.getLastErrorMessage()) pass er_response = '' if er.fp is not None: er_response = er.read() temp_log = ('getResponseApi log:%s' % log + 'now:%s\n' % now + 'elapsed time: %d.%06d sec.\n' % (delta.seconds, delta.microseconds) + 'body:%s' % str_body + 'er info:%s\n' % info + 'er read:%s\n' % er_response + 'error:%s\n' % er) self.osa_util.logger.warning(temp_log) return self.processHttpError(er)