<s> """<STR_LIT>""" <EOL> import datetime <EOL> import warnings <EOL> import six <EOL> from gcloud . bigtable . client import Client <EOL> from gcloud . bigtable . column_family import GCRuleIntersection <EOL> from gcloud . bigtable . column_family import MaxAgeGCRule <EOL> from gcloud . bigtable . column_family import MaxVersionsGCRule <EOL> from gcloud . bigtable . happybase . table import Table <EOL> from gcloud . bigtable . table import Table as _LowLevelTable <EOL> COMPAT_MODES = None <EOL> THRIFT_TRANSPORTS = None <EOL> THRIFT_PROTOCOLS = None <EOL> DEFAULT_HOST = None <EOL> DEFAULT_PORT = None <EOL> DEFAULT_TRANSPORT = None <EOL> DEFAULT_COMPAT = None <EOL> DEFAULT_PROTOCOL = None <EOL> _LEGACY_ARGS = frozenset ( ( '<STR_LIT:host>' , '<STR_LIT:port>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> _WARN = warnings . warn <EOL> _DISABLE_DELETE_MSG = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def _get_cluster ( timeout = None ) : <EOL> """<STR_LIT>""" <EOL> client_kwargs = { '<STR_LIT>' : True } <EOL> if timeout is not None : <EOL> client_kwargs [ '<STR_LIT>' ] = timeout / <NUM_LIT> <EOL> client = Client ( ** client_kwargs ) <EOL> try : <EOL> client . start ( ) <EOL> clusters , failed_zones = client . list_clusters ( ) <EOL> finally : <EOL> client . stop ( ) <EOL> if len ( failed_zones ) != <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if len ( clusters ) == <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if len ( clusters ) > <NUM_LIT:1> : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return clusters [ <NUM_LIT:0> ] <EOL> class Connection ( object ) : <EOL> """<STR_LIT>""" <EOL> _cluster = None <EOL> def __init__ ( self , timeout = None , autoconnect = True , table_prefix = None , <EOL> table_prefix_separator = '<STR_LIT:_>' , cluster = None , ** kwargs ) : <EOL> self . _handle_legacy_args ( kwargs ) <EOL> if table_prefix is not None : <EOL> if not isinstance ( table_prefix , six . string_types ) : <EOL> raise TypeError ( '<STR_LIT>' , '<STR_LIT>' , <EOL> table_prefix , type ( table_prefix ) ) <EOL> if not isinstance ( table_prefix_separator , six . string_types ) : <EOL> raise TypeError ( '<STR_LIT>' , <EOL> '<STR_LIT>' , table_prefix_separator , <EOL> type ( table_prefix_separator ) ) <EOL> self . table_prefix = table_prefix <EOL> self . table_prefix_separator = table_prefix_separator <EOL> if cluster is None : <EOL> self . _cluster = _get_cluster ( timeout = timeout ) <EOL> else : <EOL> if timeout is not None : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . _cluster = cluster . copy ( ) <EOL> if autoconnect : <EOL> self . open ( ) <EOL> self . _initialized = True <EOL> @ staticmethod <EOL> def _handle_legacy_args ( arguments_dict ) : <EOL> """<STR_LIT>""" <EOL> common_args = _LEGACY_ARGS . intersection ( six . iterkeys ( arguments_dict ) ) <EOL> if common_args : <EOL> all_args = '<STR_LIT:U+002CU+0020>' . join ( common_args ) <EOL> message = ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( all_args , ) ) <EOL> _WARN ( message ) <EOL> for arg_name in common_args : <EOL> arguments_dict . pop ( arg_name ) <EOL> if arguments_dict : <EOL> unexpected_names = arguments_dict . keys ( ) <EOL> raise TypeError ( '<STR_LIT>' , unexpected_names ) <EOL> def open ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _cluster . _client . start ( ) <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _cluster . _client . stop ( ) <EOL> def __del__ ( self ) : <EOL> if self . _cluster is not None : <EOL> self . close ( ) <EOL> def _table_name ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if self . table_prefix is None : <EOL> return name <EOL> return self . table_prefix + self . table_prefix_separator + name <EOL> def table ( self , name , use_prefix = True ) : <EOL> """<STR_LIT>""" <EOL> if use_prefix : <EOL> name = self . _table_name ( name ) <EOL> return Table ( name , self ) <EOL> def tables ( self ) : <EOL> """<STR_LIT>""" <EOL> low_level_table_instances = self . _cluster . list_tables ( ) <EOL> table_names = [ table_instance . table_id <EOL> for table_instance in low_level_table_instances ] <EOL> if self . table_prefix is not None : <EOL> prefix = self . _table_name ( '<STR_LIT>' ) <EOL> offset = len ( prefix ) <EOL> table_names = [ name [ offset : ] for name in table_names <EOL> if name . startswith ( prefix ) ] <EOL> return table_names <EOL> def create_table ( self , name , families ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( families , dict ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if not families : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( name , ) ) <EOL> gc_rule_dict = { } <EOL> for column_family_name , option in families . items ( ) : <EOL> if isinstance ( column_family_name , six . binary_type ) : <EOL> column_family_name = column_family_name . decode ( '<STR_LIT:utf-8>' ) <EOL> if column_family_name . endswith ( '<STR_LIT::>' ) : <EOL> column_family_name = column_family_name [ : - <NUM_LIT:1> ] <EOL> gc_rule_dict [ column_family_name ] = _parse_family_option ( option ) <EOL> name = self . _table_name ( name ) <EOL> low_level_table = _LowLevelTable ( name , self . _cluster ) <EOL> low_level_table . create ( ) <EOL> for column_family_name , gc_rule in gc_rule_dict . items ( ) : <EOL> column_family = low_level_table . column_family ( <EOL> column_family_name , gc_rule = gc_rule ) <EOL> column_family . create ( ) <EOL> def delete_table ( self , name , disable = False ) : <EOL> """<STR_LIT>""" <EOL> if disable : <EOL> _WARN ( _DISABLE_DELETE_MSG ) <EOL> name = self . _table_name ( name ) <EOL> _LowLevelTable ( name , self . _cluster ) . delete ( ) <EOL> def enable_table ( self , name ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def disable_table ( self , name ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def is_table_enabled ( self , name ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def compact_table ( self , name , major = False ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def _parse_family_option ( option ) : <EOL> """<STR_LIT>""" <EOL> result = option <EOL> if isinstance ( result , dict ) : <EOL> if not set ( result . keys ( ) ) <= set ( [ '<STR_LIT>' , '<STR_LIT>' ] ) : <EOL> all_keys = '<STR_LIT:U+002CU+0020>' . join ( repr ( key ) for key in result . keys ( ) ) <EOL> warning_msg = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( all_keys , ) ) <EOL> _WARN ( warning_msg ) <EOL> max_num_versions = result . get ( '<STR_LIT>' ) <EOL> max_age = None <EOL> if '<STR_LIT>' in result : <EOL> max_age = datetime . timedelta ( seconds = result [ '<STR_LIT>' ] ) <EOL> versions_rule = age_rule = None <EOL> if max_num_versions is not None : <EOL> versions_rule = MaxVersionsGCRule ( max_num_versions ) <EOL> if max_age is not None : <EOL> age_rule = MaxAgeGCRule ( max_age ) <EOL> if versions_rule is None : <EOL> result = age_rule <EOL> else : <EOL> if age_rule is None : <EOL> result = versions_rule <EOL> else : <EOL> result = GCRuleIntersection ( rules = [ age_rule , versions_rule ] ) <EOL> return result </s>
