Skip to content

Commit 32fd848

Browse files
author
joseramon.afonso
committed
CLI fixes and changes
1 parent bb43694 commit 32fd848

3 files changed

Lines changed: 52 additions & 21 deletions

File tree

devoutils/faker/scripts/faker_cli.py

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,17 @@ def cli():
4141
'but do not send it')
4242
@click.option('--template', '-t', type=click.File('r'), required=True,
4343
help='Template to send.')
44+
@click.option('--providers', '-p', type=click.File('r'), required=False,
45+
help='File with custom providers dict.')
4446
@click.option('--interactive', '-i', is_flag=True,
4547
help='Interactive mode.')
4648
@click.option('--raw_mode', '-raw', is_flag=True,
4749
help='Send raw mode.')
48-
@click.option('--prob', default=100, help='Probability (0-100).')
49-
@click.option('--freq', default="1-1", help='Frequency in seconds. Example:'
50-
'"1.0-5.0" = random time '
51-
'between 1 sec. to 5secs.')
50+
@click.option('--probability', default=100, help='Probability (0-100).')
51+
@click.option('--frequency', default="(1,1)", help='Frequency in seconds. '
52+
'Example: '
53+
'"1.0-5.0" = random time '
54+
'between 1 sec. to 5secs.')
5255
@click.option('--batch_mode', is_flag=True,
5356
help='Enable blatch mode, a lot of events will be generated as '
5457
'fast as possible and written to a file. The events will be '
@@ -73,6 +76,18 @@ def cli():
7376
def cli(**kwargs):
7477
"""Perform query by query string"""
7578
engine, cfg = configure(kwargs)
79+
providers = None
80+
81+
try:
82+
if "providers" in cfg.keys():
83+
import importlib
84+
85+
providers_module = importlib.import_module(cfg.get("providers"))
86+
providers = providers_module.get_providers()
87+
except Exception as error:
88+
print_error("Error when loading Providers", show_help=False, stop=False)
89+
print_error(error, show_help=False, stop=True)
90+
7691
params = []
7792

7893
click.echo("» Press Ctrl+C to stop the process «", file=sys.stderr)
@@ -84,7 +99,9 @@ def cli(**kwargs):
8499
params.append('Simulation')
85100
thread = SimulationFakeGenerator(cfg['template'],
86101
interactive=cfg['interactive'],
87-
prob=cfg['prob'], freq=cfg['freq'])
102+
probability=cfg['probability'],
103+
frequency=cfg['frequency'],
104+
providers=providers)
88105
elif cfg['batch_mode']:
89106
params.append('Batch mode')
90107
start_date = parser.parse(cfg['date_range'][0])
@@ -93,24 +110,31 @@ def cli(**kwargs):
93110
end_date),
94111
file=sys.stderr)
95112
thread = BatchFakeGenerator(
96-
cfg['template'], start_date, end_date, prob=cfg['prob'],
97-
freq=cfg['freq'], date_format=cfg['date_format'],
113+
cfg['template'], start_date, end_date,
114+
probability=cfg['probability'],
115+
frequency=cfg['frequency'],
116+
date_format=cfg['date_format'],
98117
dont_remove_microseconds=cfg['dont_remove_microseconds'],
99-
file_name=cfg.get('file_name', None))
118+
file_name=cfg.get('file_name', None),
119+
providers=providers)
100120
elif cfg['raw_mode']:
101121
scfg = cfg['sender']
102122
params.append('Host={0}:{1}'.format(scfg.get('address', None),
103123
scfg.get("port", None)))
104124
thread = SyslogRawFakeGenerator(engine, cfg.get('template', None),
105125
interactive=cfg['interactive'],
106-
prob=cfg['prob'], freq=cfg['freq'],
126+
probability=cfg['probability'],
127+
frequency=cfg['frequency'],
128+
providers=providers,
107129
verbose=cfg['verbose'])
108130
elif cfg.get('file_name', None):
109131
params.append('File Name {}'.format(cfg['file_name']))
110132
thread = FileFakeGenerator(cfg['template'],
111133
interactive=cfg['interactive'],
112-
prob=cfg['prob'], freq=cfg['freq'],
134+
probability=cfg['probability'],
135+
frequency=cfg['frequency'],
113136
file_name=cfg['file_name'],
137+
providers=providers,
114138
verbose=cfg['verbose'])
115139
else:
116140
scfg = cfg['sender']
@@ -124,12 +148,15 @@ def cli(**kwargs):
124148
)
125149
thread = SyslogFakeGenerator(engine, cfg['template'],
126150
interactive=cfg['interactive'],
127-
prob=cfg['prob'], freq=cfg['freq'],
128-
tag=cfg.get('tag', "my.app.faker.test"),
151+
probability=cfg['probability'],
152+
frequency=cfg['frequency'],
153+
providers=providers,
154+
tag=cfg.get("tag",
155+
"my.app.faker.test"),
129156
verbose=cfg['verbose'])
130157

131-
params.append('Prob={0}'.format(cfg['prob']))
132-
params.append('Freq={0}'.format(cfg['freq']))
158+
params.append('probability={0}'.format(cfg['probability']))
159+
params.append('frequency={0}'.format(cfg['frequency']))
133160
click.echo("» {0} «\n".format(', '.join(params)), file=sys.stderr)
134161

135162
thread.daemon = True
@@ -153,18 +180,20 @@ def configure(args):
153180
"""For load configuration file/object"""
154181

155182
if args.get('config'):
156-
config = Configuration(args.get('config'))
183+
config = Configuration(path=args.get('config'))
157184
config.mix(dict(args))
158185
else:
159186
config = dict(args)
160187

161-
if 'freq' in config.keys():
162-
parts = config['freq'].split('-')
163-
config['freq'] = (float(parts[0]), float(parts[1]))
188+
if 'frequency' in config.keys() and isinstance(config.get("frequency"),
189+
(str, bytes)):
190+
config['frequency'] = tuple([float(x)
191+
for x
192+
in config.get("frequency").split("-")])
164193

165194
config['template'] = config['template'].read()
166195

167-
# Initialize LtSender with the config credentials but only
196+
# Initialize devo.sender with the config credentials but only
168197
# if we aren't in batch mode or simulation mode
169198
engine = None
170199
if not (config['batch_mode'] or config['simulation']

docs/faker.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ events every second, with a probability of 70 (%) there will be variability.
6767
+ With this option you can create rules to change the probability and frequency based on time periods, to create
6868
false data more in line with a possible reality, for example: more data at peak times, at work hours, or on
6969
weekends, etc.
70-
+ _time_rules_ (_list_): list of objects, each objects its a rule. Each object has 3 values ->
70+
+ _time_rules_ (_list_): list of objects, each objects its a rule. This value its not available as flag in CLI
71+
mode, you need add values to the config file. Each object has 3 values ->
7172
`{"rule": "", "probability": 1, "frequency": (1,10)}`
7273
+ rule **(_string_)**: With CRON syntax you can create rules to change the probability and frequency of
7374
shipments. These rules can be executed once (For example "0 8 * * *") or they can be rules that include ranges

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"Topic :: Software Development :: Libraries :: Python Modules",
2525
]
2626

27-
INSTALL_REQUIRES = ['click==7.1.1',
27+
INSTALL_REQUIRES = ['devo-sdk==3.3.0',
28+
'click==7.1.1',
2829
'requests==2.23.0',
2930
'PyYAML==5.3.1',
3031
'devo-sdk==3.3.0',

0 commit comments

Comments
 (0)