parse args
This commit is contained in:
parent
aaca101d1f
commit
ba1090a91d
@ -1,6 +1,7 @@
|
|||||||
import argparse
|
import argparse
|
||||||
import json
|
import json
|
||||||
import locale
|
import locale
|
||||||
|
import logging
|
||||||
import re
|
import re
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
@ -241,7 +242,7 @@ def resolve_query_type(query):
|
|||||||
solutions = tokenize(query)
|
solutions = tokenize(query)
|
||||||
if not solutions:
|
if not solutions:
|
||||||
dt = get_local_now()
|
dt = get_local_now()
|
||||||
return [["now", dt, QUERY_TYPE_DT, h_default]]
|
return [["now", dt, QUERY_TYPE_DT, h_default_dt]]
|
||||||
return solutions
|
return solutions
|
||||||
|
|
||||||
|
|
||||||
@ -304,14 +305,25 @@ def test():
|
|||||||
|
|
||||||
def parse_args():
|
def parse_args():
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('query', nargs='*', default="now", help="freeform")
|
parser.add_argument('query', nargs='*', default="", help="freeform")
|
||||||
parser.add_argument('--locale', dest='locale')
|
parser.add_argument('--locale', dest='locale')
|
||||||
parser.add_argument('--handlers', dest='handlers', action='store_true')
|
parser.add_argument('--handlers', dest='handlers', action='store_true')
|
||||||
parser.add_argument('--full', dest='full', action='store_true')
|
parser.add_argument('--full', dest='full', action='store_true')
|
||||||
|
parser.add_argument('--debug', dest='debug', action='store_true')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
return args
|
return args
|
||||||
|
|
||||||
|
|
||||||
|
logging.basicConfig()
|
||||||
|
logger = logging.getLogger()
|
||||||
|
|
||||||
|
|
||||||
|
def setup_logging_level(debug=False):
|
||||||
|
log_level = logging.DEBUG if debug else logging.ERROR
|
||||||
|
logger.setLevel(log_level)
|
||||||
|
logger.debug("Debugging enabled")
|
||||||
|
|
||||||
|
|
||||||
def main(args):
|
def main(args):
|
||||||
global custom_locale
|
global custom_locale
|
||||||
custom_locale = resolve_locale(args.locale)
|
custom_locale = resolve_locale(args.locale)
|
||||||
@ -325,4 +337,5 @@ def main(args):
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
args = parse_args()
|
args = parse_args()
|
||||||
|
setup_logging_level(args.debug)
|
||||||
main(args)
|
main(args)
|
||||||
|
@ -3,26 +3,31 @@
|
|||||||
Find time now, in the past or future in any timezone or location.
|
Find time now, in the past or future in any timezone or location.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import argparse
|
|
||||||
import csv
|
import csv
|
||||||
import datetime
|
import datetime
|
||||||
import logging
|
|
||||||
import os
|
import os
|
||||||
|
import pathlib
|
||||||
|
import random
|
||||||
import re
|
import re
|
||||||
|
import string
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
from heapq import heappush, heappop
|
||||||
|
|
||||||
import dateparser
|
import dateparser
|
||||||
from datetime import datetime, timedelta
|
|
||||||
|
|
||||||
import pytz
|
import pytz
|
||||||
from dateparser import parse as parse_dt
|
from dateparser import parse as parse_dt
|
||||||
|
from dateparser.timezone_parser import StaticTzInfo
|
||||||
from datetimerange import DateTimeRange
|
from datetimerange import DateTimeRange
|
||||||
from dateutil.parser import parse as dutil_parse
|
from dateutil.parser import parse as dutil_parse
|
||||||
from dateparser.timezone_parser import StaticTzInfo
|
|
||||||
from dateutil.tz import gettz, tzlocal
|
from dateutil.tz import gettz, tzlocal
|
||||||
from fuzzywuzzy import fuzz
|
from fuzzywuzzy import fuzz
|
||||||
|
from geopy import Nominatim
|
||||||
|
from geopy.exc import GeocoderTimedOut
|
||||||
from pytz import timezone
|
from pytz import timezone
|
||||||
from pytz.exceptions import UnknownTimeZoneError
|
from pytz.exceptions import UnknownTimeZoneError
|
||||||
|
from timezonefinder import TimezoneFinder
|
||||||
|
from word2number import w2n
|
||||||
|
|
||||||
FUZZ_THRESHOLD = 70
|
FUZZ_THRESHOLD = 70
|
||||||
ISO_FORMAT = '%Y-%m-%dT%H:%M:%S%z'
|
ISO_FORMAT = '%Y-%m-%dT%H:%M:%S%z'
|
||||||
@ -30,25 +35,6 @@ DEFAULT_FORMAT = '%Y-%m-%d %H:%M:%S%z'
|
|||||||
|
|
||||||
basepath = os.path.dirname(os.path.abspath(__file__))
|
basepath = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
|
||||||
logging.basicConfig()
|
|
||||||
logger = logging.getLogger()
|
|
||||||
|
|
||||||
|
|
||||||
def parse_args():
|
|
||||||
parser = argparse.ArgumentParser()
|
|
||||||
parser.add_argument('query', nargs='*', default="now", help="<datetime-like> to <timezone-like or location string>")
|
|
||||||
parser.add_argument('--format', dest='format', default=DEFAULT_FORMAT)
|
|
||||||
parser.add_argument('--iso', dest='iso', action='store_true')
|
|
||||||
parser.add_argument('--debug', dest='debug', action='store_true')
|
|
||||||
args = parser.parse_args()
|
|
||||||
return args
|
|
||||||
|
|
||||||
|
|
||||||
def setup_logging_level(debug=False):
|
|
||||||
log_level = logging.DEBUG if debug else logging.ERROR
|
|
||||||
logger.setLevel(log_level)
|
|
||||||
logger.debug("Debugging enabled")
|
|
||||||
|
|
||||||
|
|
||||||
class Location:
|
class Location:
|
||||||
"""
|
"""
|
||||||
@ -72,7 +58,6 @@ def normalize_words_to_number(query):
|
|||||||
Converts queries like "in one hour" -> "in 1 hour"
|
Converts queries like "in one hour" -> "in 1 hour"
|
||||||
Assumes one-word numbers used
|
Assumes one-word numbers used
|
||||||
"""
|
"""
|
||||||
from word2number import w2n
|
|
||||||
|
|
||||||
normal_list = []
|
normal_list = []
|
||||||
|
|
||||||
@ -128,7 +113,6 @@ def create_if_not_exists(fname):
|
|||||||
try:
|
try:
|
||||||
fh = open(fname, 'r')
|
fh = open(fname, 'r')
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
import pathlib
|
|
||||||
|
|
||||||
path = pathlib.Path(fname)
|
path = pathlib.Path(fname)
|
||||||
path.parent.mkdir(parents=True, exist_ok=True)
|
path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
@ -157,8 +141,6 @@ def resolve_location_local(query):
|
|||||||
"""
|
"""
|
||||||
Find a location by searching in local db of countries and cities
|
Find a location by searching in local db of countries and cities
|
||||||
"""
|
"""
|
||||||
from heapq import heappush, heappop
|
|
||||||
|
|
||||||
query = query.lower()
|
query = query.lower()
|
||||||
create_if_not_exists(os.path.join(basepath, "data", ".cache.csv"))
|
create_if_not_exists(os.path.join(basepath, "data", ".cache.csv"))
|
||||||
|
|
||||||
@ -192,12 +174,6 @@ def resolve_location_local(query):
|
|||||||
|
|
||||||
|
|
||||||
def resolve_location_remote(query):
|
def resolve_location_remote(query):
|
||||||
import random
|
|
||||||
import string
|
|
||||||
|
|
||||||
from geopy.geocoders import Nominatim
|
|
||||||
from geopy.exc import GeocoderTimedOut
|
|
||||||
|
|
||||||
user_agent = ''.join(random.choices(string.ascii_uppercase + string.digits, k=20))
|
user_agent = ''.join(random.choices(string.ascii_uppercase + string.digits, k=20))
|
||||||
geolocator = Nominatim(user_agent=user_agent)
|
geolocator = Nominatim(user_agent=user_agent)
|
||||||
try:
|
try:
|
||||||
@ -294,7 +270,6 @@ def resolve_timezone(query):
|
|||||||
try:
|
try:
|
||||||
pytz_result = timezone(normal_tz)
|
pytz_result = timezone(normal_tz)
|
||||||
except UnknownTimeZoneError:
|
except UnknownTimeZoneError:
|
||||||
from timezonefinder import TimezoneFinder
|
|
||||||
logger.debug("No timezone: {}".format(normal_tz))
|
logger.debug("No timezone: {}".format(normal_tz))
|
||||||
# if the human_tz_loc contains /, assume it's a timezone
|
# if the human_tz_loc contains /, assume it's a timezone
|
||||||
# the timezone could still be guessed badly, attempt to get the city
|
# the timezone could still be guessed badly, attempt to get the city
|
||||||
@ -455,11 +430,6 @@ def time_ago(date=None, diff=None):
|
|||||||
hours, minutes, seconds))
|
hours, minutes, seconds))
|
||||||
|
|
||||||
|
|
||||||
def query_to_dt(query):
|
|
||||||
human_dt, human_tz_loc = parse_query(query)
|
|
||||||
return solve_query(human_dt, human_tz_loc)
|
|
||||||
|
|
||||||
|
|
||||||
def tzinfo_from_offset(offset: str) -> pytz.timezone:
|
def tzinfo_from_offset(offset: str) -> pytz.timezone:
|
||||||
if ':' in offset:
|
if ':' in offset:
|
||||||
offset = ''.join(offset.split(':'))
|
offset = ''.join(offset.split(':'))
|
||||||
|
Loading…
Reference in New Issue
Block a user