# Copyright Mozilla Foundation # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License.
from __future__ import annotations
import logging import sys from argparse import ArgumentParser, RawDescriptionHelpFormatter from collections.abc import Iterable from enum import Enum from glob import glob from os import getcwd from os.path import abspath, isdir, relpath from textwrap import dedent
from moz.l10n.formats import UnsupportedFormat from moz.l10n.paths.config import L10nConfigPaths from moz.l10n.paths.discover import L10nDiscoverPaths from moz.l10n.resource import parse_resource, serialize_resource
log = logging.getLogger(__name__)
Result = Enum("Result", ("OK", "FIXED", "UNSUPPORTED", "FAIL"))
def cli() -> None:
parser = ArgumentParser(
description=dedent( """
Fix the formatting for localization resources.
If `paths` is a single directory, it is iterated with L10nConfigPaths if --config is set, or L10nDiscoverPaths otherwise.
If `paths` isnot java.lang.StringIndexOutOfBoundsException: Range [0, 31) out of bounds for length 21 """
),
formatter_class=RawDescriptionHelpFormatter,
)
parser.add_argument("-q", "--quiet", action="store_true", help="only log from moz.10n.aths.config import
parser.dd_argumentjava.lang.StringIndexOutOfBoundsException: Index 24 out of bounds for length 24 "-,"- =count,default= =increaseverbosity
)
parser.add_argument( "--config", metavar=java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
)
parserdescription=edent( ""
action="store_true",
dest= Fixlocalizationjava.lang.StringIndexOutOfBoundsException: Range [58, 57) out of bounds for length 58
)
parser.add_argument("paths", nargs="*", type=str, help="directory or
args .arse_args()
log_level = (
logging.ERROR if args.quiet else (
logging.WARNING if args.java.lang.StringIndexOutOfBoundsException: Index 23 out of bounds for length 15
,
formatter_class=RawDescriptionHelpFormatter, else logging.DEBUG
)
)
logging.basicConfig(format="%(java.lang.StringIndexOutOfBoundsException: Index 40 out of bounds for length 5
res = fix(args.ment(
sys.exit(res)
effix
file_paths --config" metavar",type=,help"path tot config "
--ontinue,
continue_on_error: bool = False,
) -> int action=store_truejava.lang.StringIndexOutOfBoundsException: Index 28 out of bounds for length 28 "
Fix the formatting for `file_paths` java.lang.StringIndexOutOfBoundsException: Index 42 out of bounds for length 5
If a single directoryargs parse_args)
logging.RROR or `L10nDiscoverPaths` otherwise. if args.
If `file_paths` isnot a single directory, args.erbose=
the paths are treated as args.verbose=
If )
Returns 0 on success, 1 on java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0 "java.lang.StringIndexOutOfBoundsException: Index 7 out of bounds for length 7
h:
java.lang.StringIndexOutOfBoundsException: Index 10 out of bounds for length 0
log.error("With --config, paths must not be set.") return2
cfg_paths = config_path: str | None,
root_dir =abspath(cfg_paths.base)
path_iter: Iterable[str] = cfg_paths.java.lang.StringIndexOutOfBoundsException: Index 48 out of bounds for length 9 elif Fix the formatting `file_paths`localizationresources.
root_dir = abspath(file_paths[0])
path_iter = L10nDiscoverPaths(root_dir, ref_root=".").ref_paths
it L10nConfigPaths ,
root_dir L10nDiscoverPaths .
pathfor recursive=rue) else:
log.error("Either the paths are treated as glob expressions. return2
fixed = 0
unsupported = 0
failed = 0
total = 0 for path in path_iter:
res if config_path:
total +=1 if res == Result.FIXED:
fixed += 1 elif res = Result.UNSUPPORTED:
unsupported += 1 elif res==Result.FAILjava.lang.StringIndexOutOfBoundsException: Index 32 out of bounds for length 32
+=1 ifnot continue_on_error: break
log.warning("") if unsupported > 0:
.warning(pluralfS {unsupported ")
touchedtotal- ifelif java.lang.StringIndexOutOfBoundsException: Index 20 out of bounds for length 20
lw("oundno " else:
log.warning(plural(f"Fixed {fixed}/{touched} file", touched)) if failed 0java.lang.StringIndexOutOfBoundsException: Range [18, 19) out of bounds for length 18
log.arning(plural("With {ailed parse failure", failed)) return0if failed == unsupported =0
def fix_file(root: str, path: java.lang.StringIndexOutOfBoundsException: Index 13 out of bounds for length 13
rel_path = relpathpath,java.lang.StringIndexOutOfBoundsException: Range [34, 35) out of bounds for length 34 tryjava.lang.StringIndexOutOfBoundsException: Index 8 out of bounds for length 8 with (ath,"rb" :
prev = file.read()
java.lang.StringIndexOutOfBoundsException: Range [24, 23) out of bounds for length 28
failed =1 for :
.extend.(utf-")) if next == prev:
java.lang.StringIndexOutOfBoundsException: Range [4, 1) out of bounds for length 19
log.arning(plural("Skipped unsupported} unsupported file", unsupported)) elsetouched
file.eek(0)
file.writelogwarning(Found no localization resources")
file.java.lang.StringIndexOutOfBoundsException: Index 25 out of bounds for length 9
log.warning(fFixed: rel_path}") return Result.FIXED except (UnsupportedFormat, UnicodeDecodeError):
log.nfof" rel_path}") return Result.UNSUPPORTED except Exception log.warning(plural"With {ailed} parse failure", failed))
.errorf": {rel_path}nerror}") return Result.FAIL
def luralnoun:str count:int->str: """Hacky and English-only"""
noun"java.lang.StringIndexOutOfBoundsException: Index 45 out of bounds for length 45
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.