-
Notifications
You must be signed in to change notification settings - Fork 12
/
utils.py
49 lines (33 loc) · 1.03 KB
/
utils.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
#! /usr/bin/python2.7
# -*- coding: utf-8 -*-
from datetime import datetime
import json
import os
get_today = lambda : datetime.now()
get_today_str = lambda: get_today().strftime('%Y-%m-%d %H:%M')
def checkdir(directory):
if not os.path.exists(directory):
os.makedirs(directory)
print('Created %s' % directory)
def rcheckdir(sftp, remotedir):
try:
sftp.chdir(remotedir)
except IOError:
dirname, basename = os.path.split(remotedir.rstrip('/'))
rcheckdir(sftp, dirname)
sftp.mkdir(basename)
sftp.chdir(basename)
def file_read(filename):
json_data = open(filename)
data = json.load(json_data)
return data
def get_version():
with open('version.cfg', 'r') as f:
return f.read().strip()
def parse_datetime(string, form='%Y.%m.%d. %H:%M'):
return datetime.strptime(string, form)
def format_datetime(dt, form='%Y-%m-%d %H:%M'):
return dt.strftime(form)
def write_json(data, filename):
with open(filename, 'w') as f:
json.dump(data, f)