How to use keep_screen method in ATX

Best Python code snippet using ATX

main.py

Source:main.py Github

copy

Full Screen

1from parse import *2import count3import config4import json5import date6import diff7# Show help for a particular screen8def show_options(screen="general"):9 with open(config.help_screen) as infile:10 help_screen = json.load(infile)11 options = help_screen[screen]12 print('\n'.join(options))13def main():14 # If no file is specified read from stdin15 pool = input("pool: ") if config.pool == "" else config.pool16 works = init_works(pool)17 show_options()18 done = False19 keep_screen = False # Don't go back to general screen20 option = ""21 while not done:22 if not keep_screen:23 option = input("> ")24 if option == 'q':25 done = True26 27 if not done:28 keep_screen = general_screen(option, works)29def clean_option(option: str) -> str:30 if len(option) < 1:31 return ""32 return option[0]33### GENERAL SCREEN ###34"""35 A screen is a function which controls the flow of the program through36 user input.37 It returns a keep_screen boolean value to determine wheatehr to go back to the38 general screen (False) or stay in the current screen (True)39 The general screen is the main screen40 It can directly perform actions or redirect to other screens41"""42def general_screen(option: str, works: list) -> bool:43 res = False44 option = clean_option(option)45 46 # execute actions or goto other screen47 if option == 's':48 filename = input("filename: ")49 save_works(filename, works)50 elif option == 'a':51 res = author_screen(works)52 elif option == 'n':53 res = nation_screen(works)54 elif option == 'd':55 res = date_screen(works)56 elif option == 'l':57 res = hosts_screen(works)58 elif option == 'u':59 res = places_screen(works)60 elif option == 'p':61 print_works(works)62 elif option == '!':63 bot.post_works_on_channel(works, config.channel)64 elif option == 'c':65 print(len(works))66 elif option == 'h':67 show_options("general")68 69 return res70# Print all works in simil-BSL format71def print_works(works: list, no_link=True):72 months = init_months(config.months)73 for work in works:74 bsl = bsl_format(work, months, no_link)75 print(bsl)76### AUTHOR SCREEN ###77"""78 A screen can both have options that don't require further79 user input or options that do require it.80 The input should be asked once, and not repeatedly in more parts81 of the code if it's used by more than 1 or at most 2 options, to make the82 code more readable and the error handling more managable.83"""84def author_screen(works: list) -> bool:85 option = clean_option(input("> "))86 authors = count.count_authors(works)87 specific = list("tnc") # options for a specific author88 res = True89 if option == 'h':90 show_options("author")91 elif option == 'b':92 res = False93 elif option == 'q':94 quit()95 elif option == 'g':96 count.print_sorted_authors(authors)97 elif option == 'l':98 list_authors(authors)99 elif option in specific:100 try:101 author = get_author(authors)102 if option == 't':103 print_works_from(works, author)104 elif option == 'n':105 print(authors[author])106 elif option == 'c':107 author_countries(works, author)108 109 except KeyError as err:110 print(err)111 112 return res113def get_author(authors=None) -> str:114 buffer = input("author: ")115 pseudonyms = init_pseudonyms(config.author_pseudonyms)116 author = resolve_pseudonyms(buffer, pseudonyms)117 if authors != None:118 if author not in authors:119 raise KeyError("No such author")120 121 return author122def list_authors(authors: dict):123 for author in sorted(authors):124 print(author)125def print_works_from(works: list, author: str):126 for work in works:127 if work["author"] == author:128 print(work["title"])129def author_countries(works: list, author: str):130 countries = {}131 for work in works:132 if work["author"] == author:133 country_ls = work["nation"].split('+')134 for country in country_ls:135 if country in countries:136 countries[country] += 1137 else:138 countries[country] = 1139 140 for (country, count_) in sorted(countries.items(), key=lambda x:x[1], reverse=True):141 print("%10s: %4d" % (country, count_))142### NATION SCREEN ###143def nation_screen(works: list):144 option = clean_option(input("> "))145 nations = count.count_nations(works)146 res = True # remain in the screen147 specific = list("nalt") # options for a specific country148 if option == 'h':149 show_options("country")150 elif option == 'b':151 res = False152 elif option == 'q':153 quit()154 elif option == 'g':155 count.print_nations(nations)156 if option in specific:157 try:158 nation = get_nation(nations)159 if option == 'n':160 print(nations[nation])161 elif option == 'a':162 country_authors(works, nation, verbose=True)163 elif option == 'l':164 authors = country_authors(works, nation)165 list_authors(authors)166 elif option == 't':167 country_titles(works, nation, verbose=True)168 169 except KeyError as err:170 print(err)171 172 return res173def get_nation(nations=None) -> str:174 buffer = input("nation: ")175 pseudonyms = init_pseudonyms(config.nation_pseudonyms)176 nation = resolve_pseudonyms(buffer, pseudonyms)177 if nations != None:178 if nation not in nations:179 raise KeyError("No such nation")180 return nation181def country_authors(works: list, nation: str, verbose=False) -> dict:182 authors = {}183 184 for work in works:185 if nation == work["nation"]:186 authors_str = work["author"]187 author_ls = authors_str.split('+')188 for author in author_ls:189 if author in authors:190 authors[author] += 1191 else:192 authors[author] = 1193 194 if verbose:195 for (author, count_) in sorted(authors.items(), key=lambda x:x[1], reverse=True):196 print("%30s: %4s" % (author, count_))197 198 return authors199def country_titles(works: list, nation: str, verbose=False) -> list:200 titles = []201 for work in works:202 if work["nation"] == nation:203 titles.append(work["title"])204 205 if verbose:206 for title in titles:207 print(title)208 209 return titles210### DATE SCREEN ###211def date_screen(works: list):212 option = clean_option(input("> "))213 freq = count.count_frequency(works)214 res = True215 if option == 'q':216 quit()217 elif option == 'b':218 res = False219 elif option == 'h':220 show_options("date")221 elif option == 'c':222 count.print_frequency(freq)223 elif option == 'a':224 count.print_frequency(freq, chron=False)225 elif option == 'p':226 date.plot_works(works)227 elif option == 'd':228 try:229 date_ = get_date()230 matches = date.get_works(works, date=date_)231 print_works(matches)232 except (IndexError, ValueError) as err:233 print(err)234 235 return res236def get_date() -> tuple:237 year = int(input("year [mandatory]: "))238 month = None239 day = None240 241 print("%s[Press enter to stop]" % (" " * len("year ")))242 buffer = input("month: ")243 # parse month244 if buffer.isnumeric():245 month = int(buffer)246 if not 1 <= month <= 12:247 raise ValueError("Invalid month (%d)" % (month)) # ValueError248 elif buffer.isalpha():249 months = init_months(config.months)250 month = months.index(buffer.strip().lower()) + 1 # can raise IndexError251 252 if month != None:253 buffer = input("day: ")254 255 if buffer != "":256 day = int(buffer) # can raise ValueError257 if not 1 <= day <= 31:258 raise ValueError("Invalid day (%d)" % (day))259 260 return (year, month, day)261### HOSTS SCREEN ###262def hosts_screen(works: list) -> bool:263 option = clean_option(input("> "))264 hosts = count.count_hosts(works)265 channels = count.count_channels(works)266 res = True267 if option == 'q':268 quit()269 elif option == 'b':270 res = False271 elif option == 'h':272 show_options("hosts")273 elif option == 'o':274 count.print_hosts(hosts)275 elif option == 'c':276 count.print_channels(channels)277 elif option == 'r':278 broken = links.get_broken_links(works)279 print_works(broken)280 print("TOT: %d" % (len(broken)))281 282 return res283### PLACE SCREEN ###284def places_screen(works: list) -> bool:285 option = clean_option(input("> "))286 places = count.count_places(works)287 specific = list("ta")288 res = True289 if option == 'q':290 quit()291 elif option == 'b':292 res = False293 elif option == 'h':294 show_options("place")295 elif option == 'g':296 count.print_places(places)297 elif option == 'l':298 places = diff.get_item(works, "place", case_sensitive=True)299 for place in sorted(places):300 print(place)301 elif option in specific:302 try:303 place = get_place(places)304 if option == 't':305 titles = diff.get_item(works, "title", case_sensitive=True, pattern={"place": place})306 for title in sorted(titles):307 print(title)308 elif option == 'a':309 authors = diff.get_item(works, "author", case_sensitive=True, pattern={"place": place}, plussplit=True)310 for author in sorted(authors):311 print(author)312 313 except ValueError as err:314 print(err)315 316 return res317def get_place(places: dict) -> str:318 buffer = input("place: ").strip()319 pseudonyms = init_pseudonyms(config.place_pseudonyms)320 place = resolve_pseudonyms(buffer, pseudonyms)321 if place not in places:322 raise ValueError("No such place")323 return place324import bot325if __name__ == "__main__":...

Full Screen

Full Screen

start_daemons.py

Source:start_daemons.py Github

copy

Full Screen

1#!/usr/bin/env python32# -*- encoding: utf-8 -*-3import argparse4import os5import subprocess6if __name__ == "__main__":7 # parse args8 description = ("NetNS Daemon start parameter")9 parser = argparse.ArgumentParser(description=description)10 parser.add_argument("-k", "--keep-screen", help="Keep screen in bash after Daemon exit", dest="keep_screen", action="store_true")11 parser.add_argument("-r", "--redis", help="Redis host", dest="redis_host", default="10.0.1.0")12 parser.add_argument("-u", "--user", help="User", dest="user", default="appaware")13 parser.add_argument("-w", "--working-dir", help="Working directory for logs", dest="working_dir", default="/home/appaware")14 args = vars(parser.parse_args())15 hostname = subprocess.check_output("hostname").decode().strip()16 base_path = os.path.dirname(os.path.abspath(__file__))17 netns_list = subprocess.check_output("ip netns list", shell=True).decode().strip().split('\n')18 if args["keep_screen"]:19 keep_screen = "; /bin/bash"20 else:21 keep_screen = ""22 redis_host = "--redis {}".format(args["redis_host"])23 working_dir = "--working-dir {}".format(args["working_dir"])24 user = args["user"]25 # start daemon on physical host26 daemon_id = "{}_host0".format(hostname)27 cmd = 'screen -dmS {} bash -c "sudo -H -u {} {}/Daemon.py --id {} {} {} {}"'.format(daemon_id, user, base_path, daemon_id, working_dir, redis_host, keep_screen)28 print("running {}".format(cmd))29 subprocess.call(cmd, shell=True)30 # start dameons on virtual hosts31 for netns in netns_list:32 netns_name = netns.split(' ')[0]33 daemon_id = "{}_{}".format(hostname, netns_name)34 cmd = 'screen -dmS {} bash -c "sudo ip netns exec {} sudo -H -u {} {}/Daemon.py --id {} {} {} {}"'.format(daemon_id, netns_name, user, base_path, daemon_id, working_dir, redis_host, keep_screen)35 print("running {}".format(cmd))...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run ATX automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful