def get_app(config_name): set_log(configs.get(config_name).level) app = Flask(__name__) # 配置 app.config.from_object(configs.get(config_name)) # 数据库 db.init_app(app) # 全局可用的redis global redis_store redis_store = redis.StrictRedis(host=configs.get(config_name).REDIS_HOST, port=configs.get(config_name).REDIS_PORT) # 开启 csrf 保护 csrf.init_app(app) # 开启Session Session(app) app.url_map.converters['re'] = RegexConverter from .api_1_0 import api app.register_blueprint(api) from ihome.web_static_html import html_blue app.register_blueprint(html_blue) return app
def get_ids(name): params = {'name': name, 'communityId': community_id} domain_id = json.loads( requests.get(configs.get('collibra dgc') + "/rest/2.0/domains", params=params, auth=(configs.get('collibra username'), configs.get('collibra password'))).content) return domain_id.get('results')[0].get('id')
def desc_actions(name, type, col_type, description): with ctx.connect(host = configs.get('host'), port = configs.get('port')) as conn: if type == "Column": tab_col = name.rsplit('.', 1) conn.execute_ddl("ALTER TABLE " + tab_col[0] + " CHANGE " + tab_col[1] + " " + tab_col[1] + " " + col_type + " COMMENT '" + description + "'") elif type == "Table": conn.execute_ddl("ALTER TABLE " + name + " SET TBLPROPERTIES ('comment' = '" + description + "')") elif type == "View": conn.execute_ddl("ALTER VIEW " + name + " SET TBLPROPERTIES ('comment' = '" + description + "')")
def get_tags(asset_id): data = json.loads( requests.get(configs.get('collibra dgc') + "/rest/2.0/tags/asset/" + asset_id, auth=(configs.get('collibra username'), configs.get('collibra password'))).content) if data: tags = [] for t in data: tags.append(t.get('name')) return tags
def get_attributes(asset_id): params = { 'typeId': "00000000-0000-0000-0000-000000003114", 'assetId': asset_id } data = json.loads( requests.get(configs.get('collibra dgc') + "/rest/2.0/attributes/", params=params, auth=(configs.get('collibra username'), configs.get('collibra password'))).content) if data.get('results'): return data.get('results')[0].get('value')
def get_assets(name, domain_id): params = { 'name': name, 'nameMatchMode': "EXACT", 'simulation': False, 'domainId': domain_id, 'communityId': community_id } data = json.loads( requests.get(configs.get('collibra dgc') + "/rest/2.0/assets", params=params, auth=(configs.get('collibra username'), configs.get('collibra password'))).content) return data.get('results')[0]
def create_app(config_name): """ app工厂函数 :param config_name: 传入现在开发的环境名称 :return: 返回app """ #调用封装的日志 setupLogging(configs[config_name].LOGGING_LEAVEL) #创建app app = Flask(__name__) #加载配置文件 config_cls = configs.get(config_name) app.config.from_object(config_cls) #创建数据库连接对象,赋值给全局db db.init_app(app) global redis_store redis_store = redis.StrictRedis(host=config_cls.REDIS_HOST, port=config_cls.REDIS_PORT) #利用flask_session,将session保存到redis中 Session(app) #为flask补充csrf防护 csrf.init_app(app) #注册蓝图 from express import api_1 app.register_blueprint(api_1.api, url_prefix="/api/v1.0") return app
def tag_actions(action, db, name, type, tags): with ctx.connect(host = configs.get('host'), port = configs.get('port')) as conn: for tag in tags: nmspc_key = tag.split(".") if action == "assign": if type == "Column": tab_col = name.split(".") conn.assign_attribute(nmspc_key[0], nmspc_key[1], db, dataset=tab_col[1], column=tab_col[2], if_not_exists=True) elif type == "Table": conn.assign_attribute(nmspc_key[0], nmspc_key[1], db, dataset=name, if_not_exists=True) elif action == "unassign": if type == "Column": tab_col = name.split(".") conn.unassign_attribute(nmspc_key[0], nmspc_key[1], db, dataset=tab_col[1], column=tab_col[2], if_not_exists=True) elif type == "Table": conn.unassign_attribute(nmspc_key[0], nmspc_key[1], db, dataset=name, if_not_exists=True)
def feed(): blogs = Blogs.find_by('order by created desc limit ?', 10) for blog in blogs: if blog.tags: blog.xtags = blog.tags.split(',') url = configs.get('blog_url') user = User.find_first('') ctx.response.content_type = 'application/xml' return dict(blogs=blogs, url=url, user=user)
def create_app(config): """ Args: config(str): 应用运行配置 """ app = Flask(__name__) app.config.from_object(configs.get(config)) register_extensions(app) register_blueprints(app) register_error_handlers(app) register_filters(app) return app
def create_app(): env = configs.get(os.environ.get('service_env', 'dev')) db = env.db app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = env.db_host app.engine = create_engine(app.config['SQLALCHEMY_DATABASE_URI']) api = Api(app) add_routes(api) from src.models.invoice import Invoice db.app = app db.init_app(app) Migrate(app, db) return app
def create_app(config_name): app = Flask(__name__) # enable CORS config_class = configs.get(config_name) app.config.from_object(config_class) Mongodb.init_app(app) setupLogging(configs[config_name].LOGGIONG_LEVEL) # WTF_CSRFprotect # CSRFProtect(app) # Register BluePrint from app import api app.register_blueprint(api.api, url_prefix="/api") CORS(app, supports_credentials=True) return app
def main(): parser = argparse.ArgumentParser(description='PyTorch Kaggle') parser.add_argument('--jobtype', '-M', type=str, default='evaluate', help='what are you going to do on this function') parser.add_argument('--setting', '-S', type=str, default='setting1', help='which setting file are you going to use for training.') #parser.add_argument('--model', '-M', type=str, default='', help='') args = parser.parse_args() #cfg = setting.Config() cfg = cfgs.get(args.setting) if args.jobtype == 'preprocess': dataset.main(cfg) if args.jobtype == 'train': train.main(cfg) if args.jobtype == 'evaluate': evaluate.main(cfg)
def create_app(env): Log.init_app(True, env == 'development') app = Flask(__name__) config = configs.get(env) app.config.from_object(config) config.init_app(app) db.init_app(app) from app.base.models import SettingModel global migrate migrate = Migrate(app, db) migrate.init_app(app, render_as_batch=True) bootstrap.init_app(app) bootstrap_cdns = app.extensions['bootstrap']['cdns'] bootstrap_cdns['bootstrap'] = bootstrap_cdns['local'] bootstrap_cdns['jquery'] = bootstrap_cdns['local'] login_manager.login_view = 'user.login' login_manager.login_message_category = 'danger' login_manager.login_message = '请登录后再进行操作' login_manager.init_app(app) from app import main, user register_controllers_from_pkg(app, main) register_controllers_from_pkg(app, user) from app import filter register_all_callable_object_from_package(app, filter, True) register_app_errors(app) return app
# _*_ coding:utf-8 _*_ from zs_backend import get_app from zs_backend import system_init from config import configs config_name = configs.get('develop') # 生成app对象 app = get_app(config_name) system_init.init(config_name.SERVER_TYPE) # @app.before_first_request # def init(): # pass if __name__ == '__main__': # print app.url_map app.run(host=config_name.HOST, port=config_name.PORT)
import os import random from flask_testing import TestCase from app import create_app from config import configs db = configs.get('test').db class BaseTestCase(TestCase): def create_app(self): os.environ['service_env'] = 'test' app = create_app() return app def setUp(self): self.create_app() from src.models.invoice import Invoice self.mocks_db(Invoice) def tearDowm(self): db.session.remove() db.drop_all() def mocks_db(self, invoice): mock = invoice(access_key='123', value='xml') db.session.add(mock) db.session.commit()
#!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = 'zhu327' import json, functools from transwarp.web import ctx from config import configs _PAGE_SIZE = configs.get('page').get('page_size') class APIError(StandardError): def __init__(self, error, data='', message=''): super(StandardError, self).__init__(message) self.error = error self.data = data self.message = message class APIValueError(APIError): def __init__(self, field, message=''): super(APIValueError, self).__init__('value:invalid', field, message) class Page(dict): def __init__(self, item_count, page_index=1, page_size=_PAGE_SIZE): self.item_count = item_count self.page_size = page_size self.page_count = item_count // page_size + (1 if item_count % page_size > 0 else 0) if (item_count == 0) or (page_index < 1) or (page_index > self.page_count): self.offset = 0 self.limit = 0
def create_app(config): app = Flask(__name__) app.config.from_object(configs.get(config)) register_extensions(app) register_blueprints(app) return app
def sitemap(): blogs = db.select('select `id` from `blogs` order by `created` desc limit ?', 100) url = configs.get('blog_url') ctx.response.content_type = 'application/xml' return dict(blogs=blogs, url=url)
__author__='Jason Zh Yan' 'main application and entry' import jinja2 import logging;logging.basicConfig(level=logging.INFO) import webtool from webtool import RequestHandler,get,post import asyncio,os,json,time import datetime from datetime import datetime import orm from aiohttp import web from config import configs from models import User,Blog,Comment import hashlib _COOKIE_KEY=configs.get('session').get('secret') COOKIE_NAME=configs.get('COOKIE_NAME') def init_jinja2(app,**kw): #set jinja2 envirment for app ( in app['__templating__'] attribute ) logging.info('init jinja2...') options=dict( #set attribute autoescape=kw.get('autoescape',True), block_start_string=kw.get('block_start_string','{%'), block_end_string=kw.get('block_end_string','%}'), variable_start_string=kw.get('variable_start_string','{{'), variable_end_string=kw.get('variable_end_string','}}'), auto_reload=kw.get('auto_reload',True) ) path=kw.get('path',None) #set path if path is None: path=os.path.join(os.path.dirname(os.path.abspath(__file__)),'templates')
import json import responses from src.tests.helper.base_test import BaseTestCase from config import configs env = configs.get('test') class TestInvoiceRoute(BaseTestCase): def test_invoice_from_db(self): payload = json.dumps({"access_key": "123"}) response = self.client.post('/invoice', data=payload) data = json.loads(response.data) assert response.status_code == 200 assert data.get('xml_value') == 'xml' def test_invoice_send_no_access_key(self): response = self.client.post('/invoice') assert response.status_code == 400 @responses.activate def test_invoice_from_arquivei(self): mock_body = dict() with open('src/tests/mock/invoice_arquivei.json') as json_file: mock_body = json.load(json_file) responses.add(responses.POST, f'{env.arquivei_url}/v1/nfe/received?cursor=0&limit=50', body=mock_body) payload = json.dumps( {"access_key": "35140330290824000104550010003715421390782397"}) response = self.client.post('/invoice', data=payload)
__author__ = 'Jason Zh Yan' ' url handlers' import logging logging.basicConfig(level=logging.INFO) from webtool import get, post import asyncio import webtool import re import json from webtool import RequestHandler from models import next_id import time import hashlib from config import configs _COOKIE_KEY = configs.get('session').get( 'secret') #is this what they call 'salt'? COOKIE_NAME = configs.get('COOKIE_NAME') #compute cookie def user2cookie(user, max_age): expires = str(int(time.time() + max_age)) s = '%s-%s-%s-%s' % (user.id, user.passwd, expires, _COOKIE_KEY) L = [user.id, expires, hashlib.sha1(s.encode('utf-8')).hexdigest()] return '-'.join(L) @post('/api/authenticate') def authenticate(*, email, passwd): if not email:
__author__='Jason Zh Yan' ' url handlers' import logging;logging.basicConfig(level=logging.INFO) from webtool import get,post import asyncio import webtool import re import json from webtool import RequestHandler from models import next_id import time import hashlib from config import configs _COOKIE_KEY=configs.get('session').get('secret') #is this what they call 'salt'? COOKIE_NAME=configs.get('COOKIE_NAME') #compute cookie def user2cookie(user,max_age): expires=str(int(time.time()+max_age)) s='%s-%s-%s-%s' % (user.id, user.passwd, expires, _COOKIE_KEY) L=[user.id, expires, hashlib.sha1(s.encode('utf-8')).hexdigest()] return '-'.join(L) @post('/api/authenticate') def authenticate(*,email,passwd): if not email: raise APIValueError('email','Invalid email') if not passwd:
import json import requests from okera import context from pymongo import MongoClient from config import configs import collections client = MongoClient(port=27017) db = client.collibra_ids community = configs.get('community') community_id = json.loads( requests.get(configs.get('collibra dgc') + "/rest/2.0/communities", params={ 'name': community }, auth=(configs.get('collibra username'), configs.get('collibra password') )).content).get('results')[0].get('id') data_dict_domain = configs.get('data_dict_domain') tech_asset_domain = configs.get('tech_asset_domain') domain_info = [data_dict_domain, tech_asset_domain] assets = [] domains = [] # pyokera calls ctx = context() ctx.enable_token_auth(token_str=configs.get('token')) with ctx.connect(host=configs.get('host'), port=configs.get('port')) as conn: databases = conn.list_databases() elements = []
def create_app(self): env = configs.get(os.environ.get('service_env', 'test')) app = create_app(env) return app
import jwt, time from config import configs config = configs.get('development') def get_default_payload(uid, username, group): payload = { 'exp': int(time.time() + 86400), 'iat': int(time.time()), 'iss': 'Li', 'data': { 'uid': uid, 'name': username, 'group': group } } return payload class Auth(): @staticmethod def encode_token(uid, username, group): ''' 生成认证Token :param uid: int :param username: string :return:string ''' try: return str(
def api_get_blogs(): blogs, page = _get_blogs_by_page(_get_page_index(), configs.get('page').get('list_size')) return dict(blogs=blogs, page=page)
# -*- coding: utf-8 -*- import hashlib import json import time import logging import re from aiohttp import web from coroweb import get, post from models import User, Comment, Blog, next_id from config import configs from apis import Page, APIValueError, APIPermissionError, APIError from markdown2 import markdown COOKIE_NAME = 'awesession' _COOKIE_KEY = configs.get('session', 'abcdefg') def check_admin(request): if request.__user__ is None or not request.__user__.admin: raise APIPermissionError() def get_page_index(page_str): p = 1 try: p = int(page_str) except ValueError: pass if p < 1: p = 1 return p
import json import requests from okera import context from pymongo import MongoClient from config import configs import collections client = MongoClient(port=27017) db = client.collibra_ids community = configs.get('community') community_id = json.loads(requests.get(configs.get('collibra dgc') + "/rest/2.0/communities", params = {'name': community}, auth = (configs.get('collibra username'), configs.get('collibra password'))).content).get('results')[0].get('id') # makes /tags/asset/{asset id} REST call def get_tags(asset_id): data = json.loads(requests.get(configs.get('collibra dgc') + "/rest/2.0/tags/asset/" + asset_id, auth = (configs.get('collibra username'), configs.get('collibra password'))).content) if data: tags = [] for t in data: tags.append(t.get('name')) return tags def get_attributes(asset_id): params = { 'typeId': "00000000-0000-0000-0000-000000003114", 'assetId': asset_id } data = json.loads(requests.get(configs.get('collibra dgc') + "/rest/2.0/attributes/", params = params, auth = (configs.get('collibra username'), configs.get('collibra password'))).content) if data.get('results'): return data.get('results')[0].get('value')
from urllib2 import URLError, HTTPError import logging, json, urllib2 from config import configs import time import datetime __author__ = 'aaron' KEY_STORE_URL = configs.get('key_store') TIME_OUT = configs.get('time_out') logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger(__name__) class Util(): @staticmethod def cal_time(date1,date2): date1 = time.strptime(date1, "%Y-%m-%d %H:%M:%S") date2 = time.strptime(date2, "%Y-%m-%d %H:%M:%S") date1 = datetime.datetime(date1[0], date1[1], date1[2], date1[3], date1[4], date1[5]) date2 = datetime.datetime(date2[0], date2[1], date2[2], date2[3], date2[4], date2[5]) return date2-date1 # def read_in_chunks(filePath, chunk_size=1024*1024): # """ # Lazy function (generator) to read a file piece by piece. # Default chunk size: 1M # You can set your own chunk size # """ # file_object = open(filePath) # while True: # chunk_data = file_object.read(chunk_size) # if not chunk_data:
# -*- coding: utf-8 -*- import hashlib import json import time import logging import re from aiohttp import web from coroweb import get, post from models import User, Comment, Blog, next_id from config import configs from apis import Page, APIValueError, APIPermissionError, APIError from markdown2 import markdown COOKIE_NAME = 'awesession' _COOKIE_KEY = configs.get('session', 'abcdefg') def check_admin(request): if request.__user__ is None or not request.__user__.admin: raise APIPermissionError() def get_page_index(page_str): p = 1 try: p = int(page_str) except ValueError: pass if p < 1: p = 1 return p
'main application and entry' import jinja2 import logging logging.basicConfig(level=logging.INFO) import webtool from webtool import RequestHandler, get, post import asyncio, os, json, time import datetime from datetime import datetime import orm from aiohttp import web from config import configs from models import User, Blog, Comment import hashlib _COOKIE_KEY = configs.get('session').get('secret') COOKIE_NAME = configs.get('COOKIE_NAME') def init_jinja2( app, **kw ): #set jinja2 envirment for app ( in app['__templating__'] attribute ) logging.info('init jinja2...') options = dict( #set attribute autoescape=kw.get('autoescape', True), block_start_string=kw.get('block_start_string', '{%'), block_end_string=kw.get('block_end_string', '%}'), variable_start_string=kw.get('variable_start_string', '{{'), variable_end_string=kw.get('variable_end_string', '}}'), auto_reload=kw.get('auto_reload', True)) path = kw.get('path', None) #set path
def _get_summary(content): summary = '\n'.join(content.split('\n')[:configs.get('page').get('summary_size')]) return markdown2.markdown(summary, extras=['fenced-code-blocks', 'code-color'])
import os from app import create_app from config import configs env = configs.get(os.environ.get('service_env', 'dev')) app = create_app(env) app.run(debug=True)
''' MVC urls ''' import re, hashlib, time from collections import OrderedDict from utils import md, get_summary from transwarp import db from transwarp.web import get, post, view, interceptor, ctx, seeother, notfound from models import User, Blogs, Tags from apis import api, APIError, APIValueError, Page from config import configs _COOKIE_NAME = 'bozsession' _COOKIE_KEY = configs.get('session').get('secret') def _get_page_index(): page_index = 1 try: page_index = int(ctx.request.get('page', '1')) except ValueError: pass return page_index def _get_blogs_by_page(page_index=1, page_size=None): total = Blogs.count_all() if page_size: page = Page(total, page_index, page_size=page_size) else: page = Page(total, page_index)