Skip to content

Commit 9f38221

Browse files
authored
Simplify Python code with: ruff --select=Q,SIM --fix . (#188)
* Simplify Python code with: ruff --select=Q,SIM --fix . * Fix a long line * Fix a long line
1 parent f0ac743 commit 9f38221

File tree

16 files changed

+87
-155
lines changed

16 files changed

+87
-155
lines changed

pylib/gyp/MSVSNew.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -285,19 +285,17 @@ def Write(self, writer=gyp.common.WriteOnDiff):
285285
"\tEndProjectSection\r\n"
286286
)
287287

288-
if isinstance(e, MSVSFolder):
289-
if e.items:
290-
f.write("\tProjectSection(SolutionItems) = preProject\r\n")
291-
for i in e.items:
292-
f.write(f"\t\t{i} = {i}\r\n")
293-
f.write("\tEndProjectSection\r\n")
294-
295-
if isinstance(e, MSVSProject):
296-
if e.dependencies:
297-
f.write("\tProjectSection(ProjectDependencies) = postProject\r\n")
298-
for d in e.dependencies:
299-
f.write(f"\t\t{d.get_guid()} = {d.get_guid()}\r\n")
300-
f.write("\tEndProjectSection\r\n")
288+
if isinstance(e, MSVSFolder) and e.items:
289+
f.write("\tProjectSection(SolutionItems) = preProject\r\n")
290+
for i in e.items:
291+
f.write(f"\t\t{i} = {i}\r\n")
292+
f.write("\tEndProjectSection\r\n")
293+
294+
if isinstance(e, MSVSProject) and e.dependencies:
295+
f.write("\tProjectSection(ProjectDependencies) = postProject\r\n")
296+
for d in e.dependencies:
297+
f.write(f"\t\t{d.get_guid()} = {d.get_guid()}\r\n")
298+
f.write("\tEndProjectSection\r\n")
301299

302300
f.write("EndProject\r\n")
303301

pylib/gyp/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ def gyp_main(args):
624624
if options.generator_flags:
625625
gen_flags += options.generator_flags
626626
generator_flags = NameValueListToDict(gen_flags)
627-
if DEBUG_GENERAL in gyp.debug.keys():
627+
if DEBUG_GENERAL in gyp.debug:
628628
DebugOutput(DEBUG_GENERAL, "generator_flags: %s", generator_flags)
629629

630630
# Generate all requested formats (use a set in case we got one format request

pylib/gyp/common.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -144,20 +144,16 @@ def RelativePath(path, relative_to, follow_path_symlink=True):
144144
# symlink, this option has no effect.
145145

146146
# Convert to normalized (and therefore absolute paths).
147-
if follow_path_symlink:
148-
path = os.path.realpath(path)
149-
else:
150-
path = os.path.abspath(path)
147+
path = os.path.realpath(path) if follow_path_symlink else os.path.abspath(path)
151148
relative_to = os.path.realpath(relative_to)
152149

153150
# On Windows, we can't create a relative path to a different drive, so just
154151
# use the absolute path.
155-
if sys.platform == "win32":
156-
if (
157-
os.path.splitdrive(path)[0].lower()
158-
!= os.path.splitdrive(relative_to)[0].lower()
159-
):
160-
return path
152+
if sys.platform == "win32" and (
153+
os.path.splitdrive(path)[0].lower()
154+
!= os.path.splitdrive(relative_to)[0].lower()
155+
):
156+
return path
161157

162158
# Split the paths into components.
163159
path_split = path.split(os.path.sep)
@@ -277,10 +273,7 @@ def EncodePOSIXShellArgument(argument):
277273
if not isinstance(argument, str):
278274
argument = str(argument)
279275

280-
if _quote.search(argument):
281-
quote = '"'
282-
else:
283-
quote = ""
276+
quote = '"' if _quote.search(argument) else ""
284277

285278
encoded = quote + re.sub(_escape, r"\\\1", argument) + quote
286279

pylib/gyp/generator/analyzer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ def _GetUnqualifiedToTargetMapping(all_targets, to_find):
433433
if not to_find:
434434
return {}, []
435435
to_find = set(to_find)
436-
for target_name in all_targets.keys():
436+
for target_name in all_targets:
437437
extracted = gyp.common.ParseQualifiedTarget(target_name)
438438
if len(extracted) > 1 and extracted[1] in to_find:
439439
to_find.remove(extracted[1])

pylib/gyp/generator/cmake.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -929,10 +929,7 @@ def WriteTarget(
929929
product_prefix = spec.get("product_prefix", default_product_prefix)
930930
product_name = spec.get("product_name", default_product_name)
931931
product_ext = spec.get("product_extension")
932-
if product_ext:
933-
product_ext = "." + product_ext
934-
else:
935-
product_ext = default_product_ext
932+
product_ext = "." + product_ext if product_ext else default_product_ext
936933

937934
SetTargetProperty(output, cmake_target_name, "PREFIX", product_prefix)
938935
SetTargetProperty(

pylib/gyp/generator/compile_commands_json.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535

3636
def IsMac(params):
37-
return "mac" == gyp.common.GetFlavor(params)
37+
return gyp.common.GetFlavor(params) == "mac"
3838

3939

4040
def CalculateVariables(default_variables, params):

pylib/gyp/generator/eclipse.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,7 @@ def GetAllDefines(target_list, target_dicts, data, config_name, params, compiler
248248
continue
249249
cpp_line_parts = cpp_line.split(" ", 2)
250250
key = cpp_line_parts[1]
251-
if len(cpp_line_parts) >= 3:
252-
val = cpp_line_parts[2]
253-
else:
254-
val = "1"
251+
val = cpp_line_parts[2] if len(cpp_line_parts) >= 3 else "1"
255252
all_defines[key] = val
256253

257254
return all_defines

pylib/gyp/generator/make.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -681,10 +681,7 @@ def WriteRootHeaderSuffixRules(writer):
681681

682682
def Compilable(filename):
683683
"""Return true if the file is compilable (should be in OBJS)."""
684-
for res in (filename.endswith(e) for e in COMPILABLE_EXTENSIONS):
685-
if res:
686-
return True
687-
return False
684+
return any(res for res in (filename.endswith(e) for e in COMPILABLE_EXTENSIONS))
688685

689686

690687
def Linkable(filename):
@@ -778,7 +775,7 @@ def __init__(self, generator_flags, flavor):
778775
self.suffix_rules_objdir2 = {}
779776

780777
# Generate suffix rules for all compilable extensions.
781-
for ext in COMPILABLE_EXTENSIONS.keys():
778+
for ext in COMPILABLE_EXTENSIONS:
782779
# Suffix rules for source folder.
783780
self.suffix_rules_srcdir.update(
784781
{

pylib/gyp/generator/msvs.py

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def _FixPath(path, separator="\\"):
164164
fixpath_prefix
165165
and path
166166
and not os.path.isabs(path)
167-
and not path[0] == "$"
167+
and path[0] != "$"
168168
and not _IsWindowsAbsPath(path)
169169
):
170170
path = os.path.join(fixpath_prefix, path)
@@ -283,7 +283,7 @@ def _ToolSetOrAppend(tools, tool_name, setting, value, only_if_unset=False):
283283
if not tools.get(tool_name):
284284
tools[tool_name] = dict()
285285
tool = tools[tool_name]
286-
if "CompileAsWinRT" == setting:
286+
if setting == "CompileAsWinRT":
287287
return
288288
if tool.get(setting):
289289
if only_if_unset:
@@ -412,10 +412,7 @@ def _BuildCommandLineForRuleRaw(
412412
return input_dir_preamble + cmd
413413
else:
414414
# Convert cat --> type to mimic unix.
415-
if cmd[0] == "cat":
416-
command = ["type"]
417-
else:
418-
command = [cmd[0].replace("/", "\\")]
415+
command = ["type"] if cmd[0] == "cat" else [cmd[0].replace("/", "\\")]
419416
# Add call before command to ensure that commands can be tied together one
420417
# after the other without aborting in Incredibuild, since IB makes a bat
421418
# file out of the raw command string, and some commands (like python) are
@@ -1384,10 +1381,7 @@ def _GetDefines(config):
13841381
"""
13851382
defines = []
13861383
for d in config.get("defines", []):
1387-
if type(d) == list:
1388-
fd = "=".join([str(dpart) for dpart in d])
1389-
else:
1390-
fd = str(d)
1384+
fd = "=".join([str(dpart) for dpart in d]) if isinstance(d, list) else str(d)
13911385
defines.append(fd)
13921386
return defines
13931387

@@ -1598,10 +1592,7 @@ def _IdlFilesHandledNonNatively(spec, sources):
15981592
if rule["extension"] == "idl" and int(rule.get("msvs_external_rule", 0)):
15991593
using_idl = True
16001594
break
1601-
if using_idl:
1602-
excluded_idl = [i for i in sources if i.endswith(".idl")]
1603-
else:
1604-
excluded_idl = []
1595+
excluded_idl = [i for i in sources if i.endswith(".idl")] if using_idl else []
16051596
return excluded_idl
16061597

16071598

@@ -3020,11 +3011,10 @@ def _GetMSBuildConfigurationDetails(spec, build_file):
30203011
_AddConditionalProperty(
30213012
properties, condition, "TargetVersion", _ConfigTargetVersion(settings)
30223013
)
3023-
if character_set:
3024-
if "msvs_enable_winrt" not in spec:
3025-
_AddConditionalProperty(
3026-
properties, condition, "CharacterSet", character_set
3027-
)
3014+
if character_set and "msvs_enable_winrt" not in spec:
3015+
_AddConditionalProperty(
3016+
properties, condition, "CharacterSet", character_set
3017+
)
30283018
return _GetMSBuildPropertyGroup(spec, "Configuration", properties)
30293019

30303020

@@ -3326,15 +3316,14 @@ def _GetMSBuildToolSettingsSections(spec, configurations):
33263316
for tool_name, tool_settings in sorted(msbuild_settings.items()):
33273317
# Skip the tool named '' which is a holder of global settings handled
33283318
# by _GetMSBuildConfigurationGlobalProperties.
3329-
if tool_name:
3330-
if tool_settings:
3331-
tool = [tool_name]
3332-
for name, value in sorted(tool_settings.items()):
3333-
formatted_value = _GetValueFormattedForMSBuild(
3334-
tool_name, name, value
3335-
)
3336-
tool.append([name, formatted_value])
3337-
group.append(tool)
3319+
if tool_name and tool_settings:
3320+
tool = [tool_name]
3321+
for name, value in sorted(tool_settings.items()):
3322+
formatted_value = _GetValueFormattedForMSBuild(
3323+
tool_name, name, value
3324+
)
3325+
tool.append([name, formatted_value])
3326+
group.append(tool)
33383327
groups.append(group)
33393328
return groups
33403329

@@ -3462,10 +3451,7 @@ def _GetValueFormattedForMSBuild(tool_name, name, value):
34623451
"Link": ["AdditionalOptions"],
34633452
"Lib": ["AdditionalOptions"],
34643453
}
3465-
if tool_name in exceptions and name in exceptions[tool_name]:
3466-
char = " "
3467-
else:
3468-
char = ";"
3454+
char = " " if name in exceptions.get(tool_name, []) else ";"
34693455
formatted_value = char.join(
34703456
[MSVSSettings.ConvertVCMacrosToMSBuild(i) for i in value]
34713457
)

pylib/gyp/generator/ninja.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,10 +1815,7 @@ def ComputeOutputFileName(self, spec, type=None):
18151815
"executable": default_variables["EXECUTABLE_SUFFIX"],
18161816
}
18171817
extension = spec.get("product_extension")
1818-
if extension:
1819-
extension = "." + extension
1820-
else:
1821-
extension = DEFAULT_EXTENSION.get(type, "")
1818+
extension = "." + extension if extension else DEFAULT_EXTENSION.get(type, "")
18221819

18231820
if "product_name" in spec:
18241821
# If we were given an explicit name, use that.

0 commit comments

Comments
 (0)