1- """This file was auto-generated by datafaker but can be edited manually."""
21from collections .abc import Iterable , Mapping , MutableMapping , Sequence
3- from mimesis import Generic , Numeric , Person
2+ from mimesis import Generic
43from mimesis .locales import Locale
54import sqlalchemy
6- import sys
75from typing import Any , Callable
86import yaml
97
10- from datafaker .base import FileUploader , TableGenerator , DistributionGenerator , ColumnPresence
11- from datafaker .main import load_metadata
8+ from datafaker .base import FileUploader , TableGenerator
129from datafaker .make import TableGeneratorInfo , StoryGeneratorInfo #TODO: move these in here!
1310
1411from datafaker .providers import (
2320from datafaker .utils import logging , get_vocabulary_table_names
2421
2522generic = Generic (locale = Locale .EN_GB )
26- numeric = Numeric ()
27- person = Person ()
28- dist_gen = DistributionGenerator ()
29- column_presence = ColumnPresence ()
3023
3124generic .add_provider (BytesProvider )
3225generic .add_provider (ColumnValueProvider )
3629generic .add_provider (TimespanProvider )
3730generic .add_provider (WeightedBooleanProvider )
3831
39- #metadata = load_metadata("{{ orm_file_name }}", "{{ config_file_name }}")
40-
41- #import {{ row_generator_module_name }}
42- #import {{ story_generator_module_name }}
4332
4433def _eval_structure (config : Any , context : Mapping ) -> Any :
4534 """
@@ -110,16 +99,34 @@ def _get_src_stats(src_stats_filename: str) -> Any:
11099 """
111100 Get the SRC_STATS object
112101 """
113- with open ("{{ src_stats_filename }}" , "r" , encoding = "utf-8" ) as f :
114- return yaml .unsafe_load ( f )
102+ with open (src_stats_filename , "r" , encoding = "utf-8" ) as f :
103+ return yaml .load ( f , yaml . SafeLoader )
115104
116105
117106class TableGenerator :
118107
119- def __init__ (self , rows_per_pass : int , dst_db_conn : sqlalchemy .Connection , table_data : TableGeneratorInfo , max_unique_constraint_tries : int | None ):
108+ def __init__ (
109+ self ,
110+ rows_per_pass : int ,
111+ dst_db_conn : sqlalchemy .Connection ,
112+ table_data : TableGeneratorInfo ,
113+ max_unique_constraint_tries : int | None ,
114+ ) -> None :
115+ """
116+ Initialize a table generator.
117+
118+ :param rows_per_pass: How many rows to add for each call to ``__call__``.
119+ :param dst_db_conn: Connection to the destination database.
120+ :param table_data: Configuration for this generator.
121+ :param max_unique_constraint_tries: How many times to redo generation in
122+ an attempt to satisfy uniqueness constraints. None means never stop, but
123+ this could cause an infinite loop if there are no solutions, or very long
124+ execution if there are few solutions with many constraints.
125+ """
120126 self .num_rows_per_pass = rows_per_pass
121127 self .table_data = table_data
122128 self .max_unique_constraint_tries = max_unique_constraint_tries
129+ self .db_conn = dst_db_conn
123130 self .existing_constraint_hashes : MutableMapping [str , set [int ]] = {}
124131 self .context : Mapping = {}
125132 for constraint in table_data .unique_constraints :
@@ -130,11 +137,13 @@ def __init__(self, rows_per_pass: int, dst_db_conn: sqlalchemy.Connection, table
130137 for result in query_result
131138 ])
132139
133- def set_context (self , context : Mapping ):
140+ def set_context (self , context : Mapping ) -> None :
141+ """Sets all the Python symbols that must be known to the configuration."""
134142 self .context = context
135143
136- def __call__ (self , dst_db_conn ):
137- result = {}
144+ def __call__ (self ):
145+ """Generate some rows of the relevant table in the database."""
146+ result : dict [str , Any ] = {}
138147 columns_to_generate = set (self .table_data .nonnull_columns )
139148 # Which missingness patterns do we want?
140149 for choice in self .table_data .column_choices :
@@ -166,24 +175,33 @@ def __call__(self, dst_db_conn):
166175 self .existing_constraint_hashes .add (cf_hash )
167176 return result
168177
169- def get_table_generator_dict (self , rows_per_pass : int , dst_db_conn : sqlalchemy .Connection , tables_data : Iterable [TableGeneratorInfo ], max_unique_constraint_tries : int | None ):
178+ def get_table_generator_dict (
179+ rows_per_pass : int ,
180+ dst_db_conn : sqlalchemy .Connection ,
181+ tables_data : Iterable [TableGeneratorInfo ],
182+ max_unique_constraint_tries : int | None ,
183+ ):
184+ """Get a dict of table names to row generators that generate rows for that table."""
170185 return {
171- "{{ table_data.table_name }}" : TableGenerator (rows_per_pass , dst_db_conn , table_data , max_unique_constraint_tries )
172- for table_data in tables_data
173- }
186+ table_data .table_name : TableGenerator (rows_per_pass , dst_db_conn , table_data , max_unique_constraint_tries )
187+ for table_data in tables_data
188+ }
174189
175190
176- def get_vocab_dict (config : Mapping , metadata : sqlalchemy .MetaData ) -> Mapping [str , FileUploader ]: {
177- name : FileUploader [metadata .tables [name ]]
178- for name in get_vocabulary_table_names (config )
179- }
191+ def get_vocab_dict (config : Mapping , metadata : sqlalchemy .MetaData ) -> Mapping [str , FileUploader ]:
192+ """Get a dict of table names to objects that can populate those tables from YAML files."""
193+ return {
194+ name : FileUploader (metadata .tables [name ])
195+ for name in get_vocabulary_table_names (config )
196+ }
180197
181198def get_story_generator_list (story_generator_infos : Iterable [StoryGeneratorInfo ], context : Mapping ) -> list [Mapping ]:
199+ """Get a list of mappings describing story generators that must be run."""
182200 return [
183201 {
184202 "function" : _call_from_context (gen_data .function_call .function_name , gen_data .function_call .argument_values , context ),
185- "num_stories_per_pass" : {{ gen_data .num_stories_per_pass }} ,
186- "name" : "{{ gen_data.function_call.function_name }}" ,
203+ "num_stories_per_pass" : gen_data .num_stories_per_pass ,
204+ "name" : gen_data .function_call .function_name ,
187205 }
188206 for gen_data in story_generator_infos
189207 ]
0 commit comments