Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

877 rader
22 KiB

  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
  2. # MIT License. See license.txt
  3. # util __init__.py
  4. from __future__ import unicode_literals
  5. import webnotes
  6. user_time_zone = None
  7. user_format = None
  8. current_date = None
  9. no_value_fields = ['Section Break', 'Column Break', 'HTML', 'Table', 'FlexTable',
  10. 'Button', 'Image', 'Graph']
  11. default_fields = ['doctype', 'name', 'owner', 'creation', 'modified', 'modified_by',
  12. 'parent', 'parentfield', 'parenttype', 'idx', 'docstatus']
  13. # used in import_docs.py
  14. # TODO: deprecate it
  15. def getCSVelement(v):
  16. """
  17. Returns the CSV value of `v`, For example:
  18. * apple becomes "apple"
  19. * hi"there becomes "hi""there"
  20. """
  21. v = cstr(v)
  22. if not v: return ''
  23. if (',' in v) or ('\n' in v) or ('"' in v):
  24. if '"' in v: v = v.replace('"', '""')
  25. return '"'+v+'"'
  26. else: return v or ''
  27. def get_fullname(profile):
  28. """get the full name (first name + last name) of the user from Profile"""
  29. p = webnotes.conn.sql("""select first_name, last_name from `tabProfile`
  30. where name=%s""", profile, as_dict=1)
  31. if p:
  32. profile = " ".join(filter(None,
  33. [p[0].get('first_name'), p[0].get('last_name')])) or profile
  34. return profile
  35. def get_formatted_email(user):
  36. """get email id of user formatted as: John Doe <johndoe@example.com>"""
  37. if user == "Administrator":
  38. return user
  39. from email.utils import formataddr
  40. fullname = get_fullname(user)
  41. return formataddr((fullname, user))
  42. def extract_email_id(email):
  43. """fetch only the email part of the email id"""
  44. from email.utils import parseaddr
  45. fullname, email_id = parseaddr(email)
  46. if isinstance(email_id, basestring) and not isinstance(email_id, unicode):
  47. email_id = email_id.decode("utf-8", "ignore")
  48. return email_id
  49. def validate_email_add(email_str):
  50. """Validates the email string"""
  51. email = extract_email_id(email_str)
  52. import re
  53. return re.match("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", email.lower())
  54. def get_request_site_address(full_address=False):
  55. """get app url from request"""
  56. import os, conf
  57. if hasattr(conf, "host_name"):
  58. host_name = conf.host_name
  59. else:
  60. try:
  61. host = webnotes.request.host
  62. protocol = 'HTTPS' in webnotes.get_request_header('SERVER_PROTOCOL') and 'https://' or 'http://'
  63. host_name = protocol + webnotes.get_request_header('HTTP_HOST')
  64. except TypeError:
  65. return 'http://localhost'
  66. if full_address:
  67. return host_name + webnotes.get_request_header("REQUEST_URI", "")
  68. else:
  69. return host_name
  70. def random_string(length):
  71. """generate a random string"""
  72. import string
  73. from random import choice
  74. return ''.join([choice(string.letters + string.digits) for i in range(length)])
  75. def load_json(arg):
  76. # already a dictionary?
  77. if not isinstance(arg, basestring):
  78. return arg
  79. import json
  80. return json.loads(arg, encoding='utf-8')
  81. # Get Traceback
  82. # ==============================================================================
  83. def getTraceback():
  84. """
  85. Returns the traceback of the Exception
  86. """
  87. import sys, traceback
  88. exc_type, value, tb = sys.exc_info()
  89. trace_list = traceback.format_tb(tb, None) + \
  90. traceback.format_exception_only(exc_type, value)
  91. body = "Traceback (innermost last):\n" + "%-20s %s" % \
  92. (unicode((b"").join(trace_list[:-1]), 'utf-8'), unicode(trace_list[-1], 'utf-8'))
  93. if webnotes.logger:
  94. webnotes.logger.error('Db:'+(webnotes.conn and webnotes.conn.cur_db_name or '') \
  95. + ' - ' + body)
  96. return body
  97. def log(event, details):
  98. webnotes.logger.info(details)
  99. # datetime functions
  100. def getdate(string_date):
  101. """
  102. Coverts string date (yyyy-mm-dd) to datetime.date object
  103. """
  104. import datetime
  105. if isinstance(string_date, datetime.date):
  106. return string_date
  107. elif isinstance(string_date, datetime.datetime):
  108. return datetime.date()
  109. if " " in string_date:
  110. string_date = string_date.split(" ")[0]
  111. return datetime.datetime.strptime(string_date, "%Y-%m-%d").date()
  112. def add_to_date(date, years=0, months=0, days=0):
  113. """Adds `days` to the given date"""
  114. format = isinstance(date, basestring)
  115. if date:
  116. date = getdate(date)
  117. else:
  118. raise Exception, "Start date required"
  119. from dateutil.relativedelta import relativedelta
  120. date += relativedelta(years=years, months=months, days=days)
  121. if format:
  122. return date.strftime("%Y-%m-%d")
  123. else:
  124. return date
  125. def add_days(date, days):
  126. return add_to_date(date, days=days)
  127. def add_months(date, months):
  128. return add_to_date(date, months=months)
  129. def add_years(date, years):
  130. return add_to_date(date, years=years)
  131. def date_diff(string_ed_date, string_st_date):
  132. return (getdate(string_ed_date) - getdate(string_st_date)).days
  133. def time_diff(string_ed_date, string_st_date):
  134. return get_datetime(string_ed_date) - get_datetime(string_st_date)
  135. def time_diff_in_seconds(string_ed_date, string_st_date):
  136. return time_diff(string_ed_date, string_st_date).seconds
  137. def time_diff_in_hours(string_ed_date, string_st_date):
  138. return round(float(time_diff(string_ed_date, string_st_date).seconds) / 3600, 6)
  139. def now_datetime():
  140. from datetime import datetime
  141. return convert_utc_to_user_timezone(datetime.utcnow())
  142. def get_user_time_zone():
  143. global user_time_zone
  144. if not user_time_zone:
  145. user_time_zone = webnotes.cache().get_value("time_zone")
  146. if not user_time_zone:
  147. user_time_zone = webnotes.conn.get_value('Control Panel', None, 'time_zone') \
  148. or 'Asia/Calcutta'
  149. webnotes.cache().set_value("time_zone", user_time_zone)
  150. return user_time_zone
  151. def convert_utc_to_user_timezone(utc_timestamp):
  152. from pytz import timezone
  153. utcnow = timezone('UTC').localize(utc_timestamp)
  154. return utcnow.astimezone(timezone(get_user_time_zone()))
  155. def now():
  156. """return current datetime as yyyy-mm-dd hh:mm:ss"""
  157. if current_date:
  158. return getdate(current_date).strftime("%Y-%m-%d") + " " + now_datetime().strftime('%H:%M:%S')
  159. else:
  160. return now_datetime().strftime('%Y-%m-%d %H:%M:%S')
  161. def nowdate():
  162. """return current date as yyyy-mm-dd"""
  163. return now_datetime().strftime('%Y-%m-%d')
  164. def today():
  165. return nowdate()
  166. def nowtime():
  167. """return current time in hh:mm"""
  168. return now_datetime().strftime('%H:%M')
  169. def get_first_day(dt, d_years=0, d_months=0):
  170. """
  171. Returns the first day of the month for the date specified by date object
  172. Also adds `d_years` and `d_months` if specified
  173. """
  174. import datetime
  175. dt = getdate(dt)
  176. # d_years, d_months are "deltas" to apply to dt
  177. overflow_years, month = divmod(dt.month + d_months - 1, 12)
  178. year = dt.year + d_years + overflow_years
  179. return datetime.date(year, month + 1, 1)
  180. def get_last_day(dt):
  181. """
  182. Returns last day of the month using:
  183. `get_first_day(dt, 0, 1) + datetime.timedelta(-1)`
  184. """
  185. import datetime
  186. return get_first_day(dt, 0, 1) + datetime.timedelta(-1)
  187. def get_datetime(datetime_str):
  188. from datetime import datetime
  189. if isinstance(datetime_str, datetime):
  190. return datetime_str.replace(microsecond=0, tzinfo=None)
  191. return datetime.strptime(datetime_str, '%Y-%m-%d %H:%M:%S')
  192. def get_datetime_str(datetime_obj):
  193. if isinstance(datetime_obj, basestring):
  194. datetime_obj = get_datetime(datetime_obj)
  195. return datetime_obj.strftime('%Y-%m-%d %H:%M:%S')
  196. def formatdate(string_date=None):
  197. """
  198. Convers the given string date to :data:`user_format`
  199. User format specified in :term:`Control Panel`
  200. Examples:
  201. * dd-mm-yyyy
  202. * mm-dd-yyyy
  203. * dd/mm/yyyy
  204. """
  205. if string_date:
  206. string_date = getdate(string_date)
  207. else:
  208. string_date = now_datetime().date()
  209. global user_format
  210. if not user_format:
  211. user_format = webnotes.conn.get_default("date_format")
  212. out = user_format
  213. return out.replace("dd", string_date.strftime("%d"))\
  214. .replace("mm", string_date.strftime("%m"))\
  215. .replace("yyyy", string_date.strftime("%Y"))
  216. def global_date_format(date):
  217. """returns date as 1 January 2012"""
  218. formatted_date = getdate(date).strftime("%d %B %Y")
  219. return formatted_date.startswith("0") and formatted_date[1:] or formatted_date
  220. def dict_to_str(args, sep='&'):
  221. """
  222. Converts a dictionary to URL
  223. """
  224. import urllib
  225. t = []
  226. for k in args.keys():
  227. t.append(str(k)+'='+urllib.quote(str(args[k] or '')))
  228. return sep.join(t)
  229. def timestamps_equal(t1, t2):
  230. """Returns true if same the two string timestamps are same"""
  231. scrub = lambda x: x.replace(':', ' ').replace('-',' ').split()
  232. t1, t2 = scrub(t1), scrub(t2)
  233. if len(t1) != len(t2):
  234. return
  235. for i in range(len(t1)):
  236. if t1[i]!=t2[i]:
  237. return
  238. return 1
  239. def has_common(l1, l2):
  240. """Returns truthy value if there are common elements in lists l1 and l2"""
  241. return set(l1) & set(l2)
  242. def flt(s, precision=None):
  243. """Convert to float (ignore commas)"""
  244. if isinstance(s, basestring):
  245. s = s.replace(',','')
  246. try:
  247. num = float(s)
  248. if precision is not None:
  249. num = _round(num, precision)
  250. except Exception:
  251. num = 0
  252. return num
  253. def cint(s):
  254. """Convert to integer"""
  255. try: num = int(float(s))
  256. except: num = 0
  257. return num
  258. def cstr(s):
  259. if isinstance(s, unicode):
  260. return s
  261. elif s==None:
  262. return ''
  263. elif isinstance(s, basestring):
  264. return unicode(s, 'utf-8')
  265. else:
  266. return unicode(s)
  267. def _round(num, precision=0):
  268. """round method for round halfs to nearest even algorithm"""
  269. precision = cint(precision)
  270. multiplier = 10 ** precision
  271. # avoid rounding errors
  272. num = round(num * multiplier if precision else num, 8)
  273. import math
  274. floor = math.floor(num)
  275. decimal_part = num - floor
  276. if decimal_part == 0.5:
  277. num = floor if (floor % 2 == 0) else floor + 1
  278. else:
  279. num = round(num)
  280. return (num / multiplier) if precision else num
  281. def encode(obj, encoding="utf-8"):
  282. if isinstance(obj, list):
  283. out = []
  284. for o in obj:
  285. if isinstance(o, unicode):
  286. out.append(o.encode(encoding))
  287. else:
  288. out.append(o)
  289. return out
  290. elif isinstance(obj, unicode):
  291. return obj.encode(encoding)
  292. else:
  293. return obj
  294. def parse_val(v):
  295. """Converts to simple datatypes from SQL query results"""
  296. import datetime
  297. if isinstance(v, (datetime.date, datetime.datetime)):
  298. v = unicode(v)
  299. elif isinstance(v, datetime.timedelta):
  300. v = ":".join(unicode(v).split(":")[:2])
  301. elif isinstance(v, long):
  302. v = int(v)
  303. return v
  304. def fmt_money(amount, precision=None, currency=None):
  305. """
  306. Convert to string with commas for thousands, millions etc
  307. """
  308. number_format = webnotes.conn.get_default("number_format") or "#,###.##"
  309. decimal_str, comma_str, precision = get_number_format_info(number_format)
  310. amount = '%.*f' % (precision, flt(amount))
  311. if amount.find('.') == -1:
  312. decimals = ''
  313. else:
  314. decimals = amount.split('.')[1]
  315. parts = []
  316. minus = ''
  317. if flt(amount) < 0:
  318. minus = '-'
  319. amount = cstr(abs(flt(amount))).split('.')[0]
  320. if len(amount) > 3:
  321. parts.append(amount[-3:])
  322. amount = amount[:-3]
  323. val = number_format=="#,##,###.##" and 2 or 3
  324. while len(amount) > val:
  325. parts.append(amount[-val:])
  326. amount = amount[:-val]
  327. parts.append(amount)
  328. parts.reverse()
  329. amount = comma_str.join(parts) + (precision and (decimal_str + decimals) or "")
  330. amount = minus + amount
  331. if currency:
  332. symbol = webnotes.conn.get_value("Currency", currency, "symbol")
  333. if symbol:
  334. amount = symbol + " " + amount
  335. return amount
  336. number_format_info = {
  337. "#.###": ("", ".", 0),
  338. "#,###": ("", ",", 0),
  339. "#,###.##": (".", ",", 2),
  340. "#,##,###.##": (".", ",", 2),
  341. "#.###,##": (",", ".", 2),
  342. "# ###.##": (".", " ", 2),
  343. "#,###.###": (".", ",", 3),
  344. }
  345. def get_number_format_info(format):
  346. return number_format_info.get(format) or (".", ",", 2)
  347. #
  348. # convet currency to words
  349. #
  350. def money_in_words(number, main_currency = None, fraction_currency=None):
  351. """
  352. Returns string in words with currency and fraction currency.
  353. """
  354. d = get_defaults()
  355. if not main_currency:
  356. main_currency = d.get('currency', 'INR')
  357. if not fraction_currency:
  358. fraction_currency = webnotes.conn.get_value("Currency", main_currency, "fraction") or "Cent"
  359. n = "%.2f" % flt(number)
  360. main, fraction = n.split('.')
  361. if len(fraction)==1: fraction += '0'
  362. number_format = webnotes.conn.get_value("Currency", main_currency, "number_format") or \
  363. webnotes.conn.get_default("number_format") or "#,###.##"
  364. in_million = True
  365. if number_format == "#,##,###.##": in_million = False
  366. out = main_currency + ' ' + in_words(main, in_million).title()
  367. if cint(fraction):
  368. out = out + ' and ' + in_words(fraction, in_million).title() + ' ' + fraction_currency
  369. return out + ' only.'
  370. #
  371. # convert number to words
  372. #
  373. def in_words(integer, in_million=True):
  374. """
  375. Returns string in words for the given integer.
  376. """
  377. n=int(integer)
  378. known = {0: 'zero', 1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six', 7: 'seven', 8: 'eight', 9: 'nine', 10: 'ten',
  379. 11: 'eleven', 12: 'twelve', 13: 'thirteen', 14: 'fourteen', 15: 'fifteen', 16: 'sixteen', 17: 'seventeen', 18: 'eighteen',
  380. 19: 'nineteen', 20: 'twenty', 30: 'thirty', 40: 'forty', 50: 'fifty', 60: 'sixty', 70: 'seventy', 80: 'eighty', 90: 'ninety'}
  381. def psn(n, known, xpsn):
  382. import sys;
  383. if n in known: return known[n]
  384. bestguess, remainder = str(n), 0
  385. if n<=20:
  386. webnotes.errprint(sys.stderr)
  387. webnotes.errprint(n)
  388. webnotes.errprint("How did this happen?")
  389. assert 0
  390. elif n < 100:
  391. bestguess= xpsn((n//10)*10, known, xpsn) + '-' + xpsn(n%10, known, xpsn)
  392. return bestguess
  393. elif n < 1000:
  394. bestguess= xpsn(n//100, known, xpsn) + ' ' + 'hundred'
  395. remainder = n%100
  396. else:
  397. if in_million:
  398. if n < 1000000:
  399. bestguess= xpsn(n//1000, known, xpsn) + ' ' + 'thousand'
  400. remainder = n%1000
  401. elif n < 1000000000:
  402. bestguess= xpsn(n//1000000, known, xpsn) + ' ' + 'million'
  403. remainder = n%1000000
  404. else:
  405. bestguess= xpsn(n//1000000000, known, xpsn) + ' ' + 'billion'
  406. remainder = n%1000000000
  407. else:
  408. if n < 100000:
  409. bestguess= xpsn(n//1000, known, xpsn) + ' ' + 'thousand'
  410. remainder = n%1000
  411. elif n < 10000000:
  412. bestguess= xpsn(n//100000, known, xpsn) + ' ' + 'lakh'
  413. remainder = n%100000
  414. else:
  415. bestguess= xpsn(n//10000000, known, xpsn) + ' ' + 'crore'
  416. remainder = n%10000000
  417. if remainder:
  418. if remainder >= 100:
  419. comma = ','
  420. else:
  421. comma = ''
  422. return bestguess + comma + ' ' + xpsn(remainder, known, xpsn)
  423. else:
  424. return bestguess
  425. return psn(n, known, psn)
  426. # Get Defaults
  427. # ==============================================================================
  428. def get_defaults(key=None):
  429. """
  430. Get dictionary of default values from the :term:`Control Panel`, or a value if key is passed
  431. """
  432. return webnotes.conn.get_defaults(key)
  433. def set_default(key, val):
  434. """
  435. Set / add a default value to :term:`Control Panel`
  436. """
  437. return webnotes.conn.set_default(key, val)
  438. def remove_blanks(d):
  439. """
  440. Returns d with empty ('' or None) values stripped
  441. """
  442. empty_keys = []
  443. for key in d:
  444. if d[key]=='' or d[key]==None:
  445. # del d[key] raises runtime exception, using a workaround
  446. empty_keys.append(key)
  447. for key in empty_keys:
  448. del d[key]
  449. return d
  450. def pprint_dict(d, level=1, no_blanks=True):
  451. """
  452. Pretty print a dictionary with indents
  453. """
  454. if no_blanks:
  455. remove_blanks(d)
  456. # make indent
  457. indent, ret = '', ''
  458. for i in range(0,level): indent += '\t'
  459. # add lines
  460. comment, lines = '', []
  461. kl = d.keys()
  462. kl.sort()
  463. # make lines
  464. for key in kl:
  465. if key != '##comment':
  466. tmp = {key: d[key]}
  467. lines.append(indent + str(tmp)[1:-1] )
  468. # add comment string
  469. if '##comment' in kl:
  470. ret = ('\n' + indent) + '# ' + d['##comment'] + '\n'
  471. # open
  472. ret += indent + '{\n'
  473. # lines
  474. ret += indent + ',\n\t'.join(lines)
  475. # close
  476. ret += '\n' + indent + '}'
  477. return ret
  478. def get_common(d1,d2):
  479. """
  480. returns (list of keys) the common part of two dicts
  481. """
  482. return [p for p in d1 if p in d2 and d1[p]==d2[p]]
  483. def get_common_dict(d1, d2):
  484. """
  485. return common dictionary of d1 and d2
  486. """
  487. ret = {}
  488. for key in d1:
  489. if key in d2 and d2[key]==d1[key]:
  490. ret[key] = d1[key]
  491. return ret
  492. def get_diff_dict(d1, d2):
  493. """
  494. return common dictionary of d1 and d2
  495. """
  496. diff_keys = set(d2.keys()).difference(set(d1.keys()))
  497. ret = {}
  498. for d in diff_keys: ret[d] = d2[d]
  499. return ret
  500. def get_file_timestamp(fn):
  501. """
  502. Returns timestamp of the given file
  503. """
  504. import os
  505. from webnotes.utils import cint
  506. try:
  507. return str(cint(os.stat(fn).st_mtime))
  508. except OSError, e:
  509. if e.args[0]!=2:
  510. raise e
  511. else:
  512. return None
  513. # to be deprecated
  514. def make_esc(esc_chars):
  515. """
  516. Function generator for Escaping special characters
  517. """
  518. return lambda s: ''.join(['\\' + c if c in esc_chars else c for c in s])
  519. # esc / unescape characters -- used for command line
  520. def esc(s, esc_chars):
  521. """
  522. Escape special characters
  523. """
  524. if not s:
  525. return ""
  526. for c in esc_chars:
  527. esc_str = '\\' + c
  528. s = s.replace(c, esc_str)
  529. return s
  530. def unesc(s, esc_chars):
  531. """
  532. UnEscape special characters
  533. """
  534. for c in esc_chars:
  535. esc_str = '\\' + c
  536. s = s.replace(esc_str, c)
  537. return s
  538. def is_html(text):
  539. out = False
  540. for key in ["<br>", "<p", "<img", "<div"]:
  541. if key in text:
  542. out = True
  543. break
  544. return out
  545. def strip_html(text):
  546. """
  547. removes anything enclosed in and including <>
  548. """
  549. import re
  550. return re.compile(r'<.*?>').sub('', text)
  551. def escape_html(text):
  552. html_escape_table = {
  553. "&": "&amp;",
  554. '"': "&quot;",
  555. "'": "&apos;",
  556. ">": "&gt;",
  557. "<": "&lt;",
  558. }
  559. return "".join(html_escape_table.get(c,c) for c in text)
  560. def get_doctype_label(dt=None):
  561. """
  562. Gets label of a doctype
  563. """
  564. if dt:
  565. res = webnotes.conn.sql("""\
  566. SELECT name, dt_label FROM `tabDocType Label`
  567. WHERE name=%s""", dt)
  568. return res and res[0][0] or dt
  569. else:
  570. res = webnotes.conn.sql("SELECT name, dt_label FROM `tabDocType Label`")
  571. dt_label_dict = {}
  572. for r in res:
  573. dt_label_dict[r[0]] = r[1]
  574. return dt_label_dict
  575. def get_label_doctype(label):
  576. """
  577. Gets doctype from its label
  578. """
  579. res = webnotes.conn.sql("""\
  580. SELECT name FROM `tabDocType Label`
  581. WHERE dt_label=%s""", label)
  582. return res and res[0][0] or label
  583. def pretty_date(iso_datetime):
  584. """
  585. Takes an ISO time and returns a string representing how
  586. long ago the date represents.
  587. Ported from PrettyDate by John Resig
  588. """
  589. if not iso_datetime: return ''
  590. from datetime import datetime
  591. import math
  592. if isinstance(iso_datetime, basestring):
  593. iso_datetime = datetime.strptime(iso_datetime, '%Y-%m-%d %H:%M:%S')
  594. now_dt = datetime.strptime(now(), '%Y-%m-%d %H:%M:%S')
  595. dt_diff = now_dt - iso_datetime
  596. # available only in python 2.7+
  597. # dt_diff_seconds = dt_diff.total_seconds()
  598. dt_diff_seconds = dt_diff.days * 86400.0 + dt_diff.seconds
  599. dt_diff_days = math.floor(dt_diff_seconds / 86400.0)
  600. # differnt cases
  601. if dt_diff_seconds < 60.0:
  602. return 'just now'
  603. elif dt_diff_seconds < 120.0:
  604. return '1 minute ago'
  605. elif dt_diff_seconds < 3600.0:
  606. return '%s minutes ago' % cint(math.floor(dt_diff_seconds / 60.0))
  607. elif dt_diff_seconds < 7200.0:
  608. return '1 hour ago'
  609. elif dt_diff_seconds < 86400.0:
  610. return '%s hours ago' % cint(math.floor(dt_diff_seconds / 3600.0))
  611. elif dt_diff_days == 1.0:
  612. return 'Yesterday'
  613. elif dt_diff_days < 7.0:
  614. return '%s days ago' % cint(dt_diff_days)
  615. elif dt_diff_days < 31.0:
  616. return '%s week(s) ago' % cint(math.ceil(dt_diff_days / 7.0))
  617. elif dt_diff_days < 365.0:
  618. return '%s months ago' % cint(math.ceil(dt_diff_days / 30.0))
  619. else:
  620. return 'more than %s year(s) ago' % cint(math.floor(dt_diff_days / 365.0))
  621. def execute_in_shell(cmd, verbose=0):
  622. # using Popen instead of os.system - as recommended by python docs
  623. from subprocess import Popen
  624. import tempfile
  625. with tempfile.TemporaryFile() as stdout:
  626. with tempfile.TemporaryFile() as stderr:
  627. p = Popen(cmd, shell=True, stdout=stdout, stderr=stderr)
  628. p.wait()
  629. stdout.seek(0)
  630. out = stdout.read()
  631. stderr.seek(0)
  632. err = stderr.read()
  633. if verbose:
  634. if err: print err
  635. if out: print out
  636. return err, out
  637. def comma_or(some_list):
  638. return comma_sep(some_list, " or ")
  639. def comma_and(some_list):
  640. return comma_sep(some_list, " and ")
  641. def comma_sep(some_list, sep):
  642. if isinstance(some_list, (list, tuple)):
  643. # list(some_list) is done to preserve the existing list
  644. some_list = [unicode(s) for s in list(some_list)]
  645. if not some_list:
  646. return ""
  647. elif len(some_list) == 1:
  648. return some_list[0]
  649. else:
  650. some_list = ["'%s'" % s for s in some_list]
  651. return ", ".join(some_list[:-1]) + sep + some_list[-1]
  652. else:
  653. return some_list
  654. def filter_strip_join(some_list, sep):
  655. """given a list, filter None values, strip spaces and join"""
  656. return (cstr(sep)).join((cstr(a).strip() for a in filter(None, some_list)))
  657. def get_path(*path):
  658. import os
  659. return os.path.join(get_base_path(), *path)
  660. def get_base_path():
  661. import conf
  662. import os
  663. return os.path.dirname(os.path.abspath(conf.__file__))
  664. def get_url():
  665. import startup
  666. if hasattr(startup, "get_url"):
  667. url = startup.get_url()
  668. else:
  669. url = get_request_site_address()
  670. return url
  671. def get_url_to_form(doctype, name, base_url=None, label=None):
  672. if not base_url:
  673. base_url = get_url()
  674. if not label: label = name
  675. return """<a href="%(base_url)s/app.html#!Form/%(doctype)s/%(name)s">%(label)s</a>""" % locals()
  676. def encode_dict(d, encoding="utf-8"):
  677. for key in d:
  678. if isinstance(d[key], basestring) and isinstance(d[key], unicode):
  679. d[key] = d[key].encode(encoding)
  680. return d
  681. def decode_dict(d, encoding="utf-8"):
  682. for key in d:
  683. if isinstance(d[key], basestring) and not isinstance(d[key], unicode):
  684. d[key] = d[key].decode(encoding, "ignore")
  685. return d
  686. import operator
  687. operator_map = {
  688. # startswith
  689. "^": lambda (a, b): (a or "").startswith(b),
  690. # in or not in a list
  691. "in": lambda (a, b): operator.contains(b, a),
  692. "not in": lambda (a, b): not operator.contains(b, a),
  693. # comparison operators
  694. "=": lambda (a, b): operator.eq(a, b),
  695. "!=": lambda (a, b): operator.ne(a, b),
  696. ">": lambda (a, b): operator.gt(a, b),
  697. "<": lambda (a, b): operator.lt(a, b),
  698. ">=": lambda (a, b): operator.ge(a, b),
  699. "<=": lambda (a, b): operator.le(a, b),
  700. "not None": lambda (a, b): a and True or False,
  701. "None": lambda (a, b): (not a) and True or False
  702. }
  703. def compare(val1, condition, val2):
  704. if condition in operator_map:
  705. return operator_map[condition]((val1, val2))
  706. return False