simplify package formatter class
This commit is contained in:
parent
60ff9b3e99
commit
1c73e53c55
1 changed files with 12 additions and 49 deletions
|
@ -26,13 +26,13 @@ class PackageFormatter:
|
||||||
def command_name2keyword(keyword: str):
|
def command_name2keyword(keyword: str):
|
||||||
return '__' + keyword.upper().strip().replace(' ', '_') + '__'
|
return '__' + keyword.upper().strip().replace(' ', '_') + '__'
|
||||||
|
|
||||||
def parse_replacement_args(self, *user_args, **user_kwargs):
|
def parse_replacement_args(self, match_groups, *user_args, **user_kwargs):
|
||||||
new_args = []
|
new_args = []
|
||||||
for arg in user_args:
|
for arg in user_args:
|
||||||
if type(arg) == Attributes:
|
if type(arg) == Attributes:
|
||||||
new_args.append(getattr(self, arg.value))
|
new_args.append(getattr(self, arg.value))
|
||||||
elif type(arg) == Args:
|
elif type(arg) == Args:
|
||||||
new_args.append('{}') # Do *not* format this entry in the string
|
new_args.append(match_groups[arg.value])
|
||||||
elif type(arg) == str:
|
elif type(arg) == str:
|
||||||
new_args.append(arg)
|
new_args.append(arg)
|
||||||
else:
|
else:
|
||||||
|
@ -43,61 +43,24 @@ class PackageFormatter:
|
||||||
if type(user_kwargs[kw]) == Attributes:
|
if type(user_kwargs[kw]) == Attributes:
|
||||||
new_kwargs[kw] = getattr(self, user_kwargs[kw].value)
|
new_kwargs[kw] = getattr(self, user_kwargs[kw].value)
|
||||||
elif type(user_kwargs[kw]) == Args:
|
elif type(user_kwargs[kw]) == Args:
|
||||||
new_kwargs[kw] = '{{{}}}'.format(kw) # Do *not* format this entry in the string
|
new_kwargs[kw] = match_groups[user_kwargs[kw].value]
|
||||||
elif type(user_kwargs[kw]) == str:
|
elif type(user_kwargs[kw]) == str:
|
||||||
new_kwargs[kw] = user_kwargs[kw]
|
new_kwargs[kw] = user_kwargs[kw]
|
||||||
else:
|
else:
|
||||||
new_kwargs[kw] = 'ERROR'
|
new_kwargs[kw] = 'ERROR'
|
||||||
return new_args, new_kwargs
|
return new_args, new_kwargs
|
||||||
|
|
||||||
@staticmethod
|
def add_replacement(self, keyword: str, replacement: str, *args, **kwargs):
|
||||||
def filter_replacement_args(*user_args, **user_kwargs):
|
args, kwargs = self.parse_replacement_args([], *args, **kwargs)
|
||||||
new_args = []
|
|
||||||
for arg in user_args:
|
|
||||||
if type(arg) == Args:
|
|
||||||
new_args.append(arg)
|
|
||||||
new_args = tuple(new_args)
|
|
||||||
new_kwargs = {}
|
|
||||||
for kw in user_kwargs:
|
|
||||||
if type(user_kwargs[kw]) == Args:
|
|
||||||
new_kwargs[kw] = user_kwargs[kw]
|
|
||||||
return new_args, new_kwargs
|
|
||||||
|
|
||||||
def add_replacement(self, keyword: str, replacement: str, *args, **kwargs) -> bool:
|
|
||||||
if keyword in self.replace_dict.keys():
|
|
||||||
return False
|
|
||||||
args, kwargs = self.parse_replacement_args(*args, **kwargs)
|
|
||||||
self.replace_dict[self.command_name2keyword(keyword)] = replacement.format(*args, **kwargs)
|
self.replace_dict[self.command_name2keyword(keyword)] = replacement.format(*args, **kwargs)
|
||||||
return True
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def parse_replacement_args2(match_groups, *user_args, **user_kwargs):
|
|
||||||
new_args = []
|
|
||||||
for arg in user_args:
|
|
||||||
if type(arg) == Args:
|
|
||||||
new_args.append(match_groups[arg.value])
|
|
||||||
new_args += 'ERROR'
|
|
||||||
new_args = tuple(new_args)
|
|
||||||
new_kwargs = {}
|
|
||||||
for kw in user_kwargs:
|
|
||||||
if type(user_kwargs[kw]) == Args:
|
|
||||||
new_kwargs[kw] = match_groups[user_kwargs[kw].value]
|
|
||||||
else:
|
|
||||||
new_kwargs[kw] = 'ERROR'
|
|
||||||
return new_args, new_kwargs
|
|
||||||
|
|
||||||
def add_arg_replacement(self, num_args: int, keyword: str, replacement: str, *args, **kwargs):
|
def add_arg_replacement(self, num_args: int, keyword: str, replacement: str, *args, **kwargs):
|
||||||
if keyword in self.arg_replace_dict.keys():
|
|
||||||
return False
|
|
||||||
format_args, format_kwargs = self.parse_replacement_args(*args, **kwargs)
|
|
||||||
filtered_args, filtered_kwargs = self.filter_replacement_args(*args, **kwargs)
|
|
||||||
self.arg_replace_dict[self.command_name2keyword(keyword)] = {
|
self.arg_replace_dict[self.command_name2keyword(keyword)] = {
|
||||||
'num_args': num_args,
|
'num_args': num_args,
|
||||||
'replacement': replacement.format(*format_args, **format_kwargs),
|
'replacement': replacement,
|
||||||
'format_args': filtered_args,
|
'format_args': args,
|
||||||
'format_kwargs': filtered_kwargs
|
'format_kwargs': kwargs
|
||||||
}
|
}
|
||||||
return True
|
|
||||||
|
|
||||||
def format(self, contents: str) -> str:
|
def format(self, contents: str) -> str:
|
||||||
for key in self.replace_dict.keys():
|
for key in self.replace_dict.keys():
|
||||||
|
@ -106,14 +69,14 @@ class PackageFormatter:
|
||||||
|
|
||||||
def format_with_arg(self, contents: str) -> str:
|
def format_with_arg(self, contents: str) -> str:
|
||||||
for command in self.arg_replace_dict.keys():
|
for command in self.arg_replace_dict.keys():
|
||||||
search_regex = re.compile(r'{keyword}\({arguments}\)'.format(
|
search_regex = re.compile(r'{keyword}\({arguments}(?<!@)\)'.format(
|
||||||
keyword=command,
|
keyword=command,
|
||||||
arguments=','.join(['(.*)'] * self.arg_replace_dict[command]['num_args'])
|
arguments=','.join(['(.*?)'] * self.arg_replace_dict[command]['num_args'])
|
||||||
))
|
))
|
||||||
match = re.search(search_regex, contents)
|
match = re.search(search_regex, contents)
|
||||||
while match is not None:
|
while match is not None:
|
||||||
format_args, format_kwargs = self.parse_replacement_args2(
|
format_args, format_kwargs = self.parse_replacement_args(
|
||||||
match.groups(),
|
list(map(lambda group: group.replace('@)', ')'), match.groups())),
|
||||||
*self.arg_replace_dict[command]['format_args'],
|
*self.arg_replace_dict[command]['format_args'],
|
||||||
**self.arg_replace_dict[command]['format_kwargs']
|
**self.arg_replace_dict[command]['format_kwargs']
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue