This commit is contained in:
Ali 2021-02-22 00:04:10 +04:00
parent 248944a634
commit 2d546ef33e
5 changed files with 41531 additions and 7 deletions

View File

@ -76,7 +76,8 @@ class PositionalArgument:
class Entry:
def __init__(self, is_pluralized: bool, positional_arguments: [PositionalArgument]):
def __init__(self, name: str, is_pluralized: bool, positional_arguments: [PositionalArgument]):
self.name = name
self.is_pluralized = is_pluralized
self.positional_arguments = positional_arguments
@ -104,18 +105,154 @@ def parse_positional_arguments(string: str) -> [PositionalArgument]:
return result
def parse_entries(strings: [dict]):
entries = dict()
def parse_entries(strings: [dict]) -> [Entry]:
entries = []
pluralized = re.compile(r'^(.*?)_(0|1|2|3_10|many|any)$', re.U)
processed_entries = set()
for string in strings:
key = string['key']
m = pluralized.match(key)
if m is not None:
raw_key = m.group(1)
if raw_key not in entries:
entries[raw_key] = Entry(is_pluralized=True, positional_arguments=[])
if raw_key in processed_entries:
continue
processed_entries.add(raw_key)
entries.append(Entry(
name=raw_key,
is_pluralized=True,
positional_arguments=[]
))
else:
entries[key] = Entry(is_pluralized=False, positional_arguments=parse_positional_arguments(string['value']))
if key in processed_entries:
continue
processed_entries.add(key)
entries.append(Entry(
name=key,
is_pluralized=False,
positional_arguments=parse_positional_arguments(string['value'])
))
entries.sort(key=lambda x: x.name)
return entries
def write_string(file, string: str):
file.write((string + '\n').encode('utf-8'))
class IndexCounter:
def __init__(self):
self.dictionary = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
self.filter_ids = {
'NO',
'YES',
}
self.max_index = len(self.dictionary) - 1
self.value = []
self.increment()
def increment(self):
index = 0
while True:
if len(self.value) == index:
for i in range(len(self.value)):
self.value[i] = 0
self.value.append(0)
break
if self.value[index] + 1 <= self.max_index:
if index != 0:
for i in range(index):
self.value[i] = 0
self.value[index] += 1
break
else:
index += 1
def get(self):
result = ''
for index in reversed(self.value):
result += self.dictionary[index]
return result
def get_next_valid_id(self) -> str:
while True:
result = self.get()
self.increment()
if result not in self.filter_ids:
return result
def sanitize_entry_identifer(string: str) -> str:
return string.replace('.', '_')
def generate(header_path: str, implementation_path: str, entries: [Entry]):
test_path = '/Users/ali/build/telegram/telegram-ios/submodules/PresentationStrings/main.m'
print('Generating strings into:\n{}\n{}'.format(header_path, implementation_path))
with open(header_path, 'wb') as header_file, open(implementation_path, 'wb') as source_file, open(test_path, 'wb') as test_file:
write_string(test_file, '#import <Foundation/Foundation.h>')
write_string(test_file, '#import <PresentationStrings/PresentationStrings.h>')
write_string(test_file, '')
write_string(test_file, 'int main(int argc, const char **argv) {')
write_string(test_file, 'PresentationStrings *strings = [[PresentationStrings alloc] init];')
write_string(header_file, '#import <Foundation/Foundation.h>')
write_string(header_file, '')
write_string(header_file, '@interface PresentationStrings : NSObject')
write_string(header_file, '')
write_string(header_file, '@end')
write_string(source_file, '#import <PresentationStrings/PresentationStrings.h>')
write_string(source_file, '')
write_string(source_file, '@implementation PresentationStrings')
write_string(source_file, '@end')
write_string(source_file, '')
counter = IndexCounter()
for entry in entries:
entry_id = '_L' + counter.get_next_valid_id()
write_string(header_file, '// {}'.format(entry.name))
write_string(source_file, '// {}'.format(entry.name))
function_arguments = ''
if entry.is_pluralized:
swift_spec = 'PresentationStrings.{}(self:_:)'.format(sanitize_entry_identifer(entry.name))
function_arguments = ', int32_t value'
elif len(entry.positional_arguments) != 0:
positional_arguments_spec = ''
argument_index = -1
for argument in entry.positional_arguments:
argument_index += 1
if argument.kind == 'd':
function_arguments += ', int32_t _{}'.format(argument_index)
elif argument.kind == '@':
function_arguments += ', NSString * _Nonnull _{}'.format(argument_index)
else:
raise Exception('Unsupported argument type {}'.format(argument.kind))
positional_arguments_spec += '_:'
swift_spec = 'PresentationStrings.{}(self:{})'.format(
sanitize_entry_identifer(entry.name), positional_arguments_spec)
else:
swift_spec = 'getter:PresentationStrings.{}(self:)'.format(sanitize_entry_identifer(entry.name))
# write_string(header_file, '__attribute__((swift_name("{}")))'
# .format(swift_spec))
write_string(header_file, 'NSString * _Nonnull {}(PresentationStrings * _Nonnull _self);'.format(
entry_id, function_arguments))
write_string(test_file, '{}(strings);'.format(entry_id))
write_string(test_file, '{}(strings);'.format(entry_id))
write_string(test_file, '{}(strings);'.format(entry_id))
write_string(source_file, 'NSString * _Nonnull {}(PresentationStrings * _Nonnull _self) {{'.format(
entry_id, function_arguments))
write_string(source_file, ' return @"";'.format(entry_id))
write_string(source_file, '}')
write_string(source_file, ''.format(entry_id))
write_string(test_file, ' return 0;')
write_string(test_file, '}')
write_string(test_file, '')
if __name__ == '__main__':
@ -147,4 +284,5 @@ if __name__ == '__main__':
args = parser.parse_args()
parsed_strings = parse_strings(args.source)
parse_entries(parsed_strings)
all_entries = parse_entries(parsed_strings)
generate(header_path=args.outHeader, implementation_path=args.outImplementation, entries=all_entries)

File diff suppressed because it is too large Load Diff

Binary file not shown.

File diff suppressed because it is too large Load Diff