Files
OpenHands/opendevin/parse_commands.py
Boxuan Li dd32fa6f4a Unify linter behaviour across CI and pre-commit-hook (#1071)
* CI: Add autopep8 linter

Currently, we have autopep8 as part of pre-commit-hook. To ensure
consistent behaviour, we should have it in CI as well.

Moreover, pre-commit-hook contains a double-quote-string-fixer hook
which changes all double quotes to single quotes, but I do observe
some PRs with massive changes that do the opposite way. I suspect
that these authors 1) disable or circumvent the pre-commit-hook,
and 2) have other linters such as black in their IDE, which
automatically change all single quotes to double quotes. This
has caused a lot of unnecessary diff, made review really hard,
and led to a lot of conflicts.

* Use -diff for autopep8

* autopep8: Freeze version in CI

* Ultimate fix

* Remove pep8 long line disable workaround

* Fix lint.yml

* Fix all files under opendevin and agenthub
2024-04-14 00:19:56 -04:00

53 lines
1.7 KiB
Python

import os
from dataclasses import dataclass
import yaml
@dataclass()
class Command:
name: str
docstring: str | None = None
signature: str | None = None
def parse_command_file() -> str | None:
if not os.path.exists('commands.sh'):
return None
content = open('commands.sh', 'r').read()
lines = content.split('\n')
commands: list[Command] = []
idx = 0
docs: list[str] = []
while idx < len(lines):
line = lines[idx]
idx += 1
if line.startswith('# '):
docs.append(line[2:])
elif line.strip().endswith('() {'):
name = line.split()[0][:-2]
while lines[idx].strip() != '}':
idx += 1
docstring, signature = None, name
docs_dict = yaml.safe_load('\n'.join(docs).replace('@yaml', ''))
if docs_dict is not None:
docstring = docs_dict.get('docstring')
arguments = docs_dict.get('arguments', None)
if 'signature' in docs_dict:
signature = docs_dict['signature']
else:
if arguments is not None:
for param, settings in arguments.items():
if 'required' in settings:
signature += f' <{param}>'
else:
signature += f' [<{param}>]'
command = Command(name, docstring, signature)
commands.append(command)
docs = []
function_docs = ''
for cmd in commands:
if cmd.docstring is not None:
function_docs += f'{cmd.signature or cmd.name} - {cmd.docstring}\n'
return function_docs