<commit_before>import time
import sys
import os
import signal
import random
import threading
from urlparse import urlparse
from datetime import datetime, timedelta
from registry import NewRegistry

from boto.dynamodb2.layer1 import DynamoDBConnection
from boto.dynamodb2 import connect_to_region
from boto.dynamodb2.items import Item
from boto.dynamodb2.exceptions import ItemNotFound

HEARTBEAT_INTERVAL = 1 # 1second
TTL = 5

class Service(object):

    def __init__(self, Name, Endpoint, Expiry=None, stopHeartbeat=False, *args, **kwargs):
        self.Name = Name.get('S') if isinstance(Name, dict) else Name
        self.Endpoint = Endpoint.get('S') if isinstance(Endpoint, dict) else Endpoint
        self.Expiry = int(Expiry.get('N')) if isinstance(Expiry, dict) else Expiry # unix timestamp
        self.stopHeartbeat = int(stopHeartbeat.get('N')) if isinstance(stopHeartbeat, dict) else stopHeartbeat

    # Unregister the service
    def Unregister(self):
        self.stopHeartbeat = True

class BaseConfig(object):

    def __init__(self, registry_name='', *args, **kwargs):
        self.registry_name = registry_name
        self.region = ''

    def GetRegistryName(self):
        """
        Get registry table name
        """
        return self.registry_name or 'roster'

    def GetHashKey(self):
        return self.name

    def GetRangeKey(self):
        return self.endpoint
        
    def GetConnection(self):
        raise NotImplementedError("Subclasses shouls implement this!")

class WebServiceConfig(BaseConfig):
    from boto.dynamodb2.layer1 import DynamoDBConnection

    def SetRegion(self, region):
        self.region = region

    def GetConnection(self):
        # Environment var
        if self.region == '':
            self.region = os.getenv('AWS_REGION', '') 

        # Default
        if self.region == '':
            self.region = "us-west-2"

        return connect_to_region(self.region)

class LocalConfig(BaseConfig):

    def __init__(self, endpoint, *args, **kwargs):
        super(LocalConfig, self).__init__(*args, **kwargs)
        self.endpoint = endpoint

    def GetConnection(self):
        endpoint_data = urlparse(self.endpoint)

        return DynamoDBConnection(
            host=endpoint_data.hostname,
            port=endpoint_data.port, 
            aws_secret_access_key='foo',
            is_secure=False
        )

class CleanExit(object):
    def __enter__(self):
        return self
    def __exit__(self, exc_type, exc_value, exc_tb):
        if exc_type is KeyboardInterrupt:<commit_msg>amazon credential env. variables<commit_after>import time
import sys
import os
import signal
import random
import threading
from urlparse import urlparse
from datetime import datetime, timedelta
from registry import NewRegistry

from boto.dynamodb2.layer1 import DynamoDBConnection
from boto.dynamodb2 import connect_to_region
from boto.dynamodb2.items import Item
from boto.dynamodb2.exceptions import ItemNotFound

HEARTBEAT_INTERVAL = 1 # 1second
TTL = 5

class Service(object):

    def __init__(self, Name, Endpoint, Expiry=None, stopHeartbeat=False, *args, **kwargs):
        self.Name = Name.get('S') if isinstance(Name, dict) else Name
        self.Endpoint = Endpoint.get('S') if isinstance(Endpoint, dict) else Endpoint
        self.Expiry = int(Expiry.get('N')) if isinstance(Expiry, dict) else Expiry # unix timestamp
        self.stopHeartbeat = int(stopHeartbeat.get('N')) if isinstance(stopHeartbeat, dict) else stopHeartbeat

    # Unregister the service
    def Unregister(self):
        self.stopHeartbeat = True

class BaseConfig(object):

    def __init__(self, registry_name='', *args, **kwargs):
        self.registry_name = registry_name
        self.region = ''

    def GetRegistryName(self):
        """
        Get registry table name
        """
        return self.registry_name or 'roster'

    def GetHashKey(self):
        return self.name

    def GetRangeKey(self):
        return self.endpoint
        
    def GetConnection(self):
        raise NotImplementedError("Subclasses shouls implement this!")

class WebServiceConfig(BaseConfig):
    from boto.dynamodb2.layer1 import DynamoDBConnection

    def SetRegion(self, region):
        self.region = region

    def GetConnection(self):
        # Environment var
        if self.region == '':
            self.region = os.getenv('AWS_REGION', '') 

        # Default
        if self.region == '':
            self.region = "us-west-2"

        return connect_to_region(self.region,
            aws_access_key_id=os.getenv('AWS_ACCESS_KEY_ID', ''),
            aws_secret_access_key=os.getenv('AWS_SECRET_ACCESS_KEY', '') 
        )

class LocalConfig(BaseConfig):

    def __init__(self, endpoint, *args, **kwargs):
        super(LocalConfig, self).__init__(*args, **kwargs)
        self.endpoint = endpoint

    def GetConnection(self):
        endpoint_data = urlparse(self.endpoint)

        return DynamoDBConnection(
            host=endpoint_data.hostname,
            port=endpoint_data.port, 
            aws_secret_access_key='foo',
            is_secure=False
        )

class CleanExit(object):
    def __enter__(self):
        return self
    def __exit__(self, exc_type, exc_value, exc_tb):
        if exc_type is KeyboardInterrupt:
{'fraction_non_alphanumeric': 0.09269397772323958, 'fraction_numerical': 0.004153294317538229, 'mean_word_length': 3.035034272658035, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7343988', 'n_tokens_mistral': 1591, 'n_tokens_neox': 1527, 'n_words': 386}