jenkins-bot merged this change.
[bugfix] Improve option parsing
- pagegenerators -imagelinks option was never ported to core; it was
renamed to -imagesused, to avoid confusion with -filelinks. This
means that -images option forwarding to pagegenerators never worked
and it can be removed.
- simplify protections[option] assignment
- simplify message_properties and generator_type assignment
- always call check_protection_level even for an empty string
which is a valid protection level
detached from Ia4f257710
Change-Id: I9cdb970beaa6cd40ea03ba53ba21220714b2f717
---
M scripts/protect.py
1 file changed, 32 insertions(+), 55 deletions(-)
diff --git a/scripts/protect.py b/scripts/protect.py
index 126f536..be694ee 100755
--- a/scripts/protect.py
+++ b/scripts/protect.py
@@ -62,8 +62,6 @@
import pywikibot
from pywikibot import i18n, pagegenerators
from pywikibot.bot import SingleSiteBot
-from pywikibot.exceptions import ArgumentDeprecationWarning
-from pywikibot.tools import issue_deprecation_warning
# This is required for the text that is shown when you run this script
# with the parameter -help.
@@ -169,61 +167,41 @@
# read command line parameters
local_args = pywikibot.handle_args(args)
- gen_factory = pagegenerators.GeneratorFactory()
site = pywikibot.Site()
generator_type = None
- protection_levels = set(site.protection_levels())
- protection_types = site.protection_types()
+ protection_levels = site.protection_levels()
if '' in protection_levels:
protection_levels.add('all')
- for arg in local_args:
- if arg == '-always':
- options['always'] = True
- elif arg.startswith('-summary'):
- if len(arg) == len('-summary'):
- # fill dummy value to prevent automatic generation
- options['summary'] = None
- else:
- options['summary'] = arg[len('-summary:'):]
- elif arg.startswith('-expiry'):
- if len(arg) == len('-expiry'):
- options['expiry'] = pywikibot.input(
- 'Enter a protection expiry:')
- else:
- options['expiry'] = arg[len('-expiry:'):]
- elif arg.startswith('-images'):
- issue_deprecation_warning('-image', '-imagelinks', 2,
- ArgumentDeprecationWarning,
- since='20140213')
- local_args.append('-imagelinks' + arg[7:])
- elif arg.startswith('-unprotect'):
- default_level = 'all'
- elif arg.startswith('-default'):
- if len(arg) == len('-default'):
- default_level = 'sysop'
- else:
- default_level = arg[len('-default:'):]
- else:
- is_p_type = False
- if arg.startswith('-'):
- delimiter = arg.find(':')
- if delimiter > 0:
- p_type_arg = arg[1:delimiter]
- level = arg[delimiter + 1:]
- if p_type_arg in protection_types:
- protections[p_type_arg] = level
- is_p_type = True
- if not is_p_type:
- if not gen_factory.handleArg(arg):
- raise ValueError('Unknown parameter "{0}"'.format(arg))
- found = arg.find(':')
- if found:
- message_properties.update({'cat': arg[found + 1:],
- 'page': arg[found + 1:]})
- if 'summary' not in options:
- generator_type = arg[1:found] if found > 0 else arg[1:]
+ protection_types = site.protection_types()
+ gen_factory = pagegenerators.GeneratorFactory()
+ for arg in local_args:
+ option, sep, value = arg.partition(':')
+ if not option.startswith('-'):
+ continue
+
+ option = option[1:]
+ if option == 'always':
+ options[option] = True
+ elif option == 'summary':
+ options[option] = value or None
+ elif option == 'expiry':
+ options[option] = value or pywikibot.input(
+ 'Enter a protection expiry:')
+ elif option == 'unprotect':
+ default_level = 'all'
+ elif option == 'default':
+ default_level = value if sep else 'sysop'
+ elif option in protection_types and value:
+ protections[option] = value
+ else:
+ if not gen_factory.handleArg(arg):
+ raise ValueError('Unknown parameter "{0}"'.format(arg))
+ if value:
+ message_properties.update({'cat': value, 'page': value})
+ if 'summary' not in options:
+ generator_type = option
if generator_type in default_summaries:
message_type = default_summaries[generator_type]
@@ -241,10 +219,9 @@
# We are just protecting pages, so we have no need of using a preloading
# page generator to actually get the text of those pages.
if generator:
- if default_level:
- default_level = check_protection_level('Default level',
- default_level,
- protection_levels)
+ default_level = check_protection_level('Default level',
+ default_level,
+ protection_levels)
# set the default value for all
# None (not the string 'none') will be ignored by Site.protect()
combined_protections = {p_type: default_level
To view, visit change 508087. To unsubscribe, or for help writing mail filters, visit settings.