Skip to content
This repository has been archived by the owner on Apr 11, 2019. It is now read-only.

Commit

Permalink
added some utils
Browse files Browse the repository at this point in the history
  • Loading branch information
bcho committed Oct 21, 2013
1 parent bf81fa8 commit 7b1e903
Showing 1 changed file with 69 additions and 5 deletions.
74 changes: 69 additions & 5 deletions server/utils.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
#coding: utf-8

import time
from functools import wraps
from contextlib import contextmanager
from logging.handlers import SMTPHandler as _SMTPHandler

from flask import make_response, request
from flask import jsonify as _jsonify

import pyisbn
from isbn_hyphenate import isbn_hyphenate

from server.db import db


@contextmanager
def ignores(*exceptions):
try:
yield
except exceptions:
pass


class SMTPHandler(_SMTPHandler):
'''SMTP logging handler adapter'''

Expand Down Expand Up @@ -45,14 +58,65 @@ def allow_CORS(*args, **kwargs):


def jsonify(**kwargs):
def dictify(model):
return model.__dictify__

for k, v in kwargs.items():
if isinstance(v, db.Model):
try:
kwargs[k] = v.__dictify__
except AttributeError:
pass
with ignores(AttributeError):
if isinstance(v, db.Model):
kwargs[k] = dictify(v)
elif isinstance(v, list):
kwargs[k] = [dictify(i) for i in v]

return _jsonify(**kwargs)


def error(msg, status_code=500, *args, **kwargs):
return jsonify(error=msg, *args, **kwargs), status_code


class LogDuration(object):

def __init__(self, log, start, end, log_duration=True):
self.log = log
self.start = start or ''
self.end = end or ''
self.log_duration = log_duration

def __call__(self, func):
@wraps(func)
def wrapper(*args, **kwargs):
start = time.time()
self.log(self.start)

resp = func(*args, **kwargs)

if self.log_duration:
self.log(self.end % (time.time() - start))
else:
self.log(self.end)
return resp
return wrapper


def parse_isbn(raw):
'''将 isbn 转换成 10 / 13 位以及带 hyphen 形式'''

a, b = raw, raw
isbn = {
'isbn10': raw,
'isbn13': raw,
'isbn10-hyphen': raw,
'isbn13-hyphen': raw
}

with ignores(pyisbn.IsbnError):
a = pyisbn.convert(raw)
b = pyisbn.convert(a)
isbn = {'isbn%d' % len(i): i for i in [a, b]}

with ignores(isbn_hyphenate.IsbnMalformedError):
isbn['isbn10-hyphen'] = isbn_hyphenate.hyphenate(isbn['isbn10'])
isbn['isbn13-hyphen'] = isbn_hyphenate.hyphenate(isbn['isbn13'])

return isbn

0 comments on commit 7b1e903

Please sign in to comment.