-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.py
59 lines (46 loc) · 1.78 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
50
51
52
53
54
55
56
57
58
59
#! /usr/bin/python2.7
# -*- coding: utf-8 -*-
from glob import glob
import json
import os
from flask import Markup
import markdown
from settings import APP_STATIC, APP_TEMPLATES
ALLOWED_EXTENSIONS=set(['pptx','ppt','pdf'])
get_data_path = lambda f: os.path.join(APP_STATIC, 'data', f)
def get_notice(id):
notice_file = glob(os.path.join(APP_TEMPLATES, 'notices', '%s-*.md' % id))
if len(notice_file)==1:
with open(notice_file[0], 'r') as f:
return {'id': id,
'title': notice_file[0].split('-', 1)[-1].rsplit('.', 1)[0],
'content': Markup(markdown.markdown(f.read().decode('utf-8')))}
else:
raise Exception("Are all file IDs unique?")
def read_csv_data(filename, sep=','):
with open(get_data_path(filename), 'r') as f:
return [row.decode('utf-8').split(sep)\
for row in f.read().splitlines()]
def read_json_data(filename):
with open(get_data_path(filename), 'r') as f:
return json.load(f, encoding='utf-8')
def read_txt_data(filename):
with open(get_data_path(filename), 'r') as f:
return f.read().decode('utf-8').splitlines()
def read_seminar_data():
files = glob(os.path.join(APP_STATIC,'seminar', '*'))
files = [os.path.basename(file) for file in files]
files = [file.decode('euc-kr') for file in files]
return files
def allowed_file(filename):
return '.'in filename and filename.rsplit('.',1)[1] in ALLOWED_EXTENSIONS
def byteify(input):
if isinstance(input, dict):
return {byteify(key): byteify(value)
for key, value in input.iteritems()}
elif isinstance(input, list):
return [byteify(element) for element in input]
elif isinstance(input, unicode):
return input.encode('utf-8')
else:
return input