forked from william10240/py_fanhao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBase.py
129 lines (106 loc) · 3.93 KB
/
Base.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import configparser
import json as sjson
import os,shutil,logging
from aiohttp import web
#from smb.SMBConnection import SMBConnection
__author__ = 'SunCoder'
APP_PATH = os.path.dirname(os.path.abspath(__file__))
PHOTO_PATH = os.path.join(APP_PATH, 'photos')
STATIC_PATH = os.path.join(APP_PATH, 'static')
CONFIGBASE = os.path.join(APP_PATH, 'conf.d/config.ini.base')
CONFIG = os.path.join(APP_PATH, 'conf.d/config.ini')
"""
读取配置文件信息
"""
def getconfig(sector,item):
if not os.path.exists(CONFIG):
shutil.copyfile(CONFIGBASE, CONFIG)
logging.info("con`t find conf.ini, use the default config")
cf = configparser.ConfigParser()
cf.read(CONFIG, encoding='utf8') # 注意ini配置文件的路径
value = cf.get(sector, item)
return value
def json(code, msg='', data='', callback=None):
json_data = {
'code': code,
'msg': msg,
'data': data
}
return json_data
def jsonres(code, msg='', data='', callback=None):
json_data = {
'code': code,
'msg': msg,
'data': data
}
jsonstr = sjson.dumps(json_data)
return web.Response(body=bytes(jsonstr, encoding="utf-8"),content_type='text/html')
class Pager():
url = ''
total = 0
index = 1
size = 15
pages = []
def __init__(self, total, index, size):
'''
分页函数
:param count: 总数
:param index: 页码
:param size: 页容量
:return: object paging
'''
self.total = total
self.index = index
self.size = size
self.count, tail = divmod(total, size)
if tail is not 0:
self.count += 1
def render(self):
self.pages.clear()
self.pages.append('<nav><ul class="pagination">')
rag = range(0, self.count)
if self.count > 10:
if self.index <= 5:
rag = rag[0: 10]
elif self.index > 5:
if self.count - self.index < 5:
rag = rag[-10:]
else:
rag = rag[self.index - 5:self.index + 5]
if self.index == 1:
self.pages.append('<li class="disabled"><span>第1页</a></li>')
self.pages.append('<li class="disabled"><span>«««</a></li>')
else:
self.pages.append('<li><a href="javascript:goPage(%s);">第1页</a></li>' % 1)
self.pages.append('<li><a href="javascript:goPage(%s);">«««</a></li>' % (self.index - 1))
for i in rag:
v = i + 1
if self.index == v:
self.pages.append('<li class="active"><span>%s</span></li>' % (v if v > 9 else ('0%s' % v)))
else:
self.pages.append('<li><a href="javascript:goPage(%s);">%s</a></li>' % (v, (v if v > 9 else ('0%s' % v))))
if self.index == self.count:
self.pages.append('<li class="disabled"><span>»»»</span></li>')
self.pages.append('<li class="disabled"><span>第%s页</span></li>' % self.count)
else:
self.pages.append('<li><a href="javascript:goPage(%s);">»»»</a></li>' % (self.index + 1))
self.pages.append('<li><a href="javascript:goPage(%s);">第%s页</a></li>' % (self.count, self.count))
self.pages.append('<li><span>共%s个</span></li></ul></nav>' % self.total)
return "".join(self.pages)
def __str__(self):
return self.render()
if __name__ == '__main__':
# print(Pager(202, 10, 4))
# print(Pager(202, 2, 4))
# print(Pager(202, 50, 4))
# print(Pager(10, 2, 3))
# print(Pager(10, 3, 3))
print(os.path.join(APP_PATH,'config.ini'))
print(getconfig('db', 'host'))
print(getconfig('db', 'user'))
print(getconfig('db', 'passwd'))
print(getconfig('db', 'database'))
print(getconfig('db', 'charset'))
print(getconfig('db', 'port'))