<commit_before>"""
@fileoverview Digital Ocean API
@author <NAME>
@version 20130315
@preserve Copyright 2013 <NAME>.

Digital Ocean API
=================
Class to get the server details via the Digital Ocean API.
"""
import urllib2
import json

class Api():
  group_name  = "Digital Ocean"
  client_key  = None
  api_key     = None
  servers     = {}

  def __init__(self, **kwargs):
    for key in kwargs:
      setattr(self, key, kwargs[key])

  def grab_servers(self):
    DROPLETS_URL  = 'https%s/droplets/?client_id=%s&api_key=%s' % \
                      ('://api.digitalocean.com',
                        self.client_key,
                        self.api_key)

    droplets      = urllib2.urlopen(DROPLETS_URL)
    try:
      data        = json.loads(droplets.read())
    except:
      raise Exception("Fatal error: No droplets found")

    for droplet in data['droplets']:
      if droplet['status'] == 'active':
        name = droplet['name']
        if name not in self.servers:
          self.servers[name] = []
        self.servers[name].append(droplet['ip_address'])

  def get_servers(self, name):
    return self.servers[name] if name in self.servers else None

<commit_msg>Return a helpful exception if API is uncontactable<commit_after>"""
@fileoverview Digital Ocean API
@author <NAME>
@version 20130315
@preserve Copyright 2013 <NAME>.

Digital Ocean API
=================
Class to get the server details via the Digital Ocean API.
"""
import urllib2
import json

class Api():
  group_name  = "Digital Ocean"
  client_key  = None
  api_key     = None
  servers     = {}

  def __init__(self, **kwargs):
    for key in kwargs:
      setattr(self, key, kwargs[key])

  def grab_servers(self):
    DROPLETS_URL  = 'https%s/droplets/?client_id=%s&api_key=%s' % \
                      ('://api.digitalocean.com',
                        self.client_key,
                        self.api_key)

    try:
      droplets      = urllib2.urlopen(DROPLETS_URL)
    except urllib2.URLError:
      raise Exception("Fatal error: Unable to connect to API")

    try:
      data        = json.loads(droplets.read())
    except:
      raise Exception("Fatal error: No droplets found")

    for droplet in data['droplets']:
      if droplet['status'] == 'active':
        name = droplet['name']
        if name not in self.servers:
          self.servers[name] = []
        self.servers[name].append(droplet['ip_address'])

  def get_servers(self, name):
    return self.servers[name] if name in self.servers else None


{'fraction_non_alphanumeric': 0.12445501387237416, 'fraction_numerical': 0.011494252873563218, 'mean_word_length': 2.580141843971631, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5790192', 'n_tokens_mistral': 828, 'n_tokens_neox': 772, 'n_words': 212}