1313from transifex .native .tools .migrations .execution import (MARK_POLICY_OPTIONS ,
1414 REVIEW_POLICY_OPTIONS ,
1515 SAVE_POLICY_OPTIONS ,
16- MigrationExecutor )
16+ MigrationExecutor ,
17+ migrate_text )
1718from transifex .native .tools .migrations .gettext import (GettextMethods ,
1819 GettextMigrationBuilder )
1920
@@ -93,6 +94,12 @@ def add_arguments(self, subparsers):
9394 '--path' , '-p' , dest = 'path' ,
9495 help = 'The path of the files to migrate. Finds files recursively.' ,
9596 )
97+ parser .add_argument (
98+ '--text' , '-t' , dest = 'text' , default = '' ,
99+ help = 'If set, the migration command will display the result '
100+ 'of converting the given text to Transifex Native syntax, '
101+ 'and then exit.' ,
102+ )
96103 parser .add_argument (
97104 '--save' , dest = 'save_policy' , default = 'new' ,
98105 help = ('Determines where the migrated content will be saved: \n ' +
@@ -120,6 +127,22 @@ def handle(self, *args, **options):
120127 self .stats = {
121128 'processed_files' : 0 , 'migrations' : [], 'saved' : [], 'errors' : [],
122129 }
130+
131+ # Create a reusable migrator for templates code
132+ self .django_migration_builder = DjangoTagMigrationBuilder ()
133+ self .gettext_migration_builder = GettextMigrationBuilder (
134+ methods = GettextMethods (** GETTEXT_FUNCTIONS ),
135+ import_statement = T_IMPORT ,
136+ )
137+
138+ # -- Text mode: simply transform the given text and exit
139+ text = options ['text' ]
140+ if text :
141+ migrate_text (text , self ._migrate_text )
142+ return
143+
144+ # -- File mode: read all files based on the given options and migrate
145+ # each of them
123146 self .executor = MigrationExecutor (
124147 options , file_migrator_func = self ._migrate_file ,
125148 )
@@ -138,18 +161,27 @@ def handle(self, *args, **options):
138161 else :
139162 files = self ._find_files (self .path , 'migrate' )
140163
141- # Create a reusable migrator for templates code
142- self .django_migration_builder = DjangoTagMigrationBuilder ()
143- self .gettext_migration_builder = GettextMigrationBuilder (
144- methods = GettextMethods (** GETTEXT_FUNCTIONS ),
145- import_statement = T_IMPORT ,
146- )
147-
148164 # Execute the migration
149165 self .executor .migrate_files (files )
150166
167+ def _migrate_text (self , text ):
168+ """Create a migration to Native syntax for the given string.
169+
170+ Supports both Python files and Django template files.
171+
172+ :param unicode text: the code string
173+ :return: an object with the migration info
174+ :rtype: FileMigration
175+ """
176+ # Template code
177+ if '{%' in text :
178+ return self .django_migration_builder .build_migration (text , '' )
179+ # Python code
180+ else :
181+ return self .gettext_migration_builder .build_migration (text , '' )
182+
151183 def _migrate_file (self , translatable_file ):
152- """Extract source strings from the given file.
184+ """Create a migration to Native syntax for the given file.
153185
154186 Supports both Python files and Django template files.
155187
0 commit comments