Coverage for nineties/cli.py: 100%

36 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-02-04 20:53:48 +00:00

1# -*- coding: utf-8 -*- 

2"""Command line arg for nineties.""" 

3import json 

4import os 

5import sys 

6from typing import no_type_check 

7 

8try: 

9 from faker import Faker 

10except ImportError as err: 

11 raise ImportError('dependency faker not found - please pip install faker. Details %s' % (err,)) 

12 

13 

14import nineties.parser as p90 

15import nineties.privacy as priv 

16 

17# Below to profit from insert ordering of dicts in 3.6+ 

18if tuple(sys.version_info) < (3, 6): 

19 raise RuntimeError('python version 3.6 or higher required (better dicts)') 

20 

21 

22FAKE = Faker() 

23Faker.seed(42) 

24 

25 

26@no_type_check 

27def parse(json_text, process_with): 

28 """Dive deep ...""" 

29 data = json.loads(json_text) 

30 record = {} 

31 for key, value in data.items(): 

32 if key in process_with: 

33 record[key] = process_with[key](value) 

34 

35 return record 

36 

37 

38@no_type_check 

39def main(argv=None): 

40 """Drive the understanding ...""" 

41 argv = sys.argv if argv is None else argv 

42 parser_map = { 

43 'dsl': p90.parse_dsl_entry, 

44 'timestamp': p90.parse_timestamp, 

45 'name': priv.safe_name, 

46 } 

47 for text_or_file in argv[1:]: 

48 if os.path.isfile(text_or_file): 

49 with open(text_or_file, 'rt') as json_file: 

50 all_text = json_file.read() 

51 data = parse(all_text, parser_map) 

52 else: 

53 if p90.START_DATA in text_or_file: 

54 data = parse(text_or_file, parser_map) 

55 else: 

56 data = {'error': text_or_file} 

57 

58 print(data)