Example #1
0
def changeHeadFoot(file,header,footer):
    f = open (file, "r+b");
    r = f.read();
    ro= r;

    r1,c1 = re.subn(HF[0], header, r);
    r2,c2 = re.subn(HF[1], footer, r1);

    if ( (c1+c2) <= 0 ):
        print "-NO HEADR OR FOOTER ", file, " not changing...."
        f.close()
        return
    #print "---", ro , "====", r2, "++++"
    if ( ro == r2 ):
        print "-No changes needed - original was good: ", file
        f.close()
        return

    print('+CHANGING :' + file + " replaced Header:" + str(c1)+ " Footer: " +str(c2))

    fbak = open (file+".bak", "w");
    fbak.write(r)
    fbak.close();

    f.seek(0);
    f.write(r2)
    f.truncate()
    f.close();
Example #2
0
def get_rss_entries(request, limit=5, template_name="lfc/tags/rss_ajax_entries.html"):
    """Loads the entries for rss_ajax tag.
    """
    url = request.GET.get("url")
    feed = feedparser.parse(url)
    try:
        name = feed["feed"]["link"].split("/")[-1]
    except (KeyError, IndexError, AttributeError):
        return {
            "entries" : [],
            "link" : "",
            "LANGUAGE_CODE" : "",
        }

    entries = []
    for entry in feed.entries[0:limit]:
        summary = entry.summary.replace("%s: " % name, "")
        summary = re.subn("#\S+", "", summary)[0]
        summary = re.subn("(http://\S+)", "<a href='\g<1>'>\g<1></a>", summary)[0]

        entries.append({
            "summary" : summary,
            "date" : datetime.datetime(*entry["updated_parsed"][0:6])
        })

    return render_to_response(template_name, RequestContext(request, {
        "entries" : entries,
        "link" : feed["feed"]["link"],
    }))
Example #3
0
def cm_process(line, mode=None):
    # if we're already in a critic markup deletion mode
    # check if we can exit the mode
    # if not, return nothing
    if mode:
        (line,nsub) = re.subn(cms[mode][2],'',line)
        if nsub==0:
            #return ('',mode)
            return (None,mode)

    # otherwise
    changes = dict()
    for k in cm_order:
        vs = cms[k]
        nsub = -1
        if len(vs) == 1:
            # nonmodal critic markup: tag deletion
            while nsub != 0:
                (line, nsub) = re.subn(vs[0],'',line)
        elif len(vs) == 3:
            # modal critic markup: check if mode contained in line
            # or starts on line and continues past
            while nsub != 0:
                (line, nsub) = re.subn(vs[0],'',line)
            (line, nsub) = re.subn(vs[1],'',line)
            if nsub > 0:
                return(line,k)

    return (line,None)
Example #4
0
def get_potential_addresses(address):
    """
    Get permutations of as many potential addresses as we can make out of
    the given address.

    Permutations include the address with all punctuation removed, the
    address with directions abbreviated, and the address with street types
    abbreviated (as they are in the City of New Orleans' addresses
    shapefile).
    """
    addresses = set()
    addresses.add(address.lower())

    # Try removing punctuation from the address
    for address in list(addresses):
        addresses.add(re.subn('[^\s\w]', '', address)[0])

    #  Try abbreviating directions
    for address in list(addresses):
        abbreviated = address
        for direction, abbr in directions.items():
            abbreviated = re.subn(r'\b%s\b' % direction, abbr, abbreviated)[0]
        addresses.add(abbreviated)

    # Abbreviate street types
    for address in list(addresses):
        for street_type, abbr in street_types.items():
            abbreviated = re.subn(r'\b%s\b' % street_type, abbr, address)[0]
            addresses.add(abbreviated)

    return tuple(addresses)
Example #5
0
 def alter_path(self, path, **varargs):
     '''Modify the path passed so that it can be executed on the remote host
     
     path = path to modify
     regexp_substitution - if true, exclude \g<...> from substitution
     '''
     regexp_substitution = varargs.get("regexp_substitution", False)
     for mapping in self.mappings:
         local_directory = mapping.local_directory.value
         remote_directory = mapping.remote_directory.value
         if regexp_substitution:
             local_directory = local_directory.replace("\\", "\\\\")
             remote_directory = remote_directory.replace("\\", "\\\\")
         
         if sys.platform.startswith('win'):
             # Windows is case-insentitve so do case-insensitve mapping
             if path.upper().startswith(local_directory.upper()):
                 path = (remote_directory +
                         path[len(local_directory):])
         else:
             if path.startswith(local_directory):
                 path = (remote_directory +
                         path[len(local_directory):])
     if self.remote_host_is_windows.value:
         path = path.replace('/','\\')
     elif (regexp_substitution):
         path = re.subn('\\\\\\\\','/',path)[0]
         path = re.subn('\\\\(?!g\\<[^>]*\\>)','/',path)[0]
     else:
         path = path.replace('\\','/')
     return path
Example #6
0
	def getComments(self, response):
		Item = response.meta['item']

		res_text = response.body_as_unicode().encode('ascii', 'ignore')
		res_text = smart_str(self.parser.unescape(self.parser.unescape(res_text))).replace('\xc2\xa0','')
		res_text = res_text.replace('\n', ' ').replace('\t', ' ').replace('\r', '')
		res_text = re.subn('<script.*?</script>', '', res_text)[0]
		res_text = re.subn('<style.*?</style>', '', res_text)[0]
		hxs = HtmlXPathSelector(text=res_text)
		
		tmp = hxs.select('//div[@id="ds_div"]//text()').extract()
		comments = ''
		for val in tmp:
			val = val.strip()
			if val != '':
				comments += val + ' '
		Item['Comments'] = comments

		try:
			offers_url = 'http://offer.ebay.com/ws/eBayISAPI.dll?ViewBids&item=' + Item['eBay_Item_Number']
			if Item['eBay_Item_Number'] != 'NA' and Item['eBay_Item_Number'] != '':
				req = Request(offers_url, dont_filter=True, callback=self.getPostingDate)
				req.meta['item'] = Item
				return req
		except:
			pass

		return Item
Example #7
0
	def html2utf8(self,in_html):
		in_html = (re.subn(r'<(script).*?</\1>(?s)', '', in_html)[0])
		in_html = (re.subn(r'<(style).*?</\1>(?s)', '', in_html)[0])
		entitydict = {}

		entities = re.finditer('&([^#][A-Za-z]{1,5}?);', in_html)
		for x in entities:
			key = x.group(0)
			if key not in entitydict:
				entitydict[key] = htmlentitydefs.name2codepoint[x.group(1)]

		entities = re.finditer('&#x([0-9A-Fa-f]{2,2}?);', in_html)
		for x in entities:
			key = x.group(0)
			if key not in entitydict:
				entitydict[key] = "%d" % int(key[3:5], 16)

		entities = re.finditer('&#(\d{1,5}?);', in_html)
		for x in entities:
			key = x.group(0)
			if key not in entitydict:
				entitydict[key] = x.group(1)

		if re.search("charset=utf-8", in_html):
			for key, codepoint in iteritems(entitydict):
				in_html = in_html.replace(key, unichr(int(codepoint)))
			self.inhtml = in_html.encode('utf8')
			return

		for key, codepoint in iteritems(entitydict):
			in_html = in_html.replace(key, unichr(int(codepoint)).encode('latin-1', 'ignore'))
		self.inhtml = in_html.decode('latin-1').encode('utf8')
Example #8
0
    def __call__(self, parser, namespace, values, option_string=None):
        if is_interactive() and option_string == '--help':
            # lets use the manpage on mature systems ...
            try:
                import subprocess
                subprocess.check_call(
                    'man %s 2> /dev/null' % parser.prog.replace(' ', '-'),
                    shell=True)
                sys.exit(0)
            except (subprocess.CalledProcessError, OSError):
                # ...but silently fall back if it doesn't work
                pass
        if option_string == '-h':
            helpstr = "%s\n%s" % (
                parser.format_usage(),
                "Use '--help' to get more comprehensive information.")
        else:
            helpstr = parser.format_help()
        # better for help2man
        # For main command -- should be different sections. And since we are in
        # heavy output massaging mode...
        if "commands for dataset operations" in helpstr.lower():
            opt_args_str = '*Global options*'
            pos_args_str = '*Commands*'
            # tune up usage -- default one is way too heavy
            helpstr = re.sub('^[uU]sage: .*?\n\s*\n',
                             'Usage: datalad [global-opts] command [command-opts]\n\n',
                             helpstr,
                             flags=re.MULTILINE | re.DOTALL)
            # And altogether remove section with long list of commands
            helpstr = re.sub(r'positional arguments:\s*\n\s*{.*}\n', '', helpstr)
        else:
            opt_args_str = "*Options*"
            pos_args_str = "*Arguments*"
        helpstr = re.sub(r'optional arguments:', opt_args_str, helpstr)
        helpstr = re.sub(r'positional arguments:', pos_args_str, helpstr)
        # convert all heading to have the first character uppercase
        headpat = re.compile(r'^([a-z])(.*):$',  re.MULTILINE)
        helpstr = re.subn(
            headpat,
            lambda match: r'{0}{1}:'.format(match.group(1).upper(),
                                            match.group(2)),
            helpstr)[0]
        # usage is on the same line
        helpstr = re.sub(r'^usage:', 'Usage:', helpstr)
        if option_string == '--help-np':
            usagestr = re.split(r'\n\n[A-Z]+', helpstr, maxsplit=1)[0]
            usage_length = len(usagestr)
            usagestr = re.subn(r'\s+', ' ', usagestr.replace('\n', ' '))[0]
            helpstr = '%s\n%s' % (usagestr, helpstr[usage_length:])

        if os.environ.get('DATALAD_HELP2MAN'):
            # Convert 1-line command descriptions to remove leading -
            helpstr = re.sub('\n\s*-\s*([-a-z0-9]*):\s*?([^\n]*)', r"\n'\1':\n  \2\n", helpstr)
        else:
            # Those *s intended for man formatting do not contribute to readability in regular text mode
            helpstr = helpstr.replace('*', '')

        print(helpstr)
        sys.exit(0)
Example #9
0
def replace(inp):
    result = []
    for f in inp:
        f = f.decode("utf-8")
        
        #f = re.sub(r"([^\\])([_|\^])", r"\1\\\2", f) # do not require backslash in front of ^ or _
        #f = re.sub(r"^([_|\^])", r"\\\1", f)
        
        # escape combining marks with a space after the backslash
        for c in combiningmarks:
            offset = 0
            for s in re.finditer(c[0], f):
                f = f[:s.start()+1+offset] + " " + f[s.start()+1+offset:]
                offset += 1
            
        for r in replacements:
            f = re.sub(r[0], r[1], f)

        # expand groups of subscripts: \_{01234}    
        offset = 0
        #for s in re.finditer(r"\\_\{[^\}]+\}", f):
        for s in re.finditer(ur"_\{[0-9\+-=\(\)<>\-aeoxjhklmnpstiruv\u03B2\u03B3\u03C1\u03C6\u03C7]+\}", f):
            newstring,n = re.subn(ur"([0-9\+-=\(\)<>\-aeoxjhklmnpstiruv\u03B2\u03B3\u03C1\u03C6\u03C7])", r"_\1", s.group(0)[2:-1])
            f = f[:s.start()+offset] + newstring + f[s.end()+offset:]
            offset += n*2 - (n + 3)
            
            
        # expand groups of superscripts: \^{01234}    
        offset = 0
        #for s in re.finditer(r"\\\^\{[^\}]+\}", f):
        for s in re.finditer(ur"\^\{[0-9\+-=\(\)<>ABDEGHIJKLMNOPRTUWabcdefghijklmnoprstuvwxyz\u03B2\u03B3\u03B4\u03C6\u03C7\u222B]+\}", f):
            newstring,n = re.subn(ur"([0-9\+-=\(\)<>ABDEGHIJKLMNOPRTUWabcdefghijklmnoprstuvwxyz\u03B2\u03B3\u03B4\u03C6\u03C7\u222B])", r"^\1", s.group(0)[2:-1])
            f = f[:s.start()+offset] + newstring + f[s.end()+offset:]
            offset += n*2 - (n + 3)
 def set_new_patches(self, fns):
     self.wipe_patches()
     if not fns:
         return
     apply_method = self.patches_apply_method()
     ps = ''
     pa = ''
     for i, pfn in enumerate(fns, start=1):
         ps += "Patch%04d: %s\n" % (i, pfn)
         if apply_method == 'rpm':
             pa += "%%patch%04d -p1\n" % i
     ## PatchXXX: lines after Source0 / #patches_base=
     self._txt, n = re.subn(
         self.RE_AFTER_PATCHES_BASE,
         r'\g<1>%s\n' % ps, self.txt, count=1)
     if n != 1:
         self._txt, n = re.subn(
             self.RE_AFTER_SOURCES,
             r'\g<1>%s\n' % ps, self.txt, count=1)
     if n != 1:
         raise Exception("SpecFileParseError: Failed to append PatchXXXX: lines")
     ## %patchXXX -p1 lines after "%setup" if needed
     if apply_method == 'rpm':
         self._txt, n = re.subn(
             r'((?:^|\n)%setup[^\n]*\n)\s*',
             r'\g<1>\n%s\n' % pa, self.txt)
         if n == 0:
             raise Exception("SpecFileParseError: Failed to append %patchXXXX lines after %setup")
Example #11
0
def retrieveEspnGameIdsForDay(date, root_url):
    """

    """
    url = root_url + date
    url = "http://scores.espn.go.com/nba/scoreboard?date=20150202"
    scores_page = soupypages.soupFromUrl(url)
    if scores_page['pass']:
        scores_page = scores_page['soup']
        game_data = scores_page.body.findAll(id="main-container")[0].find('div', class_='scoreboards').attrs['data-data']
        game_data = re.subn('true', "'true'", game_data)[0]
        game_data = re.subn('false', "'false'", game_data)[0]
        game_data = eval(game_data)
        game_ids = [game['id'] for game in game_data['events']]
        return game_ids
    
##    scores_page = makePage(url, hdr=True)
##    if scores_page['pass']:
##        game_ids = key_phrase.findall(scores_page['page'])
##        return game_ids
    
    else:
        err_msg = scores_page['err']
        if type(err_msg) == str:
            print("Failed to retrieve scores page for %s. Error: %s" %\
                  (date, err_msg))
        else:
            print("Failed to retrieve scores page for %s. Unknown error." %\
                  date)
        return []
Example #12
0
def cache_customevent_trend(ids=[]):
    """
    Runs the rawdata gatherer for the specific custom events.
    Intended to be run mainly but the BibSched daemon interface.

    For a specific id, all possible timespans' rawdata is gathered.

    @param ids: The custom event ids that are subject to caching.
    @type ids: []
    """
    args = {}
    timespans = _get_timespans()

    for id in ids:
        args['id'] = id
        args['cols'] = []

        for i in range(len(timespans)):
            # Get timespans parameters
            args['timespan'] = timespans[i][0]
            args.update({ 't_start': timespans[i][2], 't_end': timespans[i][3], 'granularity': timespans[i][4],
                          't_format': timespans[i][5], 'xtic_format': timespans[i][6] })

            # Create unique filename for this combination of parameters
            filename = "webstat_customevent_%(id)s_%(timespan)s" \
                        % { 'id': re.subn("[^\w]", "_", id)[0], 'timespan': re.subn("[^\w]", "_", args['timespan'])[0] }

            # Create closure of gatherer function in case cache needs to be refreshed
            gatherer = lambda: get_customevent_trend(args)

            # Get data file from cache, ALWAYS REFRESH DATA!
            _get_file_using_cache(filename, gatherer, True).read()

    return True
Example #13
0
 def alter_path(self, path, **varargs):
     '''Modify the path passed so that it can be executed on the remote host
     
     path = path to modify
     regexp_substitution - if true, exclude \g<...> from substitution
     '''
     for mapping in self.mappings:
         if sys.platform.startswith('win'):
             # Windows is case-insentitve so do case-insensitve mapping
             if path.upper().startswith(mapping.local_directory.value.upper()):
                 path = (mapping.remote_directory.value +
                         path[len(mapping.local_directory.value):])
         else:
             if path.startswith(mapping.local_directory.value):
                 path = (mapping.remote_directory.value +
                         path[len(mapping.local_directory.value):])
     if self.remote_host_is_windows.value:
         path = path.replace('/','\\')
     elif (varargs.has_key("regexp_substitution") and
              varargs["regexp_substitution"]):
         path = re.subn('\\\\\\\\','/',path)[0]
         path = re.subn('\\\\(?!g\\<[^>]*\\>)','/',path)[0]
     else:
         path = path.replace('\\','/')
     return path
Example #14
0
 def _clean_xml(self, line):
     line2 = re.subn('"', '&quot;', line)
     line3 = re.subn('&', '&amp;', line2[0])
     line4 = re.subn("'", '&apos;', line3[0])
     line5 = re.subn('<', '&lt;', line4[0])
     line6 = re.subn('>', '&gt;', line5[0])
     return line6[0]            
Example #15
0
    def set_new_patches(self, fns):
        self.wipe_patches()
        if not fns:
            return
        apply_method = self.patches_apply_method()
        ps = ""
        pa = ""
        for i, pfn in enumerate(fns, start=1):
            ps += "Patch%04d: %s\n" % (i, pfn)
            if apply_method == "rpm":
                pa += "%%patch%04d -p1\n" % i
        # PatchXXX: lines after Source0 / #patches_base=
        self._txt, n = re.subn(self.RE_AFTER_PATCHES_BASE, r"\g<1>%s\n" % ps, self.txt, count=1)

        if n != 1:
            for m in re.finditer(self.RE_AFTER_SOURCES, self.txt):
                pass
            if not m:
                raise exception.SpecFileParseError(spec_fn=self.fn, error="Failed to append PatchXXXX: lines")
            i = m.end()
            startnl, endnl = "", ""
            if self._txt[i - 2] != "\n":
                startnl += "\n"
            if self._txt[i] != "\n":
                endnl += "\n"
            self._txt = self._txt[:i] + startnl + ps + endnl + self._txt[i:]
        # %patchXXX -p1 lines after "%setup" if needed
        if apply_method == "rpm":
            self._txt, n = re.subn(r"((?:^|\n)%setup[^\n]*\n)\s*", r"\g<1>\n%s\n" % pa, self.txt)
            if n == 0:
                raise exception.SpecFileParseError(
                    spec_fn=self.fn, error="Failed to append %patchXXXX lines after %setup"
                )
    def parse(self, response):
        response_sel = Selector(response)

        category = response_sel.xpath(u'//a[contains(@href,"category")]/@href').extract()
        sellerid = response_sel.xpath(u'//meta[contains(@content,"userId")]/@content').extract()

        # get the sellerid and replace it
        sellerId = re.findall(r'userId=(\d+)', sellerid[0])[0]
        if sellerId:
            self.rateUrl = set_query(self.rateUrl, sellerId=sellerId)
        else:
            self.logger.error("Get the sellerid error !")

        domain = re.findall(r'https:(.*)', response.url)[0]
        if domain:
            # replace the request page domain
            self.categoryUrl, result_count = re.subn(r'//(.*?)/', domain, self.categoryUrl)
            self.asyncUrl, result_count = re.subn(r'//(.*?)/', domain, self.asyncUrl)
        else:
            self.logger.error("Get the request domain error!")

        all_category = set()
        for category_url in category:
            category_id = re.findall(r'category-(\d+).htm', category_url)
            if category_id:
                all_category.add(category_id[0])

        for category_id in all_category:
            # set the category id
            result_url, result_count = re.subn(r'(\d+\d+)', category_id, self.categoryUrl)
            self.logger.info("category url : %s", result_url)
            yield Request(url=result_url, callback=self.parse_category)
Example #17
0
	def subst(self, replace, replace_with):
		"re search and replace on the current node"
		nodes = self.current_nodes[:]
		check = len(nodes) == 1
		a = self.current_attrib
		if a:
			new, num = re.subn(replace, replace_with, a.value)
			if not num:
				raise Beep
			a = self.model.set_attrib(nodes[0], a.name, new)
			self.move_to(nodes[0], a)
		else:
			self.move_to([])
			final = []
			for n in nodes:
				if n.nodeType == Node.TEXT_NODE:
					old = n.data.replace('\n', ' ')
					new, num = re.subn(replace, replace_with, old)
					if check and not num:
						self.move_to(n)
						raise Beep
					self.model.set_data(n, new)
					final.append(n)
				elif n.nodeType == Node.ELEMENT_NODE:
					old = str(n.nodeName)
					new, num = re.subn(replace, replace_with, old)
					if check and not num:
						self.move_to(n)
						raise Beep
					new_ns, x = self.model.split_qname(n, new)
					final.append(self.model.set_name(n, new_ns, new))
				else:
					self.move_to(n)
					raise Beep
			self.move_to(final)
Example #18
0
def main(inputpath):
    total_diff = 0
    second_diff = 0
    for l in ( _l.strip() for _l in open(inputpath, "r") ):
        code_count = len(l)
        content = l[1:-1]
        content, _ = re.subn(r'\\x[a-f0-9][a-f0-9]', '!', content)
        content = content.replace(r'\\', '@')
        content = content.replace(r'\"', '#')
        mem_count = len(content)
        # print l, code_count, mem_count, (code_count - mem_count)
        total_diff += (code_count - mem_count)

        # Turn each character into a number that we'll sum up at the end.
        orig_count = len(l)
        content = "1" + l + "1"
        content, _ = re.subn(r'\\x[a-f0-9][a-f0-9]', '5', content)
        content = content.replace('"', '2')
        content = content.replace('\\', '2')
        content, _ = re.subn(r'[a-z]', '1', content)
        exploded_count = sum( int(c) for c in content )
        second_diff += (exploded_count - orig_count)
        print l, content, exploded_count

    print total_diff, second_diff
Example #19
0
	def html2utf8(self,in_html):
		in_html = (re.subn(r'<(script).*?</\1>(?s)', '', in_html)[0])
		in_html = (re.subn(r'<(style).*?</\1>(?s)', '', in_html)[0])
		entitydict = {}

		entities = re.finditer('&([:_A-Za-z][:_\-.A-Za-z"0-9]*);', in_html)
		for x in entities:
			key = x.group(0)
			if key not in entitydict:
				entitydict[key] = htmlentitydefs.name2codepoint[x.group(1)]

		entities = re.finditer('&#x([0-9A-Fa-f]+);', in_html)
		for x in entities:
			key = x.group(0)
			if key not in entitydict:
				entitydict[key] = "%d" % int(x.group(1), 16)

		entities = re.finditer('&#(\d+);', in_html)
		for x in entities:
			key = x.group(0)
			if key not in entitydict:
				entitydict[key] = x.group(1)

		if 'charset="utf-8"' in in_html or 'charset=utf-8' in in_html:
			for key, codepoint in iteritems(entitydict):
				in_html = in_html.replace(key, unichr(int(codepoint)).encode('utf8'))
			self.inhtml = in_html
			return

		for key, codepoint in iteritems(entitydict):
			in_html = in_html.replace(key, unichr(int(codepoint)).encode('latin-1', 'ignore'))
		self.inhtml = in_html.decode('latin-1').encode('utf8')
Example #20
0
    def __init__(cls, name, bases, attrs):
        cls.registry = copy.deepcopy(getattr(cls, 'registry',
                                             {'mappers': {}, 'types': {}}))

        def add_mapper(f, ff, fft):
            log.debug("{} adding mapper {} for {} ({})",
                      cls.__name__, f, ff, fft)
            if f in cls.registry:
                log.warning("Overwriting {} for class {} with {} "
                            "(previous value: {})", f, name, ff,
                            cls.registry['mappers'][f])
            cls.registry['mappers'][f] = ff
            cls.registry['types'][ff] = fft

        # get form_field mappers from dunder string
        form_field_mappers = getattr(cls, '__form_fields__', {})
        for field, (form_field, form_field_type) in form_field_mappers.items():
            add_mapper(field, form_field, form_field_type)

        for key, val in attrs.items():
            try:
                form_field, form_field_type = getattr(val, 'form_field')
            except AttributeError:
                pass  # most attributes are not a form_field mapper
            else:
                field, n = re.subn(re_frender, '', key)
                assert n == 1  # only then is it a field renderer
                add_mapper(field, form_field, form_field_type)

            # get fields that need finalization
            if getattr(val, 'needs_finalization', False):
                field, n = re.subn(re_frender, '', key)
                assert n == 1  # only then is it a field renderer
                cls._to_finalize = getattr(cls, '_to_finalize', []) + [field]
Example #21
0
def book_searched(context, result):
    book = Book.objects.get(pk=result.book_id)

    # snippets = []
    # for hit in result.hits:
    #     if hit['snippets']:
    #         snippets.append(hit['snippets'])
    #     elif hit['fragment']:
    #         snippets.append(hit['fragment'].short_text)

    # We don't need hits which lead to sections but do not have
    # snippets.
    hits = filter(lambda (idx, h):
                  result.snippets[idx] is not None
                  or 'fragment' in h, enumerate(result.hits))
        #    print "[tmpl: from %d hits selected %d]" % (len(result.hits), len(hits))

    for (idx, hit) in hits:
        # currently we generate one snipper per hit though.
        if result.snippets[idx] is None:
            continue
        snip = result.snippets[idx]
        # fix some formattting
        snip = re.subn(r"(^[ \t\n]+|[ \t\n]+$)", u"",
                              re.subn(r"[ \t\n]*\n[ \t\n]*", u"\n", snip)[0])[0]

        snip = snip.replace("\n", "<br />").replace('---', '&mdash;')
        hit['snippet'] = snip

    return {
        'request': context['request'],
        'book': book,
        'hits':  hits and zip(*hits)[1] or []
    }
Example #22
0
def get_region(sequence, f_primer, r_primer):
  refhvr_cut_t = ()
  refhvr_cut = ""
  
  re_f_primer = '^.+' + f_primer
  
  re_r_primer = r_primer + '.+'
  
  hvrsequence_119_1_t = re.subn(re_f_primer, '', sequence)
  # print hvrsequence_119_1_t
  if (hvrsequence_119_1_t[1] > 0):
    refhvr_cut_t = re.subn(re_r_primer, '', hvrsequence_119_1_t[0])
    # print refhvr_cut_t
    if (refhvr_cut_t[1] > 0):
      refhvr_cut = refhvr_cut_t[0]
    else:
      if (verbose):
        print "Can't find reverse primer %s in %s" % (r_primer, sequence)
      refhvr_cut = ""
    
  else:
    if (verbose):
      print "Can't find forward primer %s in %s" % (f_primer, sequence)
    refhvr_cut = ""
    
  return refhvr_cut
def chop_end(seqs, model_end, min_modelpositions):	
	chop_seq_dict = dict()
	
	refseq = seqs[refseq_ID]
	model_pos = 0
	model_map = dict()
	for i in range(len(refseq.seq)):
		if refseq.seq[i] != ".":
			model_pos += 1
			model_map[model_pos] = i

	seq_end = model_map[model_end]
	
	for seqid in seqs.keys():
		seq = seqs[seqid]
		## slice from the end
		seqstr = str(seq.seq)[0: seq_end]
		
		if seqid != refseq_ID:
		# check if the upper case chars( model position) pass the minimum requirement
			if len(re.subn(upper_regex, "", seqstr)[0]) >= min_modelpositions:
				chop_seq_dict[seqid] =  seqstr
				
			else:	
				print "%s length %s  %s" % (seq.id, len(re.subn(upper_regex, "", seqstr)[0]), re.subn(upper_regex, "", seqstr)[0])
				pass
		else:
			chop_seq_dict[seqid] = seqstr
	return chop_seq_dict
Example #24
0
def error_027_mnemonic_codes(text):
    """Fix some cases and return (new_text, replacements_count) tuple."""
    (text, ignored) = ignore(text, r"https?://\S+")
    (text, count1) = re.subn(r"&#8211;", "–", text)
    (text, count2) = re.subn(r"&#x20;", " ", text)
    text = deignore(text, ignored)
    return (text, count1 + count2)
Example #25
0
def error_032_link_two_pipes(text):
    """Fix some cases and return (new_text, replacements_count) tuple."""
    (text, ignored) = ignore(text, r"\[\[\s*:?\s*{}.*?\]\]".format(IMAGE))
    (text, count1) = re.subn(r"\[\[([^|\[\]\n]+)\|\|([^|\[\]\n]+)\]\]", "[[\\1|\\2]]", text)
    (text, count2) = re.subn(r"\[\[([^|\[\]\n]+)\|([^|\[\]\n]+)\|\]\]", "[[\\1|\\2]]", text)
    text = deignore(text, ignored)
    return (text, count1 + count2)
Example #26
0
 def shell_remote_execution(self, tgt, arg, arg1):
     ''' Shell command execution with parameters '''
     if arg in ['disk.usage', 'network.interfaces', 'grains.items', 'test.ping', 'state.running', 'status.meminfo',
                'status.uptime', 'service.get_all']:
         params = {'client': 'local', 'tgt': tgt, 'fun': arg, 'expr_form': 'list'}
     elif arg == 'cmd.run':
         params = {'client': 'local', 'tgt': tgt, 'fun': arg, 'arg1': arg1, 'arg2': 'runas=weihu','expr_form': 'list'}
         obj = urllib.urlencode(params)
         obj, number = re.subn("arg\d", 'arg', obj)
         content = self.postRequest(obj)
         ret = content['return'][0]
         return ret
     elif arg in ['cp.get_file', 'cp.get_dir', 'cp.get_url']:
         a = arg1.split()
         a1 = a[0]
         a2 = a[1]
         params = {'client': 'local', 'tgt': tgt, 'fun': arg, 'arg1': a1, 'arg2': a2, 'expr_form': 'list'}
         obj = urllib.urlencode(params)
         obj, number = re.subn("arg\d", 'arg', obj)
         content = self.postRequest(obj)
         ret = content['return'][0]
         return ret
     else:
         params = {'client': 'local', 'tgt': tgt, 'fun': arg, 'arg': arg1, 'expr_form': 'list'}
     obj = urllib.urlencode(params)
     content = self.postRequest(obj)
     ret = content['return'][0]
     return ret
def build_template_tags(blocks):
    content = StringIO()
    block_template = '''
    {%% block %(name)s %%}
        %(comment)s

        Default:
            %(default_value)s

        %(blocks)s
    {%% endblock %(name)s %%}

    '''

    for block in blocks:
        subblocks = re.subn("\n", "\n\t", build_template_tags(block.blocks))

        if subblocks:
            subblocks = subblocks[0]

        content.write(block_template % {
            'name': block.name,
            'comment': block.comment,
            'default_value': re.subn("\n", "\n\t", str(block.default_value))[0],
            'blocks': subblocks,
        })

    return content.getvalue()
Example #28
0
def fixrow(data, key, methoddict, verify_rules, equationdict, originaldata):
    """function that i wrote to fix rows that were causing problems after results were created.
    after i did this it was clearly a problem because i had no way of knowing what caused the error.
    because of this i reworked ther interpolation script to allow for fixes in there rather than after the fact"""
    originaldata = {k: v for k, v in chain(originaldata['blockdata'].items(), originaldata['tractdata'].items())}
    def subber(matchobject):
        return '{:d}'.format(originaldata[matchobject.group(0)])

    for table, method in methoddict.items():
        testdict = verify_rules[str(method)]
        tabledata = {k.split('_')[-1][2:]: v for k, v in data.items() if table in k}
        for total, components in sorted(testdict.items(), key = lambda x: int(x[0])):
            result, valtotal, valcomps = testrule(tabledata, total, components)
            if result == 'mult':
                totalname = '{0}_av{1}'.format(table, total)
                if table == 'ds185_dt31':
                    print key, totalname, 'total', re.subn(FINDVARIABLES, subber, equationdict[totalname]['equation'])[0], valtotal
                valcomps = {k: v for k, v in valcomps.iteritems() if v == valtotal}
                lengthcomps = len(valcomps)
                for val in valcomps:
                    tabledata[val] = float(valtotal) / lengthcomps
                    fullname = '{0}_av{1}'.format(table, val)
                    if table == 'ds185_dt31':
                        print key, fullname, 'value', re.subn(FINDVARIABLES, subber, equationdict[fullname]['equation'])[0], tabledata[val]
                    data[fullname] = float(valtotal) / lengthcomps
    return data
def getDefaultEditionContent():
    '''获取标准版文件内容

    Returns:
        标准版文件内容
    '''
    partContent = ''
    for fileName in devPartFileNameList:
        partContent += open(devDirName + os.sep + fileName + '.js', 'r', encoding = encoding).read() + '\n\n'
    content = open(devDirName + os.sep + devKFOLFileName + userScriptExt, 'r', encoding = encoding).read()
    match = re.search('/\*\s*\{PartFileContent\}\s*\*/', content, flags=re.S | re.I)
    if match:
        content = content.replace(match.group(0), partContent)
    else:
        raise Exception('未找到{PartFileContent}占位符')
    content, num  = re.subn(r'(// @description.+?)(// @include)',
                     r'\g<1>'
                     '// @updateURL   https://greasyfork.org/scripts/8615-kf-online%E5%8A%A9%E6%89%8B/code/KF%20Online%E5%8A%A9%E6%89%8B.meta.js\n'
                     '// @downloadURL https://greasyfork.org/scripts/8615-kf-online%E5%8A%A9%E6%89%8B/code/KF%20Online%E5%8A%A9%E6%89%8B.user.js\n'
                     r'\g<2>',
                     content,
                     count=1,
                     flags=re.S | re.I)
    if num == 0: raise NoFoundReplaceStringError('标准版', 1)
    content, num  = re.subn(r'// @require.+?(// @version)', '\g<1>', content, count=1, flags=re.S | re.I)
    if num == 0: raise NoFoundReplaceStringError('标准版', 2)
    return content
Example #30
0
	def geturllist(self,url):	#将文档中包含的url通通加入到url列表中等待访问
		tmpurl=re.findall('<a\s+[^>]*[\s]*href[\s]*=[\s]*[\'\"]([^>^\'^\"]*)[\'\"][\s]*[^>]*>',str(self.gethtmldom(url)))#匹配文档中的url
		domain=re.findall(r'^(?:https?://)?[\w\.]*',self.url);#获取该网页的域名
		for x in tmpurl:
			x=re.subn('^\s*','',x)[0]	#将url中的开头空白字符去掉
			x=re.subn(r'\\','/',x)[0]	#所有\换成/
			if re.match('http',x,re.I):	#如果该连接是以http开头的,丢进待访问列表
				sitedomain=re.findall(r'^(?:https?://)?[\w\.]*',x)
				if self.allnet!=0 or sitedomain[0]==domain[0]:
					self.urllist.append(x)
				else:
					pass
			elif re.match('^//',x,re.I):#如果该连接是以//开头,加上http丢入列表
				x='http:'+x
				sitedomain=re.findall(r'^(?:https?://)?[\w\.]*',x)
				if self.allnet!=0 or sitedomain[0]==domain[0]:
					self.urllist.append(x)
				else:
					pass
			elif len(re.findall('^#',x))!=0:#如果是锚点就丢弃
				pass
			else:					#不然加上判断是不是绝对路径
				if len(re.findall('^/',x))!=0:	#如果是绝对路径就加上域名丢进等待列表
					x=domain[0]+x
					self.urllist.append(x)
				else:									
					if url==domain[0]:
						x=url+'/'+x
						self.urllist.append(x);
					else:
						x=re.subn(r'(/[^/]*)$','/'+x,url)[0]
						self.urllist.append(x)
		return self.urllist
Example #31
0
def _datetime(reader, elem):
    text, n = re.subn(r"(.*\.)(\d{6})\d+(\+.*)", r"\1\2\3", elem.text)
    if n > 0:
        log.debug(f"Truncate sub-microsecond time in <{QName(elem).localname}>")

    reader.push(elem, isoparse(text))
Example #32
0
def split_header_words(header_values):
    r"""Parse header values into a list of lists containing key,value pairs.

    The function knows how to deal with ",", ";" and "=" as well as quoted
    values after "=".  A list of space separated tokens are parsed as if they
    were separated by ";".

    If the header_values passed as argument contains multiple values, then they
    are treated as if they were a single value separated by comma ",".

    This means that this function is useful for parsing header fields that
    follow this syntax (BNF as from the HTTP/1.1 specification, but we relax
    the requirement for tokens).

      headers           = #header
      header            = (token | parameter) *( [";"] (token | parameter))

      token             = 1*<any CHAR except CTLs or separators>
      separators        = "(" | ")" | "<" | ">" | "@"
                        | "," | ";" | ":" | "\" | <">
                        | "/" | "[" | "]" | "?" | "="
                        | "{" | "}" | SP | HT

      quoted-string     = ( <"> *(qdtext | quoted-pair ) <"> )
      qdtext            = <any TEXT except <">>
      quoted-pair       = "\" CHAR

      parameter         = attribute "=" value
      attribute         = token
      value             = token | quoted-string

    Each header is represented by a list of key/value pairs.  The value for a
    simple token (not part of a parameter) is None.  Syntactically incorrect
    headers will not necessarily be parsed as you would want.

    This is easier to describe with some examples:

    >>> split_header_words(['foo="bar"; port="80,81"; discard, bar=baz'])
    [[('foo', 'bar'), ('port', '80,81'), ('discard', None)], [('bar', 'baz')]]
    >>> split_header_words(['text/html; charset="iso-8859-1"'])
    [[('text/html', None), ('charset', 'iso-8859-1')]]
    >>> split_header_words([r'Basic realm="\"foo\bar\""'])
    [[('Basic', None), ('realm', '"foobar"')]]

    """
    assert type(header_values) not in STRING_TYPES
    result = []
    for text in header_values:
        orig_text = text
        pairs = []
        while text:
            m = token_re.search(text)
            if m:
                text = unmatched(m)
                name = m.group(1)
                m = quoted_value_re.search(text)
                if m:  # quoted value
                    text = unmatched(m)
                    value = m.group(1)
                    value = escape_re.sub(r"\1", value)
                else:
                    m = value_re.search(text)
                    if m:  # unquoted value
                        text = unmatched(m)
                        value = m.group(1)
                        value = value.rstrip()
                    else:
                        # no value, a lone token
                        value = None
                pairs.append((name, value))
            elif text.lstrip().startswith(","):
                # concatenated headers, as per RFC 2616 section 4.2
                text = text.lstrip()[1:]
                if pairs:
                    result.append(pairs)
                pairs = []
            else:
                # skip junk
                non_junk, nr_junk_chars = re.subn("^[=\s;]*", "", text)
                assert nr_junk_chars > 0, (
                    "split_header_words bug: '%s', '%s', %s" %
                    (orig_text, text, pairs))
                text = non_junk
        if pairs:
            result.append(pairs)
    return result
from utilities import db
import re
'''
1.表示浮点数的正则表达式 -?有可能存在问号  \d+ 任意多的数字  (\.\d+)? 有可能有.数字 也可能没有 
2.格式化浮点数  format
3.如何替换原来的浮点数 sub(只返回替换结果) subn (返回元组,不仅有替换结果还有替换次数)
'''

result = re.subn('-?\d+(\.\d+)', '##',
                 'PI is 3.141592654,e is 2.71828183. -0.2 + 1.3 =1.1')
print(result)  # 将所有浮点数替换成##
print(result[0])
print(result[1])


def fun(matched):
    return format(float(matched.group()), '0.2f')


result = re.subn('-?\d+(\.\d+)', fun,
                 'PI is 3.141592654,e is 2.71828183. -0.2 + 1.3 =1.1')
print(result)
Example #35
0
 def filter(self, text):
     s = text
     for (pattern, repl) in self.patterns:
         (s, count) = re.subn(pattern, repl, s)
     return s
Example #36
0
match1 = re.search(pattern1, string11)
match2 = re.search(pattern1, string12)
if match1: print("{} matched pattern {}!".format(string11, pattern1))
if match2: print("{} matched pattern {}!".format(string12, pattern1))

# QUESTION 2
string21 = "Herecomesthe12.35kl;j;negative-12.35dj;lad"
pattern2 = r"-?[0-9]*\.[0-9]+"
# matches [.99 01.999 -1.1 etc] not integers/chars
sub = re.sub(pattern2, "float", string21)
print(sub)

# QUESTION 3
string21 = "Herecomesthe12.35kl;j;negative-12.35dj;lad"
pattern2 = r"-?[0-9]*\.[0-9]+"
sub = re.subn(pattern2, "float", string21)
print(sub[1])

# QUESTION 4
string4 = "12jkld;jafl;d-12jda;lsfja5dk;alsf6dafj;14"
pattern4 = r"-?[0-9]+"
integers = re.findall(pattern4, string4)
print(sum([int(x) for x in integers]))

# QUESTION 5
string5 = "EE364EE364EE364"
pattern5 = r"EE364"
newstring5 = re.sub(pattern5, "EE461", string5, 1)
print(newstring5)

# QUESTION 6
def buildTranslation(lang, extend=False, langPath=None):
    """Finds the file corresponding to 'lang' builds up string_cache.
    If the file is not valid, does nothing.
    Errors encountered during the process are silently ignored.
    Returns string_cache (dict) and wether the file exists (bool)."""
    log.debug("buildTranslation <<<")
    tm = time()
    if not langPath:
        langPath = getLangPath()
    #str_cache = {}
    global string_cache
    fileFound = False
    lang = u"%s" % lang
    fName = os.path.join(langPath, lang + ".trn")
    log.debug("fName: %s" % fName)
    if os.access(fName, os.F_OK) and os.path.isfile(fName) and os.access(fName, os.R_OK):
        fileFound = True
        rawData = codecs.open(fName, "r", "utf-8").read()
        log.debug("fName is valid and read.")
        log.debug("Type of file data is %s"%("%s"%type(rawData)).strip("<type '").strip(">")[:-1])
        log.debug("Parsing file and building JSON resource.")
        log.debug("  * Start on %s"%tm)
        start = re.search(r"^o\d+[ ]", rawData, re.M|re.S)
        if start:
            start = start.start()
        else:
            log.warning("    *** %s malformed. Could not find entry point.\n    Translation not loaded.")
            # exiting without further operations
            return {}, False
        data = rawData[start:]
        trnPattern = re.compile(r"^o\d+[ ]|^t\d+[ ]", re.M|re.S)
        grps = re.findall(trnPattern, data)
        if len(grps) % 2 != 0:
            grps1 = re.findall(r"^o\d+[ ]", data, re.M|re.S)
            grps2 = re.findall(r"^t\d+[ ]", data, re.M|re.S)
            log.warning("    Unpaired original and translated strings. %s is longer (oXX: %s, tXX: %s)."%({True: "tXX", False: "oXX"}[len(grps1) < len(grps2)], len(grps1), len(grps2)))
            bugStrs = []

            def compLists(lst1, lst1N, lst2, lst2N, repl, bugStrs=bugStrs):
                bug = []
                for item in lst1:
                    itm = repl + item[1:]
                    if itm in lst2:
                        idx = lst2.index(itm)
                        lst2.pop(idx)
                    else:
                        bug.append(item)
                bugStrs += ["Not found in %s"%lst1N, bug]
                bugStrs += ["Not found in %s"%lst2N, lst2]
            if len(grps1) < len(grps2):
                compLists(grps1, "grps1", grps2, "grps2", u"t")
                log.warning("    Compared oXX tXX:")
                log.warning("    %s"%bugStrs)
            else:
                compLists(grps2, "grps2", grps1, "grps1", u"o")
                log.warning("    Compared tXX oXX:")
                log.warning("    %s"%bugStrs)
            return {}, False
        n1 = len(grps) / 2
        result = u""
        n = 1
        n2 = 0
        r = u"" + data.replace(u"\\", u"\\\\").replace(u"\"", u'\\"').replace(u"\r\n", u"\n").replace(u"\r", u"\n")
        log.debug("    Replacing oXX/tXX.")
        while n:
            r, n = re.subn(r"^o\d+[ ]|\no\d+[ ]", "\",\"", r, flags=re.M|re.S)
            r, n = re.subn(r"^t\d+[ ]|\nt\d+[ ]", "\":\"", r, flags=re.M|re.S)
            n2 += n
            if n2 == n1:
                n = 0
        log.debug("    Replaced %s occurences."%n2)
        result += r[2:]
        result = u"{" + result.replace(u"\r\n", u"\\n").replace(u"\n", u"\\n").replace(u"\t", u"\\t") + u"\"}"
        log.debug("    Conversion done. Loading JSON resource.")
        try:
            str_cache = json.loads(result)
            if extend:
                string_cache.update([(a, b) for (a, b) in str_cache.items() if a not in string_cache.keys()])
        except Exception as e:
            log.debug("Error while loading JSON resource:")
            log.debug("    %s"%e)
            log.debug("Dumping JSON data in %s.json"%lang)
            f = open('%s.json'%lang, 'w')
            f.write(result)
            f.close()
            return {}, False
#         log.debug("    Setting up font.") # Forgotten this here???
        if not extend:
            line = rawData.splitlines()[0]
            if "#-# " in line:
                lngNm = line.split("#-# ")[1].strip()
            else:
                lngNm = os.path.splitext(os.path.basename(fName))[0]
            try:
                resource.setCurLang(lngNm)
            except:
                resource.__curLang = lngNm
        tm1 = time()
        log.debug("  * End on %s duration %s"%(tm, tm1 - tm))
    else:
        log.debug("fName is not valid beacause:")
        if not os.access(fName, os.F_OK):
            log.debug("  * Can't access file.")
        if not os.path.isfile(fName):
            log.debug("  * It's not a file.")
        if not os.access(fName, os.R_OK):
            log.debug("  * Is not readable.")
        log.debug("Default strings will be used.")
        str_cache = {}
    if not extend:
        string_cache = str_cache
    log.debug("buildTranslation >>>")
    return string_cache, fileFound
Example #38
0
def replaceContraction(text):
    patterns = [(re.compile(regex), repl)
                for (regex, repl) in contraction_patterns]
    for (pattern, repl) in patterns:
        (text, count) = re.subn(pattern, repl, text)
    return text
Example #39
0
    def save_files_generic(self):
        #####
        from digits.job_client import ssd_pascal
        ############## parameters #############
        # Add non-data layers
        job_path = self.path(self.train_val_file)
        solver_file = self.path('solver.prototxt')
        train_net_path = self.path('train_val.prototxt')
        test_net_path = self.path('test.prototxt')
        snapshot_path = self.path('VOC_Snapshot')
        train_data_path = self.data_dir+'/'+'VOC0712_trainval_lmdb'
        test_data_path = self.data_dir+'/'+'VOC0712_test_lmdb'
        label_map_file = self.data_dir+'/'+'labelmap_voc.prototxt'
        name_size_file = self.data_dir+'/'+'test_name_size.txt'
        output_result_dir = self.data_dir+'/'+'Main'

        iter_size = self.batch_accumulation / self.batch_size
        ################ end ##################
        print '----------------------------------------------'
        print 'train_ssd'
        print '----------------------------------------------'
        ############## train_net ##############
        # ssd_pascal.CreateTrainNet(train_net_path, train_data_path, self.batch_size) 
        ###directly edit on custom network text form page
        network_test, n = re.subn('(?<=source:)(.+)?(?=\n)', '"'+train_data_path+'"', self.network_text)
        # print (network_test)
        f = open(train_net_path, 'w')
        f.write(network_test)
        f.close()
        ################ end ##################

        ############### test_net ############## 
        # print 'create test.prototxt'
        # ssd_pascal.CreateTestNet(test_net_path, test_data_path, self.test_batch_size, 
        #     label_map_file, name_size_file, output_result_dir)
        ################# end #################
                                 
        ############## ssd solver #############
        solver = caffe_pb2.SolverParameter()

        # solver.max_iter = 120000
        solver.max_iter = self.max_iter_num
        self.solver = solver

        # Create solver
        ssd_pascal.CreateSolver(solver_file, 
            train_net_path, test_net_path, snapshot_path, 
            self.learning_rate, iter_size, self.solver_type)
        ################### end ###############

        ############## deploy_net #############
        deploy_network = caffe_pb2.NetParameter()
        # Write to file
        with open(self.path(self.deploy_file), 'w') as outfile:
            text_format.PrintMessage(deploy_network, outfile)

        with open(self.path('original.prototxt'), 'w') as outfile:
            text_format.PrintMessage(deploy_network, outfile)

        ################# end #################

        ############## snapshot ##############

        solver.snapshot_prefix = self.snapshot_prefix

        snapshot_interval = self.snapshot_interval * ( solver.max_iter / self.train_epochs )
        if 0 < snapshot_interval <= 1:
            solver.snapshot = 1  # don't round down
        elif 1 < snapshot_interval < solver.max_iter:
            solver.snapshot = int(snapshot_interval)
        else:
            solver.snapshot = 0  # only take one snapshot at the end

        ################# end #################
        
        return True
def _nameify(in_str):
    name = ''.join(
        c if c.isalnum() else '-'
        for c in in_str.lower()
    ).strip('-')
    return re.subn(r'\-+', '-', name)[0]
def new_submission(url, mode=1, samedate=False):
    """
    fetching new submission everyday

    :param url: string, the url for the new page of certain category
    :param mode: int, 0 for new, 1 for cross, 2 for both
    :param samedate: boolean, if true, there is a check to make sure the submission is for today
    :return: list of dict, containing all papers
    """
    pa = requests.get(url)
    so = BeautifulSoup(pa.text, "lxml")
    if samedate is True:
        date_filter = re.compile(r"^New submissions for ([a-zA-Z]+), .*")
        try:
            weekdaystr = date_filter.match(so("h3")[0].string).group(1)
        except AttributeError:
            return []
        if weekdaylist[datetime.today().weekday()] != weekdaystr:
            return []

    submission_pattern = re.compile(r"(.*) for .*")
    submission_list = so("h3")
    submission_dict = {}
    for s in submission_list:
        dict_key = submission_pattern.match(s.string).group(1)
        submission_dict[dict_key] = True

    if mode == 0 and submission_dict.get("New submissions", False):
        newc = so("dl")[0]
    elif mode == 1 and submission_dict.get("Cross-lists", False):
        newc = so("dl")[1]
    elif submission_dict.get("New submissions", False) and submission_dict.get(
            "Cross-lists", False):
        newc = BeautifulSoup(str(so("dl")[0]) + str(so("dl")[1]), "lxml")
    else:
        return []

    newno = len(newc("span", class_="list-identifier"))
    contents = []
    subjectabbr_filter = re.compile(r"^.*[(](.*)[)]")
    for i in range(newno):
        content = {}
        id_ = list(newc("span", class_="list-identifier")[i].children)[0].text
        content["arxiv_id"] = re.subn(r"arXiv:", "", id_)[0]
        content["arxiv_url"] = "https://arxiv.org/abs/" + content["arxiv_id"]
        title = newc("div", class_="list-title mathjax")[i].text
        content["title"] = re.subn(r"\n|Title: ", "", title)[0]
        author = newc("div", class_="list-authors")[i].text
        content["authors"] = [
            re.subn(r"\n|Authors:", "", author)[0].strip()
            for author in author.split(",")
        ]
        subject = newc("div", class_="list-subjects")[i].text
        content["subject"] = [
            re.subn(r"\n|Subjects: ", "", sub.strip())[0]
            for sub in subject.split(r";")
        ]
        content["subject_abbr"] = [
            subjectabbr_filter.match(d).group(1) for d in content["subject"]
        ]
        abstract = newc("p", class_="mathjax")[i].text
        content["summary"] = re.subn(r"\n", " ", abstract)[0]
        content["announce_date"] = date.today().strftime("%Y-%m-%d")
        contents.append(content)
    return contents
Example #42
0
#encoding=utf8
import requests, re

sql = "select table_name from information_schema.tables limit 60,1"  #WEEK44SQLIII    FLLLLLAG

sql = re.subn('\s', chr(10), sql)[0]
hex_range = [i for i in range(130) if i != 37 and i != 95]
evalstr = ''
strlist = []

for str_len in range(1, 20):
    for i in hex_range:
        if str_len == 1:
            params = {
                "user": r"a\'or(Lpad(({}),1,1)like({}))#".format(sql, hex(i))
            }
            r = requests.get("http://118.25.18.223:10088/index.php",
                             params=params)
            if r.text[:5] == 'admin':
                strlist.append(i)
                break
        else:
            params = {
                "user":
                r"a\'or(Lpad(({}),{},1)like({}))#".format(
                    sql, str_len, hex_str + hex(i)[2:])
            }
            r = requests.get("http://118.25.18.223:10088/index.php",
                             params=params)
            if r.text[:5] == 'admin':
                strlist.append(i)
Example #43
0
 def assertEqualNoWhitespace(self, a, b):
     remove_whitespace = lambda s: re.subn(r'[\s\n]', '', s)[0]
     self.assertEqual(remove_whitespace(a), remove_whitespace(b))
<_sre.SRE_Match object; span=(1, 2), match='h'>

>>> re.search("h","Hhelhlo"[2:0])
>>> re.findall("h","Hhelhlo")
['h', 'h']
>>> for match in re.finditer("h","Hhelhlo"):
...     print(match)
... 
<_sre.SRE_Match object; span=(1, 2), match='h'>
<_sre.SRE_Match object; span=(4, 5), match='h'>


>>> re.sub("h","8","Hhelhlo")
'H8el8lo'

>>> re.subn("h","8","Hhelhlo",1)
('H8elhlo', 1)

>>> re.subn("h","8","Hhelhlo",1,re.IGNORECASE)
('8helhlo', 1)

>>> re.subn("h","8","Hhelhlo",re.IGNORECASE)
('H8el8lo', 2)

>>> re.sub("h","8","Hhelhlo",re.IGNORECASE)
'H8el8lo'

>>> re.sub("h","8","Hhelhlo",3,re.IGNORECASE)
'88el8lo'

>>> re.subn("h","8","Hhelhlo",3,re.IGNORECASE)
Example #45
0
    } return res.join('');
}"""

METHODS['upper'] = """function () { // nargs: 0
    if (this.constructor !== String) return this.KEY.apply(this, arguments);
    return this.toUpperCase();
}"""

METHODS['zfill'] = """function (width) { // nargs: 1
    if (this.constructor !== String) return this.KEY.apply(this, arguments);
    return METHOD_PREFIXrjust(this, width, '0');
}"""


for key in METHODS:
    METHODS[key] = re.subn(r'METHOD_PREFIX(.+?)\(',
                           r'METHOD_PREFIX\1.call(', METHODS[key])[0]
    METHODS[key] = METHODS[key].replace(
        'KEY', key).replace(
        'FUNCTION_PREFIX', FUNCTION_PREFIX).replace(
        'METHOD_PREFIX', METHOD_PREFIX).replace(
        ', )', ')')
    
for key in FUNCTIONS:
    FUNCTIONS[key] = re.subn(r'METHOD_PREFIX(.+?)\(',
                             r'METHOD_PREFIX\1.call(', FUNCTIONS[key])[0]
    FUNCTIONS[key] = FUNCTIONS[key].replace(
        'KEY', key).replace(
        'FUNCTION_PREFIX', FUNCTION_PREFIX).replace(
        'METHOD_PREFIX', METHOD_PREFIX)
Example #46
0
def sanitize(hostname: str) -> str:
    return re.subn("[\s\.]+", "", hostname.lower())[0]
Example #47
0
    def remove_strings_with_pattern(self, content, pattern):

        result, number = re.subn(pattern, '', content)

        return result
    def update(self):
        now = time.time()
        if now - self._last_modified < 1:
            if self.verbose:
                print(f'{self.md_file} is likely up-to-date')
            return

        if self.verbose:
            print(f'{self.md_file} processing')

        prev_image_files = self.image_files.copy()
        self.image_files.clear()

        f = open(self.md_file)
        orig_content = f.read()
        f.close()

        if not orig_content:
            if self.verbose:
                print('Got empty content. Sleeping 1 sec and try again.')
            time.sleep(1)
            f = open(self.md_file)
            orig_content = f.read()
            f.close()

        if not orig_content:
            print('Empty file. Skip update!')
            return

        content = orig_content

        if content.find(header.split(None, 1)[0]) == -1:
            content = header + '\n' + content
            if not content.rstrip().endswith('<!--EOF-->'):
                content += '\n<!--EOF-->'

        elif not content.rstrip().endswith('<!--EOF-->'):
            print('Expected <!--EOF--> at the end of file. Skip update!')
            return

        if self.parent.run_pandoc:
            f = open(self.html_file, 'w')
            f.write('''
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta http-equiv="Content-Style-Type" content="text/css" />
  <meta http-equiv="refresh" content="0.5" />
  <meta name="generator" content="watch_latex_md.py" />
  <title>Wait...</title>
  <style type="text/css">code{white-space: pre;}</style>
</head>
<body>
<h1>Please wait until watch_latex_md.py updates %s...</h1>
<h2>The page will reload automatically.</h2>
</body>
</html>
            ''' % (self.html_file))
            f.close()

        if 'watch-latex-md:force-rerender' in content:
            print('Setting force-rerender')
            self._force_rerender = True

        self.equation_counter = 0
        self.label_counter = {}
        for pattern, repl in [
                (r'(?m)(?P<latex>[$]+[^$]+[$]+)', self.latex_to_img),
                ((r'(?m)([<]img\s+data[-]latex=["]\s*)(?P<latex>.*?)'
                  r'["]\s+src=.*?\s+alt="latex">'),
                 self.img_to_svg),
                ((r'(\[(?P<title>[^]]+)\][(][#](?P<label>[^)]+)[)])'),
                 self.update_label_title)]:
            content, _count = re.subn(
                pattern, repl,
                content,
                flags=re.S
            )

        if self.force_rerender or content != orig_content:
            f = open(self.md_file, 'w')
            f.write(content)
            f.close()
            self._last_modified = time.time()
            if self.verbose:
                print(f'{self.md_file} is updated')
        else:
            if self.verbose:
                print(f'{self.md_file} is up-to-date')

        self._force_rerender = False

        if self.parent.run_pandoc:
            try:
                check_output(
                    ['pandoc',
                     '-f', 'gfm',
                     '-t', 'html',
                     '--metadata',
                     'title=' + os.path.basename(self.md_file),
                     '-s', self.md_file,
                     '-o', self.html_file],
                    stderr=sys.stdout)
            except Exception as msg:
                print(f'{self.md_file} pandoc failed: {msg}')
            else:
                if self.verbose:
                    print(f'{self.html_file} is generated')

        new_files = list(self.image_files.difference(prev_image_files))
        obsolete_files = list(prev_image_files.difference(self.image_files))

        if self.use_git:
            for fn in new_files:
                if not self.git_check_added(fn):
                    self.git_add_file(fn)
            for fn in obsolete_files:
                if self.git_check_added(fn):
                    self.git_rm_file(fn)
                else:
                    self.rm_file(fn)
        else:
            if self.git_check_repo():
                for fn in obsolete_files:
                    if self.git_check_added(fn):
                        print(f'{fn} is obsolete in git repo '
                              '[use --git to enable auto-removal]')
                    else:
                        self.rm_file(fn)
            else:
                for fn in obsolete_files:
                    self.rm_file(fn)
Example #49
0
    def OnGUINotify(self, id, control, event):
        global gIsExplorer
        global gSelectedSlot
        global gPlayerList
        global gExp_HotSpot
        global gMinusExplorer
        global gMinusVisitor
        global gClickedWrongSlot

        #print "xDialogStartUp: GUI Notify id=%d, event=%d control=" % (id,event),control
        if control:
            tagID = control.getTagID()

        #################################
        ##     Visitor Player List     ##
        #################################
        if id == GUIDiag4a.id:
            if event == kAction or event == kValueChanged:
                if tagID == k4aVisitID:  ## Visit Uru ##
                    PtHideDialog("GUIDialog04a")
                    PtShowDialog("GUIDialog05")

                elif tagID == k4aQuitID:  ## Quit ##
                    PtYesNoDialog(self.key, "Are you sure you want to quit?")

                elif tagID == k4aDeleteID:  ## Delete Visitor ##
                    print gSelectedSlot
                    print type(gSelectedSlot)
                    if gSelectedSlot:
                        deleteString = U"Would you like to delete the VISITOR " + unicode(
                            gPlayerList[gSelectedSlot -
                                        gMinusVisitor][0]) + U"?"
                        ptGUIControlTextBox(
                            GUIDiag4c.dialog.getControlFromTag(
                                k4cStaticID)).setStringW(deleteString)
                        self.PlayerListNotify(GUIDiag4a, gVis_HotSpot, 0)
                        PtShowDialog("GUIDialog04c")
                    ## Or Else?? ##

                elif tagID == k4aPlayer01:  ## Click Event ##
                    if gPlayerList[0]:
                        self.SelectSlot(GUIDiag4a, tagID)
                    else:
                        PtHideDialog("GUIDialog04a")
                        PtShowDialog("GUIDialog06")

                elif tagID == k4aPlayer02 or tagID == k4aPlayer03 or k4aPlayer04 or k4aPlayer05 or k4aPlayer06:  ## Shortcut Skip to Create Visitor ##
                    gClickedWrongSlot = 1
                    PtHideDialog("GUIDialog04a")
                    PtShowDialog("GUIDialog05")

            elif event == kInterestingEvent:
                self.ToggleColor(GUIDiag4a, tagID)

        #################################
        ##    Explorer Player List     ##
        #################################
        elif id == GUIDiag4b.id:
            if event == kAction or event == kValueChanged:
                if tagID == k4bExploreID:  ## Explore Uru ##
                    if gSelectedSlot:
                        PtShowDialog("GUIDialog06a")
                        print "Player selected."

                        # start setting active player (we'll link out when this operation completes)
                        playerID = gPlayerList[gSelectedSlot -
                                               gMinusExplorer][1]
                        print "Setting active player."
                        PtSetActivePlayer(playerID)

                    ## Or Else?? ##

                elif tagID == k4bQuitID:  ## Quit ##
                    PtYesNoDialog(self.key, "Are you sure you want to quit?")

                elif tagID == k4bDeleteID:  ## Delete Explorer ##
                    if gSelectedSlot:
                        deleteString = U"Would you like to delete the EXPLORER " + unicode(
                            gPlayerList[gSelectedSlot -
                                        gMinusExplorer][0]) + U"?"
                        ptGUIControlTextBox(
                            GUIDiag4c.dialog.getControlFromTag(
                                k4cStaticID)).setStringW(deleteString)
                        self.PlayerListNotify(GUIDiag4b, gExp_HotSpot, 0)
                        PtShowDialog("GUIDialog04c")
                    ## Or Else?? ##

                elif tagID == k4bPlayer01:  ## Click Event ##
                    errorString = "As an URU subscriber, you are an EXPLORER (not a VISITOR) and are free to enjoy complete and unlimited access to URU content."
                    ptGUIControlTextBox(
                        GUIDiag4d.dialog.getControlFromTag(
                            k4dTextID)).setString(errorString)
                    PtShowDialog("GUIDialog04d")
                    self.ToggleColor(GUIDiag4b, k4bPlayer01)
                    self.PlayerListNotify(GUIDiag4b, gExp_HotSpot, 0)

                elif (tagID == k4bPlayer02 or tagID == k4bPlayer03
                      or tagID == k4bPlayer04 or tagID == k4bPlayer05 or tagID
                      == k4bPlayer06) and not (tagID == gSelectedSlot):
                    if gPlayerList[tagID - gMinusExplorer]:
                        self.SelectSlot(GUIDiag4b, tagID)
                    else:
                        PtHideDialog("GUIDialog04b")
                        PtShowDialog("GUIDialog06")

            elif event == kInterestingEvent:  ## RollOver Event ##
                self.ToggleColor(GUIDiag4b, tagID)

        #################################
        ##        Delete Player        ##
        #################################
        elif id == GUIDiag4c.id:
            if event == kAction or event == kValueChanged:
                if tagID == k4cYesID:  ## Confirm Delete ##
                    playerID = 0
                    if gIsExplorer:
                        playerID = gPlayerList[gSelectedSlot -
                                               gMinusExplorer][1]
                    else:
                        playerID = gPlayerList[gSelectedSlot -
                                               gMinusVisitor][1]
                    PtDeletePlayer(playerID)

                elif tagID == k4cNoID:  ## Cancel Delete ##
                    if not (gSelectedSlot == k4bPlayer03) or not (
                            gSelectedSlot == k4aPlayer03):
                        if gIsExplorer:
                            self.ToggleColor(GUIDiag4b, k4bPlayer03)
                        else:
                            self.ToggleColor(GUIDiag4a, k4aPlayer03)
                    PtHideDialog("GUIDialog04c")
                    if gIsExplorer:
                        self.PlayerListNotify(GUIDiag4b, gExp_HotSpot, 1)
                    else:
                        self.PlayerListNotify(GUIDiag4a, gVis_HotSpot, 1)

        #################################
        ##         Not Needed          ##
        #################################
        elif id == GUIDiag4d.id:
            if event == kAction or event == kValueChanged:
                if tagID == k4dYesID:  ## Continue ##
                    if gIsExplorer:
                        self.ToggleColor(GUIDiag4b, k4bPlayer03)
                        self.PlayerListNotify(GUIDiag4b, gExp_HotSpot, 1)
                    else:
                        self.ToggleColor(GUIDiag4a, k4aPlayer03)
                        self.PlayerListNotify(GUIDiag4a, gVis_HotSpot, 1)
                    PtHideDialog("GUIDialog04d")
                    ptGUIControlButton(
                        GUIDiag6.dialog.getControlFromTag(k6PlayID)).enable()

        #################################
        ##         Nag Screen          ##
        #################################
        elif id == GUIDiag5.id:
            if event == kAction or event == kValueChanged:
                if tagID == k5PayID:  ## Quit And Register ##
                    WebLaunchCmd(
                        "https://account.gametap.com/storefront/myst/login/login.do"
                    )

                elif tagID == k5VisitID:  ## Continue As Visitor ##
                    if gClickedWrongSlot:
                        gClickedWrongSlot = 0
                        PtHideDialog("GUIDialog05")
                        PtShowDialog("GUIDialog04a")
                    elif gPlayerList[0]:
                        PtShowDialog("GUIDialog06a")
                        print "Player selected."

                        # start setting active player (we'll link out when this operation completes)
                        playerID = gPlayerList[0][1]
                        print "Setting active player."
                        PtSetActivePlayer(playerID)
                    else:
                        PtHideDialog("GUIDialog05")
                        PtShowDialog("GUIDialog06")

                elif tagID == k5BackID:  ## Back To Player Select ##
                    PtHideDialog("GUIDialog05")
                    PtShowDialog("GUIDialog04a")

                elif tagID == k5LinkID:  ## Link ##
                    WebLaunchCmd(
                        "https://account.gametap.com/storefront/myst/login/login.do"
                    )

        #################################
        ##        Create Player        ##
        #################################
        elif id == GUIDiag6.id:
            if event == kAction or event == kValueChanged:
                if tagID == k6QuitID:  ## Quit ##
                    PtYesNoDialog(self.key, "Are you sure you want to quit?")

                elif tagID == k6BackID:  ## Back To Player Select ##
                    PtHideDialog("GUIDialog06")
                    if gIsExplorer:
                        PtShowDialog("GUIDialog04b")
                    else:
                        PtShowDialog("GUIDialog04a")

                elif tagID == k6PlayID:  ## Play ##
                    playerName = ptGUIControlEditBox(
                        GUIDiag6.dialog.getControlFromTag(
                            k6NameID)).getString()  #                 <---
                    playerNameW = ptGUIControlEditBox(
                        GUIDiag6.dialog.getControlFromTag(
                            k6NameID)).getStringW()  #                 <---
                    inviteCode = ptGUIControlEditBox(
                        GUIDiag6.dialog.getControlFromTag(
                            k6InviteID)).getString()

                    # since the code is in hex we know letters like i, l, and o can't be in the code so try
                    # converting any to 1 or 0 in case the user thought they were the wrong thing
                    inviteCode = inviteCode.lower().replace("i", "1").replace(
                        "l", "1").replace("o", "0")

                    try:
                        playerName == playerNameW
                    except:
                        errorString = "Error, invalid Name. Please enter another."
                        ptGUIControlTextBox(
                            GUIDiag4d.dialog.getControlFromTag(
                                k4dTextID)).setString(errorString)
                        PtShowDialog("GUIDialog04d")
                        if gIsExplorer:
                            self.ToggleColor(GUIDiag4b, k4bPlayer03)
                        else:
                            self.ToggleColor(GUIDiag4a, k4aPlayer03)
                        return

                    playerGender = ""
                    if ptGUIControlCheckBox(
                            GUIDiag6.dialog.getControlFromTag(
                                k6MaleID)).isChecked():
                        playerGender = "male"
                    if ptGUIControlCheckBox(
                            GUIDiag6.dialog.getControlFromTag(
                                k6FemaleID)).isChecked():
                        playerGender = "female"

                    if playerName == U"" or playerName == "":
                        errorString = "Error, you must enter a Name."
                        ptGUIControlTextBox(
                            GUIDiag4d.dialog.getControlFromTag(
                                k4dTextID)).setString(errorString)
                        PtShowDialog("GUIDialog04d")
                        if gIsExplorer:
                            self.ToggleColor(GUIDiag4b, k4bPlayer03)
                        else:
                            self.ToggleColor(GUIDiag4a, k4aPlayer03)
                    elif playerGender == "":
                        errorString = "Error, you must select a gender."
                        ptGUIControlTextBox(
                            GUIDiag4d.dialog.getControlFromTag(
                                k4dTextID)).setString(errorString)
                        PtShowDialog("GUIDialog04d")
                        if gIsExplorer:
                            self.ToggleColor(GUIDiag4b, k4bPlayer03)
                        else:
                            self.ToggleColor(GUIDiag4a, k4aPlayer03)
                    else:
                        fixedPlayerName = playerName.strip()
                        (fixedPlayerName, whitespacefixedcount) = re.subn(
                            "\s{2,}|[\t\n\r\f\v]", " ", fixedPlayerName)

                        (
                            fixedPlayerName,
                            RogueCount,
                        ) = re.subn('[\x00-\x1f]', '', fixedPlayerName)
                        if RogueCount > 0 or whitespacefixedcount > 0:
                            if RogueCount > 0:
                                errorString = "Warning, you entered invalid characters in your player name.  The invalid characters have been removed, please make sure your player name is still what you want."
                            else:
                                errorString = "Warning, your player name has some incorrect formatting.  The formatting has been corrected, please make sure your player name is still what you want."
                            ptGUIControlTextBox(
                                GUIDiag4d.dialog.getControlFromTag(
                                    k4dTextID)).setString(errorString)
                            PtShowDialog("GUIDialog04d")
                            if gIsExplorer:
                                self.ToggleColor(GUIDiag4b, k4bPlayer03)
                            else:
                                self.ToggleColor(GUIDiag4a, k4aPlayer03)

                            ptGUIControlEditBox(
                                GUIDiag6.dialog.getControlFromTag(
                                    k6NameID)).setString(fixedPlayerName)
                        else:
                            print "Creating Player"
                            PtShowDialog("GUIDialog06a")
                            ptGUIControlButton(
                                GUIDiag6.dialog.getControlFromTag(
                                    k6PlayID)).disable()
                            PtCreatePlayer(
                                fixedPlayerName, playerGender, inviteCode
                            )  #                                                  <---

                elif tagID == k6MaleID:  ## Gender Male ##
                    if ptGUIControlCheckBox(
                            GUIDiag6.dialog.getControlFromTag(
                                k6FemaleID)).isChecked():
                        ptGUIControlCheckBox(
                            GUIDiag6.dialog.getControlFromTag(
                                k6FemaleID)).setChecked(0)

                elif tagID == k6FemaleID:  ## Gender Female ##
                    if ptGUIControlCheckBox(
                            GUIDiag6.dialog.getControlFromTag(
                                k6MaleID)).isChecked():
                        ptGUIControlCheckBox(
                            GUIDiag6.dialog.getControlFromTag(
                                k6MaleID)).setChecked(0)
Example #50
0
def clean_license_name(license_name):
    """Remove the word ``license`` from the license
    :param str license_name: Receives the license name
    :return str: Return a string without the word ``license``
    """
    return re.subn(r'(.*)\s+license', r'\1', license_name, flags=re.IGNORECASE)[0]
def replace_e1(sentence, e2):
    result, _ = re.subn(r"<e1>(.+?)</e1>", "<e1>%s</e1>" % e2, sentence)
    return result
Example #52
0
 def clearAllHtmlLabel(data):
     result, number = re.subn("<[\s\S]+?>", '', data)
     return result
Example #53
0
 def _pad(s):
     return re.subn('(\n)', '\n    ', s)[0]
def replace_e2(sentence, e1):
    result, _ = re.subn(r"<e2>(.+?)</e2>", "<e2>%s</e2>" % e1, sentence)
    return result
Example #55
0
def subn(pattern, repl, string, count=0, flags=0):
    """
    subn after parsing unicode properties and set flag to unicode
    """
    re.subn(parse_unicode_properties(pattern), repl, string,
            flags | re.UNICODE)
Example #56
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            path=dict(required=True, aliases=['dest', 'destfile', 'name'], type='path'),
            regexp=dict(required=True),
            replace=dict(default='', type='str'),
            after=dict(required=False),
            before=dict(required=False),
            backup=dict(default=False, type='bool'),
            validate=dict(default=None, type='str'),
            encoding=dict(default='utf-8', type='str'),
        ),
        add_file_common_args=True,
        supports_check_mode=True
    )

    params = module.params
    path = params['path']
    encoding = params['encoding']
    res_args = dict()

    params['after'] = to_text(params['after'], errors='surrogate_or_strict', nonstring='passthru')
    params['before'] = to_text(params['before'], errors='surrogate_or_strict', nonstring='passthru')
    params['regexp'] = to_text(params['regexp'], errors='surrogate_or_strict', nonstring='passthru')
    params['replace'] = to_text(params['replace'], errors='surrogate_or_strict', nonstring='passthru')

    if os.path.isdir(path):
        module.fail_json(rc=256, msg='Path %s is a directory !' % path)

    if not os.path.exists(path):
        module.fail_json(rc=257, msg='Path %s does not exist !' % path)
    else:
        f = open(path, 'rb')
        contents = to_text(f.read(), errors='surrogate_or_strict', encoding=encoding)
        f.close()

    pattern = u''
    if params['after'] and params['before']:
        pattern = u'%s(?P<subsection>.*?)%s' % (params['before'], params['after'])
    elif params['after']:
        pattern = u'%s(?P<subsection>.*)' % params['after']
    elif params['before']:
        pattern = u'(?P<subsection>.*)%s' % params['before']

    if pattern:
        section_re = re.compile(pattern, re.DOTALL)
        match = re.search(section_re, contents)
        if match:
            section = match.group('subsection')
        else:
            res_args['msg'] = 'Pattern for before/after params did not match the given file: %s' % pattern
            res_args['changed'] = False
            module.exit_json(**res_args)
    else:
        section = contents

    mre = re.compile(params['regexp'], re.MULTILINE)
    result = re.subn(mre, params['replace'], section, 0)

    if result[1] > 0 and section != result[0]:
        if pattern:
            result = (contents.replace(section, result[0]), result[1])
        msg = '%s replacements made' % result[1]
        changed = True
        if module._diff:
            res_args['diff'] = {
                'before_header': path,
                'before': contents,
                'after_header': path,
                'after': result[0],
            }
    else:
        msg = ''
        changed = False

    if changed and not module.check_mode:
        if params['backup'] and os.path.exists(path):
            res_args['backup_file'] = module.backup_local(path)
        if params['follow'] and os.path.islink(path):
            path = os.path.realpath(path)
        write_changes(module, to_bytes(result[0], encoding=encoding), path)

    res_args['msg'], res_args['changed'] = check_file_attrs(module, changed, msg)
    module.exit_json(**res_args)
Example #57
0
s = "Levi:1994,Sunny:1993"
pattern = r"(\w+):(\d+)"
#re模块调用
l01 = re.findall(pattern,s)
print(l01)

#compile对象调用
regex = re.compile(pattern)
pos =0
endpos=12
l02 = regex.findall(s,0,12)
print(l02)
l03 = re.split(r"[^\w]+","how are  you l-qwww-1';asds as")
print(l03)
s = "时间:2020/7/25"
ns = re.subn(r"/","-",s)
print(ns)
s = "2019年建国70周年"
pattern = r"\d+"
iter01= re.finditer(pattern,s)
print(iter01)
for i in iter01:
    print(i,i.group())
#完全匹配
m= re.fullmatch(r"\w+","hello-1973")
print(m)
#匹配开始位置
m= re.match(r"[A-Z]\w+","Hello 1973")
print(m.group())
#匹配开始位置第一处
m= re.search(r"\S+","Hello 1973")
Example #58
0
    def authenticate(self, handler, data):
        username = data['username']
        password = data['password']

        # Protect against invalid usernames as well as LDAP injection attacks
        if not re.match(self.valid_username_regex, username):
            self.log.warn(
                'username:%s Illegal characters in username, must match regex %s',
                username, self.valid_username_regex)
            return None

        # No empty passwords!
        if password is None or password.strip() == '':
            self.log.warn('username:%s Login denied for blank password',
                          username)
            return None

        if self.lookup_dn:
            username = self.resolve_username(username)
            if not username:
                return None

        if self.lookup_dn:
            if str(self.lookup_dn_user_dn_attribute).upper() == 'CN':
                # Only escape commas if the lookup attribute is CN
                username = re.subn(r"([^\\]),", r"\1\,", username)[0]

        bind_dn_template = self.bind_dn_template
        if isinstance(bind_dn_template, str):
            # bind_dn_template should be of type List[str]
            bind_dn_template = [bind_dn_template]

        is_bound = False
        for dn in bind_dn_template:
            if not dn:
                self.log.warn("Ignoring blank 'bind_dn_template' entry!")
                continue
            userdn = dn.format(username=username)
            if self.escape_userdn:
                userdn = escape_filter_chars(userdn)
            msg = 'Attempting to bind {username} with {userdn}'
            self.log.debug(msg.format(username=username, userdn=userdn))
            msg = "Status of user bind {username} with {userdn} : {is_bound}"
            try:
                conn = self.get_connection(userdn, password)
            except ldap3.core.exceptions.LDAPBindError as exc:
                is_bound = False
                msg += '\n{exc_type}: {exc_msg}'.format(
                    exc_type=exc.__class__.__name__,
                    exc_msg=exc.args[0] if exc.args else '')
            else:
                is_bound = conn.bind()
            msg = msg.format(username=username,
                             userdn=userdn,
                             is_bound=is_bound)
            self.log.debug(msg)
            if is_bound:
                break

        if not is_bound:
            msg = "Invalid password for user '{username}'"
            self.log.warn(msg.format(username=username))
            return None

        if self.search_filter:
            search_filter = self.search_filter.format(
                userattr=self.user_attribute,
                username=username,
            )
            conn.search(search_base=self.user_search_base,
                        search_scope=ldap3.SUBTREE,
                        search_filter=search_filter,
                        attributes=self.attributes)
            n_users = len(conn.response)
            if n_users == 0:
                msg = "User with '{userattr}={username}' not found in directory"
                self.log.warn(
                    msg.format(userattr=self.user_attribute,
                               username=username))
                return None
            if n_users > 1:
                msg = ("Duplicate users found! "
                       "{n_users} users found with '{userattr}={username}'")
                self.log.warn(
                    msg.format(userattr=self.user_attribute,
                               username=username,
                               n_users=n_users))
                return None

        if self.allowed_groups:
            self.log.debug('username:%s Using dn %s', username, userdn)
            found = False
            for group in self.allowed_groups:
                group_filter = ('(|'
                                '(member={userdn})'
                                '(uniqueMember={userdn})'
                                '(memberUid={uid})'
                                ')')
                group_filter = group_filter.format(userdn=userdn, uid=username)
                group_attributes = ['member', 'uniqueMember', 'memberUid']
                found = conn.search(group,
                                    search_scope=ldap3.BASE,
                                    search_filter=group_filter,
                                    attributes=group_attributes)
                if found:
                    break
            if not found:
                # If we reach here, then none of the groups matched
                msg = 'username:{username} User not in any of the allowed groups'
                self.log.warn(msg.format(username=username))
                return None

        if self.use_lookup_dn_username:
            return username
        else:
            return data['username']
Example #59
0
 def replace(self, text):
     for parrent, repl in self.parrents:
         text, count = re.subn(pattern=parrent, repl=repl, string=text)
     return text
Example #60
0
def find_and_remove_repl_patterns(astr):
    names = find_repl_patterns(astr)
    astr = re.subn(named_re, '', astr)[0]
    return astr, names