def test_colorize(): with mock_stdout(): pastel.with_colors(False) assert 'some info' == pastel.colorize('<info>some info</info>') pastel.with_colors(True) assert '\033[32msome info\033[0m' == pastel.colorize('<info>some info</info>')
def test_colorize(): with mock_stdout(): pastel.with_colors(False) assert "some info" == pastel.colorize("<info>some info</info>") pastel.with_colors(True) assert "\033[32msome info\033[0m" == pastel.colorize( "<info>some info</info>")
def test_add_remove_style(): with mock_stdout(): pastel.add_style('success', 'green') assert '\033[32msome info\033[0m' == pastel.colorize('<success>some info</success>') pastel.remove_style('success') assert '<success>some info</success>' == pastel.colorize('<success>some info</success>')
def test_add_remove_style(): with mock_stdout(): pastel.add_style("success", "green") assert "\033[32msome info\033[0m" == pastel.colorize( "<success>some info</success>") pastel.remove_style("success") assert "<success>some info</success>" == pastel.colorize( "<success>some info</success>")
def _function(msg: str, data1=None, data2=None): msg = msg.format( f'<{style_value_primary}>{data1}</{style_value_primary}>' if data1 is not None else '', f'<{style_value_secondary}>{data2}</{style_value_secondary}>' if data2 is not None else '') if self.level >= level: print( pastel.colorize(f'<{style_normal}>{msg}</{style_normal}>'))
import pastel import sys import codecs block_size = 1 print(pastel.colorize('<fg=blue;option=bold> <-----------------> ')) print(pastel.colorize('<fg=blue;option=bold> <---------------------> ')) print(pastel.colorize('<fg=blue;option=bold> <----------------------->')) print(pastel.colorize('<fg=blue;option=bold><----> '), pastel.colorize('<fg=white;option=bold>FM_Z /// DX7'),pastel.colorize('<fg=blue;option=bold> <--->')) print(pastel.colorize('<fg=blue;option=bold> <--->'), pastel.colorize('<fg=white;option=bold> SYSEX DECODER'), pastel.colorize('<fg=blue;option=bold> <-->')) print(pastel.colorize('<fg=blue;option=bold> <---------------------> ')) print(pastel.colorize('<fg=blue;option=bold> <-----------------> ')) print(' ') filename = sys.argv[1] print(pastel.colorize('<fg=white;options=bold>Opening : '), filename) sysex = open(filename, "rb") print("Done!") print(' ') print(pastel.colorize('<fg=red;option=bold>----------------------- Header -----------------------')) print(' ') sysex.seek(0) #Go to the start of file block = int.from_bytes(sysex.read(block_size), byteorder='big') if block == 240 :
from openpyxl import load_workbook import pastel print("DX7 Sysex Python Tool") sysex = input(pastel.colorize('<fg=white;options=bold>Sysex file : ')) header_file = open('cartridge.h', "w") xls = load_workbook('cartridge.xlsx') print(xls.get_sheet_names()) ws1 = xls.active ws1['B2'] = 32 ws1.title = "DX7 ROM CARTRIDGE" xls.save("cartridge.xlsx")
def run(): args = docopt("""Usage: automation_parcoursup.py [options] -c --cache Use the cache (in ~/.cache/automation-parcoursup/admission.html) -B --no-browser Hide the browser -C --credentials=FILE Use the following file (.env format) for parcoursup credentials. keys: PARCOURSUP_ID (N° de dossier) and PARCOURSUP_PASS (Mot de passe) """) parcoursup_id = None parcoursup_pass = None if args['--credentials']: load_dotenv(path.expanduser(args['--credentials'])) parcoursup_id = getenv('PARCOURSUP_ID', None) parcoursup_pass = getenv('PARCOURSUP_PASS', None) else: parcoursup_id = input('N° de dossier: ') parcoursup_pass = input('Mot de passe: ') if parcoursup_id is None: print(colorize('<fg=red><options=bold>ERREUR</options=bold> Veuillez préciser votre numéro de dossier (pour le fichier: PARCOURSUP_ID="Votre numéro")')) if parcoursup_pass is None: print(colorize('<fg=red><options=bold>ERREUR</options=bold> Veuillez préciser votre mot de passe (pour le fichier: PARCOURSUP_PASS="******")')) if parcoursup_id is None or parcoursup_pass is None: exit(1) # Récup le cache si présent cachedir = path.expanduser('~/.cache/automation-parcoursup') if not path.exists(cachedir): makedirs(cachedir) yyyymmdd = date.today().isoformat() if path.exists(path.join(cachedir, 'data.json')): with open(path.join(cachedir, 'data.json'), 'r') as file: raw = file.read() or '{}' wishes_data = json.loads(raw) wishes_data[yyyymmdd] = [] else: wishes_data = { yyyymmdd: [] } cachefile = path.join(cachedir, 'admission.html') if path.exists(cachefile) and args['--cache']: soup = BeautifulSoup(open(cachefile).read(), features='lxml') else: browser = start_chrome('https://dossierappel.parcoursup.fr/Candidat/authentification', headless=args['--no-browser']) # Login write(parcoursup_id, into='N° de dossier') write(parcoursup_pass, into='Mot de passe') click('Connexion') # On attend le chargement de la page sleep(3) # On récup l'HTML de la page raw_html = S('body').web_element.get_attribute('innerHTML') # Fermer le navigateur, on en a plus besoin browser.close() # On écrit le cache with open(cachefile, 'w') as file: file.write(raw_html) # On écrit l'archive with open(path.join(cachedir, f'admission-{yyyymmdd}.html'), 'w') as file: file.write(raw_html) # On parse l'HTML avec bs4 soup = BeautifulSoup(raw_html, features='lxml') # On récupère les vœux wishes = soup.find(id='voeux_enattente').find_all('tr', class_='voeu') # Pour chaque vœux for wish in wishes: # Récup l'id wish_id = wish['id'].replace('-', '_') # Récup le nom cells = wish.find_all('td') wish_name = cells[2].string.strip().replace('\n', ' ').replace('\t', ' ') # Enlever les espaces dupliqués wish_name = re.sub(r' {2,}', ' ', wish_name) # Récup les rangs dans la popup popup = soup.find('div', id=f'lst_att_{wish_id}') print(f'Scanning {wish_id}') is_internat = 'internat' in popup.find_all('ul')[0].find_all('li')[0].contents[0] # Initial set group_capacity = None rank = None waitlist_length = None group_rank = None max_admitted_rank = None last_year_max_admitted_rank = None internat_capacity = None internat_group_waitlist_rank = None internat_rank = None internat_condition_group_waitlist_rank = None internat_condition_rank = None # Traverse DOM lists get_number = lambda ul_idx, li_idx: int(popup.find_all('ul')[ul_idx].find_all('li')[li_idx].find('span', class_='strong').string) if not is_internat: group_capacity = get_number(0, 0) rank = get_number(1, 0) waitlist_length = get_number(1, 1) group_rank = get_number(2, 0) max_admitted_rank = get_number(2, 1) last_year_max_admitted_rank = get_number(2, 2) else: internat_capacity = get_number(0, 0) internat_group_waitlist_rank = get_number(2, 0) internat_rank = get_number(2, 1) internat_situation = re.search(r'(?P<group>\d+)\s*ET[^\d]*(?P<rank>\d+)', popup.find(id="rang_cddt").find_all('p')[0].string).groupdict() internat_condition_group_waitlist_rank = int(internat_situation['group']) internat_condition_rank = int(internat_situation['rank']) # Add to wishes_data wishes_data[yyyymmdd].append({ 'id': wish_id, 'name': wish_name, 'is_internat': is_internat, 'ranks': { 'group_capacity': group_capacity, 'rank': rank, 'waitlist_length': waitlist_length, 'group_rank': group_rank, 'max_admitted_rank': max_admitted_rank, 'last_year_max_admitted_rank': last_year_max_admitted_rank, }, 'internat': { 'capacity' : internat_capacity, 'group_waitlist_rank' : internat_group_waitlist_rank, 'rank' : internat_rank, 'condition_group_waitlist_rank' : internat_condition_group_waitlist_rank, 'condition_rank' : internat_condition_rank, } }) # Output JSON with open(path.join(cachedir, 'data.json'), 'w') as file: file.write(json.dumps(wishes_data, indent=2))
def run(**cli_args): args = cli_args parcoursup_id = None parcoursup_pass = None if args["--credentials"]: load_dotenv(path.expanduser(args["--credentials"])) parcoursup_id = getenv("PARCOURSUP_ID", None) parcoursup_pass = getenv("PARCOURSUP_PASS", None) else: parcoursup_id = input("N° de dossier: ") parcoursup_pass = input("Mot de passe: ") if parcoursup_id is None: print( colorize( '<fg=red><options=bold>ERREUR</options=bold> Veuillez préciser votre numéro de dossier (pour le fichier: PARCOURSUP_ID="Votre numéro")' )) if parcoursup_pass is None: print( colorize( '<fg=red><options=bold>ERREUR</options=bold> Veuillez préciser votre mot de passe (pour le fichier: PARCOURSUP_PASS="******")' )) if parcoursup_id is None or parcoursup_pass is None: exit(1) # Récup le cache si présent cachedir = path.expanduser("~/.cache/parcoursup-dataviz") datadir = path.expanduser("~/.parcoursup-dataviz") if not path.exists(cachedir): makedirs(cachedir) yyyymmdd = date.today().isoformat() data_filepath = path.join(datadir, f"data.json") html_cache_filepath = path.join(cachedir, f"{yyyymmdd}-page.html") if path.exists(data_filepath): with open(path.join(cachedir, data_filepath), "r", encoding='utf-8') as file: raw = file.read() or "{}" wishes_data = json.loads(raw) wishes_data[yyyymmdd] = [] else: wishes_data = {yyyymmdd: []} if args["--html"] and args["--in"]: soup = BeautifulSoup(open(args["--in"], encoding='utf-8').read(), features="lxml") elif path.exists(html_cache_filepath) and not args["--no-cache"]: soup = BeautifulSoup(open(html_cache_filepath, encoding='utf-8').read(), features="lxml") else: browser = start_chrome( "https://dossierappel.parcoursup.fr/Candidat/authentification", headless=args["--no-browser"], ) # Login write(parcoursup_id, into="N° de dossier") write(parcoursup_pass, into="Mot de passe") click("Connexion") # On attend le chargement de la page sleep(3) # On récup l'HTML de la page raw_html = S("body").web_element.get_attribute("innerHTML") # Fermer le navigateur, on en a plus besoin browser.close() # On écrit le cache with open(html_cache_filepath, "w", encoding='utf-8') as file: file.write(raw_html) # On parse l'HTML avec bs4 soup = BeautifulSoup(raw_html, features="lxml") # On récupère les vœux wishes = soup.find(id="voeux_enattente").find_all("tr", class_="voeu") # Pour chaque vœux for wish in wishes: # Récup l'id wish_id = wish["id"].replace("-", "_") # Récup le nom cells = wish.find_all("td") wish_name = cells[2].string.strip().replace("\n", " ").replace("\t", " ") # Enlever les espaces dupliqués wish_name = re.sub(r" {2,}", " ", wish_name) # Récup les rangs dans la popup popup = soup.find("div", id=f"lst_att_{wish_id}") is_internat = ( "internat" in popup.find_all("ul")[0].find_all("li")[0].contents[0]) # Initial set group_capacity = None rank = None waitlist_length = None calllist_rank = None max_admitted_rank = None last_year_max_admitted_rank = None internat_capacity = None internat_group_waitlist_rank = None internat_rank = None internat_condition_group_waitlist_rank = None internat_condition_rank = None # Traverse DOM lists get_number = lambda ul_idx, li_idx: int( popup.find_all("ul")[ul_idx].find_all("li")[li_idx].find( "span", class_="strong").string) if not is_internat: group_capacity = get_number(0, 0) rank = get_number(1, 0) waitlist_length = get_number(1, 1) calllist_rank = get_number(2, 0) max_admitted_rank = get_number(2, 1) last_year_max_admitted_rank = get_number(2, 2) else: internat_capacity = get_number(0, 0) internat_group_waitlist_rank = get_number(2, 0) internat_rank = get_number(2, 1) internat_situation = re.search( r"(?P<group>\d+)\s*ET[^\d]*(?P<rank>\d+)", popup.find(id="rang_cddt").find_all("p")[0].string, ).groupdict() internat_condition_group_waitlist_rank = int( internat_situation["group"]) internat_condition_rank = int(internat_situation["rank"]) # Add to wishes_data wishes_data[yyyymmdd].append({ "id": wish_id, "name": wish_name, "is_internat": is_internat, "ranks": { "group_capacity": group_capacity, "rank": rank, "waitlist_length": waitlist_length, "calllist_rank": calllist_rank, "max_admitted_rank": max_admitted_rank, "last_year_max_admitted_rank": last_year_max_admitted_rank, }, "internat": { "capacity": internat_capacity, "group_waitlist_rank": internat_group_waitlist_rank, "rank": internat_rank, "condition_group_waitlist_rank": internat_condition_group_waitlist_rank, "condition_rank": internat_condition_rank, }, }) # Output JSON makedirs(path.dirname(data_filepath), exist_ok=True) with open(data_filepath, "w", encoding='utf-8') as file: file.write(json.dumps(wishes_data, indent=2)) return wishes_data
def wrapper(*args) -> None: print(pastel.colorize(function(*args)), end='', flush=True)
def wrapper(*args, **kwargs) -> None: print(pastel.colorize(function(*args, **kwargs)))
def update_index (payload): print (pastel.colorize('<fg=blue>updating index on algolia...\n</>')) algolia_index.add_objects(payload)
algolia_index.add_objects(payload) try: sys.setdefaultencoding('utf-8') algolia_client = algoliasearch.Client(algolia_app_id, algoia_api_key) algolia_index = algolia_client.init_index(algolia_index_name) api_response = requests.get(hubspot_blog_api_base_url + hubspot_api_key + hubspot_api_params + hubspot_blog_id) blog_data = api_response.json() # First up - clear the index so any deleted blog posts will no longer be present. # Ref: https://www.algolia.com/doc/api-reference/api-methods/clear-index/ algolia_index.clear_index() # iterate over all posts in the hubspot blog for post in blog_data['objects']: print (pastel.colorize('<fg=red>dealing with post titled: ' + post['html_title'])) # batch is used to collect updates to be made in algolia index for each blog post batch = [] segment = collections.OrderedDict() segment["objectID"] = post['id'] for key in post.items(): if key[0] in hubspot_post_params: if type(key[1]) is not int: segment[key[0]] = str(strip_tags(key[1])) # segment[key[0]] = str(key[1]) else: segment[key[0]] = key[1] batch.append(segment) update_index(batch) print (pastel.colorize('<fg=white;bg=blue;options=bold>Done processing ' + str(len(blog_data['objects'])) + ' posts!</>\n'))
def test_text(): with mock_stdout(): assert "\033[32msome info\033[0m" == pastel.colorize( "<info>some info</info>")
import pastel import sys import codecs from time import sleep block_size = 1 print(pastel.colorize('<fg=blue;option=bold> <-----------------> ')) print(pastel.colorize('<fg=blue;option=bold> <---------------------> ')) print(pastel.colorize('<fg=blue;option=bold> <----------------------->')) print(pastel.colorize('<fg=blue;option=bold><----> '), pastel.colorize('<fg=white;option=bold>FM_Z /// DX7'), pastel.colorize('<fg=blue;option=bold> <--->')) print(pastel.colorize('<fg=blue;option=bold> <--->'), pastel.colorize('<fg=white;option=bold> SYSEX DECODER'), pastel.colorize('<fg=blue;option=bold> <-->')) print(pastel.colorize('<fg=blue;option=bold> <---------------------> ')) print(pastel.colorize('<fg=blue;option=bold> <-----------------> ')) print(' ') filename = input(pastel.colorize('<fg=white;options=bold>Sysex file : ')) print(pastel.colorize('<fg=white;options=bold>Opening : '), filename) sysex = open(filename, "rb") print(pastel.colorize('<fg=white;options=bold>Creating : '), 'cartridges.h') header_file = open('../../src/cartridge.h', "w") #Find if it is 1 or 32 voice dump sysex.seek(3) block = sysex.read(block_size)
if type(key[1]) == unicode and string_to_replace in key[1]: corrected_data = {} print bcolors.BOLD + "\nFound string in item: " + key[ 0] + bcolors.ENDC json_to_string = str(key[1].encode('utf-8', 'replace')) json_to_string = json_to_string.replace( string_to_replace, replacement_string) corrected_data[key[0]] = json_to_string print bcolors.BLUE + "Original content:\n" + key[ 1] + bcolors.ENDC print bcolors.GREEN + "Updated content:\n" + json_to_string + bcolors.ENDC if save_local is True: save_post(post) update_post(json.dumps(corrected_data), post['analytics_page_id']) if 'corrected_data' not in locals(): print bcolors.FAIL + "\nFound nothing to update in this post...\n" + bcolors.ENDC proceed = raw_input( bcolors.WARNING + "\nContinue to the next post (enter to continue or ctrl-d to abort)?" + bcolors.ENDC) os.system('clear') except requests.exceptions.RequestException as e: print( pastel.colorize( '<fg=yellow;bg=red;options=bold>Connection error while making API call to HubSpot! ' + e + ' Aborted.</>\n')) sys.exit(1)
def test_text(): with mock_stdout(): assert '\033[32msome info\033[0m' == pastel.colorize('<info>some info</info>')