Skip to content

Commit

Permalink
More cinfig items
Browse files Browse the repository at this point in the history
Working on #23
  • Loading branch information
Skippern committed Nov 14, 2017
1 parent 70d4f62 commit 73b3931
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
15 changes: 5 additions & 10 deletions gpx/gpx_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,12 @@

__LOG = logging.getLogger("gpx_loader")

# __SERVERS = []
# __SERVERS.append('http://overpass-api.de/api/interpreter')
# __SERVERS.append('http://overpass.osm.rambler.ru/cgi/interpreter')
# __SERVERS.append('http://api.openstreetmap.fr/oapi/interpreter')
# __SERVERS.append('http://overpass.osm.ch/api/interpreter')
config = gpx_utils.load_config()

# -- Server also runs several other services so only for light usage
# __SERVERS.append('http://overpass.openstreetmap.ie/api/')

# __SERVER = random.choice(__SERVERS)
overpass_server = 'http://overpass-api.de/api/interpreter'
try:
overpass_server = config['overpass_server']
except:
overpass_server = 'http://overpass-api.de/api/interpreter'

# Delay between each retry on 'normal' failures.
__OVERPASS_RETRY_DELAY = 10
Expand Down
7 changes: 6 additions & 1 deletion gpx/gpx_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@

import gpx_utils

config = gpx_utils.load_config()

__LOG = logging.getLogger('gpx_uploader')
__UPLOAD_FAILURE_DELAY = 60
__UPLOAD_MAX_TRIES = 10
__TRACK_VISIBILITY = u'public' # public, private, trackable, identifiable
try:
__TRACK_VISIBILITY = gpx_utils.enforce_unicode(config['track_visibility'])
except:
__TRACK_VISIBILITY = u'public' # public, private, trackable, identifiable


def upload_gpx(gpx_file, tags, description):
Expand Down
30 changes: 22 additions & 8 deletions gpx/gpx_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@
import yaml
from shapely.geometry import Point, MultiPoint

config = load_config()

__LOG = logging.getLogger('gpx_utils')
__CODECS = ['ascii', 'iso-8859-2', 'iso-8859-1', 'iso-8859-3', 'iso-8859-4', 'iso-8859-5', 'iso-8859-6', 'iso-8859-7', 'iso-8859-8', 'iso-8859-9', 'iso-8859-10', 'iso-8859-11', 'iso-8859-12', 'iso-8859-13', 'iso-8859-14', 'iso-8859-15', 'iso-8859-16', 'mac-latin2', 'big5', 'cp037', 'cp1006', 'cp1026', 'cp1140', 'cp1250', 'cp1251', 'cp1252', 'cp1253', 'cp1254', 'cp1255', 'cp1256', 'cp1257', 'cp1258', 'cp424', 'cp437', 'cp500', 'cp720', 'cp737', 'cp755', 'cp850', 'cp852', 'cp855', 'cp856', 'cp857', 'cp858', 'cp860', 'cp861', 'cp862', 'cp863', 'cp864', 'cp865', 'cp866', 'cp869', 'cp874', 'cp875', 'cp932', 'cp949', 'cp950', 'euc_jis-2004', 'gb18030', 'gb2312', 'gbk', 'hp-roman8', 'mac_arabic', 'mac_centeuro', 'mac_croatian', 'mac_cyrillic', 'mac_farsi', 'mac_greek', 'mac_iceland', 'mac_roman', 'mac_romanian', 'mac_turkish', 'palmos', 'ptcp154', 'tis_620', 'mbcs', 'utf-8']
__DEBUGGING = False
__NAME_LANGUAGES = ['en', 'no', 'pt']
try:
__NAME_LANGUAGES = config['languages']
except:
__NAME_LANGUAGES = ['en', 'no', 'pt']


class BBox(object):
Expand Down Expand Up @@ -51,15 +56,15 @@ def enforce_unicode(s):
codec = encoding['encoding']
try:
s = s.encode(codec).decode('utf8')
__LOG.debug(u'clean(i) Text identified as %s', codec)
__LOG.debug(u'enforce_unicode(i) Text identified as %s', codec)
except Exception as e:
__LOG.debug(u'clean(i) failed to detect codec: %s' % e.message)
__LOG.debug(u'enforce_unicode(i) failed to detect codec: %s' % e.message)
except:
pass
__LOG.debug(u'clean(i) codec is %s', codec)
__LOG.debug(u'enforce_unicode(i) codec is %s', codec)
if isinstance(s, unicode):
return s
__LOG.debug(u'Need to run down codecList in clean(i)')
__LOG.debug(u'Need to run down codecList in enforce_unicode(i)')
for codec in __CODECS:
try:
s = s.encode(codec).decode('utf8')
Expand Down Expand Up @@ -91,8 +96,8 @@ def swap(a, b):

def remove_duplicates(values):
"""
:param [] values:
:return []: The
:param [] values: list of values
:return []: The same list without duplicated values
"""
output = []
seen = set()
Expand All @@ -107,7 +112,7 @@ def test_object(track, obj):
"""
:param track: The track to test.
:param obj: The polygon object to test against.
:return bool: If the object
:return bool: If the track is within or interesects the object
"""
if obj == None:
return False
Expand Down Expand Up @@ -223,12 +228,21 @@ def get_tags(tags):

return out

def store_config(obj)
config_file = '%s/.gpx_upload.yaml' % os.environ['HOME']
try:
with open(config_file, 'w') as f:
f.write(yaml.dump(obj, Dumper=yaml.Dumper))
except IOError:
pass

def load_config():
obj = {
'cache_dir': '%s/.cache/gpx' % os.environ['HOME'],
'enable_upload': True,
'overpass_server': 'http://overpass-api.de/api/interpreter',
'track_visibility': 'public',
'languages': [ 'en' ],
}
config_file = '%s/.gpx_upload.yaml' % os.environ['HOME']
try:
Expand Down
6 changes: 3 additions & 3 deletions gpx/test_gpx_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

def test_get_name():
assert gpx_utils.get_name({
'name': u'El Nomo',
'name': u'El Nome',
'name:en': u'The Name'
}) == u'The Name', 'Wrong name from english-name'
assert gpx_utils.get_name({
'name': u'El Nomo',
'name': u'El Nome',
'name:no': u'Navnet'
}) == u'El Nomo', 'Wrong name from local-name'
}) == u'El Nome', 'Wrong name from local-name'
try:
gpx_utils.get_name({})
assert False, 'No exception'
Expand Down

0 comments on commit 73b3931

Please sign in to comment.