-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasa_name_objects_to_capitalised.py
74 lines (58 loc) · 2.22 KB
/
asa_name_objects_to_capitalised.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
'''
-*- coding: utf-8 -*-
title : asa_name_objects_to_capitalised.py
description : Performs a lookup in object group and find the appropriate IP address
author : [email protected]
date : 14/03/2018
version : 0.1
usage : ./asa_name_objects_to_capitalised.py --help
notes :
python_version : 3.5.2
Requirements:
- python3
- pip install logzero argprse
simply converts all name elements to uppercase, cause I like my objects to be in caps
Usage:
python asa_name_objects_to_capitalised.py -i <'path/to/name-to-object.txt'> -o <'path/to/file.txt'>
=======================================================================
'''
import os
import re
import sys
from logzero import logger
import argparse
def CheckArgs(args=None):
parser = argparse.ArgumentParser(
description='Convert name to objects')
parser.add_argument('-i', '--input_file', required=True,
help='File to convert.')
parser.add_argument('-o', '--output_file', required=True,
help='File to output to.')
parser.add_argument('-c', '--cleanup', required=False, action='store_true',
help='Removes output file first')
results = parser.parse_args(args)
return (
results.input_file,
results.output_file,
results.cleanup
)
def RemoveFile(file):
logger.info('Cleanup requested, removing {}'.format(file))
os.remove(file)
if __name__ == "__main__":
INPUT_FILE, OUTPUT_FILE, cleanup_last_run = CheckArgs(sys.argv[1:])
if cleanup_last_run:
RemoveFile(OUTPUT_FILE)
with open(INPUT_FILE, 'r') as imp:
alist = [line.rstrip() for line in imp]
with open(OUTPUT_FILE, 'a') as out:
for line in alist:
elements = line.split(' ')
logger.info('processing: {}'.format(line))
# in case the name element has a description
if len(elements) >= 4:
out.write('{} {} {} {}'.format(elements[0], elements[1], elements[2].upper(), ' '.join(elements[3:])))
else:
out.write('{} {} {}'.format(elements[0], elements[1], elements[2].upper()))
out.write('\n')