Electronic stock management. -- not updated anymore
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

utils.py 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import re
  2. from unidecode import unidecode
  3. from models import Apitoken
  4. import random
  5. import string
  6. import os
  7. _punct_re = re.compile(r'[\t !"#$%&\'()*\-/<=>?@\[\\\]^_`{|},.]+')
  8. def slugify(text, delim=u'_'):
  9. """
  10. Generate a slug in ASCII-only form
  11. :param text: Text to slugify
  12. :param delim: Delimiter to join
  13. :return: str slug
  14. """
  15. result = []
  16. for word in _punct_re.split(text.lower()):
  17. result.extend(unidecode(word).split())
  18. return delim.join(result)
  19. def gen_random_str(size=10):
  20. """
  21. Generate random string
  22. :param size: Size of string
  23. :return: Random string
  24. """
  25. return ''.join(random.choice(string.ascii_uppercase +
  26. string.ascii_lowercase +
  27. string.digits) for _ in range(size))
  28. def path_or_none(fbase, ftype, fname):
  29. """
  30. Return path or none
  31. :param fbase: Base directory
  32. :param ftype: Type directory
  33. :param fname: Filename
  34. :return: Full path or None
  35. """
  36. if not fbase or not ftype or not fname:
  37. return None
  38. fpath = os.path.join(fbase, ftype, fname)
  39. return fpath if os.path.isfile(fpath) else None
  40. def generate_uniques_apitoken():
  41. """
  42. Generate an unique API Token
  43. :return: Dict of token and secret pair
  44. """
  45. while 1:
  46. tmp_token = gen_random_str(20)
  47. tmp_secret = gen_random_str(20)
  48. blip = Apitoken.query.filter_by(token=tmp_token,
  49. secret=tmp_secret).first()
  50. if blip:
  51. continue
  52. else:
  53. return {"token": tmp_token, "secret": tmp_secret}
  54. return None
  55. def f7(seq):
  56. seen = set()
  57. seen_add = seen.add
  58. return [x for x in seq if not (x in seen or seen_add(x))]