Skip to content

Commit

Permalink
Parse timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
lovef committed Feb 6, 2021
1 parent 522cb53 commit 8de9ca5
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 32 deletions.
11 changes: 11 additions & 0 deletions lovef/io.py
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
38 changes: 21 additions & 17 deletions lovef/parse.py
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')
19 changes: 4 additions & 15 deletions lovef/pretty.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
#!/usr/bin/env python3

import json
import xml.dom.minidom
from xml.etree import ElementTree
import sys
import fileinput
import argparse

from . import io

helpText = """Tries to prettify input
currently supports JSON, XML"""

def main(args=sys.argv[1:]):
args = parseArguments(args)

inputString = readFromClipboard() if args.clipboard \
inputString = io.readFromClipboard() if args.clipboard \
else args.input if args.input \
else readFromStdin()
else io.readFromStdin()

return Pretty(args).prettify(inputString)

Expand All @@ -27,16 +26,6 @@ def parseArguments(args):
parser.add_argument("-e", "--escape", help="Escape non-ascii characters", action="store_true")
return parser.parse_args(args)

def readFromClipboard():
import tkinter
return tkinter.Tk().clipboard_get()

def readFromStdin():
text = ""
for line in fileinput.input():
text = text + line
return text

class Pretty:
args = None

Expand Down
10 changes: 10 additions & 0 deletions tests/parse.py
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()

0 comments on commit 8de9ca5

Please sign in to comment.