Config file settings

Google API key and OpenStreetMap Email can be written to a config file
This commit is contained in:
2018-03-01 10:10:47 +09:00
parent cf06ef35eb
commit e28659fd88
2 changed files with 62 additions and 3 deletions

View File

@@ -25,8 +25,8 @@ reverse_geolocate.py [-h] -x
[XMP SOURCE FOLDER [XMP SOURCE FOLDER ...]] [XMP SOURCE FOLDER [XMP SOURCE FOLDER ...]]
[-l LIGHTROOM FOLDER] [-s] [-l LIGHTROOM FOLDER] [-s]
[-f <overwrite, location, city, state, country, countrycode>] [-f <overwrite, location, city, state, country, countrycode>]
[-g GOOGLE API KEY] [-o] [-e EMIL ADDRESS] [-n] [-g GOOGLE API KEY] [-o] [-e EMIL ADDRESS] [-w]
[-v] [--debug] [--test] [-n] [-v] [--debug] [--test]
### Arguments ### Arguments
@@ -40,6 +40,7 @@ Argument | Argument Value | Description
-o, --openstreetmap | | Use OpenStreetMap instead of the default google maps -o, --openstreetmap | | Use OpenStreetMap instead of the default google maps
-e, --email | email address | For OpenStreetMap with a large number of access -e, --email | email address | For OpenStreetMap with a large number of access
-g, --google | Google Maps API Key | If available, to avoid the access limitations to the reverse location lookup -g, --google | Google Maps API Key | If available, to avoid the access limitations to the reverse location lookup
-w, --write-settings | | Write the Google API key or the OpenStreetMap email into the settings file
-v, --verbose | | More verbose output. Currently not used -v, --verbose | | More verbose output. Currently not used
--debug | | Full detailed debug output. Will print out alot of data --debug | | Full detailed debug output. Will print out alot of data
--test | | Does not write any changed back to the XMP sidecar file. For testing purposes --test | | Does not write any changed back to the XMP sidecar file. For testing purposes
@@ -62,6 +63,18 @@ reverse_geolocate.py -x Photos/2017/01/Event-01/some_photo.xmp -f location
Only works on *some_photo.xmp* file and will only set the *location* field if it is not yet set. Only works on *some_photo.xmp* file and will only set the *location* field if it is not yet set.
### Config File
The Google Maps API key and the OpenStreetMap Email address can be written to a config file with the -w argument. The config file is located in $HOME/.config/reverseGeolocate/reverse_geolocate.cfg in the following format
```
[API]
googleapikey = <google api key>
openstreetmapemail = <email>
```
if no -g or -e flag is given the keys are read from the config file. If the -g or -e parameter is given it will override the one found in the config file. A new parameter can be written to this config file with -w parameter.
### Google data priority ### Google data priority
Based in the JSON return data the following fields are set in order. If one can not be found for a target set, the next one below is used Based in the JSON return data the following fields are set in order. If one can not be found for a target set, the next one below is used

View File

@@ -16,6 +16,7 @@ from libxmp import XMPMeta, XMPError, consts
import sqlite3 import sqlite3
import requests import requests
from shutil import copyfile from shutil import copyfile
import configparser
############################################################## ##############################################################
### FUNCTIONS ### FUNCTIONS
@@ -396,6 +397,13 @@ parser.add_argument('-e', '--email',
help = 'An email address for OpenStreetMap' help = 'An email address for OpenStreetMap'
) )
# write api/email settings to config file
parser.add_argument('-w', '--write-seettings',
dest = 'config_write',
action = 'store_true',
help = 'Write Google API or OpenStreetMap email to config file'
)
# Do not create backup files # Do not create backup files
parser.add_argument('-n', '--nobackup', parser.add_argument('-n', '--nobackup',
dest = 'no_xmp_backup', dest = 'no_xmp_backup',
@@ -446,13 +454,51 @@ if args.email and not args.use_openstreetmap:
error = True error = True
# if email and not basic valid email (@ .) # if email and not basic valid email (@ .)
if args.email: if args.email:
if re.match('^.+@.+\..+$', args.email): if not re.match('^.+@.+\.[A-Za-z]{1,}$', args.email):
print("Not a valid email for OpenStreetMap: {}".format(args.email)) print("Not a valid email for OpenStreetMap: {}".format(args.email))
error = True error = True
# on error exit here # on error exit here
if error: if error:
sys.exit(1) sys.exit(1)
config = configparser.ConfigParser()
# try to find config file in following order
# $HOME/.config/
config_file = 'reverse_geolocate.cfg'
config_folder = os.path.expanduser('~/.config/reverseGeolocate/')
config_data = '{}{}'.format(config_folder, config_file)
# if file exists read, if not skip unless we have write flag and google api or openstreetmaps email
if os.path.isfile(config_data):
config.read(config_data)
# check if api group & setting is there. also never overwrite argument given data
if 'API' in config:
if 'googleapikey' in config['API']:
if not args.google_api_key:
args.google_api_key = config['API']['googleapikey']
if 'openstreetmapemail' in config['API']:
if not args.email:
args.email = config['API']['openstreetmapemail']
# write data if exists and changed
if args.config_write and (args.google_api_key or args.email):
config_change = False
# check if new value differs, if yes, change and write
if 'API' not in config:
config['API'] = {}
if args.google_api_key and ('googleapikey' not in config['API'] or config['API']['googleapikey'] != args.google_api_key):
config['API']['googleapikey'] = args.google_api_key
config_change = True
if args.email and ('openstreetmapemail' not in config['API'] or config['API']['openstreetmapemail'] != args.email):
config['API']['openstreetmapemail'] = args.email
config_change = True
if config_change:
# if we do not have the base folder create that first
if not os.path.exists(config_folder):
os.makedirs(config_folder)
with open(config_data, 'w') as fptr:
config.write(fptr)
if args.debug:
print("### OVERRIDE API: G: {}, O: {}".format(args.google_api_key, args.email))
# The XMP fields const lookup values # The XMP fields const lookup values
# XML/XMP # XML/XMP
# READ: # READ: