-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
46 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import fileinput | ||
|
||
def readFromClipboard(): | ||
import tkinter | ||
return tkinter.Tk().clipboard_get() | ||
|
||
def readFromStdin(): | ||
text = "" | ||
for line in fileinput.input(): | ||
text = text + line | ||
return text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,49 @@ | ||
import sys | ||
import fileinput | ||
import argparse | ||
import base64 | ||
import datetime | ||
|
||
from . import io | ||
|
||
helpText = """Tries to parse input | ||
currently supports base64""" | ||
|
||
|
||
def main(): | ||
args = parseArguments() | ||
|
||
inputString = readFromClipboard() if args.clipboard \ | ||
inputString = io.readFromClipboard() if args.clipboard \ | ||
else args.input if args.input \ | ||
else readFromStdin() | ||
else io.readFromStdin() | ||
|
||
print(parse(inputString)) | ||
|
||
|
||
def parseArguments(): | ||
parser = argparse.ArgumentParser(description = helpText, formatter_class=argparse.RawTextHelpFormatter) | ||
parser = argparse.ArgumentParser(description=helpText, formatter_class=argparse.RawTextHelpFormatter) | ||
parser.add_argument("input", nargs='?', help="Input to parse") | ||
parser.add_argument("-c", "--clipboard", help="Take input from clipboard", | ||
action="store_true") | ||
parser.add_argument("-c", "--clipboard", help="Take input from clipboard", action="store_true") | ||
return parser.parse_args() | ||
|
||
def readFromClipboard(): | ||
import tkinter | ||
return tkinter.Tk().clipboard_get() | ||
|
||
def readFromStdin(): | ||
text = "" | ||
for line in fileinput.input(): | ||
text = text + line | ||
return text | ||
|
||
def parse(inputString): | ||
errors = "" | ||
try: | ||
return str(datetime.datetime.fromtimestamp(int(inputString))) | ||
except Exception as e: | ||
errors += "Failed to parse date time from seconds " + str(e) + "\n" | ||
try: | ||
return datetime.datetime.fromtimestamp(int(inputString)/1000) \ | ||
.isoformat(sep=' ', timespec='milliseconds') | ||
except Exception as e: | ||
errors += "Failed to parse date time from miliseconds " + str(e) + "\n" | ||
try: | ||
return parseBase64(inputString) | ||
except Exception as e: | ||
errors = errors + "Failed to parse Base64 " + str(e) + "\n" | ||
print(errors, end = "") | ||
errors += "Failed to parse Base64 " + str(e) + "\n" | ||
print(errors, end="") | ||
|
||
|
||
def parseBase64(inputString): | ||
return base64.b64decode(inputString).decode('utf-8') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,21 @@ | ||
import lovef.parse | ||
import unittest | ||
|
||
|
||
class Tests(unittest.TestCase): | ||
|
||
def test_parse_base64(self): | ||
self.assertEqual(lovef.parse.parse("ZXhhbXBsZQ=="), "example") | ||
self.assertEqual(lovef.parse.parse("w6XDpMO2"), "åäö") | ||
|
||
def test_parse_seconds_since_epoch(self): | ||
self.assertEqual(lovef.parse.parse( | ||
"1612625669"), "2021-02-06 16:34:29") | ||
|
||
def test_parse_milliseconds_since_epoch(self): | ||
self.assertEqual(lovef.parse.parse("1612625669123"), | ||
"2021-02-06 16:34:29.123") | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |