diff --git a/tz.py b/tz.py index c04a1fc..b03a0f7 100644 --- a/tz.py +++ b/tz.py @@ -1,6 +1,6 @@ """ """ - +import os import argparse import logging import sys @@ -11,6 +11,8 @@ from pytz.exceptions import UnknownTimeZoneError FUZZ_THRESHOLD = 70 DEFAULT_FORMAT = '%Y-%m-%d %H:%M:%S%z' +basepath = os.path.dirname(os.path.abspath(__file__)) + logging.basicConfig() logger = logging.getLogger() @@ -90,11 +92,10 @@ def create_if_not_exists(fname): def write_to_cache(query, location): - import os import csv logger.debug("Writing location to cache") - with open(os.path.join("data",".cache.csv"), 'a+') as wf: + with open(os.path.join(basepath, "data",".cache.csv"), 'a+') as wf: cachewriter = csv.writer(wf) cachewriter.writerow([query, location.latitude, @@ -113,19 +114,18 @@ def resolve_location_local(query): """ Find a location by searching in local db of countries and cities """ - import os import csv from heapq import heappush, heappop from fuzzywuzzy import fuzz query = query.lower() - create_if_not_exists(os.path.join("data",".cache.csv")) + create_if_not_exists(os.path.join(basepath, "data",".cache.csv")) # location hypothesis heap heap = [] for fname in [".cache", "countries", "cities"]: - with open(os.path.join("data", "{}.csv".format(fname))) as f: + with open(os.path.join(basepath, "data", "{}.csv".format(fname))) as f: cfile = csv.reader(f) for row in cfile: entry = row[0]