Skip to content

Commit

Permalink
pre-commit: enable ruff and black (#1597)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Oct 22, 2024
1 parent 2246cc5 commit 1f695b2
Show file tree
Hide file tree
Showing 12 changed files with 225 additions and 136 deletions.
1 change: 1 addition & 0 deletions .config/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ bitness
bthornto
cacheable
cfgs
charliermarsh
checode
chromedriver
chronographer
Expand Down
16 changes: 15 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ exclude: >
minimum_pre_commit_version: 2.9.0 # types_or
repos:
- repo: https://github.com/streetsidesoftware/cspell-cli
rev: v8.14.0
rev: v8.15.2
hooks:
- id: cspell
# name: Spell check with cspell
Expand All @@ -41,6 +41,20 @@ repos:
- --color=always
- -e
- SC1091
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.7.0
hooks:
- id: ruff
args:
- --fix
- --exit-non-zero-on-fix
types_or: [python, pyi]
- id: ruff-format # must be after ruff
types_or: [python, pyi]
- repo: https://github.com/psf/black # must be after ruff
rev: 24.10.0
hooks:
- id: black
- repo: local
hooks:
- id: depcheck
Expand Down
1 change: 1 addition & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"recommendations": [
"DavidAnson.vscode-markdownlint",
"charliermarsh.ruff",
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode",
"hbenl.vscode-mocha-test-adapter",
Expand Down
38 changes: 18 additions & 20 deletions examples/dynamic_inventory.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env python

'''
"""
Example custom dynamic inventory script for Ansible, in Python.
'''
"""

import argparse
from time import sleep
Expand All @@ -12,6 +12,7 @@
except ImportError:
import simplejson as json


class ExampleInventory(object):

def __init__(self):
Expand All @@ -29,40 +30,37 @@ def __init__(self):
else:
self.inventory = self.empty_inventory()

print (json.dumps(self.inventory));
print(json.dumps(self.inventory))

# Example inventory for testing.
def example_inventory(self):
sleep(1)
return {
'python_hosts': {
'hosts': ['10.220.21.24', '10.220.21.27'],
'vars': {
'ansible_ssh_user': 'projectuser',
}
"python_hosts": {
"hosts": ["10.220.21.24", "10.220.21.27"],
"vars": {
"ansible_ssh_user": "projectuser",
},
},
'_meta': {
'hostvars': {
'10.220.21.24': {
'host_specific_var': 'testhost'
},
'10.220.21.27': {
'host_specific_var': 'towerhost'
}
"_meta": {
"hostvars": {
"10.220.21.24": {"host_specific_var": "testhost"},
"10.220.21.27": {"host_specific_var": "towerhost"},
}
}
},
}

# Empty inventory for testing.
def empty_inventory(self):
return {'_meta': {'hostvars': {}}}
return {"_meta": {"hostvars": {}}}

# Read the command line args passed to the script.
def read_cli_args(self):
parser = argparse.ArgumentParser()
parser.add_argument('--list', action = 'store_true')
parser.add_argument('--host', action = 'store')
parser.add_argument("--list", action="store_true")
parser.add_argument("--host", action="store")
self.args = parser.parse_args()


# Get the inventory.
ExampleInventory()
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
#!/usr/bin/env python3

'''
"""
Example custom dynamic inventory script for Ansible, in Python.
'''
"""

import argparse
from time import sleep

try:
import json
except ImportError:
import simplejson as json

class ExampleInventory(object):

class ExampleInventory(object):
def __init__(self):
self.inventory = {}
self.read_cli_args()
Expand All @@ -29,41 +28,38 @@ def __init__(self):
else:
self.inventory = self.empty_inventory()

print(json.dumps(self.inventory));
print(json.dumps(self.inventory))

# Example inventory for testing.
def example_inventory(self):
# sleep is added to create an impression of a complex dynamic inventory file that takes time to fetch hosts
# sleep(1)
return {
'python_hosts': {
'hosts': ['10.220.21.24', '10.220.21.27'],
'vars': {
'ansible_ssh_user': 'projectuser',
}
"python_hosts": {
"hosts": ["10.220.21.24", "10.220.21.27"],
"vars": {
"ansible_ssh_user": "projectuser",
},
},
'_meta': {
'hostvars': {
'10.220.21.24': {
'host_specific_var': 'testhost'
},
'10.220.21.27': {
'host_specific_var': 'towerhost'
}
"_meta": {
"hostvars": {
"10.220.21.24": {"host_specific_var": "testhost"},
"10.220.21.27": {"host_specific_var": "towerhost"},
}
}
},
}

# Empty inventory for testing.
def empty_inventory(self):
return {'_meta': {'hostvars': {}}}
return {"_meta": {"hostvars": {}}}

# Read the command line args passed to the script.
def read_cli_args(self):
parser = argparse.ArgumentParser()
parser.add_argument('--list', action = 'store_true')
parser.add_argument('--host', action = 'store')
parser.add_argument("--list", action="store_true")
parser.add_argument("--host", action="store")
self.args = parser.parse_args()


# Get the inventory.
ExampleInventory()
4 changes: 1 addition & 3 deletions packages/ansible-language-server/tools/version_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ def sync_als_version_in_vscode_ansible_devel():
with open("../vscode-ansible/package.json") as fp:
package_json_vscode_ansible = json.load(fp)

package_json_vscode_ansible["dependencies"][
"@ansible/ansible-language-server"
] = version_als
package_json_vscode_ansible["dependencies"]["@ansible/ansible-language-server"] = version_als
with open("../vscode-ansible/package.json", "w") as fp:
json.dump(package_json_vscode_ansible, fp, indent=4)

Expand Down
11 changes: 11 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
[tool.black]
enable-unstable-feature = ["string_processing"]
line-length = 100
preview = true

[tool.codespell]
builtin = "clear,rare,code"
check-filenames = true
Expand All @@ -24,3 +29,9 @@ norecursedirs = [
"out",
"node_modules",
]

[tool.ruff]
exclude = ["examples"]
fix = true
line-length = 100
target-version = "py310"
80 changes: 42 additions & 38 deletions syntaxes/plist2xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,26 @@


@click.command()
@click.argument('source', required=True)
@click.argument('dest', required=True)
@click.argument("source", required=True)
@click.argument("dest", required=True)
def main(source, dest):
ast = parse(source)

plist: ET.Element = ET.Element('plist', version="1.0")
plist: ET.Element = ET.Element("plist", version="1.0")
generate_xml(plist, ast)

ET.indent(plist, ' ')
ET.indent(plist, " ")
xml = ET.tostring(
plist, encoding='UTF-8', xml_declaration=True,
doctype='<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">'
plist,
encoding="UTF-8",
xml_declaration=True,
doctype=(
'<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"'
' "http://www.apple.com/DTDs/PropertyList-1.0.dtd">'
),
)

with open(dest, 'wb') as f:
with open(dest, "wb") as f:
f.write(xml)


Expand All @@ -29,60 +34,59 @@ def parse(plistPath):
# https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/PropertyLists/OldStylePlists/OldStylePLists.html#//apple_ref/doc/uid/20001012-BBCBDBJE
LPAR, RPAR, LBRACE, RBRACE, EQUALS, SEMI, COL = map(pp.Suppress, "(){}=;,")

array = pp.Forward().setName('array')
dictionary = pp.Forward().setName('dict')
string = (pp.QuotedString('"', escChar="\\", multiline=True, convertWhitespaceEscapes=True) |
pp.QuotedString("'", escQuote="''", multiline=True, convertWhitespaceEscapes=False) |
pp.Word(pp.alphanums+'_-'))('string').setName('simple string or quoted string')
array = pp.Forward().setName("array")
dictionary = pp.Forward().setName("dict")
string = (
pp.QuotedString('"', escChar="\\", multiline=True, convertWhitespaceEscapes=True)
| pp.QuotedString("'", escQuote="''", multiline=True, convertWhitespaceEscapes=False)
| pp.Word(pp.alphanums + "_-")
)("string").setName("simple string or quoted string")

# order here is very important when using '-'
element = pp.Group(string | array | dictionary)('value')
element = pp.Group(string | array | dictionary)("value")

# that '+' is required for matching the optional colon
array_elements = element - pp.ZeroOrMore(COL + element) - pp.Optional(COL)
array << pp.Group(LPAR - pp.Optional(array_elements) -
RPAR)('array')
array << pp.Group(LPAR - pp.Optional(array_elements) - RPAR)("array")

dict_item = pp.Group(string('key') - EQUALS - element - SEMI)
dictionary << pp.Group(LBRACE -
pp.ZeroOrMore(dict_item) -
RBRACE)('dict')
dict_item = pp.Group(string("key") - EQUALS - element - SEMI)
dictionary << pp.Group(LBRACE - pp.ZeroOrMore(dict_item) - RBRACE)("dict")

res = dictionary.parseFile(plistPath, parseAll=True)
return res


def generate_xml(parent: ET.Element, data: pp.ParseResults, level=0, indent=4):
if 'dict' in data:
dictionary = data['dict']
parent = ET.SubElement(parent, 'dict')
if "dict" in data:
dictionary = data["dict"]
parent = ET.SubElement(parent, "dict")
for dict_item in dictionary:
key = dict_item['key']
x_key = ET.SubElement(parent, 'key')
key = dict_item["key"]
x_key = ET.SubElement(parent, "key")
x_key.text = key
value = dict_item['value']
generate_xml(parent, value, level+1, indent)
value = dict_item["value"]
generate_xml(parent, value, level + 1, indent)
pass
elif 'array' in data:
array = data['array']
parent = ET.SubElement(parent, 'array')
elif "array" in data:
array = data["array"]
parent = ET.SubElement(parent, "array")
for array_item in array:
generate_xml(parent, array_item, level+1, indent)
elif 'string' in data:
string: str = data['string']
lines = string.split('\n')
generate_xml(parent, array_item, level + 1, indent)
elif "string" in data:
string: str = data["string"]
lines = string.split("\n")
reindented_lines = [lines[0]]
for line in lines[1:]:
m: Match = re.match(f'^[ ]{{{(level-1)*indent}}}', line)
m: Match = re.match(f"^[ ]{{{(level-1)*indent}}}", line)
if m:
# remove plist indent
line = line[len(m.group(0)):]
line = line[len(m.group(0)) :]
# insert XML indent
line = ' '*((level)*indent) + line
line = " " * ((level) * indent) + line
reindented_lines.append(line)

x_string = ET.SubElement(parent, 'string')
x_string.text = '\n'.join(reindented_lines)
x_string = ET.SubElement(parent, "string")
x_string.text = "\n".join(reindented_lines)


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit 1f695b2

Please sign in to comment.