-
Notifications
You must be signed in to change notification settings - Fork 22
/
__init__.py
102 lines (77 loc) · 2.73 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import sys
import os
import math
import requests
import json
from datetime import datetime, timedelta
from .parser import ParserWindow # noqa: F401
def get_version():
version = None
try:
r = requests.get('http://nparse.nomns.com/info/version')
version = json.loads(r.text)['version']
except:
pass
return version
def parse_line(line):
"""
Parses and then returns an everquest log entry's date and text.
"""
index = line.find("]") + 1
sdate = line[1:index - 1].strip()
text = line[index:].strip()
return datetime.strptime(sdate, '%a %b %d %H:%M:%S %Y'), text
def strip_timestamp(line):
"""
Strings EQ Timestamp from log entry.
"""
return line[line.find("]") + 1:].strip()
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS # pylint: disable=E1101
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
def to_range(number, min_number, max_number):
""" Returns number of within min/max, else min/max. """
return min(max_number, max(min_number, number))
def within_range(number, min_number, max_number):
""" Returns true/false if number is within min/max. """
return (number >= min_number and number <= max_number)
def to_real_xy(x, y):
""" Convert Everquest 'x, y' to standard 'x, y'. """
return -y, -x
def to_eq_xy(x, y):
""" Convert standard x, y to Everquest x, y. """
return -y, -x
def get_degrees_from_line(x1, y1, x2, y2):
return -math.degrees(math.atan2((x2 - x1), (y2 - y1)))
def format_time(time_delta):
"""Returns a string from a timedelta '#d #h #m #s', but only 's' if d, h, m are all 0."""
time_string = ''
days = time_delta.days
hours, remainder = divmod(time_delta.seconds, 3600)
minutes, seconds = divmod(remainder, 60)
if sum([days, hours, minutes]):
time_string += '{}d'.format(days) if days else ''
time_string += '{}h'.format(hours) if hours else ''
time_string += '{}m'.format(minutes) if minutes else ''
time_string += '{}s'.format(seconds) if seconds else ''
return time_string
else:
return str(seconds)
def text_time_to_seconds(text_time):
""" Returns string 'hh:mm:ss' -> seconds """
parts = text_time.split(':')
seconds, minutes, hours = 0, 0, 0
try:
seconds = int(parts[-1])
minutes = int(parts[-2])
hours = int(parts[-3])
except IndexError:
pass
except ValueError:
return
return timedelta(hours=hours, minutes=minutes, seconds=seconds).total_seconds()