def ensure_explicit_namespace(fpath, namespace, varname_list): import re import utool as ut text = ut.read_from(fpath) orig_text = text new_text = text for varname in varname_list: regex = ''.join(( ut.named_field('prefix', '[^.]'), ut.named_field('var', ut.whole_word(varname)), )) repl = ''.join(( ut.bref_field('prefix'), namespace, '.', ut.bref_field('var') )) new_text = re.sub(regex, repl, new_text) textdiff = ut.get_textdiff(orig_text, new_text) print(textdiff) if ut.user_cmdline_prompt('Does the text look good?'): # if diff looks good write ut.write_to(fpath, new_text)
def update_wildbook_ia_config(ibs, wildbook_tomcat_path, dryrun=False): """ #if use_config_file and wildbook_tomcat_path: # # Update the Wildbook configuration to see *THIS* wbia database # with lockfile.LockFile(lock_fpath): # update_wildbook_ia_config(ibs, wildbook_tomcat_path, dryrun) """ wildbook_properteis_dpath = join(wildbook_tomcat_path, 'WEB-INF/classes/bundles/') logger.info('[ibs.update_wildbook_ia_config()] Wildbook properties=%r' % (wildbook_properteis_dpath, )) # The src file is non-standard. It should be remove here as well wildbook_config_fpath_dst = join(wildbook_properteis_dpath, 'commonConfiguration.properties') ut.assert_exists(wildbook_properteis_dpath) # for come reason the .default file is not there, that should be ok though orig_content = ut.read_from(wildbook_config_fpath_dst) content = orig_content # Make sure wildbook knows where to find us if False: # Old way of telling WB where to find IA content = re.sub('IBEIS_DB_path = .*', 'IBEIS_DB_path = ' + ibs.get_db_core_path(), content) content = re.sub('IBEIS_image_path = .*', 'IBEIS_image_path = ' + ibs.get_imgdir(), content) web_port = ibs.get_web_port_via_scan() if web_port is None: raise ValueError('IA web server is not running on any expected port') ia_hostport = 'http://localhost:%s' % (web_port, ) ia_rest_prefix = ut.named_field('prefix', 'IBEISIARestUrl.*') host_port = ut.named_field('host_port', 'http://.*?:[0-9]+') content = re.sub(ia_rest_prefix + host_port, ut.bref_field('prefix') + ia_hostport, content) # Write to the configuration if it is different if orig_content != content: need_sudo = not ut.is_file_writable(wildbook_config_fpath_dst) if need_sudo: quoted_content = '"%s"' % (content, ) logger.info( 'Attempting to gain sudo access to update wildbook config') command = [ 'sudo', 'sh', '-c', "'", 'echo', quoted_content, '>', wildbook_config_fpath_dst, "'", ] # ut.cmd(command, sudo=True) command = ' '.join(command) if not dryrun: os.system(command) else: ut.write_to(wildbook_config_fpath_dst, content)
def load_list(decklist_text, mydiff): lines = decklist_text.split('\n') line_regex = ut.named_field('num', r'\d+') + ' ' + ut.named_field('cardname', r'.*') line_re = re.compile(line_regex) def parse_line(line): match = line_re.match(line) groupdict_ = match.groupdict() return [groupdict_['cardname']] * int(groupdict_['num']) cardname_list = ut.flatten([ parse_line(line) #[line[2:]] * int(line[0]) for line in lines if ( len(line) > 0 and not line.startswith('SB') and not line.startswith('#') ) ]) card_list = load_cards(cardname_list) #card_list = list(map(upgrade_card, card_list)) deck = Deck(card_list) print('len(deck) = %r' % (len(deck),)) # APPLY DIFF if False: print('Apply diff') new_cardlist = deck.card_list[:] for cardname in mydiff.split('\n'): if len(cardname) == 0: continue sign = cardname[0] num = cardname[1] cardname = cardname[3:] if sign == '+': for _ in range(int(num)): new_cardlist.append(lookup_card(cardname)) elif sign == '-': for _ in range(int(num)): card_ = None for card__ in new_cardlist: if card__.name == cardname: card_ = card__ break if card_ is None: assert False, 'cannot remove nonexistant' new_cardlist.remove(card_) deck = Deck(new_cardlist) return deck
def _find_wbia_attrs(ibs, objname, blacklist=[]): r""" Developer function to help figure out what attributes are available Args: ibs (wbia.IBEISController): images analysis api CommandLine: python -m wbia.images _find_wbia_attrs Example: >>> # DISABLE_DOCTEST >>> from wbia._wbia_object import * # NOQA >>> import wbia >>> ibs = wbia.opendb(defaultdb='testdb1') >>> objname = 'images' >>> blacklist = [] >>> _find_wbia_attrs(ibs, objname, blacklist) Example: >>> # DISABLE_DOCTEST >>> from wbia._wbia_object import * # NOQA >>> import wbia >>> ibs = wbia.opendb(defaultdb='testdb1') >>> objname = 'annot' >>> blacklist = ['annot_pair'] >>> _find_wbia_attrs(ibs, objname, blacklist) """ import re getter_prefix = 'get_' + objname + '_' found_getters = ut.search_module(ibs, getter_prefix) pat = getter_prefix + ut.named_field('attr', '.*') for stopword in blacklist: found_getters = [fn for fn in found_getters if stopword not in fn] matched_getters = [ re.match(pat, fn).groupdict()['attr'] for fn in found_getters ] setter_prefix = 'set_' + objname + '_' found_setters = ut.search_module(ibs, setter_prefix) pat = setter_prefix + ut.named_field('attr', '.*') for stopword in blacklist: found_setters = [fn for fn in found_setters if stopword not in fn] matched_setters = [ re.match(pat, fn).groupdict()['attr'] for fn in found_setters ] return matched_getters, matched_setters
def preprocess_research(input_str): """ test of an em --- dash test of an em — dash """ import utool as ut inside = ut.named_field('ref', '.*?') input_str = re.sub(r'\\emph{' + inside + '}', ut.bref_field('ref'), input_str) # input_str = input_str.decode('utf-8') input_str = ut.ensure_unicode(input_str) pause = re.escape(' <break time="300ms"/> ') # pause = ', ' emdash = u'\u2014' # # print('input_str = %r' % (input_str,)) # print('emdash = %r' % (emdash,)) # print('emdash = %s' % (emdash,)) input_str = re.sub('\s?' + re.escape('---') + '\s?', pause, input_str) input_str = re.sub('\s?' + emdash + '\s?', pause, input_str) # print('input_str = %r' % (input_str,)) input_str = re.sub('\\\\cite{[^}]*}', '', input_str) input_str = re.sub('et al.', 'et all', input_str) # Let rob say et al. input_str = re.sub(' i\.e\.', ' i e ' + pause, input_str) # Let rob say et al. input_str = re.sub(r'\\r', '', input_str) # input_str = re.sub(r'\\n', '', input_str) # input_str = re.sub('\\\\', '', input_str) # #input_str = re.sub('[a-z]?[a-z]', 'et all', input_str) # Let rob say et al. input_str = re.sub('\\.[^a-zA-Z0-1]+', '.\n', input_str) # Split the document at periods input_str = re.sub('\r\n', '\n', input_str) input_str = re.sub('^ *$\n', '', input_str) input_str = re.sub('\n\n*', '\n', input_str) return input_str
def update_wildbook_ia_config(ibs, wildbook_tomcat_path, dryrun=False): """ #if use_config_file and wildbook_tomcat_path: # # Update the Wildbook configuration to see *THIS* ibeis database # with lockfile.LockFile(lock_fpath): # update_wildbook_ia_config(ibs, wildbook_tomcat_path, dryrun) """ wildbook_properteis_dpath = join(wildbook_tomcat_path, 'WEB-INF/classes/bundles/') print('[ibs.update_wildbook_ia_config()] Wildbook properties=%r' % ( wildbook_properteis_dpath, )) # The src file is non-standard. It should be remove here as well wildbook_config_fpath_dst = join(wildbook_properteis_dpath, 'commonConfiguration.properties') ut.assert_exists(wildbook_properteis_dpath) # for come reason the .default file is not there, that should be ok though orig_content = ut.read_from(wildbook_config_fpath_dst) content = orig_content # Make sure wildbook knows where to find us if False: # Old way of telling WB where to find IA content = re.sub('IBEIS_DB_path = .*', 'IBEIS_DB_path = ' + ibs.get_db_core_path(), content) content = re.sub('IBEIS_image_path = .*', 'IBEIS_image_path = ' + ibs.get_imgdir(), content) ia_hostport = 'http://localhost:5000' ia_rest_prefix = ut.named_field('prefix', 'IBEISIARestUrl.*') host_port = ut.named_field('host_port', 'http://.*?:[0-9]+') content = re.sub(ia_rest_prefix + host_port, ut.bref_field('prefix') + ia_hostport, content) # Write to the configuration if it is different if orig_content != content: need_sudo = not ut.is_file_writable(wildbook_config_fpath_dst) if need_sudo: quoted_content = '"%s"' % (content, ) print('Attempting to gain sudo access to update wildbook config') command = ['sudo', 'sh', '-c', '\'', 'echo', quoted_content, '>', wildbook_config_fpath_dst, '\''] # ut.cmd(command, sudo=True) command = ' '.join(command) if not dryrun: os.system(command) else: ut.write_to(wildbook_config_fpath_dst, content)
def get_block_id(block): import re fpath_regex = ut.named_field('fpath', '\S+') funcname_regex = ut.named_field('funcname', '\S+') lineno_regex = ut.named_field('lineno', '[0-9]+') fileline_regex = 'File: ' + fpath_regex + '$' funcline_regex = 'Function: ' + funcname_regex + ' at line ' + lineno_regex + '$' fileline_match = re.search(fileline_regex, block, flags=re.MULTILINE) funcline_match = re.search(funcline_regex, block, flags=re.MULTILINE) if fileline_match is not None and funcline_match is not None: fpath = fileline_match.groupdict()['fpath'] funcname = funcline_match.groupdict()['funcname'] lineno = funcline_match.groupdict()['lineno'] block_id = funcname + ':' + fpath + ':' + lineno else: block_id = 'None:None:None' return block_id
def modify_code_indent_formatdict(code, locals_): # Parse out search and replace locations in code ncl1 = ut.negative_lookbehind('{') ncl2 = ut.negative_lookahead('{') ncr1 = ut.negative_lookbehind('}') ncr2 = ut.negative_lookahead('}') left = ncl1 + '{' + ncl2 right = ncr1 + '}' + ncr2 fmtpat = left + ut.named_field('key', '[^}]*') + right spacepat = ut.named_field('indent', '^\s+') pattern = spacepat + fmtpat import re seen_ = set([]) for m in re.finditer(pattern, code, flags=re.MULTILINE): indent = (m.groupdict()['indent']) key = (m.groupdict()['key']) if key in locals_ and key not in seen_: seen_.add(key) locals_[key] = ut.indent_rest(locals_[key], indent)
def tokenize_manacost(mana_cost): r""" CommandLine: python -m mtgmonte.mtgobjs --exec-tokenize_manacost Example: >>> # ENABLE_DOCTEST >>> from mtgmonte.mtgobjs import * # NOQA >>> cards = load_cards(['Naya Hushblade', 'Gitaxian Probe', 'Spectral Procession', 'Emrakul, the Aeons Torn']) >>> manacost_list = [card.mana_cost for card in cards] >>> result = (ut.repr2([tokenize_manacost(mana_cost) for mana_cost in manacost_list], nl=2, nobraces=True)) >>> print(result) [ ('(R/W)', 'hybrid'), ('G', 'colored'), ], [ ('(U/P)', 'phyrexian'), ], [ ('(2/W)', 'hybrid'), ('(2/W)', 'hybrid'), ('(2/W)', 'hybrid'), ], [ ('15', 'uncolored'), ], """ if mana_cost == '{*}': return [('*', 'special')] colored_pat = ut.named_field('colored', '[' + MANA_SYMBOLS + 'C' + ']') uncolored_pat = ut.named_field('uncolored', '[0-9]+', ) life_pat = ut.named_field('life', 'P', ) phyrexian_pat = ut.named_field('phyrexian', '\([' + MANA_SYMBOLS + ']/P\)') hybrid_pat = ut.named_field('hybrid', '\([0-9' + MANA_SYMBOLS + ']/[' + MANA_SYMBOLS + ']\)') patern = ut.regex_or([uncolored_pat, colored_pat, hybrid_pat, phyrexian_pat, life_pat]) groupdicts = [x.groupdict() for x in re.finditer(patern, mana_cost)] tokens = [(v, k) for d in groupdicts for k, v in d.items() if v is not None] # tokens = [x.groups() for x in re.finditer(patern, card.mana_cost)] # assert all([len(t) == 1 for t in tokens]) # tokens = [t[0] for t in tokens] return tokens
def latex_sanitize_command_name(_cmdname): r""" Args: _cmdname (?): Returns: ?: command_name CommandLine: python -m utool.util_latex --exec-latex_sanitize_command_name Example: >>> # DISABLE_DOCTEST >>> from utool.util_latex import * # NOQA >>> _cmdname = '#foo bar.' >>> command_name = latex_sanitize_command_name(_cmdname) >>> result = ('command_name = %s' % (str(command_name),)) >>> print(result) FooBar """ import re import utool as ut command_name = _cmdname try: def subroman(match): import roman try: groupdict = match.groupdict() num = int(groupdict['num']) if num == 0: return '' return roman.toRoman(num) except Exception as ex: util_dbg.printex(ex, keys=['groupdict']) raise command_name = re.sub(ut.named_field('num', r'\d+'), subroman, command_name) except ImportError as ex: if ut.SUPER_STRICT: util_dbg.printex(ex) raise # remove numbers command_name = re.sub(r'[\d' + re.escape('#()[]{}.') + ']', '', command_name) # Remove _ for camel case #def to_camel_case(str_list): # # hacky # return ''.join([str_ if len(str_) < 1 else str_[0].upper() + str_[1:] for str_ in str_list]) #command_name = to_cammel_case(re.split('[_ ]', command_name)[::2]) str_list = re.split('[_ ]', command_name) #command_name = to_cammel_case(str_list) command_name = ut.to_camel_case('_'.join(str_list), mixed=True) return command_name
def find_item_types(clean_text): item_type_pat = ut.named_field('gtem_type', '\w+') pattern = '@' + item_type_pat + '{' #header_list = [match.string[match.start():match.end()] for match in re.finditer(item_type_pat, clean_text)] header_list = [ match.groupdict()['item_type'] for match in re.finditer(pattern, clean_text) ] header_list = ut.unique_keep_order2(header_list) header_reprs = '\n'.join([' \'' + x + '\', ' for x in header_list]) print(header_reprs)
def glossterms(): re_glossterm = ut.named_field('glossterm', '.' + ut.REGEX_NONGREEDY) pat = r'\\glossterm{' + re_glossterm + '}' tup = ut.grep(pat, fpath_list=testdata_fpaths(), verbose=True) found_fpath_list, found_lines_list, found_lxs_list = tup glossterm_list = [] for line in ut.flatten(found_lines_list): match = re.search(pat, line) glossterm = match.groupdict()['glossterm'] glossterm_list.append(glossterm) print('Glossary Terms: ') print(ut.repr2(ut.dict_hist(glossterm_list), nl=True, strvals=True))
def ensure_explicit_namespace(fpath, namespace, varname_list): import re import utool as ut text = ut.read_from(fpath) orig_text = text new_text = text for varname in varname_list: regex = ''.join(( ut.named_field('prefix', '[^.]'), ut.named_field('var', ut.whole_word(varname)), )) repl = ''.join( (ut.bref_field('prefix'), namespace, '.', ut.bref_field('var'))) new_text = re.sub(regex, repl, new_text) textdiff = ut.get_textdiff(orig_text, new_text) print(textdiff) if ut.user_cmdline_prompt('Does the text look good?'): # if diff looks good write ut.write_to(fpath, new_text)
def find_citations(text): """ tex_fpath = 'chapter1-intro.tex' """ # remove comments text = re.sub('%.*', '', text) pattern = 'cite{' + ut.named_field('contents', '[^}]*') + '}' citekey_list = [] for match in re.finditer(pattern, text, re.MULTILINE | re.DOTALL): contents = match.groupdict()['contents'] #match.string[match.start():match.end()] citekey_list.extend( contents.replace(' ', '').replace('\n', '').split(',')) return citekey_list
def _rectify_text(self, text): text = text.replace('±', '\\pm') # Put all numbers in math mode pat = ( # ut.negative_lookbehind('[A-Za-z]') ut.named_field('num', '[0-9.]+(\\\\pm)?[0-9.]*') # + ut.negative_lookahead('[A-Za-z]') + '') text2 = re.sub(pat, '$' + ut.bref_field('num') + '$', text) # if True: # # def _boldface_best(): # # pass # # text2 = re.sub(pat, '$' + ut.bref_field('num') + '$', text2) # latex_str = re.sub(' -0.00 ', ' 0.00 ', latex_str) return text2
def get_fetched_lands(block, card): fetch_regex = ( 'Search your library for an? ' + _fill('landtypes') + ' card and put it onto the battlefield' + ut.named_field('istapped', ' tapped') + '?') match = re.search(fetch_regex, block) valid_types = None if match is not None: groupdict = match.groupdict() landtypes = groupdict['landtypes'].split(' or ') valid_types = [ [x.lower() for x in type_.split(' ')] for type_ in landtypes ] #landtypes = groupdict['landtypes'].split(' or ') #if groupdict['istapped']: # landtypes = ['tap-' + type_ for type_ in landtypes] return valid_types
def tozip(): re_fpath = ut.named_field('fpath', 'figure.*?[jp][pn]g') + '}' patterns = [ 'chapter4-application.tex', 'figdef4*', 'main.tex', 'def.tex', 'Crall*', 'thesis.cls', 'header*', 'colordef.tex', '*.bib' ] exclude_dirs = ['guts'] fpaths = sorted( ut.glob('.', patterns, recursive=True, exclude_dirs=exclude_dirs)) tup = ut.grep(re_fpath, fpath_list=fpaths, verbose=True) found_fpath_list, found_lines_list, found_lxs_list = tup fig_fpath_list = [] for line in ut.flatten(found_lines_list): if not line.startswith('%'): for match in re.finditer(re_fpath, line): fig_fpath = match.groupdict()['fpath'] if 'junc' not in fig_fpath and 'markov' not in fig_fpath and 'bayes' not in fig_fpath: fig_fpath_list += [fig_fpath] fpath_list = fig_fpath_list + fpaths ut.archive_files('chap4.zip', fpath_list)
def sort_module_functions(): from os.path import dirname, join import utool as ut import ibeis.control import re #import re #regex = r'[^@]*\ndef' modfpath = dirname(ibeis.control.__file__) fpath = join(modfpath, 'manual_annot_funcs.py') #fpath = join(modfpath, 'manual_dependant_funcs.py') #fpath = join(modfpath, 'manual_lblannot_funcs.py') #fpath = join(modfpath, 'manual_name_species_funcs.py') text = ut.read_from(fpath, verbose=False) lines = text.splitlines() indent_list = [ut.get_indentation(line) for line in lines] isfunc_list = [line.startswith('def ') for line in lines] isblank_list = [len(line.strip(' ')) == 0 for line in lines] isdec_list = [line.startswith('@') for line in lines] tmp = ['def' if isfunc else indent for isfunc, indent in zip(isfunc_list, indent_list)] tmp = ['b' if isblank else t for isblank, t in zip(isblank_list, tmp)] tmp = ['@' if isdec else t for isdec, t in zip(isdec_list, tmp)] #print('\n'.join([str((t, count + 1)) for (count, t) in enumerate(tmp)])) block_list = re.split('\n\n\n', text, flags=re.MULTILINE) #for block in block_list: # print('#====') # print(block) isfunc_list = [re.search('^def ', block, re.MULTILINE) is not None for block in block_list] whole_varname = ut.whole_word(ut.REGEX_VARNAME) funcname_regex = r'def\s+' + ut.named_field('funcname', whole_varname) def findfuncname(block): match = re.search(funcname_regex, block) return match.group('funcname') funcnameblock_list = [findfuncname(block) if isfunc else None for isfunc, block in zip(isfunc_list, block_list)] funcblock_list = ut.filter_items(block_list, isfunc_list) funcname_list = ut.filter_items(funcnameblock_list, isfunc_list) nonfunc_list = ut.filterfalse_items(block_list, isfunc_list) nonfunc_list = ut.filterfalse_items(block_list, isfunc_list) ismain_list = [re.search('^if __name__ == ["\']__main__["\']', nonfunc) is not None for nonfunc in nonfunc_list] mainblock_list = ut.filter_items(nonfunc_list, ismain_list) nonfunc_list = ut.filterfalse_items(nonfunc_list, ismain_list) newtext_list = [] for nonfunc in nonfunc_list: newtext_list.append(nonfunc) newtext_list.append('\n') #funcname_list for funcblock in ut.sortedby(funcblock_list, funcname_list): newtext_list.append(funcblock) newtext_list.append('\n') for mainblock in mainblock_list: newtext_list.append(mainblock) newtext = '\n'.join(newtext_list) print('newtext = %s' % (newtext,)) print('len(newtext) = %r' % (len(newtext),)) print('len(text) = %r' % (len(text),)) backup_fpath = ut.augpath(fpath, augext='.bak', augdir='_backup', ensure=True) ut.write_to(backup_fpath, text) ut.write_to(fpath, newtext)
def sort_module_functions(): from os.path import dirname, join import utool as ut import ibeis.control import re #import re #regex = r'[^@]*\ndef' modfpath = dirname(ibeis.control.__file__) fpath = join(modfpath, 'manual_annot_funcs.py') #fpath = join(modfpath, 'manual_dependant_funcs.py') #fpath = join(modfpath, 'manual_lblannot_funcs.py') #fpath = join(modfpath, 'manual_name_species_funcs.py') text = ut.read_from(fpath, verbose=False) lines = text.splitlines() indent_list = [ut.get_indentation(line) for line in lines] isfunc_list = [line.startswith('def ') for line in lines] isblank_list = [len(line.strip(' ')) == 0 for line in lines] isdec_list = [line.startswith('@') for line in lines] tmp = [ 'def' if isfunc else indent for isfunc, indent in zip(isfunc_list, indent_list) ] tmp = ['b' if isblank else t for isblank, t in zip(isblank_list, tmp)] tmp = ['@' if isdec else t for isdec, t in zip(isdec_list, tmp)] #print('\n'.join([str((t, count + 1)) for (count, t) in enumerate(tmp)])) block_list = re.split('\n\n\n', text, flags=re.MULTILINE) #for block in block_list: # print('#====') # print(block) isfunc_list = [ re.search('^def ', block, re.MULTILINE) is not None for block in block_list ] whole_varname = ut.whole_word(ut.REGEX_VARNAME) funcname_regex = r'def\s+' + ut.named_field('funcname', whole_varname) def findfuncname(block): match = re.search(funcname_regex, block) return match.group('funcname') funcnameblock_list = [ findfuncname(block) if isfunc else None for isfunc, block in zip(isfunc_list, block_list) ] funcblock_list = ut.filter_items(block_list, isfunc_list) funcname_list = ut.filter_items(funcnameblock_list, isfunc_list) nonfunc_list = ut.filterfalse_items(block_list, isfunc_list) nonfunc_list = ut.filterfalse_items(block_list, isfunc_list) ismain_list = [ re.search('^if __name__ == ["\']__main__["\']', nonfunc) is not None for nonfunc in nonfunc_list ] mainblock_list = ut.filter_items(nonfunc_list, ismain_list) nonfunc_list = ut.filterfalse_items(nonfunc_list, ismain_list) newtext_list = [] for nonfunc in nonfunc_list: newtext_list.append(nonfunc) newtext_list.append('\n') #funcname_list for funcblock in ut.sortedby(funcblock_list, funcname_list): newtext_list.append(funcblock) newtext_list.append('\n') for mainblock in mainblock_list: newtext_list.append(mainblock) newtext = '\n'.join(newtext_list) print('newtext = %s' % (newtext, )) print('len(newtext) = %r' % (len(newtext), )) print('len(text) = %r' % (len(text), )) backup_fpath = ut.augpath(fpath, augext='.bak', augdir='_backup', ensure=True) ut.write_to(backup_fpath, text) ut.write_to(fpath, newtext)
def mana_source_stats(card, deck=None): rule_blocks = card.rules_text.split(';') costs = [] mana_generated = [] if 'Land' in card.types: if 'Basic' in card.types: mana_generated = ['{' + card.rules_text + '}'] costs = [] else: for block in rule_blocks: block = block.strip(' ') manasymbol = ut.named_field('manasym', '{[WUBRG]}') managen_regex = ut.named_field('managen', '.*?') landtypes_regex = ut.named_field('landtypes', '.*?') managen_line_regexes = [ '{T}: Add ' + managen_regex + ' to your mana pool.', re.escape('({T}: Add ') + managen_regex + re.escape(' to your mana pool.)'), ] for managen_line in managen_line_regexes: match = re.match(managen_line, block) if match is not None: break if match is not None: manatext = match.groupdict()['managen'] for x in re.finditer(manasymbol, manatext): mana_generated += [x.groupdict()['manasym']] # Is tapland ETB = ' enters the battlefield ' if card.name + ETB + 'tapped.' == block: costs.append('ETB_Tapped()') if card.name + ETB + 'tapped unless you control two or more basic lands.' == block: costs.append('if len(controlled_basics) >= 2: ETB_Tapped()') # from mtgmonte import mtgrules # mtgrules.get_fetch_search_targets(block, card, player) # Is fetchland fetch_regex = ( 'Search your library for an? ' + landtypes_regex + ' card and put it onto the battlefield. Then shuffle your library.') match = re.search(fetch_regex, block) if match is not None: valid_targets = set([]) for type_ in match.groupdict()['landtypes'].split(' or '): if deck is None: card = lookup_card(type_) valid_targets.add(card) else: for card in deck.library: if type_ in card.subtypes: valid_targets.add(card) costs = [] mana_generated = [] for card in valid_targets: # marginalize mana gen, cost = card.mana_source_stats() mana_generated.append(gen) mana_generated = list(set(ut.flatten(mana_generated))) #else: # costs = [] # mana_generated = [] manastats = mana_generated, costs else: manastats = None return manastats
def install_wildbook(verbose=ut.NOT_QUIET): """ Script to setup wildbook on a unix based system (hopefully eventually this will generalize to win32) CommandLine: # Reset python -m ibeis --tf reset_local_wildbook # Setup python -m ibeis --tf install_wildbook # Startup python -m ibeis --tf startup_wildbook_server --show --exec-mode # Reset python -m ibeis.control.manual_wildbook_funcs --test-reset_local_wildbook # Setup python -m ibeis.control.manual_wildbook_funcs --test-install_wildbook # Startup python -m ibeis.control.manual_wildbook_funcs --test-startup_wildbook_server --show --exec-mode Example: >>> # SCRIPT >>> from ibeis.control.manual_wildbook_funcs import * # NOQA >>> verbose = True >>> result = install_wildbook() >>> print(result) """ # TODO: allow custom specified tomcat directory from os.path import basename, splitext, join import time import re import subprocess try: output = subprocess.check_output(['java', '-version'], stderr=subprocess.STDOUT) _java_version = output.split('\n')[0] _java_version = _java_version.replace('java version ', '') java_version = _java_version.replace('"', '') print('java_version = %r' % (java_version,)) if not java_version.startswith('1.7'): print('Warning wildbook is only supported for java 1.7') except OSError: output = None if output is None: raise ImportError( 'Cannot find java on this machine. ' 'Please install java: http://www.java.com/en/download/') tomcat_dpath = find_or_download_tomcat() assert tomcat_dpath is not None, 'Could not find tomcat' war_fpath = find_or_download_wilbook_warfile() war_fname = basename(war_fpath) wb_target = splitext(war_fname)[0] # Ensure environment variables #os.environ['JAVA_HOME'] = find_java_jvm() #os.environ['TOMCAT_HOME'] = tomcat_dpath #os.environ['CATALINA_HOME'] = tomcat_dpath # Move the war file to tomcat webapps if not there webapps_dpath = join(tomcat_dpath, 'webapps') deploy_war_fpath = join(webapps_dpath, war_fname) if not ut.checkpath(deploy_war_fpath, verbose=verbose): ut.copy(war_fpath, deploy_war_fpath) # Ensure that the war file has been unpacked unpacked_war_dpath = join(webapps_dpath, wb_target) if not ut.checkpath(unpacked_war_dpath, verbose=verbose): # Need to make sure you start catalina in the same directory otherwise # the derby databsae gets put in in cwd tomcat_startup_dir = get_tomcat_startup_tmpdir() with ut.ChdirContext(tomcat_startup_dir): # Starting and stoping catalina should be sufficient to unpack the # war startup_fpath = join(tomcat_dpath, 'bin', 'startup.sh') shutdown_fpath = join(tomcat_dpath, 'bin', 'shutdown.sh') ut.cmd(ut.quote_single_command(startup_fpath)) print('It is NOT ok if the startup.sh fails\n') # wait for the war to be unpacked for retry_count in range(0, 6): time.sleep(1) if ut.checkpath(unpacked_war_dpath, verbose=True): break else: print('Retrying') # ensure that the server is ruuning import requests print('Checking if we can ping the server') response = requests.get('http://localhost:8080') if response is None or response.status_code != 200: print('There may be an error starting the server') else: print('Seem able to ping the server') # assert tht the war was unpacked ut.assertpath(unpacked_war_dpath, ( 'Wildbook war might have not unpacked correctly. This may ' 'be ok. Try again. If it fails a second time, then there is a ' 'problem.'), verbose=True) # shutdown the server ut.cmd(ut.quote_single_command(shutdown_fpath)) print('It is ok if the shutdown.sh fails') time.sleep(.5) # Make sure permissions are correctly set in wildbook # Comment out the line that requires authentication permission_fpath = join(unpacked_war_dpath, 'WEB-INF/web.xml') ut.assertpath(permission_fpath) permission_text = ut.readfrom(permission_fpath) lines_to_remove = [ '/EncounterSetMarkedIndividual = authc, roles[admin]' ] new_permission_text = permission_text[:] for line in lines_to_remove: re.search(re.escape(line), permission_text) prefix = ut.named_field('prefix', '\\s*') suffix = ut.named_field('suffix', '\\s*\n') pattern = ('^' + prefix + re.escape(line) + suffix) match = re.search(pattern, permission_text, flags=re.MULTILINE | re.DOTALL) if match is None: continue newline = '<!--%s -->' % (line,) repl = ut.bref_field('prefix') + newline + ut.bref_field('suffix') new_permission_text = re.sub(pattern, repl, permission_text, flags=re.MULTILINE | re.DOTALL) assert new_permission_text != permission_text, ( 'text should have changed') if new_permission_text != permission_text: print('Need to write new permission texts') ut.writeto(permission_fpath, new_permission_text) else: print('Permission file seems to be ok') print('Wildbook is installed and waiting to be started')
INF = 1000 # pretty much infinite def english_number(numtext): try: return int(numtext) except Exception: lookup = {'one': 1, 'two': 2, 'three': 3, 'four': 4} # , '∞': INF} return lookup[numtext] ETB = ' enters the battlefield ' COLORLESS_SYMS = 'C' COLOR_SYMS = 'WUBRG' ALL_MANA_SYM = COLOR_SYMS + COLORLESS_SYMS COLORLESS_MANASYM = ut.named_field('colorless_manasym', '{[0-9]+}') MANASYM = ut.named_field('manasym', '({[' + ALL_MANA_SYM + ']})+') def _fill(name=None): return ut.named_field(name, '.*?') def is_ability_block(block): return ':' in block or (len(block) == 1 and (block in ALL_MANA_SYM)) def mana_generated(block, card, new=False, debug=False): r""" Parse the string representation of mana generated
def fix_section_title_capitalization(tex_fpath, dryrun=True): # Read in text and ensure ascii format text = ut.read_from(tex_fpath) section_type_list = [ 'chapter', 'section', 'subsection', 'subsubsection', 'paragraph', ] re_section_type = ut.named_field('section_type', ut.regex_or(section_type_list)) re_section_title = ut.named_field('section_title', '[^}]*') re_spaces = ut.named_field('spaces', '^ *') pattern = re_spaces + re.escape( '\\') + re_section_type + '{' + re_section_title + '}' def fix_capitalization(match): dict_ = match.groupdict() section_title = dict_['section_title'] #if section_title == 'The Great Zebra Count': # return match.string[slice(*match.span())] # #return 'The Great Zebra Count' # general logic #words = section_title.split(' ') tokens = re.split(ut.regex_or([' ', '/']), section_title) #if 'Coverage' in section_title: # ut.embed() # pass #words = [word if count == 0 else word.lower() for count, word in enumerate(words)] #new_section_title = ' '.join(words) tokens = [ t if count == 0 else t.lower() for count, t in enumerate(tokens) ] new_section_title = ''.join(tokens) # hacks for caps of expanded titles search_repl_list = constants_tex_fixes.CAPITAL_TITLE_LIST for repl in search_repl_list: new_section_title = re.sub(re.escape(repl), repl, new_section_title, flags=re.IGNORECASE) # hacks fo acronyms for full, acro in constants_tex_fixes.ACRONYMN_LIST: new_section_title = re.sub(r'\b' + re.escape(acro) + r'\b', acro, new_section_title, flags=re.IGNORECASE) #'the great zebra and giraffe count' #new_section_title = section_title.lower() new_text = dict_['spaces'] + '\\' + dict_[ 'section_type'] + '{' + new_section_title + '}' VERBOSE = 0 if VERBOSE: old_text = match.string[slice(*match.span())] if new_text != old_text: print(ut.dict_str(dict_)) print('--- REPL ---') print(old_text) print(new_text) return new_text #for match in re.finditer(pattern, text, flags=re.MULTILINE): # fix_capitalization(match) new_text = re.sub(pattern, fix_capitalization, text, flags=re.MULTILINE) if not dryrun: ut.write_to(tex_fpath, new_text) else: ut.print_difftext(ut.get_textdiff(text, new_text, 0))
def _fill(name=None): return ut.named_field(name, '.*?')
def update_wildbook_install_config(webapps_dpath, unpacked_war_dpath): """ CommandLine: python -m ibeis ensure_local_war python -m ibeis update_wildbook_install_config python -m ibeis update_wildbook_install_config --show Example: >>> from ibeis.control.wildbook_manager import * # NOQA >>> import ibeis >>> tomcat_dpath = find_installed_tomcat() >>> webapps_dpath = join(tomcat_dpath, 'webapps') >>> wb_target = ibeis.const.WILDBOOK_TARGET >>> unpacked_war_dpath = join(webapps_dpath, wb_target) >>> locals_ = ut.exec_func_src(update_wildbook_install_config, globals()) >>> #update_wildbook_install_config(webapps_dpath, unpacked_war_dpath) >>> ut.quit_if_noshow() >>> ut.vd(unpacked_war_dpath) >>> ut.editfile(locals_['permission_fpath']) >>> ut.editfile(locals_['jdoconfig_fpath']) >>> ut.editfile(locals_['asset_store_fpath']) """ mysql_mode = not ut.get_argflag('--nomysql') #if ut.get_argflag('--vd'): # ut.vd(unpacked_war_dpath) #find_installed_tomcat # Make sure permissions are correctly set in wildbook # Comment out the line that requires authentication permission_fpath = join(unpacked_war_dpath, 'WEB-INF/web.xml') ut.assertpath(permission_fpath) permission_text = ut.readfrom(permission_fpath) lines_to_remove = [ # '/ImageSetSetMarkedIndividual = authc, roles[admin]' '/EncounterSetMarkedIndividual = authc, roles[admin]' ] new_permission_text = permission_text[:] for line in lines_to_remove: re.search(re.escape(line), permission_text) prefix = ut.named_field('prefix', '\\s*') suffix = ut.named_field('suffix', '\\s*\n') pattern = ('^' + prefix + re.escape(line) + suffix) match = re.search(pattern, permission_text, flags=re.MULTILINE | re.DOTALL) if match is None: continue newline = '<!--%s -->' % (line, ) repl = ut.bref_field('prefix') + newline + ut.bref_field('suffix') new_permission_text = re.sub(pattern, repl, permission_text, flags=re.MULTILINE | re.DOTALL) assert new_permission_text != permission_text, ( 'text should have changed') if new_permission_text != permission_text: print('Need to write new permission texts') ut.writeto(permission_fpath, new_permission_text) else: print('Permission file seems to be ok') # Make sure we are using a non-process based database jdoconfig_fpath = join(unpacked_war_dpath, 'WEB-INF/classes/bundles/jdoconfig.properties') print('Fixing backend database config') print('jdoconfig_fpath = %r' % (jdoconfig_fpath, )) ut.assertpath(jdoconfig_fpath) jdoconfig_text = ut.readfrom(jdoconfig_fpath) #ut.vd(dirname(jdoconfig_fpath)) #ut.editfile(jdoconfig_fpath) if mysql_mode: jdoconfig_text = ut.toggle_comment_lines(jdoconfig_text, 'mysql', False) jdoconfig_text = ut.toggle_comment_lines(jdoconfig_text, 'derby', 1) jdoconfig_text = ut.toggle_comment_lines(jdoconfig_text, 'sqlite', 1) mysql_user = '******' mysql_passwd = 'somepassword' mysql_dbname = 'ibeiswbtestdb' # Use mysql jdoconfig_text = re.sub('datanucleus.ConnectionUserName = .*$', 'datanucleus.ConnectionUserName = '******'datanucleus.ConnectionPassword = .*$', 'datanucleus.ConnectionPassword = '******'datanucleus.ConnectionURL *= *jdbc:mysql:.*$', 'datanucleus.ConnectionURL = jdbc:mysql://localhost:3306/' + mysql_dbname, jdoconfig_text, flags=re.MULTILINE) jdoconfig_text = re.sub('^.*jdbc:mysql://localhost:3306/shepherd.*$', '', jdoconfig_text, flags=re.MULTILINE) else: # Use SQLIIte jdoconfig_text = ut.toggle_comment_lines(jdoconfig_text, 'derby', 1) jdoconfig_text = ut.toggle_comment_lines(jdoconfig_text, 'mysql', 1) jdoconfig_text = ut.toggle_comment_lines(jdoconfig_text, 'sqlite', False) ut.writeto(jdoconfig_fpath, jdoconfig_text) # Need to make sure wildbook can store information in a reasonalbe place #tomcat_data_dir = join(tomcat_startup_dir, 'webapps', 'wildbook_data_dir') tomcat_data_dir = join(webapps_dpath, 'wildbook_data_dir') ut.ensuredir(tomcat_data_dir) ut.writeto(join(tomcat_data_dir, 'test.txt'), 'A hosted test file') asset_store_fpath = join(unpacked_war_dpath, 'createAssetStore.jsp') asset_store_text = ut.read_from(asset_store_fpath) #data_path_pat = ut.named_field('data_path', 'new File(".*?").toPath') new_line = 'LocalAssetStore as = new LocalAssetStore("example Local AssetStore", new File("%s").toPath(), "%s", true);' % ( tomcat_data_dir, 'http://localhost:8080/' + basename(tomcat_data_dir)) # HACKY asset_store_text2 = re.sub('^LocalAssetStore as = .*$', new_line, asset_store_text, flags=re.MULTILINE) ut.writeto(asset_store_fpath, asset_store_text2)
def update_wildbook_install_config(webapps_dpath, unpacked_war_dpath): """ CommandLine: python -m ibeis ensure_local_war python -m ibeis update_wildbook_install_config python -m ibeis update_wildbook_install_config --show Example: >>> from ibeis.control.wildbook_manager import * # NOQA >>> import ibeis >>> tomcat_dpath = find_installed_tomcat() >>> webapps_dpath = join(tomcat_dpath, 'webapps') >>> wb_target = ibeis.const.WILDBOOK_TARGET >>> unpacked_war_dpath = join(webapps_dpath, wb_target) >>> locals_ = ut.exec_func_src(update_wildbook_install_config, globals()) >>> #update_wildbook_install_config(webapps_dpath, unpacked_war_dpath) >>> ut.quit_if_noshow() >>> ut.vd(unpacked_war_dpath) >>> ut.editfile(locals_['permission_fpath']) >>> ut.editfile(locals_['jdoconfig_fpath']) >>> ut.editfile(locals_['asset_store_fpath']) """ mysql_mode = not ut.get_argflag('--nomysql') #if ut.get_argflag('--vd'): # ut.vd(unpacked_war_dpath) #find_installed_tomcat # Make sure permissions are correctly set in wildbook # Comment out the line that requires authentication permission_fpath = join(unpacked_war_dpath, 'WEB-INF/web.xml') ut.assertpath(permission_fpath) permission_text = ut.readfrom(permission_fpath) lines_to_remove = [ # '/ImageSetSetMarkedIndividual = authc, roles[admin]' '/EncounterSetMarkedIndividual = authc, roles[admin]' ] new_permission_text = permission_text[:] for line in lines_to_remove: re.search(re.escape(line), permission_text) prefix = ut.named_field('prefix', '\\s*') suffix = ut.named_field('suffix', '\\s*\n') pattern = ('^' + prefix + re.escape(line) + suffix) match = re.search(pattern, permission_text, flags=re.MULTILINE | re.DOTALL) if match is None: continue newline = '<!--%s -->' % (line,) repl = ut.bref_field('prefix') + newline + ut.bref_field('suffix') new_permission_text = re.sub(pattern, repl, permission_text, flags=re.MULTILINE | re.DOTALL) assert new_permission_text != permission_text, ( 'text should have changed') if new_permission_text != permission_text: print('Need to write new permission texts') ut.writeto(permission_fpath, new_permission_text) else: print('Permission file seems to be ok') # Make sure we are using a non-process based database jdoconfig_fpath = join(unpacked_war_dpath, 'WEB-INF/classes/bundles/jdoconfig.properties') print('Fixing backend database config') print('jdoconfig_fpath = %r' % (jdoconfig_fpath,)) ut.assertpath(jdoconfig_fpath) jdoconfig_text = ut.readfrom(jdoconfig_fpath) #ut.vd(dirname(jdoconfig_fpath)) #ut.editfile(jdoconfig_fpath) if mysql_mode: jdoconfig_text = ut.toggle_comment_lines(jdoconfig_text, 'mysql', False) jdoconfig_text = ut.toggle_comment_lines(jdoconfig_text, 'derby', 1) jdoconfig_text = ut.toggle_comment_lines(jdoconfig_text, 'sqlite', 1) mysql_user = '******' mysql_passwd = 'somepassword' mysql_dbname = 'ibeiswbtestdb' # Use mysql jdoconfig_text = re.sub( 'datanucleus.ConnectionUserName = .*$', 'datanucleus.ConnectionUserName = '******'datanucleus.ConnectionPassword = .*$', 'datanucleus.ConnectionPassword = '******'datanucleus.ConnectionURL *= *jdbc:mysql:.*$', 'datanucleus.ConnectionURL = jdbc:mysql://localhost:3306/' + mysql_dbname, jdoconfig_text, flags=re.MULTILINE) jdoconfig_text = re.sub( '^.*jdbc:mysql://localhost:3306/shepherd.*$', '', jdoconfig_text, flags=re.MULTILINE) else: # Use SQLIIte jdoconfig_text = ut.toggle_comment_lines(jdoconfig_text, 'derby', 1) jdoconfig_text = ut.toggle_comment_lines(jdoconfig_text, 'mysql', 1) jdoconfig_text = ut.toggle_comment_lines(jdoconfig_text, 'sqlite', False) ut.writeto(jdoconfig_fpath, jdoconfig_text) # Need to make sure wildbook can store information in a reasonalbe place #tomcat_data_dir = join(tomcat_startup_dir, 'webapps', 'wildbook_data_dir') tomcat_data_dir = join(webapps_dpath, 'wildbook_data_dir') ut.ensuredir(tomcat_data_dir) ut.writeto(join(tomcat_data_dir, 'test.txt'), 'A hosted test file') asset_store_fpath = join(unpacked_war_dpath, 'createAssetStore.jsp') asset_store_text = ut.read_from(asset_store_fpath) #data_path_pat = ut.named_field('data_path', 'new File(".*?").toPath') new_line = 'LocalAssetStore as = new LocalAssetStore("example Local AssetStore", new File("%s").toPath(), "%s", true);' % ( tomcat_data_dir, 'http://localhost:8080/' + basename(tomcat_data_dir) ) # HACKY asset_store_text2 = re.sub('^LocalAssetStore as = .*$', new_line, asset_store_text, flags=re.MULTILINE) ut.writeto(asset_store_fpath, asset_store_text2)