github.com/tmlbl/deis@v1.0.2/controller/api/utils.py (about)

     1  """
     2  Helper functions used by the Deis server.
     3  """
     4  import base64
     5  import hashlib
     6  import random
     7  
     8  
     9  def generate_app_name():
    10      """Return a randomly-generated memorable name."""
    11      adjectives = [
    12          'ablest', 'absurd', 'actual', 'allied', 'artful', 'atomic', 'august',
    13          'bamboo', 'benign', 'blonde', 'blurry', 'bolder', 'breezy', 'bubbly',
    14          'candid', 'casual', 'cheery', 'classy', 'clever', 'convex', 'cubist',
    15          'dainty', 'dapper', 'decent', 'deluxe', 'docile', 'dogged', 'drafty',
    16          'earthy', 'easier', 'edible', 'elfish', 'excess', 'exotic', 'expert',
    17          'fabled', 'famous', 'feline', 'finest', 'flaxen', 'folksy', 'frozen',
    18          'gaslit', 'gentle', 'gifted', 'ginger', 'global', 'golden', 'grassy',
    19          'hearty', 'hidden', 'hipper', 'honest', 'humble', 'hungry', 'hushed',
    20          'iambic', 'iconic', 'indoor', 'inward', 'ironic', 'island', 'italic',
    21          'jagged', 'jangly', 'jaunty', 'jiggly', 'jovial', 'joyful', 'junior',
    22          'kabuki', 'karmic', 'keener', 'kindly', 'kingly', 'klutzy', 'knotty',
    23          'lambda', 'leader', 'linear', 'lively', 'lonely', 'loving', 'luxury',
    24          'madras', 'marble', 'mellow', 'metric', 'modest', 'molten', 'mystic',
    25          'native', 'nearby', 'nested', 'newish', 'nickel', 'nimbus', 'nonfat',
    26          'oblong', 'offset', 'oldest', 'onside', 'orange', 'outlaw', 'owlish',
    27          'padded', 'peachy', 'pepper', 'player', 'preset', 'proper', 'pulsar',
    28          'quacky', 'quaint', 'quartz', 'queens', 'quinoa', 'quirky',
    29          'racing', 'rental', 'rising', 'rococo', 'rubber', 'rugged', 'rustic',
    30          'sanest', 'scenic', 'shadow', 'skiing', 'stable', 'steely', 'syrupy',
    31          'taller', 'tender', 'timely', 'trendy', 'triple', 'truthy', 'twenty',
    32          'ultima', 'unbent', 'unisex', 'united', 'upbeat', 'uphill', 'usable',
    33          'valued', 'vanity', 'velcro', 'velvet', 'verbal', 'violet', 'vulcan',
    34          'webbed', 'wicker', 'wiggly', 'wilder', 'wonder', 'wooden', 'woodsy',
    35          'yearly', 'yeasty', 'yeoman', 'yogurt', 'yonder', 'youthy', 'yuppie',
    36          'zaftig', 'zanier', 'zephyr', 'zeroed', 'zigzag', 'zipped', 'zircon',
    37      ]
    38      nouns = [
    39          'anaconda', 'airfield', 'aqualung', 'armchair', 'asteroid', 'autoharp',
    40          'babushka', 'bagpiper', 'barbecue', 'bookworm', 'bullfrog', 'buttress',
    41          'caffeine', 'chinbone', 'countess', 'crawfish', 'cucumber', 'cutpurse',
    42          'daffodil', 'darkroom', 'doghouse', 'dragster', 'drumroll', 'duckling',
    43          'earthman', 'eggplant', 'electron', 'elephant', 'espresso', 'eyetooth',
    44          'falconer', 'farmland', 'ferryman', 'fireball', 'footwear', 'frosting',
    45          'gadabout', 'gasworks', 'gatepost', 'gemstone', 'goldfish', 'greenery',
    46          'handbill', 'hardtack', 'hawthorn', 'headwind', 'henhouse', 'huntress',
    47          'icehouse', 'idealist', 'inchworm', 'inventor', 'insignia', 'ironwood',
    48          'jailbird', 'jamboree', 'jerrycan', 'jetliner', 'jokester', 'joyrider',
    49          'kangaroo', 'kerchief', 'keypunch', 'kingfish', 'knapsack', 'knothole',
    50          'ladybird', 'lakeside', 'lambskin', 'larkspur', 'lollipop', 'lungfish',
    51          'macaroni', 'mackinaw', 'magician', 'mainsail', 'mongoose', 'moonrise',
    52          'nailhead', 'nautilus', 'neckwear', 'newsreel', 'novelist', 'nuthatch',
    53          'occupant', 'offering', 'offshoot', 'original', 'organism', 'overalls',
    54          'painting', 'pamphlet', 'paneling', 'pendulum', 'playroom', 'ponytail',
    55          'quacking', 'quadrant', 'queendom', 'question', 'quilting', 'quotient',
    56          'rabbitry', 'radiator', 'renegade', 'ricochet', 'riverbed', 'rucksack',
    57          'sailfish', 'sandwich', 'sculptor', 'seashore', 'seedcake', 'stickpin',
    58          'tabletop', 'tailbone', 'teamwork', 'teaspoon', 'traverse', 'turbojet',
    59          'umbrella', 'underdog', 'undertow', 'unicycle', 'universe', 'uptowner',
    60          'vacation', 'vagabond', 'valkyrie', 'variable', 'villager', 'vineyard',
    61          'waggoner', 'waxworks', 'waterbed', 'wayfarer', 'whitecap', 'woodshed',
    62          'yachting', 'yardbird', 'yearbook', 'yearling', 'yeomanry', 'yodeling',
    63          'zaniness', 'zeppelin', 'ziggurat', 'zirconia', 'zoologer', 'zucchini',
    64      ]
    65      return "{}-{}".format(
    66          random.choice(adjectives), random.choice(nouns))
    67  
    68  
    69  def dict_diff(dict1, dict2):
    70      """
    71      Returns the added, changed, and deleted items in dict1 compared with dict2.
    72  
    73      :param dict1: a python dict
    74      :param dict2: an earlier version of the same python dict
    75      :return: a new dict, with 'added', 'changed', and 'removed' items if
    76               any were found.
    77  
    78      >>> d1 = {1: 'a'}
    79      >>> dict_diff(d1, d1)
    80      {}
    81      >>> d2 = {1: 'a', 2: 'b'}
    82      >>> dict_diff(d2, d1)
    83      {'added': {2: 'b'}}
    84      >>> d3 = {2: 'B', 3: 'c'}
    85      >>> expected = {'added': {3: 'c'}, 'changed': {2: 'B'}, 'deleted': {1: 'a'}}
    86      >>> dict_diff(d3, d2) == expected
    87      True
    88      """
    89      diff = {}
    90      set1, set2 = set(dict1), set(dict2)
    91      # Find items that were added to dict2
    92      diff['added'] = {k: dict1[k] for k in (set1 - set2)}
    93      # Find common items whose values differ between dict1 and dict2
    94      diff['changed'] = {
    95          k: dict1[k] for k in (set1 & set2) if dict1[k] != dict2[k]
    96      }
    97      # Find items that were deleted from dict2
    98      diff['deleted'] = {k: dict2[k] for k in (set2 - set1)}
    99      return {k: diff[k] for k in diff if diff[k]}
   100  
   101  
   102  def fingerprint(key):
   103      """
   104      Return the fingerprint for an SSH Public Key
   105      """
   106      key = base64.b64decode(key.strip().split()[1].encode('ascii'))
   107      fp_plain = hashlib.md5(key).hexdigest()
   108      return ':'.join(a + b for a, b in zip(fp_plain[::2], fp_plain[1::2]))
   109  
   110  
   111  def encode(obj):
   112      """Return UTF-8 encoding for string objects."""
   113      if isinstance(obj, basestring):
   114          return obj.encode('utf-8')
   115      else:
   116          return obj
   117  
   118  
   119  if __name__ == "__main__":
   120      import doctest
   121      doctest.testmod()