def print_app_with_verbose_names(app): print('##################################################################') header = '# -*- coding: utf-8 -*-\n' for line in getsourcelines(app.models_module): if line in ['# -*- coding: utf-8 -*-', 'from django.utils.translation import ugettext as _', ]: continue elif line == 'from django.db import models': header += '''from django.db import models from django.utils.translation import ugettext_lazy as _ ''' elif 'class' in line: break else: header += line + '\n' print(header.strip()) for model in app.models.values(): print('\n') for p in source_with_verbose_names(model): print(p)
def print_app_with_verbose_names(app): print('##################################################################') header = '# -*- coding: utf-8 -*-\n' for line in getsourcelines(app.models_module): if line in [ '# -*- coding: utf-8 -*-', 'from django.utils.translation import ugettext as _', ]: continue elif line == 'from django.db import models': header += '''from django.db import models from django.utils.translation import ugettext_lazy as _ ''' elif 'class' in line: break else: header += line + '\n' print(header.strip()) for model in app.models.values(): print('\n') for p in source_with_verbose_names(model): print(p)
def source_with_verbose_names(model): source = getsourcelines(model) title, labels, non_matched = extract_verbose_names(model) field_regex = ' *(.+) = (models\.[^\(]*)\((.*verbose_name=_\(.*\)|.*)\)' new_lines = [] class_meta_already_exists = False for line in source[1:]: for regex, split in [(field_regex + ' *# (.+)', lambda groups: groups), (field_regex, lambda groups: groups + ('', ))]: match = re.match(regex, line) if match: name, path, args, legacy_name = split(match.groups()) if name in labels and 'verbose_name' not in args: args = [args] if args.strip() else [] args.append("verbose_name=_('%s')" % labels[name]) args = ', '.join(args) new_lines.append( (' %s = %s(%s)' % (name, path, args), legacy_name)) break else: if 'class Meta:' in line: class_meta_already_exists = True new_lines.append((line, '')) yield source[0].rstrip() cols = max(map(len, [line for line, _ in new_lines])) for line, legacy_name in new_lines: line = line.rstrip().ljust(cols) if legacy_name: yield line + ' # ' + legacy_name else: yield line # class Meta if class_meta_already_exists: return if title == 'Tabelas Auxiliares': title = '' title = title if title else '' def add_s(name): return ' '.join(p if p.endswith('s') else p + 's' for p in name.split()) def remove_s(name): return ' '.join(p[:-1] if p.endswith('s') else p for p in name.split()) if not title: # default title from model name title_singular = ' '.join(re.findall('[A-Z][^A-Z]*', model.__name__)) title_singular = re.sub('cao\\b', 'ção', title_singular) title_singular = re.sub('ao\\b', 'ão', title_singular) title_plural = add_s( title_singular.replace('ção', 'ções').replace('ão', 'ões')) elif title.endswith('s'): title_singular = remove_s( title.replace('ções', 'ção').replace('ões', 'ão')) title_plural = title else: title_singular = title title_plural = add_s(title.replace('ção', 'ções').replace('ão', 'ões')) yield """ class Meta: verbose_name = _('%s') verbose_name_plural = _('%s')""" % (title_singular, title_plural)
def source_with_verbose_names(model): source = getsourcelines(model) title, labels, non_matched = extract_verbose_names(model) field_regex = ' *(.+) = (models\.[^\(]*)\((.*verbose_name=_\(.*\)|.*)\)' new_lines = [] class_meta_already_exists = False for line in source[1:]: for regex, split in [ (field_regex + ' *# (.+)', lambda groups: groups), (field_regex, lambda groups: groups + ('',))]: match = re.match(regex, line) if match: name, path, args, legacy_name = split(match.groups()) if name in labels and 'verbose_name' not in args: args = [args] if args.strip() else [] args.append("verbose_name=_('%s')" % labels[name]) args = ', '.join(args) new_lines.append( (' %s = %s(%s)' % (name, path, args), legacy_name)) break else: if 'class Meta:' in line: class_meta_already_exists = True new_lines.append((line, '')) yield source[0].rstrip() cols = max(map(len, [line for line, _ in new_lines])) for line, legacy_name in new_lines: line = line.rstrip().ljust(cols) if legacy_name: yield line + ' # ' + legacy_name else: yield line # class Meta if class_meta_already_exists: return if title == 'Tabelas Auxiliares': title = '' title = title if title else '' def add_s(name): return ' '.join( p if p.endswith('s') else p + 's' for p in name.split()) def remove_s(name): return ' '.join(p[:-1] if p.endswith('s') else p for p in name.split()) if not title: # default title from model name title_singular = ' '.join(re.findall('[A-Z][^A-Z]*', model.__name__)) title_singular = re.sub('cao\\b', 'ção', title_singular) title_singular = re.sub('ao\\b', 'ão', title_singular) title_plural = add_s( title_singular.replace('ção', 'ções').replace('ão', 'ões')) elif title.endswith('s'): title_singular = remove_s( title.replace('ções', 'ção').replace('ões', 'ão')) title_plural = title else: title_singular = title title_plural = add_s(title.replace('ção', 'ções').replace('ão', 'ões')) yield """ class Meta: verbose_name = _('%s') verbose_name_plural = _('%s')""" % (title_singular, title_plural)