BVB Source Codes

rq Show utils.py Source code

Return Download rq: download utils.py Source code - Download rq Source code - Type:.py
  1. # -*- coding: utf-8 -*-
  2. """
  3. Miscellaneous helper functions.
  4.  
  5. The formatter for ANSI colored console output is heavily based on Pygments
  6. terminal colorizing code, originally by Georg Brandl.
  7. """
  8. from __future__ import (absolute_import, division, print_function,
  9.                         unicode_literals)
  10.  
  11. import calendar
  12. import datetime
  13. import importlib
  14. import logging
  15. import sys
  16. from collections import Iterable
  17.  
  18. from .compat import as_text, is_python_version, string_types
  19.  
  20.  
  21. class _Colorizer(object):
  22.     def __init__(self):
  23.         esc = "\x1b["
  24.  
  25.         self.codes = {}
  26.         self.codes[""] = ""
  27.         self.codes["reset"] = esc + "39;49;00m"
  28.  
  29.         self.codes["bold"] = esc + "01m"
  30.         self.codes["faint"] = esc + "02m"
  31.         self.codes["standout"] = esc + "03m"
  32.         self.codes["underline"] = esc + "04m"
  33.         self.codes["blink"] = esc + "05m"
  34.         self.codes["overline"] = esc + "06m"
  35.  
  36.         dark_colors = ["black", "darkred", "darkgreen", "brown", "darkblue",
  37.                        "purple", "teal", "lightgray"]
  38.         light_colors = ["darkgray", "red", "green", "yellow", "blue",
  39.                         "fuchsia", "turquoise", "white"]
  40.  
  41.         x = 30
  42.         for d, l in zip(dark_colors, light_colors):
  43.             self.codes[d] = esc + "%im" % x
  44.             self.codes[l] = esc + "%i;01m" % x
  45.             x += 1
  46.  
  47.         del d, l, x
  48.  
  49.         self.codes["darkteal"] = self.codes["turquoise"]
  50.         self.codes["darkyellow"] = self.codes["brown"]
  51.         self.codes["fuscia"] = self.codes["fuchsia"]
  52.         self.codes["white"] = self.codes["bold"]
  53.  
  54.         if hasattr(sys.stdout, "isatty"):
  55.             self.notty = not sys.stdout.isatty()
  56.         else:
  57.             self.notty = True
  58.  
  59.     def reset_color(self):
  60.         return self.codes["reset"]
  61.  
  62.     def colorize(self, color_key, text):
  63.         if self.notty:
  64.             return text
  65.         else:
  66.             return self.codes[color_key] + text + self.codes["reset"]
  67.  
  68.     def ansiformat(self, attr, text):
  69.         """
  70.        Format ``text`` with a color and/or some attributes::
  71.  
  72.            color       normal color
  73.            *color*     bold color
  74.            _color_     underlined color
  75.            +color+     blinking color
  76.        """
  77.         result = []
  78.         if attr[:1] == attr[-1:] == '+':
  79.             result.append(self.codes['blink'])
  80.             attr = attr[1:-1]
  81.         if attr[:1] == attr[-1:] == '*':
  82.             result.append(self.codes['bold'])
  83.             attr = attr[1:-1]
  84.         if attr[:1] == attr[-1:] == '_':
  85.             result.append(self.codes['underline'])
  86.             attr = attr[1:-1]
  87.         result.append(self.codes[attr])
  88.         result.append(text)
  89.         result.append(self.codes['reset'])
  90.         return ''.join(result)
  91.  
  92.  
  93. colorizer = _Colorizer()
  94.  
  95.  
  96. def make_colorizer(color):
  97.     """Creates a function that colorizes text with the given color.
  98.  
  99.    For example:
  100.  
  101.        green = make_colorizer('darkgreen')
  102.        red = make_colorizer('red')
  103.  
  104.    Then, you can use:
  105.  
  106.        print "It's either " + green('OK') + ' or ' + red('Oops')
  107.    """
  108.     def inner(text):
  109.         return colorizer.colorize(color, text)
  110.     return inner
  111.  
  112.  
  113. class ColorizingStreamHandler(logging.StreamHandler):
  114.  
  115.     levels = {
  116.         logging.WARNING: make_colorizer('darkyellow'),
  117.         logging.ERROR: make_colorizer('darkred'),
  118.         logging.CRITICAL: make_colorizer('darkred'),
  119.     }
  120.  
  121.     def __init__(self, exclude=None, *args, **kwargs):
  122.         self.exclude = exclude
  123.         if is_python_version((2, 6)):
  124.             logging.StreamHandler.__init__(self, *args, **kwargs)
  125.         else:
  126.             super(ColorizingStreamHandler, self).__init__(*args, **kwargs)
  127.  
  128.     @property
  129.     def is_tty(self):
  130.         isatty = getattr(self.stream, 'isatty', None)
  131.         return isatty and isatty()
  132.  
  133.     def format(self, record):
  134.         message = logging.StreamHandler.format(self, record)
  135.         if self.is_tty:
  136.             colorize = self.levels.get(record.levelno, lambda x: x)
  137.  
  138.             # Don't colorize any traceback
  139.             parts = message.split('\n', 1)
  140.             parts[0] = " ".join([parts[0].split(" ", 1)[0], colorize(parts[0].split(" ", 1)[1])])
  141.  
  142.             message = '\n'.join(parts)
  143.  
  144.         return message
  145.  
  146.  
  147. def import_attribute(name):
  148.     """Return an attribute from a dotted path name (e.g. "path.to.func")."""
  149.     module_name, attribute = name.rsplit('.', 1)
  150.     module = importlib.import_module(module_name)
  151.     return getattr(module, attribute)
  152.  
  153.  
  154. def utcnow():
  155.     return datetime.datetime.utcnow()
  156.  
  157.  
  158. def utcformat(dt):
  159.     return dt.strftime(as_text('%Y-%m-%dT%H:%M:%SZ'))
  160.  
  161.  
  162. def utcparse(string):
  163.     try:
  164.         return datetime.datetime.strptime(string, '%Y-%m-%dT%H:%M:%SZ')
  165.     except ValueError:
  166.         # This catches RQ < 0.4 datetime format
  167.         return datetime.datetime.strptime(string, '%Y-%m-%dT%H:%M:%S.%f+00:00')
  168.  
  169.  
  170. def first(iterable, default=None, key=None):
  171.     """
  172.    Return first element of `iterable` that evaluates true, else return None
  173.    (or an optional default value).
  174.  
  175.    >>> first([0, False, None, [], (), 42])
  176.    42
  177.  
  178.    >>> first([0, False, None, [], ()]) is None
  179.    True
  180.  
  181.    >>> first([0, False, None, [], ()], default='ohai')
  182.    'ohai'
  183.  
  184.    >>> import re
  185.    >>> m = first(re.match(regex, 'abc') for regex in ['b.*', 'a(.*)'])
  186.    >>> m.group(1)
  187.    'bc'
  188.  
  189.    The optional `key` argument specifies a one-argument predicate function
  190.    like that used for `filter()`.  The `key` argument, if supplied, must be
  191.    in keyword form.  For example:
  192.  
  193.    >>> first([1, 1, 3, 4, 5], key=lambda x: x % 2 == 0)
  194.    4
  195.  
  196.    """
  197.     if key is None:
  198.         for el in iterable:
  199.             if el:
  200.                 return el
  201.     else:
  202.         for el in iterable:
  203.             if key(el):
  204.                 return el
  205.  
  206.     return default
  207.  
  208.  
  209. def is_nonstring_iterable(obj):
  210.     """Returns whether the obj is an iterable, but not a string"""
  211.     return isinstance(obj, Iterable) and not isinstance(obj, string_types)
  212.  
  213.  
  214. def ensure_list(obj):
  215.     """
  216.    When passed an iterable of objects, does nothing, otherwise, it returns
  217.    a list with just that object in it.
  218.    """
  219.     return obj if is_nonstring_iterable(obj) else [obj]
  220.  
  221.  
  222. def current_timestamp():
  223.     """Returns current UTC timestamp"""
  224.     return calendar.timegm(datetime.datetime.utcnow().utctimetuple())
  225.  
  226.  
  227. def enum(name, *sequential, **named):
  228.     values = dict(zip(sequential, range(len(sequential))), **named)
  229.  
  230.     # NOTE: Yes, we *really* want to cast using str() here.
  231.     # On Python 2 type() requires a byte string (which is str() on Python 2).
  232.     # On Python 3 it does not matter, so we'll use str(), which acts as
  233.     # a no-op.
  234.     return type(str(name), (), values)
  235.  
downloadutils.py Source code - Download rq Source code
Related Source Codes/Software:
amazon-dsstne - Deep Scalable Sparse Tensor Network Engine (DSSTNE... 2017-01-08
webpack-demos - a collection of simple demos of Webpack 2017-01-08
Squire - HTML5 rich text editor. Try the demo integration a... 2017-01-08
thor - Thor is a toolkit for building powerful command-li... 2017-01-08
glide - Package Management for Golang h... 2017-01-08
emmet-vim - emmet for vim: http://emmet.io/ ... 2017-01-08
prose - A Content Editor for GitHub. ht... 2017-01-08
sshrc - ring your .bashrc, .vimrc, etc. with you when you ... 2017-01-08
typed.js - A jQuery typing animation script. ... 2017-01-08
find - High-precision indoor positioning framework for mo... 2017-01-08
discover-flask - Full Stack Web Development with Flask. ... 2017-01-12
spring-mvc-showcase - Demonstrates the features of the Spring MVC web fr... 2017-01-12
tushare - TuShare is a utility for crawling historical data ... 2017-01-12
raml-spec - RAML Specification http://raml.... 2017-01-12
android-stackblur - Android StackBlur is a library that can perform a ... 2017-01-12
sound-redux - A Soundcloud client built with React / Redux ... 2017-01-12
httpstat - curl statistics made simple 2017-01-12
vim - Vim configuration file and plug-ins 2017-01-12
appframework - The definitive HTML5 mobile javascript framework ... 2017-01-12
BaiduExporter - Assistant for Baidu to export download links to ar... 2017-01-11

 Back to top