def redis_config(): _params = dict( address = config.get('REDIS', 'address'), db = config.get('REDIS', 'db'), minsize = config.get('REDIS', 'min'), maxsize = config.get('REDIS', 'max') ) _password = config.get('REDIS', 'password') if _password: _params['password'] = _password return _params
async def _start(self, loop): await listen(config.get(self.NAME, 'host_local'), config.get(self.NAME, 'port')) try: _backdoor_port = config.get(self.NAME, 'adminport') if isinstance(_backdoor_port, int) and _backdoor_port > 1024: from backdoor import listen as blisten await blisten(int(_backdoor_port)) else: raise Exception('unknown backdoor port:{0}'.format(_backdoor_port)) except Exception as e: log.warn('can not open backdoor. error:{0}'.format(e)) load_all_routes(self.NAME)
def __init__(self): self.jira_ticket_url = None self.jira_exists = None if len(sys.argv) != 2: exit("Error: please enter exactly 1 CLI argument - AWS profile name") # boto3 client for aws - we take the account alias from telegraf's first CLI argument self.iam = boto3.session.Session(profile_name=sys.argv[1]).client('iam') self.dynamo_client = boto3.session.Session(profile_name=config["aws_dynamodb_profile_name"], region_name=config["aws_dynamodb_region"]).client("dynamodb") # check if env variable is logging info level if os.environ.get("LOGGING_LEVEL") == "INFO": logging.basicConfig(level=logging.INFO) else: # log the config logging.basicConfig(level=logging.WARNING) if config.get("expire_age") == '' or config.get("expire_age") is None: config["expire_age"] = 180 logging.info("Program configs are: Expire_age={}, Slack={}, dump_metric_to_console={}, Jira={}, " "Disabling_expired_keys={}".format(config["expire_age"], config["slack"], config["dump_metric_to_console"], config["jira_ticket"], config["disable_key"])) self.accountAlias = sys.argv[1] # fetching api keys from credstash self.ATLASSIAN_API_TOKEN = config["atlassian_api_token"] self.SLACK_API_TOKEN = config["slack_api_token"] self.key_rotation_string = """ To rotate your key you must: You can rotate your access key using the `awscli`: `aws iam create-access-key --user-name <user_name>` `aws iam update-access-key --access-key-id <old_key_id> --status Inactive --user-name <user_name>` `aws iam delete-access-key --access-key-id <old_key_id> --user-name <user_name>` For more information visit aws.amazon.com/blogs/security/how-to-rotate-access-keys-for-iam-users/ """ self.run()
def get_monthly_history(): users = config.get('WITHINGS', 'users').split(',') history_list_header = ['Month'] for user in users: history_list_header += ['', user[0] + ' avg', user[0] + ' min', user[0] + ' max', user[0] + ' #'] history_list_header = [history_list_header] history_list = [] with get_session_manager() as session_manager: (min_date, ) = session_manager.query(Weights.date).order_by(Weights.date).first() min_date = datetime.date(min_date.year, min_date.month, 1) while min_date < datetime.date.today(): month_string = '{year}-{month:0>2}'.format(year=min_date.year, month=min_date.month) month_count = 0 month_row = [month_string] for user in users: (avg_weight, min_weight, max_weight, count_weight) = \ session_manager.query(func.avg(Weights.weight), func.min(Weights.weight), func.max(Weights.weight), func.count(Weights.weight)).\ filter(and_(Weights.user == user, Weights.date.like(month_string + '%'))).first() month_count += count_weight month_row += ['', avg_weight, min_weight, max_weight, count_weight] if month_count > 0: history_list += [month_row] min_date = min_date + datetime.timedelta(days=32) min_date = datetime.date(min_date.year, min_date.month, 1) history_list = sorted(history_list, reverse=True) history_list = history_list_header + history_list #print history_list return history_list
def get_stat_last_period(days = 30): users = config.get('WITHINGS', 'users').split(',') stat_list_header = ['Day'] for user in users: stat_list_header += ['', user[0] + ' weight', user[0] + ' avg weight', user[0] + ' weight loss'] stat_list_header = [stat_list_header] stat_list = [] last_weights = {} last_avg_weights = {} min_avg_weight = {} min_weights = {} for user in users: last_weights[user] = [] last_avg_weights[user] = [] min_avg_weight[user] = 100 min_weights[user] = 100 # TODO ??? better ??? min_date = datetime.datetime.now() - datetime.timedelta(days=days+20) min_date = datetime.date(min_date.year, min_date.month, min_date.day) with get_session_manager() as session_manager: while min_date <= datetime.date.today(): stat_row = [str(min_date)] for user in users: weight, avg_weight, weight_loss = None, None, None (weight,) = session_manager.query(func.avg(Weights.weight)).\ filter(and_(Weights.user == user, Weights.date == str(min_date))).first() if weight: last_weights[user].append(weight) else: if last_weights[user]: last_weights[user].append(last_weights[user][-1]) weight = last_weights[user][-1] if last_weights[user]: if not last_avg_weights[user] or len(last_avg_weights[user])<5: avg_weight = last_weights[user][-1] weight_loss = 0 last_avg_weights[user].append(avg_weight) else: avg_weight = last_avg_weights[user][-1] + (weight - last_avg_weights[user][-5])/10 weight_loss = (last_avg_weights[user][-5]-avg_weight)/5 last_avg_weights[user].append(avg_weight) last_weights[user] = last_weights[user][-10:] last_avg_weights[user] = last_avg_weights[user][-10:] if weight and avg_weight and weight_loss: if avg_weight < min_avg_weight[user]: min_avg_weight[user] = avg_weight avg_weight = ('background:#CC9999', avg_weight) if weight < min_weights[user]: min_weights[user] = weight weight = ('background:#CC9999', weight) stat_row += ['', weight, avg_weight, max(weight_loss,0)] else: stat_row += ['', '', '', ''] #print stat_row stat_list += [stat_row] min_date += datetime.timedelta(days=1) stat_list = sorted(stat_list, reverse=True) stat_list = stat_list[:days] stat_list = stat_list_header + stat_list return stat_list
def supervisord_server_url(server_name): _service_conf = config.get('DASHBOARD', 'servers') for name, _url in _service_conf: if name == server_name: return _url else: return ''
def rename_suffix(channel_list, suffix): apk_dir = os.path.expanduser(config.get('build', 'out_dir')) for channel in channel_list: naming = Naming(channel) cur_name = naming.signed_aligned() renamed_name = naming.signed_aligned(suffix) cur_file = os.path.join(apk_dir, cur_name) replaced_file = os.path.join(apk_dir, renamed_name) os.rename(cur_file, replaced_file)
def delete_apk(channel_list): apk_dir = os.path.expanduser(config.get('build', 'out_dir')) for channel in channel_list: name = Naming(channel).signed_aligned() apk_file = os.path.join(apk_dir, name) if os.path.exists(apk_file): print 'remove', channel os.remove(apk_file) else: print channel, 'not exists'
def get_user_graph_section(days=40): users = config.get('WITHINGS', 'users').split(',') header = 'Graphs:' section = '' for user in users: graph_title = 'Last {days} days for {user}'.format(days=days, user=user) section += create_graph(graph_title, get_user_stat_last_period(user, days), ['weight', 'avg. weight']) return HTML_SECTION.format(header=header, section=section)
async def _start(self, loop): await listen('', config.get(self.NAME, 'port')) try: _backdoor_port = config.get(self.NAME, 'adminport') if isinstance(_backdoor_port, int) and _backdoor_port > 1024: from backdoor import listen as blisten await blisten(int(_backdoor_port)) else: raise Exception('unknown backdoor port:{0}'.format(_backdoor_port)) except Exception as e: log.warn('can not open backdoor. error:{0}'.format(e)) load_all_routes(self.NAME) await self.connectGW() await self.connectCS() conf = config.get('REDIS') await redis.init(**conf)
def dbconfig(): return dict( host = config.get('DB', 'host'), port = config.get('DB', 'port'), user = config.get('DB', 'user'), password = config.get('DB', 'password'), db = config.get('DB', 'userdb'), minsize = config.get('DB', 'min'), maxsize = config.get('DB', 'max') )
def channel_list(): all = [] channel_file_dir = os.path.expanduser( config.get('channel', 'channel_file_dir')) for channel_file_name in os.listdir(channel_file_dir): channel_file = os.path.join(channel_file_dir, channel_file_name) if os.path.isfile(channel_file): with open(channel_file) as f: channels = f.read().strip().splitlines() for channel in channels: if channel not in all: all.append(channel) return all
def message_center(): gmail_service = get_gmail_service() users = config.get('WITHINGS', 'users').split(',') for user in ['Bitten']: email_body = '' email_body += check_for_enough_weighings(user) email_body += check_for_goals_reached(user) email_body += check_maximum_weight(user) if email_body: user_email_address = get_email_address(user) print('Sending email to user: '******'Message from weights.', email_body) print(email_message) send_message(gmail_service, email_message)
def get_goal_status(): # TODO check this works after message center implemented goal_list = [['User', 'Avg. weight this week', 'BMI', 'Next goal', 'Final goal', 'Weight loss', 'Expected days next goal', 'Expected days final goal']] users = config.get('WITHINGS', 'users').split(',') with get_session_manager() as session_manager: for user in users: (goal, next_goal, height) = session_manager.query(Users.goal, Users.next_goal, Users.height_in_cm).filter(Users.user == user).first() end_date = datetime.datetime.now() start_date = end_date - datetime.timedelta(days=7) (avg_this_week,) = session_manager.query(func.avg(Weights.weight)).filter(and_(Weights.user == user, Weights.date > start_date, Weights.date <= end_date)).first() end_date = start_date start_date = end_date - datetime.timedelta(days=7) (avg_last_week,) = session_manager.query(func.avg(Weights.weight)).filter(and_(Weights.user == user, Weights.date > start_date, Weights.date <= end_date)).first() weight_loss = (avg_last_week - avg_this_week)/7 days_to_next_goal = (avg_this_week-next_goal)/weight_loss days_to_final_goal = (avg_this_week-goal)/weight_loss if days_to_next_goal < 0: days_to_next_goal = 'NA' days_to_final_goal = 'NA' else: days_to_next_goal = int(days_to_next_goal) days_to_final_goal = int(days_to_final_goal) bmi = avg_this_week / ((height/100.0) ** 2) goal_list += [[user, avg_this_week, bmi, next_goal, goal, weight_loss, days_to_next_goal, days_to_final_goal]] return goal_list
#!/usr/bin/env python from config_parser import config APP_NAME = config.get('app', 'app_name') VERSION_CODE = config.get('app', 'version_code') APP_SUFFIX = config.get('app', 'app_suffix') class Naming: def __init__(self, channel): self.channel = channel self.base_name = '{app_name}-{version_code}-{channel}'.format( app_name=APP_NAME, version_code=VERSION_CODE, channel=channel) def unsigned(self): return '{name}.apk'.format(name=self.base_name) def signed(self): return '{name}-signed.apk'.format(name=self.base_name) def signed_aligned(self, suffix=APP_SUFFIX): return '{name}-signed-aligned-{suffix}.apk'.format(name=self.base_name, suffix=suffix) if __name__ == '__main__': naming = Naming('testchannel') print naming.signed_aligned()
def get_host(self, name): port = config.get(name, 'port') host = config.get(name, 'host_local') return host, port
#!/usr/bin/env python import zipfile import argparse import shutil import os, os.path from config_parser import config from apk_naming import Naming import apkfile BUILD_DIR = os.path.expanduser(config.get('build', 'build_dir')) OUT_DIR = os.path.expanduser(config.get('build', 'out_dir')) META_CHANNEL_FILE_NAME_TEMPLATE = "META-INF/channel_{channel}" def make(src_apk_path, channel_list): tmp_src_apk_path = os.path.join(BUILD_DIR, 'temp.apk') shutil.copy(src_apk_path, tmp_src_apk_path) channel_files = apkfile.find_channel_files(tmp_src_apk_path, 'META-INF/channel_') apkfile.remove_channel_files(tmp_src_apk_path, channel_files) if not os.path.exists(OUT_DIR): os.makedirs(OUT_DIR) empty_file_name = 'empty' empty_file_path = os.path.join(OUT_DIR, empty_file_name) open(empty_file_path, 'w').close() for channel in channel_list:
#!/usr/bin/env python import shutil from config_parser import config import os, os.path from apk_naming import Naming OUT_DIR = os.path.expanduser(config.get('build', 'out_dir')) APKS_DIR = os.path.join(OUT_DIR, 'apks') shutil.rmtree(APKS_DIR, ignore_errors=True) os.mkdir(APKS_DIR) channel_file_dir = os.path.expanduser(config.get('channel', 'channel_file_dir')) for channel_file_name in os.listdir(channel_file_dir): if channel_file_name == '.DS_Store': continue dir = os.path.join(APKS_DIR, channel_file_name) os.mkdir(dir) channel_file = os.path.join(channel_file_dir, channel_file_name) with open(channel_file) as f: channels = f.read().strip().splitlines() for channel in channels: apk_file_name = Naming(channel).signed_aligned() src_apk_file = os.path.join(OUT_DIR, apk_file_name) dst_apk_file = os.path.join(dir, apk_file_name) if os.path.exists(src_apk_file): shutil.copy(src_apk_file, dst_apk_file)
def all_servers(): return [name for name, _ in config.get('DASHBOARD', 'servers')]
def get_stat_last_period_section(): last_period_in_days = int(config.get('FLAGS', 'last_period_in_days')) header = 'Last {days} days:'.format(days=last_period_in_days) section = get_html_table(get_stat_last_period(last_period_in_days)) return HTML_SECTION.format(header=header, section=section)
#-*-coding: utf-8-*- # Version: 0.1 # Author: Don Li <*****@*****.**> # License: Copyright(c) 2015 Don.Li # Summary: from config_parser import config from osutil import root_path URL_ROOT = 'http://{0}:{1}'.format( config.get('DASHBOARD', 'hostname'), config.get('DASHBOARD', 'port').split(':')[1] ) ROOT_PATH = root_path() STATIC_PATH = ROOT_PATH + config.get('DASHBOARD', 'static_path') TEMPLATE_PATH = ROOT_PATH + config.get('DASHBOARD', 'template_path')
#!/usr/bin/env python from config_parser import config import os import os.path import xlrd import re from collections import OrderedDict CHANNEL_FILE_DIR = os.path.expanduser(config.get('channel', 'channel_file_dir')) CHANNEL_SHEET_FILE = os.path.expanduser( config.get('channel', 'channel_sheet_file')) CONFIG = [ dict(file='market', sheet_index=0, column_index=4), dict(file='ad', sheet_index=1, column_index=4) ] if not os.path.exists(CHANNEL_FILE_DIR): os.makedirs(CHANNEL_FILE_DIR) def dedupe(_list): return OrderedDict((item, None) for item in _list).keys() def is_ascii(s): return all(ord(c) < 128 for c in s)
from rpc import route from log import log from time import time from config_parser import config from server import server from constant import * from errorno import * from protocol_manager import g_protoMgr, gs_call, ms_call, alli_call import redis httpport = config.get(server.NAME, 'httpport') REGISTERING_ACCOUTs = set() @route() async def register(p, request): res_err = [REGISTER_FAILED, [None, int(time()), httpport, 0]] _account_name = getattr(p, 'account', None) if not _account_name: p.kick('no account info when register') return res_err lead_id, nick_name, login_type, platform_id, version, sid = req had_cid = await redis.hget(DICT_NICKNAME_REGISTERED, nick_name) if had_cid: log.error('The nick_name had been used. had_cid: {0}, nick_name: {1}.'.format( had_cid, nick_name ))