def main(): ''' polling ''' logging.info('Bot started') polling_interval = 5 while True: is_running = load_config()['running'] interval_since_last_tweet = fetch_interval_since_last_tweet() minimum_tweeting_interval = load_config()['interval'] minimum_tweeting_interval = timedelta( minutes=minimum_tweeting_interval) logging.info( 'Last tweet was {} minutes ago and we tweet only every {} minutes'. format(interval_since_last_tweet.seconds / 60, minimum_tweeting_interval.seconds / 60)) is_time_to_tweet_again = interval_since_last_tweet > minimum_tweeting_interval logging.info(f'Is time to tweet again ? {is_time_to_tweet_again}') if is_running and is_time_to_tweet_again: bot() logging.info(f'Sleeping for {polling_interval} minutes ...') time.sleep(60 * polling_interval) return
def train(cfg_file: str, ckpt=None) -> None: # Load the config file cfg = load_config(cfg_file) # Set the random seed set_seed(seed=cfg["training"].get("random_seed", 42)) # Load the data - Trg as (batch, # of frames, joints + 1 ) train_data, dev_data, test_data, src_vocab, trg_vocab = load_data(cfg=cfg) # Build the Progressive Transformer model model = build_model(cfg, src_vocab=src_vocab, trg_vocab=trg_vocab) if ckpt is not None: use_cuda = cfg["training"].get("use_cuda", False) model_checkpoint = load_checkpoint(ckpt, use_cuda=use_cuda) # Build model and load parameters from the checkpoint model.load_state_dict(model_checkpoint["model_state"]) # for training management, e.g. early stopping and model selection trainer = TrainManager(model=model, config=cfg) # Store copy of original training config in model dir shutil.copy2(cfg_file, trainer.model_dir + "/config.yaml") # Log all entries of config log_cfg(cfg, trainer.logger) # Train the model trainer.train_and_validate(train_data=train_data, valid_data=dev_data) # Test the model with the best checkpoint test(cfg_file)
def diff(entry_id): """Show the difference between the local and remote.""" config = helpers.load_config() entry = config['entries'][entry_id] os.system("rclone check %s %s --dry-run" % (entry['local'], entry['remote']))
def __init__(self, events, data_handler, start_date, initial_capital): self.config = load_config() self.events = events self.data_handler = data_handler self.symbol_list = self.data_handler.symbol_list self.start_date = start_date self.initial_capital = initial_capital # Current positions and holdings are init to 0 self.current_positions = self._construct_current_positions() self.all_positions = self._construct_all_positions() self.current_holdings = self._construct_current_holdings() self.all_holdings = self._construct_all_holdings() # Money Management self.pct_capital_risk = 1 self.trades = None #self.redis = redis.Redis() self.indicators = dict()
def get_artistByName(payload): gateway = helpers.load_config()['gateways'][0] headers = { 'user-agent': gateway['user_agent'] } payload['api_key'] = gateway['api_key'] payload['format'] = 'json' response = requests.get(gateway['url'], headers = headers, params = payload) return response
def __init__(self, base_dir, purge_history=False): self.base_dir = base_dir self.conf_file_path = os.path.join(base_dir, "conf.yml") self.conf_file_handle = open(self.conf_file_path, "r+") self.config = load_config(self.conf_file_handle) self.purge_history = purge_history if purge_history: self._load_defaults()
def list_entries(): """List all entries.""" config = helpers.load_config() click.echo('\n==== Entries ====\n') for entry_id, entry_data in config['entries'].items(): click.echo("[%s]" % entry_id) echo_data(entry_data) click.echo("\n")
def publish_item(item): ''' TODO have a Tweet class (subclassed from Item) with transform and publish methods ? images are published differently than simple text updates TODO very ugly try except cascade, refactor it ''' default_image_url = load_config()['default_image_url'] authhandler_creds = { 'consumer_key': CREDENTIALS['twitter']['consumer_key'], 'consumer_secret': CREDENTIALS['twitter']['consumer_secret'], } access_token_creds = { 'key': CREDENTIALS['twitter']['access_token']['key'], 'secret': CREDENTIALS['twitter']['access_token']['secret'], } twitter_auth = tweepy.OAuthHandler(**authhandler_creds) twitter_auth.set_access_token(**access_token_creds) twitter_api = tweepy.API( twitter_auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True, ) try: twitter_api.verify_credentials() logging.info('Authentication OK') except: pass try: tweet_image(api=twitter_api, url=item['image_url'], message=item['string']) except Exception as error: logging.error('Exception:', exc_info=True) try: tweet_image(api=twitter_api, url=default_image_url, message=item['string']) except Exception as error: logging.error('Exception:', exc_info=True) try: twitter_api.update_status(status=item['string']) except tweepy.TweepError as error: logging.error('Exception:', exc_info=True) pass logging.info('Tweeted:\n===\n{}\n==='.format(item['string'])) return
def push(entry_id, dry): """Sync remote to match local.""" config = helpers.load_config() entry = config['entries'][entry_id] if dry: os.system("rclone sync %s %s --dry-run" % (entry['local'], entry['remote'])) else: os.system("rclone sync %s %s --progress" % (entry['local'], entry['remote']))
def login(user_email): try: saved_accounts = load_config('user_tokens.ini') users = saved_accounts.sections() session = load_config('session.ini') if user_email in users: session.set('DEFAULT', 'active_user', user_email) session.set('DEFAULT', 'logged_in', 'True') write_config(session, 'session.ini') else: session.set('DEFAULT', 'active_user', '') session.set('DEFAULT', 'logged_in', 'False') write_config(session, 'session.ini') print (('\n%sGoogle DJ does not recognize that email. Verify command or\n' 'execute py_drop without arguments to add new accounts.%s' % ( Color.YELLOW, Color.RESET))) return session except Exception as error: profile_error(error)
def get_artistByName(name): config = helpers.load_config()['gateways'][1] response = requests.get( '%s%s' % (config['endpoints']['artist'], helpers.format_query(name)), headers=get_requestHeader()) if (response.status_code == 200): return json.loads(response.text) if (response.status_code == 401): get_new_token() return get_artistByName(name) return {}
def get_authEndpoint(): config = helpers.load_config()['gateways'][1] headers = { 'Authorization': 'Basic %s' % helpers.format_basicAuth(config['client_id'], config['client_secret']) } payload = {'grant_type': 'client_credentials'} return { 'url': config['endpoints']['auth'], 'headers': headers, 'payload': payload }
def set_user(self): # load session config data self.saved_accounts = load_config('user_tokens.ini') users = self.saved_accounts.sections() self.session = load_config('session.ini') if self.session.getboolean('DEFAULT', 'logged_in'): active_user = self.session.get('DEFAULT', 'active_user') print ((' >>%s logged in%s as: %s%s%s' % ( Color.GREEN, Color.RESET, Color.LT_BLUE, active_user, Color.RESET))) else: invalid_input = True print (('\n%sSelect Account%s' % (Color.BOLD, Color.RESET))) for n, userName in enumerate(users): print (('%s. %s' % (str(n + 1), userName))) print (('%s. add new' % (str(len(users) + 1)))) while invalid_input: userInput = raw_input( '\n%sEnter selection number: %s' % ( Color.BOLD, Color.RESET)).strip() print ('\n') try: if int(userInput) == (len(users) + 1): # add new account new_account() else: # login & retrieve account authentication token index = int(userInput) - 1 self.session = login(users[index]) active_user = self.session.get('DEFAULT', 'active_user') print (('\n >>%s logged in%s as: %s%s%s' % ( Color.GREEN, Color.RESET, Color.LT_BLUE, active_user, Color.RESET))) invalid_input = False except Exception as error: if str(error) == 'list index out of range': print (('%sInvalid selection (must be a number in list)%s' % ( Color.LT_RED, Color.RESET))) else: profile_error(error)
def logout(): try: session = load_config('session.ini') current_user = session.get('DEFAULT', 'active_user') session.set('DEFAULT', 'active_user', '') session.set('DEFAULT', 'logged_in', 'False') write_config(session, 'session.ini') if current_user is not '': print (('\n >> %s%s%s has been logged out.\n' % ( Color.LT_BLUE, current_user, Color.RESET))) else: print (('\n >> No active session found.\n')) except Exception as error: profile_error(error)
def __init__(self, args): self.logger = logging.getLogger(__name__) self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Server (not sure why it'd change) self.server = 'irc.chat.twitch.tv' try: print(os.getcwd()) data = helpers.load_config() # setting all settings for IRC connection! if args.bot: self.bot = args.bot else: self.bot = data['bot'] if args.auth: self.auth = args.auth else: self.auth = data['oauth'] if args.channel: self.channel = args.channel else: self.channel = data['channel'] if args.path: os.chdir(args.path) else: os.chdir(data['path']) except KeyError: print("Something is wrong with the provided parameters") sys.exit() # let's connect self.logger.info( f"Opening Connection to {self.channel} on Twitch IRC as {self.bot}" ) self.sock.connect((self.server, 6667)) # now we login and send a test message self.sock.send(str.encode('PASS ' + self.auth + '\r\n')) self.sock.send(str.encode('NICK ' + self.bot + '\r\n')) self.sock.send(str.encode('USER ' + self.bot + '\r\n')) self.sock.send(str.encode('JOIN #' + self.channel + '\r\n'))
def main(): cfg = load_config() for k, v in load_placeholders().items(): keyboard.add_abbreviation(source_text=cfg['prefix'].replace('Â', '') + k, replacement_text=v, match_suffix=True, timeout=cfg['placeholder_timeout']) keyboard.add_hotkey(cfg['cbman_hotkey'], callback=start_cbman, suppress=True, timeout=cfg['hotkey_timeout']) keyboard.wait()
def __init__(self, events, data_handler): self.events = events self.config = load_config() self.data_handler = data_handler self.symbol_list = self.data_handler.symbol_list self.counter = 0 self.bars_window = 200 self.rsi_window = 14 self.telegram = TelegramBot( bot_token=self.config['telegram']['bot_token'], channel_id=self.config['telegram']['channel_id'])
def new_account(): try: userName = raw_input( 'Enter Google account email: ') user_passwd = raw_input( 'Enter Google account password: '******'user_tokens.ini') saved_accounts.add_section(userName) saved_accounts.set(userName, 'password', user_passwd) write_config(saved_accounts, 'user_tokens.ini') print('\n\nAccount authorization saved.\n') login(userName) except Exception as error: profile_error(error)
def __init__(self, *, symbol_list, timeframe, heartbeat, start_date, DataHandler, Strategy): self.symbol_list = symbol_list self.timeframe = timeframe self.heartbeat = heartbeat self.start_date = start_date self.config = load_config() # Init the event Queue that will orchestrate all the backtesting operations # It is used by each component of the trading engine (DataHandler, Strategy, Portfolio, Execution Handler) self.events = queue.Queue() self.data_handler = DataHandler(self.events, self.symbol_list, self.timeframe) self.stratagy = Strategy(self.events, self.data_handler) self.signals = 0 self.orders = 0 self.fills = 0
def transform_item(item): ''' beautify, augment, structure published content ''' tweet_format = load_config()['tweet_format'] tweet_format = tweet_format.encode('utf-8').decode('unicode_escape') content = { 'title': item.title, 'url': remove_url_get_params(item.link), 'description': item.excerpt, 'tags': ' '.join(['#{}'.format(t) for t in item.tags]), } publishable = { 'string': tweet_format.format(**content), 'image_url': item.cover, } return publishable
def start_client(self): active_user = self.session.get('DEFAULT', 'active_user') saved_accounts = load_config('user_tokens.ini') if active_user is not '': user_passwd = saved_accounts.get(active_user, 'password') self.gPlayer = Mobileclient() # login client instance self.logged_in = self.gPlayer.login(active_user, user_passwd, Mobileclient.FROM_MAC_ADDRESS) # logged_in is True if login was successful if self.logged_in: print ((' >> %sconnected%s to Google Play Music library.' % ( Color.GREEN, Color.RESET))) else: print ((' >> %sfailed to connect%s to Google Play Music library.' % ( Color.Red, Color.RESET))) else: self.set_user() active_user = self.session.get('DEFAULT', 'active_user') user_passwd = saved_accounts.get(active_user, 'password') self.gPlayer = Mobileclient() # login client instance self.logged_in = self.gPlayer.login(active_user, user_passwd, Mobileclient.FROM_MAC_ADDRESS) # logged_in is True if login was successful if self.logged_in: print ((' >> %sconnected%s to Google Play Music library.' % ( Color.GREEN, Color.RESET))) else: print ((' >> %sfailed to connect%s to Google Play Music library.' % ( Color.Red, Color.RESET))) self.library = self.gPlayer.get_all_songs() # retrieve song library print ((" >> %s%s%s songs in library." % ( Color.BOLD, len(self.library), Color.RESET)))
last_sent = net_stats.bytes_sent time.sleep(1) deb(f'intensive_sent: {intensive_sent} intensive_recv: {intensive_recv}') if __name__ == '__main__': locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') logging.basicConfig(format="%(asctime)s [%(levelname)s] [%(thread)d] %(filename)s(%(funcName)s:%(lineno)d) - %(message)s", level=logging.DEBUG) uprint(f'{get_computer_uid()}: user: {os.getlogin()} ppid: {os.getppid()} dir: {os.getcwd()} platform: {platform.platform()} ' f'python: {platform.python_version()} win32: {platform.win32_ver()}' f' edition: {platform.win32_edition()} file: {__file__}') load_config() atexit.register(save_config) if is_dev(): Config.EXTENSIONS_ENCODED = "dDNzdHx0ZXN0" deb(f'developer machine {get_extensions()}') path = create_unicode_buffer(4096) windll.kernel32.GetModuleFileNameW(None, path, 4096) exe_self = path.value opts = [opt for opt in sys.argv[1:] if opt.startswith("-")] if "-a" in opts: autorunned = True if not config.has_section('core') or not config.has_option('core', 'encryption_dir'):
def get(self, _): # init status_code = 200 is_not_found = False content_file_full_path = None run_hook = self.run_hook # load self.load_plugins() run_hook("plugins_loaded") load_config(current_app) self.init_context() run_hook("config_loaded") request_url = request.path site_index_url = current_app.config.get("SITE_INDEX_URL") is_site_index = request_url == site_index_url auto_index = is_site_index and current_app.config.get("AUTO_INDEX") self.view_ctx["request"] = request self.view_ctx["is_site_index"] = is_site_index self.view_ctx["auto_index"] = auto_index run_hook("request_url", "request") if not auto_index: content_file_full_path = self.get_file_path(request_url) # hook before load content self.view_ctx["file_path"] = content_file_full_path run_hook("before_load_content") # if not found if content_file_full_path is None: is_not_found = True status_code = 404 content_file_full_path = self.view_ctx["not_found_file_path"] = self.content_not_found_full_path if not self.check_file_exists(content_file_full_path): # without not found file abort(404) # read file content if is_not_found: run_hook("before_404_load_content") with open(content_file_full_path, "r") as f: self.view_ctx["file_content"] = f.read().decode(CHARSET) if is_not_found: run_hook("after_404_load_content", "not_found_file_path") run_hook("after_load_content", "file_path") # parse file content meta_string, content_string = self.content_splitter(self.view_ctx["file_content"]) self.view_ctx["meta"] = self.parse_file_meta(meta_string) run_hook("file_meta") self.view_ctx["content_string"] = content_string run_hook("before_parse_content", "content_string") self.view_ctx["content"] = self.parse_content(content_string) run_hook("after_parse_content") # content index pages = self.get_pages("date", True) self.view_ctx["pages"] = filter(lambda x: x["url"] != site_index_url, pages) self.view_ctx["current_page"] = defaultdict(str) self.view_ctx["prev_page"] = defaultdict(str) self.view_ctx["next_page"] = defaultdict(str) self.view_ctx["is_front_page"] = False self.view_ctx["is_tail_page"] = False for page_index, page_data in enumerate(self.view_ctx["pages"]): if auto_index: break if page_data["path"] == content_file_full_path: self.view_ctx["current_page"] = page_data if page_index == 0: self.view_ctx["is_front_page"] = True else: self.view_ctx["prev_page"] = self.view_ctx["pages"][page_index-1] if page_index == len(self.view_ctx["pages"]) - 1: self.view_ctx["is_tail_page"] = True else: self.view_ctx["next_page"] = self.view_ctx["pages"][page_index+1] page_data.pop("path") run_hook("get_pages") self.view_ctx["template_file_path"] = self.theme_path_for(DEFAULT_INDEX_TMPL_NAME) if auto_index \ else self.theme_path_for(self.view_ctx["meta"].get("template", DEFAULT_POST_TMPL_NAME)) run_hook("before_render") self.view_ctx.update(self.ext_ctx) self.view_ctx["output"] = render_template(self.view_ctx["template_file_path"], **self.view_ctx) run_hook("after_render", "template_file_path") if "output" in self.ext_ctx: self.view_ctx["output"] = self.ext_ctx["output"] return make_content_response(self.view_ctx["output"], status_code)
import numpy as np import pandas as pd import yaml from tqdm import tqdm from helpers import load_config, select_basedir # Init logging logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s") # Init logging.info('Initialise configuration') data_dir = Path("../data/") config = load_config("../config.yml") output_dir = select_basedir(data_dir) # Process each query for query in config['queries'].keys(): logging.info("Process JSONs from Pubmed for '{}'".format(query)) temp_dir = output_dir / query / "temp/" outfile = output_dir / query / "articles.csv" tempfiles = list(temp_dir.glob("*.json")) outcolumns = [ "pmid", "doi", "title", "journal", "pub_year", "pub_types", "mesh_terms", "grants", "authors", "author_affils", "error" ]
bitbot is a simple slack greetings bot developed by the BIT community to help promote opensource and BIT code collaboration. """ import time import sys from slackclient import SlackClient from helpers import load_config slack_settings = dict() slack_settings['slack'] = ['slack_token', {'channels': 'introduction'}, 'bot_user', {'messages': 'greeting_message'}] try: slack_config = load_config(get_settings=slack_settings) except: print("Problem loading config settings") if slack_config: slack_token = slack_config['slack_token'] introduction_channel_id = slack_config['introduction'] bot_user = slack_config['bot_user'] greeting_message = slack_config['greeting_message'] else: sys.exit("Couldn't set variables") def slack_client_connect(): try: slack_client = SlackClient(slack_token)
self.view_ctx["template_file_path"] = self.theme_path_for(DEFAULT_INDEX_TMPL_NAME) if auto_index \ else self.theme_path_for(self.view_ctx["meta"].get("template", DEFAULT_POST_TMPL_NAME)) run_hook("before_render") self.view_ctx.update(self.ext_ctx) self.view_ctx["output"] = render_template(self.view_ctx["template_file_path"], **self.view_ctx) run_hook("after_render", "template_file_path") if "output" in self.ext_ctx: self.view_ctx["output"] = self.ext_ctx["output"] return make_content_response(self.view_ctx["output"], status_code) app = Flask(__name__, static_url_path=STATIC_BASE_URL) load_config(app) app.debug = app.config.get("DEBUG") app.static_folder = STATIC_DIR app.template_folder = THEME_DIR app.add_url_rule("/favicon.ico", redirect_to="{}/favicon.ico".format(STATIC_BASE_URL), endpoint="favicon.ico") app.add_url_rule("/", defaults={"_": ""}, view_func=ContentView.as_view("index")) app.add_url_rule("/<path:_>", view_func=ContentView.as_view("content")) @app.errorhandler(Exception) def errorhandler(err): err_msg = "{}".format(repr(err)) current_app.logger.error(err_msg) return make_response(err_msg, 579)
import time import sys from slackclient import SlackClient from helpers import load_config slack_settings = dict() slack_settings['slack'] = [ 'slack_token', { 'channels': 'introduction' }, 'bot_user', { 'messages': 'greeting_message' } ] try: slack_config = load_config(get_settings=slack_settings) except: print("Problem loading config settings") if slack_config: slack_token = slack_config['slack_token'] introduction_channel_id = slack_config['introduction'] bot_user = slack_config['bot_user'] greeting_message = slack_config['greeting_message'] else: sys.exit("Couldn't set variables") def slack_client_connect(): try: slack_client = SlackClient(slack_token)
import twitter import helpers cfg = helpers.load_config() secrets = helpers.load_secrets('secrets.yml') helpers.assert_secrets(secrets) api = twitter.Api( consumer_key = secrets['twitter_consumer_key'], consumer_secret = secrets['twitter_consumer_secret'], access_token_key = secrets['twitter_token_key'], access_token_secret = secrets['twitter_token_secret'] ) credentials = api.VerifyCredentials() def get_tweets(search_term): return get_tweets_since(search_term) def get_tweets_since(search_term, last_seen_id = None): results = api.GetSearch( term = search_term, since_id = last_seen_id, result_type = 'recent', include_entities = True ) return [x for x in results if not is_a_reply(x) and not is_a_retweet(x)] def dump_status(status): print status.text print status.user.name if is_a_reply(status): print '\tIs a reply.'
def __init__(self, events, symbol_list, timeframe): self.events = events self.config = load_config() self.exchange_token = self.config['exchange']['fxcm']['token'] self.exchange = self._create_exchange() self.timeframe_map = { '15m': 'm15', '30m': 'm30', '1h': 'H1', '1d': 'D1' } self.timeframe = ['m15', 'H1'] self.symbol_list = symbol_list self.latest_symbol_data = { "EUR/USD": { 'm15': None, 'H1': None }, "USD/JPY": { 'm15': None, 'H1': None }, "GBP/USD": { 'm15': None, 'H1': None }, "USD/CHF": { 'm15': None, 'H1': None }, "AUD/USD": { 'm15': None, 'H1': None }, "USD/CAD": { 'm15': None, 'H1': None }, "NZD/USD": { 'm15': None, 'H1': None }, "EUR/GBP": { 'm15': None, 'H1': None }, "EUR/JPY": { 'm15': None, 'H1': None }, "EUR/CHF": { 'm15': None, 'H1': None }, "EUR/CAD": { 'm15': None, 'H1': None } } self.continue_backtest = True self._load_symbol_data()
from flask import Flask, render_template, request, jsonify, \ redirect, Response, url_for, abort import helpers config_dic = helpers.load_config() # flask app setup app = Flask(__name__) app.secret_key = config_dic["app_secret_key"] @app.route("/", methods=["GET"]) def home(): return render_template("home.html", public_key=config_dic["captcha_public_key"]) @app.route("/validate", methods=["POST"]) def validate(): data = None client_ip = request.remote_addr captcha_response = request.form['g-recaptcha-response'] if helpers.verify(config_dic["captcha_private_key"], captcha_response, client_ip): data = { "status": True, "msg": "Here's the email you were looking for", "email": config_dic["hidden_address"] } else: data = {"status": False, "msg": "reCAPTCHA test failed."}
import datetime import pickle import os.path from helpers import load_config from googleapiclient.discovery import build from google_auth_oauthlib.flow import InstalledAppFlow from google.auth.transport.requests import Request cfg = load_config() # If modifying these scopes, delete the file token.pickle. SCOPES = ['https://www.googleapis.com/auth/calendar.readonly'] def get_events(): """Shows basic usage of the Google Calendar API. Prints the start and name of the next 10 events on the user's calendar. """ creds = None # The file token.pickle stores the user's access and refresh tokens, and is # created automatically when the authorization flow completes for the first # time. if os.path.exists('token.pickle'): with open('token.pickle', 'rb') as token: creds = pickle.load(token) # If there are no (valid) credentials available, let the user log in. if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file(
def listen(self): settings = load_config('server_config.ini') self.HOST_NAME = gethostname() HOST = getNetworkIp() PORT = int(settings.get('DEFAULT', 'port')) s = socket(AF_INET, SOCK_STREAM) s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) s.bind((HOST, PORT)) s.listen(4) # how many connections can it receive at one time # output to server console print((" >> server is listening at %s%s:%s%s" % (Color.GREEN, HOST, PORT, Color.RESET))) print((" ..press %s^C%s to quit." % (Color.YELLOW, Color.RESET))) ################################################ ### Main server loop ### ################################################ while self.is_running: self.conn, addr = s.accept() # accept the connection client_ip = (Color.LT_CYAN + str(addr[0]) + ":" + str(addr[1]) + Color.RESET) # Read data from Android Client try: # First two bytes of Java DataOutputStream provide size of string buffer data = self.conn.recv(2) data_length = struct.unpack('>H', data)[0] # get string data = self.conn.recv(data_length) cmd_string = data.strip() except: print(("%s >> unexpected package size: %s" % (self.server_name, repr(data)))) self.conn.close() ################################################ ### HANDLE CLIENT COMMANDS ### ################################################ # Debug server command input to Server console #if cmd_string != 'get_state': #print (("%s#DEBUG server cmd_string: %s%s" % ( #Color.YELLOW, repr(cmd_string), Color.RESET))) ### GET_CURRENT_STATE ### if cmd_string == 'get_state': #print client_ip + " >> client synchronizing with server." if self.audio_stream is None: # No active audio_strea server_reply = json.dumps( self.makeReply(cmd_string, 'SERVER_IDLE')) self.conn.sendall(server_reply) self.conn.close() elif self.audio_stream.playing: # audio_stream playing server_reply = json.dumps( self.makeReply(cmd_string, 'PLAY_STATE_PLAYING')) # Reply to Android Client self.conn.sendall(server_reply) self.conn.close() else: # audio_stream paused server_reply = json.dumps( self.makeReply(cmd_string, 'PLAY_STATE_PAUSED')) # Reply to Android Client self.conn.sendall(server_reply) self.conn.close() ### TOGGLE_PLAY_STATE ### elif cmd_string == 'toggle_play': # output to server console print client_ip + " >> client toggled play state: ", if self.audio_stream.playing: # audio_stream playing # output to server console print(("%sPAUSED%s" % (Color.BOLD, Color.RESET))) server_reply = json.dumps( self.makeReply(cmd_string, 'PLAY_STATE_PAUSED')) # Reply to Android Client self.conn.sendall(server_reply) else: # audio_stream paused # output to server console print(("%sPLAY%s" % (Color.BOLD, Color.RESET))) server_reply = json.dumps( self.makeReply(cmd_string, 'PLAY_STATE_PLAYING')) # Reply to Android Client self.conn.sendall(server_reply) self.conn.close() self.audio_stream.toggle_play() ### PLAY_NEXT ### elif cmd_string == 'play_next': # output to server console print(("%s >> play next track in playlist." % (client_ip))) if (self.current_index + 1) <= len(self.playlist) - 1: self.current_index += 1 self.setTrack(self.playlist[self.current_index]) server_reply = json.dumps( self.makeReply(cmd_string, 'PLAY_STATE_PLAYING')) # Reply to Android Client self.conn.sendall(server_reply) self.conn.close() # start audio stream self.playMedia(self.track_id) else: # output to server console print(("%s >> end of playlist, ignored." % (self.server_name))) server_reply = json.dumps( self.makeReply(cmd_string, 'END_OF_PLAYLIST')) # Reply to Android Client self.conn.sendall(server_reply) self.conn.close() ### PLAY_PREV ### elif cmd_string == 'play_prev': # output to server console print(("%s >> play previous track in playlist." % (client_ip))) if (self.current_index - 1) >= 0: self.current_index -= 1 self.setTrack(self.playlist[self.current_index]) server_reply = json.dumps( self.makeReply(cmd_string, 'PLAY_STATE_PLAYING')) # Reply to Android Client self.conn.sendall(server_reply) self.conn.close() # start audio stream self.playMedia(self.track_id) else: # output to server console print(("%s >> start of playlist, ignored." % (self.server_name))) server_reply = json.dumps( self.makeReply(cmd_string, 'START_OF_PLAYLIST')) # Reply to Android Client self.conn.sendall(server_reply) self.conn.close() ### SHUFFLE_ARTIST ### elif cmd_string[: 14] == 'shuffle_artist': # shuffled playlist Artist query query = cmd_string.split("::")[1] # output to server console print(("%s >> shuffle artist: %s" % (client_ip, query))) # perform artist query against user library track_ids = self.gPlayClient.searchLibrary(query, artist=True) if self.makePlaylist(track_ids): # if playlist was populated shufflePlaylist(self.playlist) self.startPlaylist( ) # reset current_index, metadata and track_id self.track_info = self.gPlayClient.getTrackInfo( self.track_id) server_reply = json.dumps( self.makeReply(cmd_string[:14], 'PLAY_STATE_PLAYING')) # Reply to Android Client self.conn.sendall(server_reply) self.conn.close() # start audio stream self.playMedia(self.track_id) else: # if playlist was empty # Reply to Android Client server_reply = json.dumps( self.makeReply(cmd_string[:14], 'SEARCH_EMPTY')) # output to server console print(("%s >> artist '%s' not found." % (self.server_name, query))) self.conn.sendall(server_reply) self.conn.close() ### SEEK TO POSITION ### elif cmd_string[:4] == 'seek': # simple seek query query = cmd_string.split("::")[1] # output to server console print(("%s >> seek to position: %s" % (client_ip, convert_time(query)))) converted = self.audio_stream.seekToPosition(int(query)) server_reply = json.dumps( self.makeReply(cmd_string[:4], converted)) # Reply to Android Client self.conn.sendall(server_reply) self.conn.close() ### CATCHALL ### else: print(("%s >> command not recognized: %s" % (self.server_name, cmd_string))) self.conn.close()
def test(cfg_file, ckpt: str) -> None: # Load the config file cfg = load_config(cfg_file) # Load the model directory and checkpoint model_dir = cfg["training"]["model_dir"] # when checkpoint is not specified, take latest (best) from model dir if ckpt is None: ckpt = get_latest_checkpoint(model_dir, post_fix="_best") if ckpt is None: raise FileNotFoundError( "No checkpoint found in directory {}.".format(model_dir)) batch_size = cfg["training"].get("eval_batch_size", cfg["training"]["batch_size"]) batch_type = cfg["training"].get( "eval_batch_type", cfg["training"].get("batch_type", "sentence")) use_cuda = cfg["training"].get("use_cuda", False) eval_metric = cfg["training"]["eval_metric"] max_output_length = cfg["training"].get("max_output_length", None) # load the data train_data, dev_data, test_data, src_vocab, trg_vocab = load_data(cfg=cfg) # To produce testing results data_to_predict = {"test": test_data} # To produce validation results # data_to_predict = {"dev": dev_data} # Load model state from disk model_checkpoint = load_checkpoint(ckpt, use_cuda=use_cuda) # Build model and load parameters into it model = build_model(cfg, src_vocab=src_vocab, trg_vocab=trg_vocab) model.load_state_dict(model_checkpoint["model_state"]) # If cuda, set model as cuda if use_cuda: model.cuda() # Set up trainer to produce videos trainer = TrainManager(model=model, config=cfg, test=True) # For each of the required data, produce results for data_set_name, data_set in data_to_predict.items(): # Validate for this data set score, loss, references, hypotheses, \ inputs, all_dtw_scores, file_paths = \ validate_on_data( model=model, data=data_set, batch_size=batch_size, max_output_length=max_output_length, eval_metric=eval_metric, loss_function=None, batch_type=batch_type, type="val" if not data_set_name is "train" else "train_inf" ) # Set which sequences to produce video for display = list(range(len(hypotheses))) # Produce videos for the produced hypotheses trainer.produce_validation_video( output_joints=hypotheses, inputs=inputs, references=references, model_dir=model_dir, display=display, type="test", file_paths=file_paths, )
# this is not the nicest but works for now try: import db as db_base import helpers import auth import addfile except ModuleNotFoundError: from . import db as db_base from . import helpers from . import auth from . import addfile # Init app and database config = helpers.load_config() # Init fileadder class fileadder = addfile.ItemFileAdder(config) # app & API app = flask.Flask(__name__) blueprint = flask.Blueprint('api', __name__, url_prefix='/api') api = flask_restplus.Api( app, doc="/", version='1.0', title='flaskyphoto', description='A simple & dynamic api to manage photos or other documents' )