-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
75 lines (62 loc) · 1.8 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# -*- coding: utf-8 -*-
# * File Name : utils.py
#
# * Copyright (C) 2012 Gaston TJEBBES <[email protected]>
# * Company : Majerti ( http://www.majerti.fr )
#
# This software is distributed under GPLV3
# License: http://www.gnu.org/licenses/gpl-3.0.txt
#
# * Creation Date : 31-07-2013
# * Last Modified :
#
# * Project :
#
"""
Some usefull funcs
"""
import urlparse
import datetime
def read_locale_date(value):
"""
return a date object based on a string expected to be in the local
format : ddmmYYYY
"""
day, month, year = map(int, value.split('-'))
return datetime.date(year, month, day)
def write_locale_date(date_obj):
"""
Return the date object as a string in local format
"""
return date_obj.strftime('%d-%m-%Y')
def get_base_url(url):
"""
Return scheme + location
"""
result = urlparse.urlsplit(url)
scheme = result.scheme or 'http'
return "{0}://{1}/".format(scheme, result.netloc)
def get_action_path_and_method(expense_dict):
"""
Return the path and the method to be used to synchronize this expense
"""
if expense_dict['todo'] == 'add':
method = 'POST'
path = 'expenses'
elif expense_dict['todo'] == 'update':
method = 'PUT'
path = 'expenses/{id}'.format(id=expense_dict['id'])
elif expense_dict['todo'] == 'delete':
method = 'DELETE'
exp_id = expense_dict.get('id', '00')
path = 'expenses/{id}'.format(id=exp_id)
return path, method
def filter_expenses(expense, expenses, keys):
for e in expenses:
f = lambda x: cmp_attribute(x, expense, e)
if all(map(f, keys)):
yield {k: e.get(k) for k in keys}
def cmp_attribute(attr, e1, e2):
if not attr in e1:
return True
return e1[attr] == e2.get(attr)