Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

767 wiersze
19 KiB

  1. # Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. #
  3. # MIT License (MIT)
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #
  22. # util __init__.py
  23. from __future__ import unicode_literals
  24. import webnotes
  25. user_time_zone = None
  26. user_format = None
  27. no_value_fields = ['Section Break', 'Column Break', 'HTML', 'Table', 'FlexTable',
  28. 'Button', 'Image', 'Graph']
  29. default_fields = ['doctype', 'name', 'owner', 'creation', 'modified', 'modified_by',
  30. 'parent', 'parentfield', 'parenttype', 'idx', 'docstatus']
  31. # used in import_docs.py
  32. # TODO: deprecate it
  33. def getCSVelement(v):
  34. """
  35. Returns the CSV value of `v`, For example:
  36. * apple becomes "apple"
  37. * hi"there becomes "hi""there"
  38. """
  39. v = cstr(v)
  40. if not v: return ''
  41. if (',' in v) or ('\n' in v) or ('"' in v):
  42. if '"' in v: v = v.replace('"', '""')
  43. return '"'+v+'"'
  44. else: return v or ''
  45. def get_fullname(profile):
  46. """get the full name (first name + last name) of the user from Profile"""
  47. p = webnotes.conn.sql("""select first_name, last_name from `tabProfile`
  48. where name=%s""", profile, as_dict=1)
  49. if p:
  50. profile = " ".join(filter(None,
  51. [p[0].get('first_name'), p[0].get('last_name')])) or profile
  52. return profile
  53. # TODO: deprecate it
  54. def decode_email_header(s):
  55. import email.header
  56. # replace double quotes with blank
  57. # double quotes in header prohibit decoding of header
  58. decoded_header_tuple = email.header.decode_header(s.replace('"', ''))
  59. decoded_list = map(lambda h: unicode(h[0], encoding=h[1] or 'utf-8'), decoded_header_tuple)
  60. return " ".join(decoded_list)
  61. def get_email_id(user):
  62. """get email id of user formatted as: John Doe <johndoe@example.com>"""
  63. if user == "Administrator":
  64. return user
  65. from email.utils import formataddr
  66. fullname = get_fullname(user)
  67. return formataddr((fullname, user))
  68. def extract_email_id(email):
  69. """fetch only the email part of the email id"""
  70. import re
  71. sender_email = re.findall("<([^>]*)>", email)
  72. return sender_email and sender_email[0] or ""
  73. def validate_email_add(email_str):
  74. """Validates the email string"""
  75. from email.utils import parseaddr
  76. real_name, email = parseaddr(email_str)
  77. import re
  78. 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())
  79. def sendmail(recipients, sender='', msg='', subject='[No Subject]'):
  80. """Send an email. For more details see :func:`email_lib.sendmail`"""
  81. import webnotes.utils.email_lib
  82. return email_lib.sendmail(recipients, sender, msg, subject)
  83. def get_request_site_address():
  84. """get app url from request"""
  85. import os
  86. try:
  87. return 'HTTPS' in os.environ.get('SERVER_PROTOCOL') and 'https://' or 'http://' \
  88. + os.environ.get('HTTP_HOST')
  89. except TypeError, e:
  90. return 'http://localhost'
  91. def random_string(length):
  92. """generate a random string"""
  93. import string
  94. from random import choice
  95. return ''.join([choice(string.letters + string.digits) for i in range(length)])
  96. def load_json(arg):
  97. # already a dictionary?
  98. if not isinstance(arg, basestring):
  99. return arg
  100. import json
  101. return json.loads(arg, encoding='utf-8')
  102. # Get Traceback
  103. # ==============================================================================
  104. def getTraceback():
  105. """
  106. Returns the traceback of the Exception
  107. """
  108. import sys, traceback, string
  109. exc_type, value, tb = sys.exc_info()
  110. trace_list = traceback.format_tb(tb, None) + \
  111. traceback.format_exception_only(exc_type, value)
  112. body = "Traceback (innermost last):\n" + "%-20s %s" % \
  113. (unicode((b"").join(trace_list[:-1]), 'utf-8'), unicode(trace_list[-1], 'utf-8'))
  114. if webnotes.logger:
  115. webnotes.logger.error('Db:'+(webnotes.conn and webnotes.conn.cur_db_name or '') \
  116. + ' - ' + body)
  117. return body
  118. def log(event, details):
  119. webnotes.logger.info(details)
  120. # datetime functions
  121. def getdate(string_date):
  122. """
  123. Coverts string date (yyyy-mm-dd) to datetime.date object
  124. """
  125. import datetime
  126. if isinstance(string_date, datetime.date):
  127. return string_date
  128. elif isinstance(string_date, datetime.datetime):
  129. return datetime.date()
  130. if " " in string_date:
  131. string_date = string_date.split(" ")[0]
  132. try:
  133. return datetime.datetime.strptime(string_date, "%Y-%m-%d").date()
  134. except ValueError, e:
  135. webnotes.msgprint("Cannot understand date - '%s'" % \
  136. (string_date,), raise_exception=1)
  137. def add_to_date(date, years=0, months=0, days=0):
  138. """Adds `days` to the given date"""
  139. format = isinstance(date, basestring)
  140. if date:
  141. date = getdate(date)
  142. else:
  143. raise Exception, "Start date required"
  144. from dateutil.relativedelta import relativedelta
  145. date += relativedelta(years=years, months=months, days=days)
  146. if format:
  147. return date.strftime("%Y-%m-%d")
  148. else:
  149. return date
  150. def add_days(date, days):
  151. return add_to_date(date, days=days)
  152. def add_months(date, months):
  153. return add_to_date(date, months=months)
  154. def add_years(date, years):
  155. return add_to_date(date, years=years)
  156. def date_diff(string_ed_date, string_st_date):
  157. return (getdate(string_ed_date) - getdate(string_st_date)).days
  158. def time_diff_in_seconds(string_ed_date, string_st_date):
  159. return (getdate(string_ed_date) - getdate(string_st_date)).seconds
  160. def now_datetime():
  161. global user_time_zone
  162. from datetime import datetime
  163. from pytz import timezone
  164. # get localtime
  165. if not user_time_zone:
  166. user_time_zone = webnotes.conn.get_value('Control Panel', None, 'time_zone') \
  167. or 'Asia/Calcutta'
  168. # convert to UTC
  169. utcnow = timezone('UTC').localize(datetime.utcnow())
  170. # convert to user time zone
  171. return utcnow.astimezone(timezone(user_time_zone))
  172. def now():
  173. """return current datetime as yyyy-mm-dd hh:mm:ss"""
  174. return now_datetime().strftime('%Y-%m-%d %H:%M:%S')
  175. def nowdate():
  176. """return current date as yyyy-mm-dd"""
  177. return now_datetime().strftime('%Y-%m-%d')
  178. def nowtime():
  179. """return current time in hh:mm"""
  180. return now_datetime().strftime('%H:%M')
  181. def get_first_day(dt, d_years=0, d_months=0):
  182. """
  183. Returns the first day of the month for the date specified by date object
  184. Also adds `d_years` and `d_months` if specified
  185. """
  186. import datetime
  187. dt = getdate(dt)
  188. # d_years, d_months are "deltas" to apply to dt
  189. overflow_years, month = divmod(dt.month + d_months - 1, 12)
  190. year = dt.year + d_years + overflow_years
  191. return datetime.date(year, month + 1, 1)
  192. def get_last_day(dt):
  193. """
  194. Returns last day of the month using:
  195. `get_first_day(dt, 0, 1) + datetime.timedelta(-1)`
  196. """
  197. import datetime
  198. return get_first_day(dt, 0, 1) + datetime.timedelta(-1)
  199. def get_datetime(datetime_str):
  200. from datetime import datetime
  201. if isinstance(datetime_str, datetime):
  202. return datetime_str.replace(microsecond=0, tzinfo=None)
  203. return datetime.strptime(datetime_str, '%Y-%m-%d %H:%M:%S')
  204. def get_datetime_str(datetime_obj):
  205. if isinstance(datetime_obj, basestring):
  206. datetime_obj = get_datetime(datetime_obj)
  207. return datetime_obj.strftime('%Y-%m-%d %H:%M:%S')
  208. def formatdate(string_date=None):
  209. """
  210. Convers the given string date to :data:`user_format`
  211. User format specified in :term:`Control Panel`
  212. Examples:
  213. * dd-mm-yyyy
  214. * mm-dd-yyyy
  215. * dd/mm/yyyy
  216. """
  217. if string_date:
  218. string_date = getdate(string_date)
  219. else:
  220. string_date = nowdate()
  221. global user_format
  222. if not user_format:
  223. user_format = webnotes.conn.get_value('Control Panel', None, 'date_format')
  224. out = user_format
  225. return out.replace("dd", string_date.strftime("%d"))\
  226. .replace("mm", string_date.strftime("%m"))\
  227. .replace("yyyy", string_date.strftime("%Y"))
  228. def global_date_format(date):
  229. """returns date as 1 January 2012"""
  230. formatted_date = getdate(date).strftime("%d %B %Y")
  231. return formatted_date.startswith("0") and formatted_date[1:] or formatted_date
  232. def dict_to_str(args, sep='&'):
  233. """
  234. Converts a dictionary to URL
  235. """
  236. import urllib
  237. t = []
  238. for k in args.keys():
  239. t.append(str(k)+'='+urllib.quote(str(args[k] or '')))
  240. return sep.join(t)
  241. def timestamps_equal(t1, t2):
  242. """Returns true if same the two string timestamps are same"""
  243. scrub = lambda x: x.replace(':', ' ').replace('-',' ').split()
  244. t1, t2 = scrub(t1), scrub(t2)
  245. if len(t1) != len(t2):
  246. return
  247. for i in range(len(t1)):
  248. if t1[i]!=t2[i]:
  249. return
  250. return 1
  251. def has_common(l1, l2):
  252. """Returns truthy value if there are common elements in lists l1 and l2"""
  253. return set(l1) & set(l2)
  254. def flt(s, precision=None):
  255. """Convert to float (ignore commas)"""
  256. if isinstance(s, basestring):
  257. s = s.replace(',','')
  258. try:
  259. num = float(s)
  260. if precision:
  261. num = round(num, precision)
  262. except Exception, e:
  263. num = 0
  264. return num
  265. def cint(s):
  266. """Convert to integer"""
  267. try: num = int(float(s))
  268. except: num = 0
  269. return num
  270. def cstr(s):
  271. if isinstance(s, unicode):
  272. return s
  273. elif s==None:
  274. return ''
  275. elif isinstance(s, basestring):
  276. return unicode(s, 'utf-8')
  277. else:
  278. return unicode(s)
  279. def encode(obj, encoding="utf-8"):
  280. if isinstance(obj, list):
  281. out = []
  282. for o in obj:
  283. if isinstance(o, unicode):
  284. out.append(o.encode(encoding))
  285. else:
  286. out.append(o)
  287. return out
  288. elif isinstance(obj, unicode):
  289. return obj.encode(encoding)
  290. else:
  291. return obj
  292. def parse_val(v):
  293. """Converts to simple datatypes from SQL query results"""
  294. import datetime
  295. if isinstance(v, (datetime.date, datetime.datetime)):
  296. v = unicode(v)
  297. elif isinstance(v, datetime.timedelta):
  298. v = ":".join(unicode(v).split(":")[:2])
  299. elif isinstance(v, long):
  300. v = int(v)
  301. return v
  302. def fmt_money(amount, precision=2):
  303. """
  304. Convert to string with commas for thousands, millions etc
  305. """
  306. curr = webnotes.conn.get_value('Control Panel', None, 'currency_format') or 'Millions'
  307. amount = '%.*f' % (precision, flt(amount))
  308. val = 2
  309. if curr == 'Millions': val = 3
  310. if amount.find('.') == -1: temp = '00'
  311. else: temp = amount.split('.')[1]
  312. l = []
  313. minus = ''
  314. if flt(amount) < 0: minus = '-'
  315. amount = ''.join(amount.split(','))
  316. amount = cstr(abs(flt(amount))).split('.')[0]
  317. # main logic
  318. if len(amount) > 3:
  319. nn = amount[len(amount)-3:]
  320. l.append(nn)
  321. amount = amount[0:len(amount)-3]
  322. while len(amount) > val:
  323. nn = amount[len(amount)-val:]
  324. l.insert(0,nn)
  325. amount = amount[0:len(amount)-val]
  326. if len(amount) > 0: l.insert(0,amount)
  327. amount = ','.join(l)+'.'+temp
  328. amount = minus + amount
  329. return amount
  330. #
  331. # convet currency to words
  332. #
  333. def money_in_words(number, main_currency = None, fraction_currency=None):
  334. """
  335. Returns string in words with currency and fraction currency.
  336. """
  337. d = get_defaults()
  338. if not main_currency:
  339. main_currency = d.get('currency', 'INR')
  340. if not fraction_currency:
  341. fraction_currency = d.get('fraction_currency', 'paise')
  342. n = "%.2f" % flt(number)
  343. main, fraction = n.split('.')
  344. if len(fraction)==1: fraction += '0'
  345. out = main_currency + ' ' + in_words(main).title()
  346. if cint(fraction):
  347. out = out + ' and ' + in_words(fraction).title() + ' ' + fraction_currency
  348. return out + ' only.'
  349. #
  350. # convert number to words
  351. #
  352. def in_words(integer):
  353. """
  354. Returns string in words for the given integer.
  355. """
  356. in_million = webnotes.conn.get_default('currency_format')=='Millions' and 1 or 0
  357. n=int(integer)
  358. known = {0: 'zero', 1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six', 7: 'seven', 8: 'eight', 9: 'nine', 10: 'ten',
  359. 11: 'eleven', 12: 'twelve', 13: 'thirteen', 14: 'fourteen', 15: 'fifteen', 16: 'sixteen', 17: 'seventeen', 18: 'eighteen',
  360. 19: 'nineteen', 20: 'twenty', 30: 'thirty', 40: 'forty', 50: 'fifty', 60: 'sixty', 70: 'seventy', 80: 'eighty', 90: 'ninety'}
  361. def psn(n, known, xpsn):
  362. import sys;
  363. if n in known: return known[n]
  364. bestguess, remainder = str(n), 0
  365. if n<=20:
  366. print >>sys.stderr, n, "How did this happen?"
  367. assert 0
  368. elif n < 100:
  369. bestguess= xpsn((n//10)*10, known, xpsn) + '-' + xpsn(n%10, known, xpsn)
  370. return bestguess
  371. elif n < 1000:
  372. bestguess= xpsn(n//100, known, xpsn) + ' ' + 'hundred'
  373. remainder = n%100
  374. else:
  375. if in_million:
  376. if n < 1000000:
  377. bestguess= xpsn(n//1000, known, xpsn) + ' ' + 'thousand'
  378. remainder = n%1000
  379. elif n < 1000000000:
  380. bestguess= xpsn(n//1000000, known, xpsn) + ' ' + 'million'
  381. remainder = n%1000000
  382. else:
  383. bestguess= xpsn(n//1000000000, known, xpsn) + ' ' + 'billion'
  384. remainder = n%1000000000
  385. else:
  386. if n < 100000:
  387. bestguess= xpsn(n//1000, known, xpsn) + ' ' + 'thousand'
  388. remainder = n%1000
  389. elif n < 10000000:
  390. bestguess= xpsn(n//100000, known, xpsn) + ' ' + 'lakh'
  391. remainder = n%100000
  392. else:
  393. bestguess= xpsn(n//10000000, known, xpsn) + ' ' + 'crore'
  394. remainder = n%10000000
  395. if remainder:
  396. if remainder >= 100:
  397. comma = ','
  398. else:
  399. comma = ''
  400. return bestguess + comma + ' ' + xpsn(remainder, known, xpsn)
  401. else:
  402. return bestguess
  403. return psn(n, known, psn)
  404. # Get Defaults
  405. # ==============================================================================
  406. def get_defaults(key=None):
  407. """
  408. Get dictionary of default values from the :term:`Control Panel`, or a value if key is passed
  409. """
  410. return webnotes.conn.get_defaults(key)
  411. def set_default(key, val):
  412. """
  413. Set / add a default value to :term:`Control Panel`
  414. """
  415. return webnotes.conn.set_default(key, val)
  416. def remove_blanks(d):
  417. """
  418. Returns d with empty ('' or None) values stripped
  419. """
  420. empty_keys = []
  421. for key in d:
  422. if d[key]=='' or d[key]==None:
  423. # del d[key] raises runtime exception, using a workaround
  424. empty_keys.append(key)
  425. for key in empty_keys:
  426. del d[key]
  427. return d
  428. def pprint_dict(d, level=1, no_blanks=True):
  429. """
  430. Pretty print a dictionary with indents
  431. """
  432. if no_blanks:
  433. remove_blanks(d)
  434. # make indent
  435. indent, ret = '', ''
  436. for i in range(0,level): indent += '\t'
  437. # add lines
  438. comment, lines = '', []
  439. kl = d.keys()
  440. kl.sort()
  441. # make lines
  442. for key in kl:
  443. if key != '##comment':
  444. tmp = {key: d[key]}
  445. lines.append(indent + str(tmp)[1:-1] )
  446. # add comment string
  447. if '##comment' in kl:
  448. ret = ('\n' + indent) + '# ' + d['##comment'] + '\n'
  449. # open
  450. ret += indent + '{\n'
  451. # lines
  452. ret += indent + ',\n\t'.join(lines)
  453. # close
  454. ret += '\n' + indent + '}'
  455. return ret
  456. def get_common(d1,d2):
  457. """
  458. returns (list of keys) the common part of two dicts
  459. """
  460. return [p for p in d1 if p in d2 and d1[p]==d2[p]]
  461. def get_common_dict(d1, d2):
  462. """
  463. return common dictionary of d1 and d2
  464. """
  465. ret = {}
  466. for key in d1:
  467. if key in d2 and d2[key]==d1[key]:
  468. ret[key] = d1[key]
  469. return ret
  470. def get_diff_dict(d1, d2):
  471. """
  472. return common dictionary of d1 and d2
  473. """
  474. diff_keys = set(d2.keys()).difference(set(d1.keys()))
  475. ret = {}
  476. for d in diff_keys: ret[d] = d2[d]
  477. return ret
  478. def get_file_timestamp(fn):
  479. """
  480. Returns timestamp of the given file
  481. """
  482. import os
  483. from webnotes.utils import cint
  484. try:
  485. return str(cint(os.stat(fn).st_mtime))
  486. except OSError, e:
  487. if e.args[0]!=2:
  488. raise e
  489. else:
  490. return None
  491. # to be deprecated
  492. def make_esc(esc_chars):
  493. """
  494. Function generator for Escaping special characters
  495. """
  496. return lambda s: ''.join(['\\' + c if c in esc_chars else c for c in s])
  497. # esc / unescape characters -- used for command line
  498. def esc(s, esc_chars):
  499. """
  500. Escape special characters
  501. """
  502. if not s:
  503. return ""
  504. for c in esc_chars:
  505. esc_str = '\\' + c
  506. s = s.replace(c, esc_str)
  507. return s
  508. def unesc(s, esc_chars):
  509. """
  510. UnEscape special characters
  511. """
  512. for c in esc_chars:
  513. esc_str = '\\' + c
  514. s = s.replace(esc_str, c)
  515. return s
  516. def strip_html(text):
  517. """
  518. removes anything enclosed in and including <>
  519. """
  520. import re
  521. return re.compile(r'<.*?>').sub('', text)
  522. def escape_html(text):
  523. html_escape_table = {
  524. "&": "&amp;",
  525. '"': "&quot;",
  526. "'": "&apos;",
  527. ">": "&gt;",
  528. "<": "&lt;",
  529. }
  530. return "".join(html_escape_table.get(c,c) for c in text)
  531. def get_doctype_label(dt=None):
  532. """
  533. Gets label of a doctype
  534. """
  535. if dt:
  536. res = webnotes.conn.sql("""\
  537. SELECT name, dt_label FROM `tabDocType Label`
  538. WHERE name=%s""", dt)
  539. return res and res[0][0] or dt
  540. else:
  541. res = webnotes.conn.sql("SELECT name, dt_label FROM `tabDocType Label`")
  542. dt_label_dict = {}
  543. for r in res:
  544. dt_label_dict[r[0]] = r[1]
  545. return dt_label_dict
  546. def get_label_doctype(label):
  547. """
  548. Gets doctype from its label
  549. """
  550. res = webnotes.conn.sql("""\
  551. SELECT name FROM `tabDocType Label`
  552. WHERE dt_label=%s""", label)
  553. return res and res[0][0] or label
  554. def pretty_date(iso_datetime):
  555. """
  556. Takes an ISO time and returns a string representing how
  557. long ago the date represents.
  558. Ported from PrettyDate by John Resig
  559. """
  560. if not iso_datetime: return ''
  561. from datetime import datetime
  562. import math
  563. if isinstance(iso_datetime, basestring):
  564. iso_datetime = datetime.strptime(iso_datetime, '%Y-%m-%d %H:%M:%S')
  565. now_dt = datetime.strptime(now(), '%Y-%m-%d %H:%M:%S')
  566. dt_diff = now_dt - iso_datetime
  567. # available only in python 2.7+
  568. # dt_diff_seconds = dt_diff.total_seconds()
  569. dt_diff_seconds = dt_diff.days * 86400.0 + dt_diff.seconds
  570. dt_diff_days = math.floor(dt_diff_seconds / 86400.0)
  571. # differnt cases
  572. if dt_diff_seconds < 60.0:
  573. return 'just now'
  574. elif dt_diff_seconds < 120.0:
  575. return '1 minute ago'
  576. elif dt_diff_seconds < 3600.0:
  577. return '%s minutes ago' % cint(math.floor(dt_diff_seconds / 60.0))
  578. elif dt_diff_seconds < 7200.0:
  579. return '1 hour ago'
  580. elif dt_diff_seconds < 86400.0:
  581. return '%s hours ago' % cint(math.floor(dt_diff_seconds / 3600.0))
  582. elif dt_diff_days == 1.0:
  583. return 'Yesterday'
  584. elif dt_diff_days < 7.0:
  585. return '%s days ago' % cint(dt_diff_days)
  586. elif dt_diff_days < 31.0:
  587. return '%s week(s) ago' % cint(math.ceil(dt_diff_days / 7.0))
  588. elif dt_diff_days < 365.0:
  589. return '%s months ago' % cint(math.ceil(dt_diff_days / 30.0))
  590. else:
  591. return 'more than %s year(s) ago' % cint(math.floor(dt_diff_days / 365.0))
  592. def execute_in_shell(cmd, verbose=0):
  593. # using Popen instead of os.system - as recommended by python docs
  594. from subprocess import Popen, PIPE
  595. import tempfile
  596. with tempfile.TemporaryFile() as stdout:
  597. with tempfile.TemporaryFile() as stderr:
  598. p = Popen(cmd, shell=True, stdout=stdout, stderr=stderr)
  599. p.wait()
  600. stdout.seek(0)
  601. out = stdout.read()
  602. stderr.seek(0)
  603. err = stderr.read()
  604. if verbose:
  605. if err: print err
  606. if out: print out
  607. return err, out
  608. def comma_or(some_list):
  609. return comma_sep(some_list, " or ")
  610. def comma_and(some_list):
  611. return comma_sep(some_list, " and ")
  612. def comma_sep(some_list, sep):
  613. if isinstance(some_list, (list, tuple)):
  614. # list(some_list) is done to preserve the existing list
  615. some_list = [unicode(s) for s in list(some_list)]
  616. if not some_list:
  617. return ""
  618. elif len(some_list) == 1:
  619. return some_list[0]
  620. else:
  621. some_list = ["'%s'" % s for s in some_list]
  622. return ", ".join(some_list[:-1]) + sep + some_list[-1]
  623. else:
  624. return some_list
  625. def get_base_path():
  626. import conf
  627. import os
  628. return os.path.dirname(os.path.abspath(conf.__file__))