
    ,%                         d Z ddlmZ ddlmZ ddlmZ ddlZddlmZ ddl	mZ ddl
mZ ddlmZ ddlmZ dd	lmZ d
Zd Zd Zd ZddZddZddZddZd Zd ZddZd Zy)zIA shared library to support implementation of Firebase Test Lab commands.    )absolute_import)division)unicode_literalsN)
exceptions)apis)log)
propertiesz
          table[box](
            outcome.color(red=Fail, green=Pass, blue=Flaky, yellow=Inconclusive),
            axis_value:label=TEST_AXIS_VALUE,
            test_details:label=TEST_DETAILS
          )
c                     	 t        j                  | j                        }|d   d   }|d   d   }dj	                  ||      S # t        $ r | j                  cY S w xY w)ao  Returns a ready-to-print string representation from the http response.

  Args:
    error: the Http error response, whose content is a JSON-format string for
      most cases (e.g. invalid test dimension), but can be just a string other
      times (e.g. invalid URI for CLOUDSDK_TEST_ENDPOINT).

  Returns:
    A ready-to-print string representation of the error.
  errorcodemessagezResponseError {0}: {1})jsonloadscontent
ValueErrorformat)r   datar   r   s       0lib/googlecloudsdk/api_lib/firebase/test/util.pyGetErrorr   )   se    ::emm$D 
gv	$M)$'	!	(	(w	77 
 ==s   A AAc                 `    t        j                  | j                        }|d   d   |d   d   fS )a  Returns the individual error code and message from a JSON http response.

  Prefer using GetError(error) unless you need to examine the error code and
  take specific action on it.

  Args:
    error: the Http error response, whose content is a JSON-format string.

  Returns:
    (code, msg) A tuple holding the error code and error message string.

  Raises:
    ValueError: if the error is not in JSON format.
  r   r   r   )r   r   r   )r   r   s     r   GetErrorCodeAndMessager   >   s3     
EMM	"$	gv	Wi 8	88    c                      t         j                  j                  j                  j	                         } | st        j                  d      | S )a(  Get the user's project id from the core project properties.

  Returns:
    The id of the GCE project to use while running the test.

  Raises:
    MissingProjectError: if the user did not specify a project id via the
      --project flag or via running "gcloud config set project PROJECT_ID".
  zNo project specified. Please add --project PROJECT_ID to the command line or first run
  $ gcloud config set project PROJECT_ID)r	   VALUEScoreprojectGetr   MissingProjectError)r   s    r   
GetProjectr   Q   sI     ""**..0'	

(
(	GH H 
.r   c                     | r| d   }| d   }n,t        j                  dd      }t        j                  dd      }|j                  j                  j
                  }t        |||      j                  S )ao  Gets the device IP block catalog from the TestEnvironmentDiscoveryService.

  Args:
    context: {str:object}, The current context, which is a set of key-value
      pairs that can be used for common initialization among commands.

  Returns:
    The device IP block catalog

  Raises:
    calliope_exceptions.HttpException: If it could not connect to the service.
  testing_clienttesting_messagestestingv1)r   GetClientInstanceGetMessagesModule'TestingTestEnvironmentCatalogGetRequestEnvironmentTypeValueValuesEnumDEVICE_IP_BLOCKS_GetCatalogdeviceIpBlockCatalogcontextclientmessagesenv_types       r   GetDeviceIpBlocksr1   c   st     %&F)*H##It4F%%i6H 66%%&6&6  
VXx	0	E	EEr   c                     | r| d   }| d   }n,t        j                  dd      }t        j                  dd      }|j                  j                  j
                  }t        |||      j                  S )a`  Gets the Android catalog from the TestEnvironmentDiscoveryService.

  Args:
    context: {str:object}, The current context, which is a set of key-value
      pairs that can be used for common initialization among commands.

  Returns:
    The android catalog.

  Raises:
    calliope_exceptions.HttpException: If it could not connect to the service.
  r!   r"   r#   r$   )r   r%   r&   r'   r(   ANDROIDr*   androidDeviceCatalogr,   s       r   GetAndroidCatalogr5   }   sr     %&F)*H##It4F%%i6H 66$$WW  
VXx	0	E	EEr   c                     | r| d   }| d   }n,t        j                  dd      }t        j                  dd      }|j                  j                  j
                  }t        |||      j                  S )aX  Gets the iOS catalog from the TestEnvironmentDiscoveryService.

  Args:
    context: {str:object}, The current context, which is a set of key-value
      pairs that can be used for common initialization among commands.

  Returns:
    The iOS catalog.

  Raises:
    calliope_exceptions.HttpException: If it could not connect to the service.
  r!   r"   r#   r$   )r   r%   r&   r'   r(   IOSr*   iosDeviceCatalogr,   s       r   GetIosCatalogr9      sr     %&F)*H##It4F%%i6H 66$$SS  
VXx	0	A	AAr   c                     | r| d   }| d   }n,t        j                  dd      }t        j                  dd      }|j                  j                  j
                  }t        |||      j                  S )ap  Gets the network profile catalog from the TestEnvironmentDiscoveryService.

  Args:
    context: {str:object}, The current context, which is a set of key-value
      pairs that can be used for common initialization among commands.

  Returns:
    The network profile catalog.

  Raises:
    calliope_exceptions.HttpException: If it could not connect to the service.
  r!   r"   r#   r$   )r   r%   r&   r'   r(   NETWORK_CONFIGURATIONr*   networkConfigurationCatalogr,   s       r   GetNetworkProfileCatalogr=      st     %&F)*H##It4F%%i6H 66$$%:%:  
VXx	0	L	LLr   c                 h   t         j                  j                  j                  j	                         }|j                  ||      }	 | j                  j	                  |      S # t        j                  $ r&}t        j                  dt        |      z         d}~w t        j                  d        xY w)az  Gets a test environment catalog from the TestEnvironmentDiscoveryService.

  Args:
    client: The Testing API client object.
    messages: The Testing API messages object.
    environment_type: {enum} which EnvironmentType catalog to get.

  Returns:
    The test environment catalog.

  Raises:
    calliope_exceptions.HttpException: If it could not connect to the service.
  )environmentType	projectIdz/Unable to access the test environment catalog: Nz;Unable to access the Firebase Test Lab environment catalog.)r	   r   r   r   r   r'   testEnvironmentCatalogapitools_exceptions	HttpErrorcalliope_exceptionsHttpExceptionr   r   r   )r.   r/   environment_type
project_idrequestr   s         r   r*   r*      s       %%--113*<<& = '	
((,,W55		&	& M

+
+9HUOKM M
 IIKL	s   A" "B15!BB1c           	      H   | j                  d      }|d   }t        |      dkD  r%t        j                  ddj	                  |             t        |      dk(  rd}||fS |d   }g d	}||vr5t        j                  dd
j	                  |dj                  |                  ||fS )a  Returns a tuple representing a directive's type and resource name.

  Args:
    key: the directive key, which can be "<type>:<resource>" or "<resource>"

  Returns:
    A tuple of the directive's parsed type and resource name. If no type is
    specified, "text" will be returned as the default type.

  Raises:
    InvalidArgException: if the input format is incorrect or if the specified
    type is unsupported.
  :   robo_directiveszYInvalid format for key [{0}]. Use a colon only to separate action type and resource name.   textr   )rO   clickignorez9Unsupported action type [{0}]. Please choose one of [{1}]z, )splitlenr   InvalidArgExceptionr   join)keypartsresource_nameaction_typesupported_action_typess        r   ParseRoboDirectiveKeyr[      s     ))C.%)-Z!^

(
( FFLfG 
 	Z1_K }	%% (K800**

E
L
L499%;<>? ?
 }	%%r   c                 N    | D ]   }|j                   D ]  }d|v sd|z  c c S  " y)zCReturns a warning string iff any device model is marked deprecated.
deprecatedz|Some devices are deprecated. Learn more at https://firebase.google.com/docs/test-lab/%s/available-testing-devices#deprecatedN)tags)modelsplatformmodeltags       r   GetDeprecatedTagWarningrc     s;    ezz		79AB 	C   
r   c                 2    | j                  d      r| dd S | S )zDReturns the relative device path that can be joined with GCS bucket./rN   N)
startswith)device_paths    r   GetRelativeDevicePathrh     s     '2237QRH[Hr   )N)android)__doc__
__future__r   r   r   r   apitools.base.pyr   rB   $googlecloudsdk.api_lib.firebase.testgooglecloudsdk.api_lib.utilr   googlecloudsdk.callioperD   googlecloudsdk.corer   r	   OUTCOMES_FORMATr   r   r   r1   r5   r9   r=   r*   r[   rc   rh    r   r   <module>rs      sm     P &  '  > ; , E # *8*9&$F4F4B4M4
<%&PIr   