-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutility.py
58 lines (49 loc) · 1.42 KB
/
utility.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
from datetime import datetime
import xml.etree.ElementTree as ET
import re
import os
def timestamp_to_data_time(timestamp):
if not timestamp:
raise ValueError("timestamp must not be empty")
elif type(timestamp).__name__ != 'int':
raise TypeError("timestamp must be integer")
date_time_obj = datetime.fromtimestamp(timestamp)
date_time = date_time_obj.strftime("%I:%M %p, %d %b %Y")
return date_time
def parse_xml(xml):
if not xml:
raise ValueError("input xml can't be empty")
elif type(xml).__name__ != 'str':
raise TypeError("input xml must be string")
paste_list = []
xml = '<Data>' + xml + '</Data>'
root = ET.fromstring(xml)
for paste in root:
paste_item = {}
for item in paste:
paste_item[item.tag] = item.text
paste_list.append(paste_item)
return paste_list
def sort_on(item):
return item['paste_date']
def get_key_from_url(url,pattern):
if not url or not pattern:
raise ValueError("Url and pattern can't be empty")
elif type(url).__name__ != 'str' or type(pattern).__name__ != 'str':
raise ValueError("Url and pattern both should be string")
m = re.search(pattern,url)
if m:
x = re.split(pattern,url,1)
key = x[-1]
if "/" in key:
return (False,None)
else:
return (True,key)
else:
return (False,None)
def get_file_path(file_name):
if not file_name:
return
dir_path = os.path.dirname(os.path.realpath(__file__))
file_path = os.path.join(dir_path,file_name)
return file_path