当前位置: 首页>>代码示例>>Python>>正文


Python formatter.color_format函数代码示例

本文整理汇总了Python中pywikibot.tools.formatter.color_format函数的典型用法代码示例。如果您正苦于以下问题:Python color_format函数的具体用法?Python color_format怎么用?Python color_format使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了color_format函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: cherry_pick

def cherry_pick(oldtext, newtext, n=0, by_letter=False):
    """Propose a list of changes for approval.

    Text with approved changes will be returned.
    n: int, line of context as defined in difflib.get_grouped_opcodes().
    by_letter: if text_a and text_b are single lines, comparison can be done

    """
    FORMAT = "{2}{lightpurple}{0:{1}^50}{default}{2}"

    patch = PatchManager(oldtext, newtext, n=n, by_letter=by_letter)
    pywikibot.output(color_format(FORMAT, "  ALL CHANGES  ", "*", "\n"))

    for hunk in patch.hunks:
        pywikibot.output(hunk.diff_text)
    pywikibot.output(color_format(FORMAT, "  REVIEW CHANGES  ", "*", "\n"))

    text_list = patch.apply()
    pywikibot.output(color_format(FORMAT, "  APPROVED CHANGES  ", "*", "\n"))

    if any(hunk.reviewed == hunk.APPR for hunk in patch.hunks):
        for hunk in patch.hunks:
            if hunk.reviewed == hunk.APPR:
                pywikibot.output(hunk.diff_text)
    else:
        pywikibot.output(color_format(FORMAT, "None.", "", ""))

    text = "".join(text_list)

    return text
开发者ID:PersianWikipedia,项目名称:pywikibot-core,代码行数:30,代码来源:diff.py

示例2: save

 def save(self, text, page, comment, minorEdit=True, botflag=True):
     """Save the text."""
     if text != page.text:
         # Show the title of the page we're working on.
         # Highlight the title in purple.
         pywikibot.output(color_format(
             '\n\n>>> {lightpurple}{0}{default} <<<', page.title()))
         # show what was changed
         pywikibot.showDiff(page.get(), text)
         pywikibot.output(u'Comment: %s' % comment)
         if not self.dry:
             if pywikibot.input_yn(
                     u'Do you want to accept these changes?',
                     default=False, automatic_quit=False):
                 page.text = text
                 try:
                     # Save the page
                     page.save(summary=comment, minorEdit=minorEdit,
                               botflag=botflag)
                 except pywikibot.LockedPage:
                     pywikibot.output(u"Page %s is locked; skipping."
                                      % page.title(asLink=True))
                 except pywikibot.EditConflict:
                     pywikibot.output(
                         u'Skipping %s because of edit conflict'
                         % (page.title()))
                 except pywikibot.SpamfilterError as error:
                     pywikibot.output(
                         u'Cannot change %s because of spam blacklist entry '
                         u'%s' % (page.title(), error.url))
                 else:
                     return True
开发者ID:KaiCode2,项目名称:pywikibot-core,代码行数:32,代码来源:blockreview.py

示例3: __init__

    def __init__(self, *arg):
        pywikibot.output(color_format("{lightgreen}* Initialization of bot{default}"))

        pywikibot.botirc.IRCBot.__init__(self, *arg)

        # init environment with minimal changes (try to do as less as possible)
        # - Lua -
        pywikibot.output("** Redirecting Lua print in order to catch it")
        lua.execute("__print = print")
        lua.execute("print = python.globals().pywikibot.output")
        # It may be useful in debugging to install the 'print' builtin
        # as the 'print' function in lua. To do this:
        # lua.execute('print = python.builtins().print')

        # init constants
        templ = pywikibot.Page(self.site, bot_config["ConfCSSshell"])
        cron = pywikibot.Page(self.site, bot_config["ConfCSScrontab"])

        self.templ = templ.title()
        self.cron = cron.title()
        self.refs = {self.templ: templ, self.cron: cron}
        pywikibot.output("** Pre-loading all relevant page contents")
        for item in self.refs:
            # security; first check if page is protected, reject any data if not
            if os.path.splitext(self.refs[item].title().lower())[1] not in [".css", ".js"]:
                raise ValueError(
                    "%s config %s = %s is not a secure page; "
                    "it should be a css or js userpage which are "
                    "automatically semi-protected." % (self.__class__.__name__, item, self.refs[item])
                )
            self.refs[item].get(force=True)  # load all page contents

        # init background timer
        pywikibot.output("** Starting crontab background timer thread")
        self.on_timer()
开发者ID:KaiCode2,项目名称:pywikibot-core,代码行数:35,代码来源:script_wui.py

示例4: test_marker

 def test_marker(self):
     r"""Test that the \03 marker is only allowed in front of colors."""
     self.assertEqual(formatter.color_format("{0}\03{black}", 42), "42\03{black}")
     # literal before a normal field
     self.assertRaisesRegex(ValueError, r".*\\03", formatter.color_format, "\03{0}{black}", 42)
     # literal before a color field
     self.assertRaisesRegex(ValueError, r".*\\03", formatter.color_format, "{0}\03before{black}", 42)
开发者ID:metakgp,项目名称:batman,代码行数:7,代码来源:tools_formatter_tests.py

示例5: main

def main():
    """Main function."""
    fg_colors = [col for col in colors if col != 'default']
    bg_colors = fg_colors[:]
    n_fg_colors = len(fg_colors)
    fg_colors.insert(3 * int(n_fg_colors / 4), 'default')
    fg_colors.insert(2 * int(n_fg_colors / 4), 'default')
    fg_colors.insert(int(n_fg_colors / 4), 'default')
    fg_colors.insert(0, 'default')

    # Max len of color names for padding.
    max_len_fg_colors = len(max(fg_colors, key=len))
    max_len_bc_color = len(max(bg_colors, key=len))

    for bg_col in bg_colors:
        # Three lines per each backgoung color.
        for fg_col_group in itergroup(fg_colors, n_fg_colors / 4 + 1):
            line = ''
            for fg_col in fg_col_group:
                line += ' '
                line += color_format('{color}{0}{default}',
                                     fg_col.ljust(max_len_fg_colors),
                                     color='%s;%s' % (fg_col, bg_col))

            line = '{0} {1}'.format(bg_col.ljust(max_len_bc_color), line)
            pywikibot.output(line)

        pywikibot.output('')
开发者ID:PersianWikipedia,项目名称:pywikibot-core,代码行数:28,代码来源:colors.py

示例6: useHashGenerator

 def useHashGenerator(self):
     """Use hash generator."""
     # https://toolserver.org/~multichill/nowcommons.php?language=it&page=2&filter=
     lang = self.site.lang
     num_page = 0
     word_to_skip_translated = i18n.translate(self.site, word_to_skip)
     images_processed = list()
     while 1:
         url = ('https://toolserver.org/~multichill/nowcommons.php?'
                'language=%s&page=%s&filter=') % (lang, num_page)
         HTML_text = self.site.getUrl(url, no_hostname=True)
         reg = r'<[Aa] href="(?P<urllocal>.*?)">(?P<imagelocal>.*?)</[Aa]> +?</td><td>\n\s*?'
         reg += r'<[Aa] href="(?P<urlcommons>http[s]?://commons.wikimedia.org/.*?)" \
                >Image:(?P<imagecommons>.*?)</[Aa]> +?</td><td>'
         regex = re.compile(reg, re.UNICODE)
         found_something = False
         change_page = True
         for x in regex.finditer(HTML_text):
             found_something = True
             image_local = x.group('imagelocal')
             image_commons = x.group('imagecommons')
             if image_local in images_processed:
                 continue
             change_page = False
             images_processed.append(image_local)
             # Skip images that have something in the title (useful for it.wiki)
             image_to_skip = False
             for word in word_to_skip_translated:
                 if word.lower() in image_local.lower():
                     image_to_skip = True
             if image_to_skip:
                 continue
             url_local = x.group('urllocal')
             url_commons = x.group('urlcommons')
             pywikibot.output(color_format(
                 '\n\n>>> {lightpurple}{0}{default} <<<',
                 image_local))
             pywikibot.output(u'Local: %s\nCommons: %s\n'
                              % (url_local, url_commons))
             webbrowser.open(url_local, 0, 1)
             webbrowser.open(url_commons, 0, 1)
             if image_local.split('Image:')[1] == image_commons:
                 choice = pywikibot.input_yn(
                     u'The local and the commons images have the same name, '
                     'continue?', default=False, automatic_quit=False)
             else:
                 choice = pywikibot.input_yn(
                     u'Are the two images equal?',
                     default=False, automatic_quit=False)
             if choice:
                 yield [image_local, image_commons]
             else:
                 continue
         # The page is dinamically updated, so we may don't need to change it
         if change_page:
             num_page += 1
         # If no image found means that there aren't anymore, break.
         if not found_something:
             break
开发者ID:AbdealiJK,项目名称:pywikibot-core,代码行数:59,代码来源:nowcommons.py

示例7: main

def main(*args):
    """
    Process command line arguments and invoke bot.

    If args is an empty list, sys.argv is used.

    @param args: command line arguments
    @type args: list of unicode
    """
    featured = False
    gen = None

    # Process global args and prepare generator args parser
    local_args = pywikibot.handle_args(args)
    genFactory = pagegenerators.GeneratorFactory()

    for arg in local_args:
        if arg == '-featured':
            featured = True
        elif genFactory.handleArg(arg):
            pass

    mysite = pywikibot.Site()
    if mysite.sitename == 'wikipedia:nl':
        pywikibot.output(color_format(
            '{lightred}There is consensus on the Dutch Wikipedia that '
            'bots should not be used to fix redirects.{default}'))
        return

    if featured:
        repo = mysite.data_repository()
        if repo:
            dp = pywikibot.ItemPage(repo, featured_articles)
            try:
                ref = pywikibot.Category(mysite, dp.getSitelink(mysite))
            except pywikibot.NoPage:
                pass
            else:
                gen = ref.articles(namespaces=0, content=True)
        if not gen:
            suggest_help(
                unknown_parameters=['-featured'],
                additional_text='Option is not available for this site.')
            return False
    else:
        gen = genFactory.getCombinedGenerator()
        if gen:
            gen = mysite.preloadpages(gen)
    if gen:
        bot = FixingRedirectBot(generator=gen)
        bot.run()
        return True
    else:
        suggest_help(missing_generator=True)
        return False
开发者ID:AbdealiJK,项目名称:pywikibot-core,代码行数:55,代码来源:fixing_redirects.py

示例8: _flush

def _flush(stop=True):
    """
    Drop this process from the throttle log, after pending threads finish.

    Wait for the page-putter to flush its queue. Also drop this process from the
    throttle log. Called automatically at Python exit.
    """
    _logger = "wiki"

    debug('_flush() called', _logger)

    def remaining():
        remainingPages = page_put_queue.qsize()
        if stop:
            # -1 because we added a None element to stop the queue
            remainingPages -= 1

        remainingSeconds = datetime.timedelta(
            seconds=(remainingPages * config.put_throttle))
        return (remainingPages, remainingSeconds)

    if stop:
        # None task element leaves async_manager
        page_put_queue.put((None, [], {}))

    num, sec = remaining()
    if num > 0 and sec.total_seconds() > config.noisysleep:
        output(color_format(
            '{lightblue}Waiting for {num} pages to be put. '
            'Estimated time remaining: {sec}{default}', num=num, sec=sec))

    while _putthread.isAlive() and page_put_queue.qsize() > 0:
        try:
            _putthread.join(1)
        except KeyboardInterrupt:
            if input_yn('There are {0} pages remaining in the queue. '
                        'Estimated time remaining: {1}\nReally exit?'
                        ''.format(*remaining()),
                        default=False, automatic_quit=False):
                return

    # only need one drop() call because all throttles use the same global pid
    try:
        list(_sites.values())[0].throttle.drop()
        log(u"Dropped throttle(s).")
    except IndexError:
        pass
开发者ID:Darkdadaah,项目名称:pywikibot-core,代码行数:47,代码来源:__init__.py

示例9: stopme

def stopme():
    """
    Drop this process from the throttle log, after pending threads finish.

    Can be called manually if desired, but if not, will be called automatically
    at Python exit.
    """
    global stopped
    _logger = "wiki"

    if not stopped:
        debug(u"stopme() called", _logger)

        def remaining():
            remainingPages = page_put_queue.qsize() - 1
            # -1 because we added a None element to stop the queue

            remainingSeconds = datetime.timedelta(
                seconds=(remainingPages * config.put_throttle))
            return (remainingPages, remainingSeconds)

        page_put_queue.put((None, [], {}))
        stopped = True

        if page_put_queue.qsize() > 1:
            num, sec = remaining()
            output(color_format(
                '{lightblue}Waiting for {num} pages to be put. '
                'Estimated time remaining: {sec}{default}', num=num, sec=sec))

        while(_putthread.isAlive()):
            try:
                _putthread.join(1)
            except KeyboardInterrupt:
                if input_yn('There are %i pages remaining in the queue. '
                            'Estimated time remaining: %s\nReally exit?'
                            % remaining(), default=False, automatic_quit=False):
                    return

    # only need one drop() call because all throttles use the same global pid
    try:
        list(_sites.values())[0].throttle.drop()
        log(u"Dropped throttle(s).")
    except IndexError:
        pass
开发者ID:amperser,项目名称:pywikibot-core,代码行数:45,代码来源:__init__.py

示例10: _ColorCodeWordScreen

 def _ColorCodeWordScreen(self, word):
     res = ''
     lastIsCyr = word[0] in self.localLtr
     if lastIsCyr:
         res += self.colorFormatLocalColor
     else:
         res += self.colorFormatLatinColor
     for l in word:
         if l in self.localLtr:
             if not lastIsCyr:
                 res += self.colorFormatLocalColor
                 lastIsCyr = True
         elif l in self.latLtr:
             if lastIsCyr:
                 res += self.colorFormatLatinColor
                 lastIsCyr = False
         res += l
     return formatter.color_format(res + self.colorFormatSuffix)
开发者ID:Tillsa,项目名称:pywikibot_test_wikidata,代码行数:18,代码来源:casechecker.py

示例11: __init__

    def __init__(self, *arg):
        """Constructor."""
        pywikibot.output(color_format(
            '{lightgreen}* Initialization of bot{default}'))

        pywikibot.botirc.IRCBot.__init__(self, *arg)

        # init environment with minimal changes (try to do as less as possible)
        # - Lua -
        pywikibot.output(u'** Redirecting Lua print in order to catch it')
        lua.execute('__print = print')
        lua.execute('print = python.globals().pywikibot.output')
        # It may be useful in debugging to install the 'print' builtin
        # as the 'print' function in lua. To do this:
        # lua.execute('print = python.builtins().print')

        # init constants
        templ = pywikibot.Page(self.site, bot_config['ConfCSSshell'])
        cron = pywikibot.Page(self.site, bot_config['ConfCSScrontab'])

        self.templ = templ.title()
        self.cron = cron.title()
        self.refs = {self.templ: templ,
                     self.cron: cron,
                     }
        pywikibot.output(u'** Pre-loading all relevant page contents')
        for item in self.refs:
            # First check if page is protected, reject any data if not
            parts = self.refs[item].title().lower().rsplit('.')
            if len(parts) == 1 or parts[1] not in ['.css', '.js']:
                raise ValueError('%s config %s = %s is not a secure page; '
                                 'it should be a css or js userpage which are '
                                 'automatically semi-protected.'
                                 % (self.__class__.__name__, item,
                                    self.refs[item]))
            try:
                self.refs[item].get(force=True)   # load all page contents
            except pywikibot.NoPage:
                pywikibot.error("The configuation page %s doesn't exists"
                                % self.refs[item].title(asLink=True))
                raise
        # init background timer
        pywikibot.output(u'** Starting crontab background timer thread')
        self.on_timer()
开发者ID:magul,项目名称:pywikibot-core,代码行数:44,代码来源:script_wui.py

示例12: featuredArticles

    def featuredArticles(self, site, task, cache):
        articles = []
        info = globals()[task + '_name']
        if task == 'lists':
            code = site.code
        else:
            code = 'wikidata'
        try:
            method = info[code][0]
        except KeyError:
            pywikibot.error(
                u'language %s doesn\'t has %s category source.'
                % (code, task))
            return
        name = info[code][1]
        # hide #-sorted items on en-wiki
        try:
            hide = info[code][2]
        except IndexError:
            hide = None
        for p in method(site, name, hide):
            if p.namespace() == 0:  # Article
                articles.append(p)
            # Article talk (like in English)
            elif p.namespace() == 1 and site.code != 'el':
                articles.append(pywikibot.Page(p.site,
                                p.title(withNamespace=False)))
        pywikibot.output(color_format(
            '{lightred}** {0} has {1} {2} articles{default}',
            site, len(articles), task))
        while articles:
            p = articles.pop(0)
            if p.title() < self.getOption('afterpage'):
                continue

            if u"/" in p.title() and p.namespace() != 0:
                pywikibot.output(u"%s is a subpage" % p.title())
                continue

            if p.title() in cache:
                pywikibot.output(u"(cached) %s -> %s" % (p.title(),
                                                         cache[p.title()]))
                continue
            yield p
开发者ID:magul,项目名称:pywikibot-core,代码行数:44,代码来源:featured.py

示例13: treat_page

    def treat_page(self):
        """Do the magic."""
        # set origin
        origin = self.current_page.title()
        site = self.current_page.site

        # create redirect title
        if not self.getOption('reversed'):
            redir = pywikibot.Page(site, origin.replace('–', '-')
                                               .replace('—', '-'))
        else:
            redir = pywikibot.Page(site, origin.replace('-', '–'))

        # skip unchanged
        if redir.title() == origin:
            pywikibot.output('No need to process %s, skipping…'
                             % redir.title())
            # suggest -reversed parameter
            if '-' in origin and not self.getOption('reversed'):
                pywikibot.output('Consider using -reversed parameter '
                                 'for this particular page')
        else:
            # skip existing
            if redir.exists():
                pywikibot.output('%s already exists, skipping…'
                                 % redir.title())
            else:
                # confirm and save redirect
                if self.user_confirm(
                    color_format(
                        'Redirect from {lightblue}{0}{default} doesn\'t exist '
                        'yet.\nDo you want to create it?',
                        redir.title())):
                    # If summary option is None, it takes the default
                    # i18n summary from i18n subdirectory with summary key.
                    if self.getOption('summary'):
                        summary = self.getOption('summary')
                    else:
                        summary = i18n.twtranslate(site,
                                                   'ndashredir-create',
                                                   {'title': origin})
                    redir.set_redirect_target(self.current_page, create=True,
                                              summary=summary)
开发者ID:magul,项目名称:pywikibot-core,代码行数:43,代码来源:ndashredir.py

示例14: revert

 def revert(self, item):
     """Revert a single item."""
     page = pywikibot.Page(self.site, item["title"])
     history = list(page.revisions(total=2))
     if len(history) > 1:
         rev = history[1]
     else:
         return False
     comment = i18n.twtranslate(
         self.site, "revertbot-revert", {"revid": rev.revid, "author": rev.user, "timestamp": rev.timestamp}
     )
     if self.comment:
         comment += ": " + self.comment
     pywikibot.output(
         color_format(
             "\n\n>>> {lightpurple}{0}{default} <<<", page.title(asLink=True, forceInterwiki=True, textlink=True)
         )
     )
     if not self.rollback:
         old = page.text
         page.text = rev.text
         pywikibot.showDiff(old, page.text)
         page.save(comment)
         return comment
     try:
         pywikibot.data.api.Request(
             self.site,
             parameters={
                 "action": "rollback",
                 "title": page,
                 "user": self.user,
                 "token": rev.rollbacktoken,
                 "markbot": True,
             },
         ).submit()
     except pywikibot.data.api.APIError as e:
         if e.code == "badtoken":
             pywikibot.error("There was an API token error rollbacking the edit")
         else:
             pywikibot.exception()
         return False
     return "The edit(s) made in %s by %s was rollbacked" % (page.title(), self.user)
开发者ID:h4ck3rm1k3,项目名称:pywikibot-core,代码行数:42,代码来源:revertbot.py

示例15: showStatus

def showStatus(n=0):
    """Output colorized status."""
    staColor = {
        0: 'lightpurple',
        1: 'lightaqua',
        2: 'lightgreen',
        3: 'lightyellow',
        4: 'lightred',
        5: 'lightblue'
    }
    staMsg = {
        0: 'MSG',
        1: 'NoAct',
        2: 'Match',
        3: 'Skip',
        4: 'Warning',
        5: 'Done',
    }
    pywikibot.output(color_format('{color}[{0:5}]{default} ',
                                  staMsg[n], color=staColor[n]), newline=False)
开发者ID:PersianWikipedia,项目名称:pywikibot-core,代码行数:20,代码来源:welcome.py


注:本文中的pywikibot.tools.formatter.color_format函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。