OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """Generates profile dictionaries for Autofill. |
| 7 |
| 8 Used to test autofill.AutoFillTest.FormFillLatencyAfterSubmit. |
| 9 Can be used as a stand alone script with -h to print out help text by running: |
| 10 python autofill_dataset_generator.py -h |
| 11 """ |
| 12 |
| 13 import codecs |
| 14 import logging |
| 15 from optparse import OptionParser |
| 16 import os |
| 17 import random |
| 18 import re |
| 19 import sys |
| 20 |
| 21 |
| 22 class NullHandler(logging.Handler): |
| 23 def emit(self, record): |
| 24 pass |
| 25 |
| 26 |
| 27 class DatasetGenerator(object): |
| 28 """Generates a dataset of dictionaries. |
| 29 |
| 30 The lists (such as address_construct, city_construct) define the way the |
| 31 corresponding field is generated. They accomplish this by specifying a |
| 32 list of function-args lists. |
| 33 """ |
| 34 address_construct = [ |
| 35 [ random.randint, 1, 10000], |
| 36 [ None, u'foobar'], |
| 37 [ random.choice, [ u'St', u'Ave', u'Ln', u'Ct', ]], |
| 38 [ random.choice, [ u'#1', u'#2', u'#3', ]], |
| 39 ] |
| 40 |
| 41 city_construct = [ |
| 42 [ random.choice, [ u'San Jose', u'San Francisco', u'Sacramento', |
| 43 u'Los Angeles', ]], |
| 44 ] |
| 45 |
| 46 state_construct = [ |
| 47 [ None, u'CA'] |
| 48 ] |
| 49 |
| 50 zip_construct = [ |
| 51 [ None, u'95110'] |
| 52 ] |
| 53 |
| 54 re_single_quote = re.compile('\'', re.UNICODE) |
| 55 logger = logging.getLogger(__name__) |
| 56 logger.addHandler(NullHandler()) |
| 57 log_handlers = {'StreamHandler': None} |
| 58 |
| 59 def __init__(self, output_filename=None, logging_level=None): |
| 60 """Constructs dataset generator object. |
| 61 |
| 62 Creates 'fields' data member which is a list of pair (two values) lists. |
| 63 These pairs are comprised of a field key e.g. u'NAME_FIRST' and a |
| 64 generator method e.g. self.GenerateNameFirst which will generate the value. |
| 65 If we want the value to always be the same e.g. u'John' we can use this |
| 66 instead of a method. We can even use None keyword which will give |
| 67 a value of u''. |
| 68 |
| 69 'output_pattern' for one field would have been: "{u'NAME_FIRST': u'%s',}" |
| 70 which is ready to accept a value for the 'NAME_FIRST' field key once |
| 71 this value is generated. |
| 72 'output_pattern' is used in 'GenerateNextDict()' to generate the next |
| 73 dict line. |
| 74 |
| 75 Args: |
| 76 output_filename: specified filename of generated dataset to be saved. |
| 77 Default value is None and no saving takes place. |
| 78 logging_level: set verbosity levels, default is None. |
| 79 """ |
| 80 if logging_level: |
| 81 if not self.log_handlers['StreamHandler']: |
| 82 console = logging.StreamHandler() |
| 83 console.setLevel(logging.INFO) |
| 84 self.log_handlers['StreamHandler'] = console |
| 85 self.logger.addHandler(console) |
| 86 self.logger.setLevel(logging_level) |
| 87 else: |
| 88 if self.log_handlers['StreamHandler']: |
| 89 self.logger.removeHandler(self._log_handlers['StreamHandler']) |
| 90 self.log_handlers['StreamHandler'] = None |
| 91 |
| 92 self.output_filename = output_filename |
| 93 |
| 94 self.dict_no = 0 |
| 95 self.fields = [ |
| 96 [u'NAME_FIRST', self.GenerateNameFirst], |
| 97 [u'NAME_MIDDLE', None], |
| 98 [u'NAME_LAST', None], |
| 99 [u'EMAIL_ADDRESS', self.GenerateEmail], |
| 100 [u'COMPANY_NAME', None], |
| 101 [u'ADDRESS_HOME_LINE1', self.GenerateAddress], |
| 102 [u'ADDRESS_HOME_LINE2', None], |
| 103 [u'ADDRESS_HOME_CITY', self.GenerateCity], |
| 104 [u'ADDRESS_HOME_STATE', self.GenerateState], |
| 105 [u'ADDRESS_HOME_ZIP', self.GenerateZip], |
| 106 [u'ADDRESS_HOME_COUNTRY', u'United States'], |
| 107 [u'PHONE_HOME_WHOLE_NUMBER', None], |
| 108 [u'PHONE_FAX_WHOLE_NUMBER', u'6501234555'], |
| 109 ] |
| 110 |
| 111 self.dict_length = len(self.fields) |
| 112 self.output_pattern = u'{' |
| 113 for key_and_method in self.fields: |
| 114 self.output_pattern += u'u"%s": u"%s", ' % (key_and_method[0], "%s") |
| 115 self.output_pattern = self.output_pattern[:-1] + '},' # Del last space. |
| 116 |
| 117 def _GenerateField(self, field_construct): |
| 118 """Generates each field in each dictionary. |
| 119 |
| 120 Args: |
| 121 field_construct: it is a list of lists. |
| 122 The first value (index 0) of each containing list is a function or None. |
| 123 The remaining values are the args. If function is None then arg is just |
| 124 returned. |
| 125 |
| 126 Example 1: zip_construct = [[ None, u'95110']]. There is one |
| 127 containing list only and function here is None and arg is u'95110'. |
| 128 This just returns u'95110'. |
| 129 |
| 130 Example 2: address_construct = [ [ random.randint, 1, 10000], |
| 131 [ None, u'foobar'] ] This has two containing lists and it will return |
| 132 the result of: |
| 133 random.randint(1, 10000) + ' ' + u'foobar' |
| 134 which could be u'7832 foobar' |
| 135 """ |
| 136 parts = [] |
| 137 for function_and_args in field_construct: |
| 138 function = function_and_args[0] |
| 139 args = function_and_args[1:] |
| 140 if not function: |
| 141 function = lambda x: x |
| 142 parts.append(u'%s' % function(*args)) |
| 143 return (' ').join(parts) |
| 144 |
| 145 def GenerateAddress(self): |
| 146 """Uses _GenerateField() and address_construct to gen a random address. |
| 147 |
| 148 Returns: |
| 149 A random address. |
| 150 """ |
| 151 return self._GenerateField(self.address_construct) |
| 152 |
| 153 def GenerateCity(self): |
| 154 """Uses _GenerateField() and city_construct to gen a random city. |
| 155 |
| 156 Returns: |
| 157 A random city. |
| 158 """ |
| 159 return self._GenerateField(self.city_construct) |
| 160 |
| 161 def GenerateState(self): |
| 162 """Uses _GenerateField() and state_construct to generate a state. |
| 163 |
| 164 Returns: |
| 165 A random state. |
| 166 """ |
| 167 return self._GenerateField(self.state_construct) |
| 168 |
| 169 def GenerateZip(self): |
| 170 """Uses _GenerateField() and zip_construct to generate a zip code. |
| 171 |
| 172 Returns: |
| 173 A random zip code. |
| 174 """ |
| 175 return self._GenerateField(self.zip_construct) |
| 176 |
| 177 def GenerateCountry(self): |
| 178 """Uses _GenerateField() and country_construct to generate a country. |
| 179 |
| 180 Returns: |
| 181 A random country. |
| 182 """ |
| 183 return self._GenerateField(self.country_construct) |
| 184 |
| 185 def GenerateNameFirst(self): |
| 186 """Generates a numerical first name. |
| 187 |
| 188 The name is the number of the current dict. |
| 189 i.e. u'1', u'2', u'3' |
| 190 |
| 191 Returns: |
| 192 A random first name. |
| 193 """ |
| 194 return u'%s' % self.dict_no |
| 195 |
| 196 def GenerateEmail(self): |
| 197 """Generates an email that corresponds to the first name. |
| 198 |
| 199 i.e. u'1@example.com', u'2@example.com', u'3@example.com' |
| 200 |
| 201 Returns: |
| 202 A random email address. |
| 203 """ |
| 204 return u'%s@example.com' % self.dict_no |
| 205 |
| 206 |
| 207 def GenerateNextDict(self): |
| 208 """Generates next dictionary of the dataset. |
| 209 |
| 210 Returns: |
| 211 The output dictionary. |
| 212 """ |
| 213 self.dict_no += 1 |
| 214 output_dict = {} |
| 215 for key, method_or_value in self.fields: |
| 216 if not method_or_value: |
| 217 output_dict[key] = '' |
| 218 elif type(method_or_value) in [str, unicode]: |
| 219 output_dict[key] = '%s' % method_or_value |
| 220 else: |
| 221 output_dict[key] = method_or_value() |
| 222 output_dict[key] = self.re_single_quote.sub( |
| 223 r'\'', output_dict[key]) # Escaping single quote: "'" -> '\'' |
| 224 return output_dict |
| 225 |
| 226 def GenerateDataset(self, num_of_dict_to_generate=10): |
| 227 """Generates a list of dictionaries. |
| 228 |
| 229 Args: |
| 230 num_of_dict_to_generate: The number of dictionaries to be generated. |
| 231 Default value is 10. |
| 232 |
| 233 Returns: |
| 234 The dictionary list. |
| 235 """ |
| 236 if self.output_filename: |
| 237 output_file = codecs.open( |
| 238 self.output_filename, mode='wb', encoding='utf-8-sig') |
| 239 else: |
| 240 output_file = None |
| 241 try: |
| 242 list_of_dict = [] |
| 243 if output_file: |
| 244 output_file.write('[') |
| 245 output_file.write(os.linesep) |
| 246 |
| 247 while self.dict_no < num_of_dict_to_generate: |
| 248 output_dict = self.GenerateNextDict() |
| 249 list_of_dict.append(output_dict) |
| 250 output_line = self.output_pattern % tuple( |
| 251 [output_dict[key_and_method[0]] for key_and_method in self.fields]) |
| 252 if output_file: |
| 253 output_file.write(output_line) |
| 254 output_file.write(os.linesep) |
| 255 self.logger.info( |
| 256 '%d: %s' % (self.dict_no, output_line.encode(sys.stdout.encoding, |
| 257 'ignore'))) |
| 258 |
| 259 if output_file: |
| 260 output_file.write(']') |
| 261 output_file.write(os.linesep) |
| 262 self.logger.info('%d dictionaries generated SUCCESSFULLY!', self.dict_no) |
| 263 self.logger.info('--- FINISHED ---') |
| 264 return list_of_dict |
| 265 finally: |
| 266 if output_file: |
| 267 output_file.close() |
| 268 |
| 269 def main(): |
| 270 # Command line options. |
| 271 parser = OptionParser() |
| 272 parser.add_option( |
| 273 '-o', '--output', dest='output_filename', default='', |
| 274 help='write output to FILE [optional]', metavar='FILE') |
| 275 parser.add_option( |
| 276 '-d', '--dict', type='int', dest='dict_no', metavar='DICT_NO', default=10, |
| 277 help='DICT_NO: number of dictionaries to be generated [default: %default]') |
| 278 parser.add_option( |
| 279 '-l', '--log_level', dest='log_level', default='debug', |
| 280 metavar='LOG_LEVEL', |
| 281 help='LOG_LEVEL: "debug", "info", "warning" or "error" [default: %default]') |
| 282 |
| 283 (options, args) = parser.parse_args() |
| 284 if args: |
| 285 parser.print_help() |
| 286 sys.exit(1) |
| 287 options.log_level = options.log_level.lower() |
| 288 if options.log_level not in ['debug', 'info', 'warning', 'error']: |
| 289 parser.error('Wrong log_level argument.') |
| 290 else: |
| 291 if options.log_level == 'debug': |
| 292 options.log_level = logging.DEBUG |
| 293 elif options.log_level == 'info': |
| 294 options.log_level = logging.INFO |
| 295 elif options.log_level == 'warning': |
| 296 options.log_level = logging.WARNING |
| 297 elif options.log_level == 'error': |
| 298 options.log_level = logging.ERROR |
| 299 |
| 300 gen = DatasetGenerator(options.output_filename, options.log_level) |
| 301 gen.GenerateDataset(options.dict_no) |
| 302 |
| 303 |
| 304 if __name__ == '__main__': |
| 305 main() |
OLD | NEW |