def search(args, nick): url = "http://www.urbandictionary.com/define.php?term=" + common.quote(args) content = common.read_url(url) match = re.search(r'<div class="definition">(.+?)</div>.*?<div class="example">(.+?)</div>', content) try: definition = match.group(1) example = match.group(2) except AttributeError: result = "Inga träffar. ({0})".format(url) else: def fixhtml(str): str = re.sub(r'(<br ?/>)+', r' ', str) str = re.sub('\n', r' ', str) str = re.sub('\r', r' ', str) str = re.sub(r'(<.+?>)+', r'', str) return common.unescape_html(str) # magic constants used when calculating space_left: # * 450 -- the smallest length of an irc message (hopefully; depends on server.) # * 20 -- the longest channel name including # (hopefully; i have no idea) # * 16 -- the length of the static crap in the result string (definitely) space_left = (450 - len('PRIVMSG :') - 20 - len(nick + ': ') - len(url) - 16) definition = common.truncate(fixhtml(definition), int(space_left/2)) example = common.truncate(fixhtml(example), int(space_left/2)) result = '{0} (Exempel: {1}) -- {2}'.format(definition, example, url) return result
def list_fmt(L): max_width, max_height = os.get_terminal_size() max_height = int(os.get_terminal_size().lines / 2) half_height = int(max_height / 2) list_length = len(L) space_count = len(str(list_length)) + 2 max_width = max_width - space_count - 5 if len(str(L)) < max_width: return str(L) elif list_length > max_height: lines = [] for i in range(0, half_height): L_string = truncate(str(L[i]), max_width) padded_i = f"[{i}]".rjust(space_count) lines.append(f"{padded_i}: {L_string}") lines.append(f"{' ' * space_count} ...") for i in range(list_length - half_height, list_length): L_string = truncate(str(L[i]), max_width) padded_i = f"[{i}]".rjust(space_count) lines.append(f"{padded_i}: {L_string}") return "\n".join(lines) else: lines = [] for i in range(0, list_length): L_string = truncate(str(L[i]), max_width) padded_i = f"[{i}]".rjust(space_count) lines.append(f"{padded_i}: {L_string}") return "\n".join(lines)
def xkcd_info(url, nick): """ Return the transcript and title of an xkcd page. """ try: data = common.read_url(url) except HTTPError: return '{}: kunde inte ladda sidan: {}'.format(nick, url) title_re = re.compile(r'<title>xkcd: (.+?)</div>') titlebackup_re = re.compile(r'<div id="ctitle">(.+?)</div>') transcript_re = re.compile(r'<div id="transcript" .+?>(?P<transcript>.*?)(\{\{(?P<alt>.+?)\}\})?</div>', re.DOTALL) # Transcript result = transcript_re.search(data) transcript = [line.strip() for line in result.group('transcript').splitlines() if line.strip()] if not transcript: transcript = ['Ingen beskrivning än!'] # Unused for now - also borken if no transcript is available # alttext = result.group('alt').strip() # Title title = title_re.search(data) if not title: title = titlebackup_re.search(data) firstline = '{} – {}'.format(title.group(1), url) return [common.truncate(common.unescape_html(x), 400) for x in [firstline] + transcript[:3]]
def pretty(self): from ic import Table from common import truncate dictionaries = [("i", "method", "local_count", "locals")] dictionaries = dictionaries + [ (i, D.method, D.local_count, truncate(str(D.locals), 120)) for i, D in enumerate(self.frames) ] table = Table() for row in dictionaries: table.row(row)
def print_variables(D): import types from common import truncate from ic import ic exclude_types = [ types.FunctionType, types.ModuleType, types.BuiltinFunctionType, type ] table = [{ "name": k, "type": type(v), "value": truncate(str(v), 160) } for k, v in D.items() if type(v) not in exclude_types and k[0:2] != "__" and str(v)[0] != "<" and not isinstance(v, Exception)] ic(table)
def fmt(cells, column_tuples): S = self.separator G = self.gutter all_strings_are_single_line = all("\n" not in str(cell) for cell in cells) if all_strings_are_single_line: return G + S.join( just(truncate(str(cell), width), width) for cell, (width, just) in zip(cells, column_tuples)) else: cells = [str(cell) for cell in cells] largest_height = max(len(cell.split("\n")) for cell in cells) v_pad_cells = [ v_pad(cell.split("\n"), largest_height) for cell in cells ] return_lines = [] for i in range(largest_height): parts = [v_pad_cell[i] for v_pad_cell in v_pad_cells] parts = [ just(truncate(part, width), width) for part, (width, just) in zip(parts, column_tuples) ] return_lines.append(G + S.join(parts)) return "\n".join(return_lines)
def pp_info(url, nick): """ Return the transcript and link to the image of a Profound Programmer page. """ try: data = common.read_url(url) except HTTPError: return '{}: kunde inte ladda sidan: {}'.format(nick, url) main_re = re.compile(r""" <li\ class="post\ photo"> \s* <img\ src="(?P<img>.+?)" .+? <div\ class="caption"><p> \[(?P<transcript>.+?)\] </p> \s* (<p><a\ href="(?P<hdimg>.+?)">\[HD\ Version\]</a>)? """, re.DOTALL | re.VERBOSE) transcript_re = re.compile(r'text\s?:? (“(?P<title1>.+?)”|‘(?P<title2>.+?)’)?([,;] )?(?P<transcript>.+)', re.DOTALL) result = main_re.search(data) if not result: print(url) raise AttributeError('.profound could not match the regex! Has theprofoundprogrammer.org change format?') rawtranscript = transcript_re.match(common.unescape_html(sanitize(result.group('transcript')))) title = None if rawtranscript: for t in ('title1', 'title2'): if rawtranscript.group(t): title = rawtranscript.group(t) transcript = rawtranscript.group('transcript') else: transcript = common.unescape_html(result.group('transcript')) if result.group('hdimg'): image = result.group('hdimg') else: image = result.group('img') out = ['[{}]'.format(transcript)] + [image] if title: out = ['"{}"'.format(title)] + out return [common.truncate(x, 400) for x in out]
def move_extension(extension, folder_name): glob_pattern = "{downloads_directory}/*.{extension}".format( downloads_directory=global_downloads_directory, extension=extension) input_paths = glob(glob_pattern) if input_paths == []: return basenames = [ os.path.basename(absolute_path) for absolute_path in input_paths ] output_paths = [ os.path.join(global_downloads_directory, folder_name, basename) for basename in basenames ] for input_path, output_path in zip(input_paths, output_paths): os.rename(input_path, output_path) message = "[MOVE] Moved {count} {extension} files to {folder_name}: {trunc_files}".format( count=make_string_green(len(basenames)), extension=make_string_green(extension), folder_name=make_string_green(folder_name), trunc_files=truncate(", ".join(basenames), 80)) print(message)
def custom_str(v): if is_function(v): return v.__doc__.strip().split( "\n")[0] if v.__doc__ else "No __doc__" else: return truncate(str(v), 160)
def insert_dictionaries(cursor, table_name, dictionaries, constraint_D={}): if dictionaries == []: return print_statements = False dictionaries = [prepare_dict(D) for D in dictionaries] dictionaries = add_date_column(dictionaries) dictionary_schema = Schema(dictionaries) table_schema = get_schema(cursor, table_name) table_does_not_exist = len(table_schema) == 0 # 1. CREATE TABLE IF IT DOES NOT EXIST if table_does_not_exist: create_table_statement = f"CREATE TABLE {table_name} ({dictionary_schema.column_definitions(constraint_D=constraint_D)});" cursor.execute(create_table_statement) if print_statements: print(create_table_statement) # 2. ADD NEW COLUMNS add_column_statements = add_new_columns(cursor, table_name, dictionary_schema) batch_execute(cursor, add_column_statements, print_statements) # 3. MODIFY UPDATED COLUMN TYPES modify_column_statements = modify_existing_column_types( cursor, table_name, dictionary_schema, constraint_D=constraint_D) batch_execute(cursor, modify_column_statements, print_statements) # 4.1. SETUP VCS TABLE {{{ vcs_print_statements = False vcs_table_name = f"{table_name}_vcs" vcs_table_schema = get_schema(cursor, vcs_table_name) vcs_table_does_not_exist = len(vcs_table_schema) == 0 if vcs_table_does_not_exist: create_vcs_table_statement = f"CREATE TABLE {vcs_table_name} ({dictionary_schema.column_definitions()});" cursor.execute(create_vcs_table_statement) if vcs_print_statements: print(create_vcs_table_statement) vcs_add_column_statements = add_new_columns(cursor, vcs_table_name, dictionary_schema) batch_execute(cursor, vcs_add_column_statements, vcs_print_statements) vcs_modify_column_statements = modify_existing_column_types( cursor, vcs_table_name, dictionary_schema) batch_execute(cursor, vcs_modify_column_statements, vcs_print_statements) # }}} # 4.2. ADD VCS TRIGGER trigger_name = f"update_trigger_{table_name}_vcs" if len(vcs_add_column_statements) > 0 or len( vcs_modify_column_statements) > 0: # SNIPPET 1 for debug cursor.execute(f"DROP TRIGGER IF EXISTS {trigger_name}") if vcs_print_statements: print(f"DROP TRIGGER IF EXISTS {trigger_name}") trigger_exists = check_if_trigger_exists(cursor, table_name, trigger_name) if not trigger_exists: primary_key_supplied = len([ value for value in constraint_D.values() if value.index("PRIMARY KEY") > -1 ]) == 1 if primary_key_supplied: create_trigger_statement = get_create_trigger_statement( cursor, table_name, trigger_name, vcs_table_name) cursor.execute(create_trigger_statement) if vcs_print_statements: print(create_trigger_statement) # 5. INSERT for D in dictionaries: insert_statement, values = get_insert_pair_with_on_conflict_clause( D, table_name) try: cursor.execute(insert_statement, values) except Exception as e: print("[ERROR]", insert_statement) raise if print_statements: print(insert_statement, make_string_green(truncate(str(values), 200))) print()