add ee file, update command line args
This commit is contained in:
parent
89c31388d0
commit
155ce6fc1f
143
get_ee_from_region.py
Executable file
143
get_ee_from_region.py
Executable file
@ -0,0 +1,143 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
''' This is a template for creating new python scripts,
|
||||||
|
complete with argument processing and simple I/O '''
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import stat
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import argparse
|
||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
|
class Timerlog:
|
||||||
|
|
||||||
|
def __init__(self, name):
|
||||||
|
self.time_start = 0.0
|
||||||
|
self.time_end = 0.0
|
||||||
|
self.name = name
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
self.time_start = time.perf_counter()
|
||||||
|
return self
|
||||||
|
|
||||||
|
def end(self):
|
||||||
|
self.time_end = time.perf_counter()
|
||||||
|
return self
|
||||||
|
|
||||||
|
def report(self, duration_only=False):
|
||||||
|
if duration_only:
|
||||||
|
return "Timer [{}] duration [{:.5f}]".format(
|
||||||
|
self.name, self.time_end - self.time_start)
|
||||||
|
return "Timer [{}] begin [{:.5f}] end [{:.5f}] duration [{:.5f}]".format(
|
||||||
|
self.name, self.time_start, self.time_end, self.time_end - self.time_start)
|
||||||
|
|
||||||
|
program_name = 'get_ee_from_region.py'
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
prog=program_name,
|
||||||
|
description='get a url from google ee api based on supplied region',
|
||||||
|
epilog='Good luck')
|
||||||
|
|
||||||
|
group = parser.add_mutually_exclusive_group()
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"-l",
|
||||||
|
"--logging",
|
||||||
|
dest='logging',
|
||||||
|
metavar='log_file',
|
||||||
|
required=False,
|
||||||
|
action='store',
|
||||||
|
help="log to file")
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"-v",
|
||||||
|
"--verbose",
|
||||||
|
dest='verbose',
|
||||||
|
required=False,
|
||||||
|
action='store_true',
|
||||||
|
help="verbose mode, log output goes to stderr")
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"-c",
|
||||||
|
"--country",
|
||||||
|
dest='country',
|
||||||
|
metavar='country',
|
||||||
|
default='United States',
|
||||||
|
action='store',
|
||||||
|
help="country to get topo image for")
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"-r",
|
||||||
|
"--region",
|
||||||
|
dest='region',
|
||||||
|
metavar='region',
|
||||||
|
dest='region',
|
||||||
|
metavar='region',
|
||||||
|
default='None',
|
||||||
|
action='store',
|
||||||
|
help="subregion to get, default is to get entire country")
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
|
if args.logging or args.verbose:
|
||||||
|
handlers = []
|
||||||
|
if args.logging:
|
||||||
|
file_handler = logging.FileHandler(filename=args.logging)
|
||||||
|
handlers.append(file_handler)
|
||||||
|
if args.verbose:
|
||||||
|
stderr_handler = logging.StreamHandler(stream=sys.stderr)
|
||||||
|
handlers.append(stderr_handler)
|
||||||
|
logging.basicConfig(
|
||||||
|
encoding='utf-8',
|
||||||
|
handlers=handlers,
|
||||||
|
format='%(asctime)s.%(msecs)03d %(levelname)s {%(module)s} [%(funcName)s] %(message)s',
|
||||||
|
datefmt='%Y-%m-%d,%H:%M:%S',
|
||||||
|
level=logging.DEBUG)
|
||||||
|
if args.logging:
|
||||||
|
logging.debug('logging to log_file requested')
|
||||||
|
if args.verbose:
|
||||||
|
logging.debug('logging to stderr requested')
|
||||||
|
|
||||||
|
if args.dump:
|
||||||
|
print(open(sys.argv[0]).read())
|
||||||
|
exit(0)
|
||||||
|
|
||||||
|
if args.filename:
|
||||||
|
with open(args.filename, "w") as fd:
|
||||||
|
fd.write(open(sys.argv[0]).read())
|
||||||
|
mode = os.fstat(fd.fileno()).st_mode
|
||||||
|
mode |= stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
|
||||||
|
os.fchmod(fd.fileno(), stat.S_IMODE(mode))
|
||||||
|
exit(0)
|
||||||
|
|
||||||
|
if args.binary:
|
||||||
|
args.utf8 = False
|
||||||
|
input_file = sys.stdin.buffer
|
||||||
|
if args.input_file != 'stdin':
|
||||||
|
input_file = open(args.input_file, "rb")
|
||||||
|
|
||||||
|
output_file = sys.stdout.buffer
|
||||||
|
if args.output_file != 'stdout':
|
||||||
|
output_file = open(args.output_file, "wb")
|
||||||
|
|
||||||
|
output_file.write(input_file.read())
|
||||||
|
exit(0)
|
||||||
|
|
||||||
|
if args.utf8:
|
||||||
|
input_file = sys.stdin
|
||||||
|
if args.input_file != 'stdin':
|
||||||
|
input_file = open(args.input_file, "r")
|
||||||
|
|
||||||
|
output_file = sys.stdout
|
||||||
|
if args.output_file != 'stdout':
|
||||||
|
output_file = open(args.output_file, "w")
|
||||||
|
|
||||||
|
try:
|
||||||
|
output_file.write(input_file.read())
|
||||||
|
except UnicodeDecodeError as e:
|
||||||
|
print("Error: input data is not Unicode.\nTry again with the -b option to force binary mode",
|
||||||
|
file=sys.stderr)
|
||||||
|
exit(1)
|
||||||
|
exit(0)
|
||||||
Loading…
x
Reference in New Issue
Block a user