forked from jantman/misc-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskeleton.py
executable file
·81 lines (65 loc) · 2.89 KB
/
skeleton.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
75
76
77
78
79
80
81
#!/usr/bin/env python
"""
Generic python2.7+ (incl. py3x) command line script skeleton.
This implements most of the common stuff I put in one-off scripts
to make them actually not-so-shitty.
If you have ideas for improvements, or want the latest version, it's at:
<https://github.com/jantman/misc-scripts/blob/master/skeleton.py>
NOTE that I've converted this from using the now-deprecated optparse
module for option parsing, to the new argparse module. This was only
introduced in Python 2.7; if you need to run this on an older python,
you'll likely need to flip back to using optparse. In that case,
see <https://docs.python.org/2/library/optparse.html>.
Copyright 2014 Jason Antman <jason@jasonantman.com> <http://www.jasonantman.com>
Free for any use provided that patches are submitted back to me.
CHANGELOG:
2014-12-25 Jason Antman <jason@jasonantman.com>:
- switch to use argparse instead of optparse
- use class instead of module functions
- add some more examples for those new to Python
2014-05-30 Jason Antman <jason@jasonantman.com>:
- remove superfluous, broken line
2014-05-07 Jason Antman <jason@jasonantman.com>:
- initial version of script
"""
import sys
import argparse
import logging
FORMAT = "[%(levelname)s %(filename)s:%(lineno)s - %(funcName)20s() ] %(message)s"
logging.basicConfig(level=logging.ERROR, format=FORMAT)
class SimpleScript:
""" might as well use a class. It'll make things easier later. """
def __init__(self, logger=None, dry_run=False, verbose=0):
""" init method, run at class creation """
# setup a logger; allow an existing one to be passed in to use
self.logger = logger
if logger is None:
self.logger = logging.getLogger(self.__class__.__name__)
if verbose > 1:
self.logger.setLevel(logging.DEBUG)
elif verbose > 0:
self.logger.setLevel(logging.INFO)
self.dry_run = dry_run
def run(self):
""" do stuff here """
self.logger.info("info-level log message")
self.logger.debug("debug-level log message")
self.logger.error("error-level log message")
print("run.")
def parse_args(argv):
"""
parse arguments/options
this uses the new argparse module instead of optparse
see: <https://docs.python.org/2/library/argparse.html>
"""
p = argparse.ArgumentParser(description='Sample python script skeleton.')
p.add_argument('-d', '--dry-run', dest='dry_run', action='store_true', default=False,
help="dry-run - don't actually make any changes")
p.add_argument('-v', '--verbose', dest='verbose', action='count', default=0,
help='verbose output. specify twice for debug-level output.')
args = p.parse_args(argv)
return args
if __name__ == "__main__":
args = parse_args(sys.argv[1:])
script = SimpleScript(dry_run=args.dry_run, verbose=args.verbose)
script.run()