
'''simple docstring'''

import subprocess
from argparse import ArgumentParser
from typing import List, Union

from huggingface_hub.hf_api import HfFolder, create_repo, whoami
from requests.exceptions import HTTPError

from . import BaseTransformersCLICommand







class 	__lowerCAmelCase    (  __a				):



     @staticmethod
     def     snake_case_ (lowerCAmelCase__	):
          _UpperCAmelCase					:  int         =    parser.add_parser("""login"""						,	help="""Log in using the same credentials as on huggingface.co"""	)
          login_parser.set_defaults(func=lambda lowerCAmelCase__	: LoginCommand(lowerCAmelCase__	)	)
          _UpperCAmelCase					:  Optional[Any]         =    parser.add_parser("""whoami"""						,	help="""Find out which huggingface.co account you are logged in as."""	)
          whoami_parser.set_defaults(func=lambda lowerCAmelCase__	: WhoamiCommand(lowerCAmelCase__	)	)
          _UpperCAmelCase					:  Tuple         =    parser.add_parser("""logout"""						,	help="""Log out"""	)
          logout_parser.set_defaults(func=lambda lowerCAmelCase__	: LogoutCommand(lowerCAmelCase__	)	)

          # new system: git-based repo system
          _UpperCAmelCase					:  int         =    parser.add_parser(
              """repo"""						,	help="""Deprecated: use `huggingface-cli` instead. Commands to interact with your huggingface.co repos."""						,	)
          _UpperCAmelCase					:  str         =    repo_parser.add_subparsers(
              help="""Deprecated: use `huggingface-cli` instead. huggingface.co repos related commands"""	)
          _UpperCAmelCase					:  int         =    repo_subparsers.add_parser(
              """create"""						,	help="""Deprecated: use `huggingface-cli` instead. Create a new repo on huggingface.co"""	)
          repo_create_parser.add_argument(
              """name"""						,	type=lowerCAmelCase__						,	help="""Name for your model's repo. Will be namespaced under your username to build the model id."""						,	)
          repo_create_parser.add_argument("""--organization"""						,	type=lowerCAmelCase__						,	help="""Optional: organization namespace."""	)
          repo_create_parser.add_argument("""-y"""						,	"""--yes"""						,	action="""store_true"""						,	help="""Optional: answer Yes to the prompt"""	)
          repo_create_parser.set_defaults(func=lambda lowerCAmelCase__	: RepoCreateCommand(lowerCAmelCase__	)	)







class 	__lowerCAmelCase    :
     snake_case   :    int							      =      """\u001b[1m"""
     snake_case   :    Tuple							      =      """\u001b[31m"""
     snake_case   :    str							      =      """\u001b[90m"""
     snake_case   :    int							      =      """\u001b[0m"""



     @classmethod
     def     snake_case_ (cls						,	lowerCAmelCase__	):
          return F"{cls._bold}{s}{cls._reset}"



     @classmethod
     def     snake_case_ (cls						,	lowerCAmelCase__	):
          return F"{cls._bold}{cls._red}{s}{cls._reset}"



     @classmethod
     def     snake_case_ (cls						,	lowerCAmelCase__	):
          return F"{cls._gray}{s}{cls._reset}"







def __A  (    lowerCAmelCase_ ,       lowerCAmelCase_       ):
     _UpperCAmelCase					:  List[Any]         =    [max(len(str(lowerCAmelCase_       )       ) for x in col       ) for col in zip(*lowerCAmelCase_ ,       lowerCAmelCase_       )]
     _UpperCAmelCase					:  Any         =    ("""{{:{}}} """ * len(lowerCAmelCase_       )).format(*lowerCAmelCase_       )
     _UpperCAmelCase					:  Tuple         =    []
     lines.append(row_format.format(*lowerCAmelCase_       )       )
     lines.append(row_format.format(*["""-""" * w for w in col_widths]       )       )
     for row in rows:
          lines.append(row_format.format(*lowerCAmelCase_       )       )
     return "\n".join(lowerCAmelCase_       )







class 	__lowerCAmelCase    :



     def __init__(self						,	lowerCAmelCase__	):
          _UpperCAmelCase					:  Optional[int]         =    args







class 	__lowerCAmelCase    (  __a				):



     def     snake_case_ (self	):
          print(
              ANSI.red(
                  """ERROR! `huggingface-cli login` uses an outdated login mechanism """
                  """that is not compatible with the Hugging Face Hub backend anymore. """
                  """Please use `huggingface-cli login instead."""	)	)







class 	__lowerCAmelCase    (  __a				):



     def     snake_case_ (self	):
          print(
              ANSI.red(
                  """WARNING! `transformers-cli whoami` is deprecated and will be removed in v5. Please use """
                  """`huggingface-cli whoami` instead."""	)	)
          _UpperCAmelCase					:  Optional[Any]         =    HfFolder.get_token()
          if token is None:
               print("""Not logged in"""	)
               exit()
          try:
               _UpperCAmelCase  ,     _UpperCAmelCase					:  str         =    whoami(lowerCAmelCase__	)
               print(lowerCAmelCase__	)
               if orgs:
                    print(ANSI.bold("""orgs: """	)						,	""",""".join(lowerCAmelCase__	)	)
          except HTTPError as e:
               print(lowerCAmelCase__	)
               print(ANSI.red(e.response.text	)	)
               exit(1	)







class 	__lowerCAmelCase    (  __a				):



     def     snake_case_ (self	):
          print(
              ANSI.red(
                  """ERROR! `transformers-cli logout` uses an outdated logout mechanism """
                  """that is not compatible with the Hugging Face Hub backend anymore. """
                  """Please use `huggingface-cli logout instead."""	)	)







class 	__lowerCAmelCase    (  __a				):



     def     snake_case_ (self	):
          print(
              ANSI.red(
                  """WARNING! Managing repositories through transformers-cli is deprecated. """
                  """Please use `huggingface-cli` instead."""	)	)
          _UpperCAmelCase					:  str         =    HfFolder.get_token()
          if token is None:
               print("""Not logged in"""	)
               exit(1	)
          try:
               _UpperCAmelCase					:  Optional[int]         =    subprocess.check_output(["""git""", """--version"""]	).decode("""utf-8"""	)
               print(ANSI.gray(stdout.strip()	)	)
          except FileNotFoundError:
               print("""Looks like you do not have git installed, please install."""	)

          try:
               _UpperCAmelCase					:  str         =    subprocess.check_output(["""git-lfs""", """--version"""]	).decode("""utf-8"""	)
               print(ANSI.gray(stdout.strip()	)	)
          except FileNotFoundError:
               print(
                   ANSI.red(
                       """Looks like you do not have git-lfs installed, please install."""
                       """ You can install from https://git-lfs.github.com/."""
                       """ Then run `git lfs install` (you only have to do this once)."""	)	)
          print(""""""	)

          _UpperCAmelCase  ,     _UpperCAmelCase					:  Optional[Any]         =    whoami(lowerCAmelCase__	)
          _UpperCAmelCase					:  List[str]         =    self.args.organization if self.args.organization is not None else user
          _UpperCAmelCase					:  Union[str, Any]         =    F"{namespace}/{self.args.name}"
          print(F"You are about to create {ANSI.bold(lowerCAmelCase__	)}"	)

          if not self.args.yes:
               _UpperCAmelCase					:  Dict         =    input("""Proceed? [Y/n] """	).lower()
               if not (choice == "" or choice == "y" or choice == "yes"):
                    print("""Abort"""	)
                    exit()
          try:
               _UpperCAmelCase					:  Union[str, Any]         =    create_repo(lowerCAmelCase__						,	name=self.args.name						,	organization=self.args.organization	)
          except HTTPError as e:
               print(lowerCAmelCase__	)
               print(ANSI.red(e.response.text	)	)
               exit(1	)
          print("""\nYour repo now lives at:"""	)
          print(F"  {ANSI.bold(lowerCAmelCase__	)}"	)
          print("""\nYou can clone it locally with the command below, and commit/push as usual."""	)
          print(F"\n  git clone {url}"	)
          print(""""""	)








import unittest
from dataclasses import dataclass
from typing import Optional

from transformers.utils import ModelOutput
@dataclass
class       __a					( __lowerCamelCase     ):


     """simple docstring"""


     _A				:    float
     _A				:    Optional[float]									=  None
     _A				:    Optional[float]									=  None






class       __a					( unittest.TestCase     ):


     """simple docstring"""


     def   __A				(	self			: Dict						)		->   Optional[int]:


         '''simple docstring'''





         SCREAMING_SNAKE_CASE__								=ModelOutputTest(a=3_0						)
         self.assertEqual(x.a				,3_0						)
         self.assertIsNone(x.b						)
         self.assertIsNone(x.c						)
         with self.assertRaises(_UpperCamelCase						):
             SCREAMING_SNAKE_CASE__								=x.d




     def   __A				(	self			: Tuple						)		->   Dict:


         '''simple docstring'''





         SCREAMING_SNAKE_CASE__								=ModelOutputTest(a=3_0				,b=1_0						)
         self.assertEqual(x[0]				,3_0						)
         self.assertEqual(x[1]				,1_0						)
         self.assertEqual(x[:2]				,(3_0, 1_0)						)
         self.assertEqual(x[:]				,(3_0, 1_0)						)

         SCREAMING_SNAKE_CASE__								=ModelOutputTest(a=3_0				,c=1_0						)
         self.assertEqual(x[0]				,3_0						)
         self.assertEqual(x[1]				,1_0						)
         self.assertEqual(x[:2]				,(3_0, 1_0)						)
         self.assertEqual(x[:]				,(3_0, 1_0)						)




     def   __A				(	self			: List[str]						)		->   Dict:


         '''simple docstring'''





         SCREAMING_SNAKE_CASE__								=ModelOutputTest(a=3_0				,b=1_0						)
         self.assertEqual(x["""a"""]				,3_0						)
         self.assertEqual(x["""b"""]				,1_0						)
         with self.assertRaises(_UpperCamelCase						):
             SCREAMING_SNAKE_CASE__								=x["""c"""]

         SCREAMING_SNAKE_CASE__								=ModelOutputTest(a=3_0				,c=1_0						)
         self.assertEqual(x["""a"""]				,3_0						)
         self.assertEqual(x["""c"""]				,1_0						)
         with self.assertRaises(_UpperCamelCase						):
             SCREAMING_SNAKE_CASE__								=x["""b"""]




     def   __A				(	self			: Optional[int]						)		->   List[str]:


         '''simple docstring'''





         SCREAMING_SNAKE_CASE__								=ModelOutputTest(a=3_0						)
         self.assertEqual(list(x.keys()						)				,["""a"""]						)
         self.assertEqual(list(x.values()						)				,[3_0]						)
         self.assertEqual(list(x.items()						)				,[("""a""", 3_0)]						)
         self.assertEqual(list(_UpperCamelCase						)				,["""a"""]						)

         SCREAMING_SNAKE_CASE__								=ModelOutputTest(a=3_0				,b=1_0						)
         self.assertEqual(list(x.keys()						)				,["""a""", """b"""]						)
         self.assertEqual(list(x.values()						)				,[3_0, 1_0]						)
         self.assertEqual(list(x.items()						)				,[("""a""", 3_0), ("""b""", 1_0)]						)
         self.assertEqual(list(_UpperCamelCase						)				,["""a""", """b"""]						)

         SCREAMING_SNAKE_CASE__								=ModelOutputTest(a=3_0				,c=1_0						)
         self.assertEqual(list(x.keys()						)				,["""a""", """c"""]						)
         self.assertEqual(list(x.values()						)				,[3_0, 1_0]						)
         self.assertEqual(list(x.items()						)				,[("""a""", 3_0), ("""c""", 1_0)]						)
         self.assertEqual(list(_UpperCamelCase						)				,["""a""", """c"""]						)

         with self.assertRaises(_UpperCamelCase						):
             SCREAMING_SNAKE_CASE__								=x.update({"""d""": 2_0}						)
         with self.assertRaises(_UpperCamelCase						):
             del x["a"]
         with self.assertRaises(_UpperCamelCase						):
             SCREAMING_SNAKE_CASE__								=x.pop("""a"""						)
         with self.assertRaises(_UpperCamelCase						):
             SCREAMING_SNAKE_CASE__								=x.setdefault("""d"""				,3_2						)




     def   __A				(	self			: int						)		->   List[Any]:


         '''simple docstring'''





         SCREAMING_SNAKE_CASE__								=ModelOutputTest(a=3_0						)
         SCREAMING_SNAKE_CASE__								=1_0
         self.assertEqual(x.a				,1_0						)
         self.assertEqual(x["""a"""]				,1_0						)




     def   __A				(	self			: Union[str, Any]						)		->   Optional[Any]:


         '''simple docstring'''





         SCREAMING_SNAKE_CASE__								=ModelOutputTest(a=3_0						)
         SCREAMING_SNAKE_CASE__								=1_0
         self.assertEqual(x.a				,1_0						)
         self.assertEqual(x["""a"""]				,1_0						)




     def   __A				(	self			: Dict						)		->   List[str]:


         '''simple docstring'''





         SCREAMING_SNAKE_CASE__								=ModelOutputTest({"""a""": 3_0, """b""": 1_0}						)
         self.assertEqual(list(x.keys()						)				,["""a""", """b"""]						)
         self.assertEqual(x.a				,3_0						)
         self.assertEqual(x.b				,1_0						)




     def   __A				(	self			: Optional[int]						)		->   int:


         '''simple docstring'''





         SCREAMING_SNAKE_CASE__								=ModelOutputTest([("""a""", 3_0), ("""b""", 1_0)]						)
         self.assertEqual(list(x.keys()						)				,["""a""", """b"""]						)
         self.assertEqual(x.a				,3_0						)
         self.assertEqual(x.b				,1_0						)

         with self.assertRaises(_UpperCamelCase						):
             SCREAMING_SNAKE_CASE__								=ModelOutputTest([("""a""", 3_0), (1_0, 1_0)]						)

         SCREAMING_SNAKE_CASE__								=ModelOutputTest(a=(3_0, 3_0)						)
         self.assertEqual(list(x.keys()						)				,["""a"""]						)
         self.assertEqual(x.a				,(3_0, 3_0)						)






