66import code42cli .util as util
77
88
9+ _DEFAULT_VALUE = u"__DEFAULT__"
10+
11+
912class ConfigurationKeys (object ):
1013 USER_SECTION = u"Code42"
1114 AUTHORITY_KEY = u"c42_authority_url"
@@ -16,33 +19,37 @@ class ConfigurationKeys(object):
1619
1720
1821def get_config_profile ():
22+ """Get your config file profile."""
1923 parser = ConfigParser ()
2024 if not profile_has_been_set ():
21- util .print_error ("Profile is not set ." )
22- print ("" )
23- print ("To set, use: " )
24- util .print_bold ("\t code42 profile set -s <authority-URL> -u <username>" )
25- print ("" )
25+ util .print_error (u "Profile has not completed setup ." )
26+ print (u "" )
27+ print (u "To set, use: " )
28+ util .print_bold (u "\t code42 profile set -s <authority-URL> -u <username>" )
29+ print (u "" )
2630 exit (1 )
2731
2832 return _get_config_profile_from_parser (parser )
2933
3034
31- def mark_as_set ():
35+ def profile_has_been_set ():
36+ """Whether you have, at one point in time, set your username and authority server URL."""
3237 parser = ConfigParser ()
3338 config_file_path = _get_config_file_path ()
3439 parser .read (config_file_path )
3540 settings = parser [ConfigurationKeys .INTERNAL_SECTION ]
36- settings [ConfigurationKeys .HAS_SET_PROFILE_KEY ] = "True"
37- _save (parser , ConfigurationKeys .HAS_SET_PROFILE_KEY )
41+ return settings .getboolean (ConfigurationKeys .HAS_SET_PROFILE_KEY )
3842
3943
40- def profile_has_been_set ():
44+ def mark_as_set_if_complete ():
45+ if not _profile_can_be_set ():
46+ return
4147 parser = ConfigParser ()
4248 config_file_path = _get_config_file_path ()
4349 parser .read (config_file_path )
4450 settings = parser [ConfigurationKeys .INTERNAL_SECTION ]
45- return settings .getboolean (ConfigurationKeys .HAS_SET_PROFILE_KEY )
51+ settings [ConfigurationKeys .HAS_SET_PROFILE_KEY ] = u"True"
52+ _save (parser , ConfigurationKeys .HAS_SET_PROFILE_KEY )
4653
4754
4855def set_username (new_username ):
@@ -66,22 +73,31 @@ def set_ignore_ssl_errors(new_value):
6673 _save (parser , ConfigurationKeys .IGNORE_SSL_ERRORS_KEY )
6774
6875
69- def _get_config_file_path ():
70- path = "{}config.cfg" .format (util .get_user_project_path ())
71- if not os .path .exists (path ) or not _verify_config_file (path ):
72- _create_new_config_file (path )
73-
74- return path
76+ def _profile_can_be_set ():
77+ """Whether your current username and authority URL are set,
78+ but your profile has not been marked as set.
79+ """
80+ parser = ConfigParser ()
81+ profile = _get_config_profile_from_parser (parser )
82+ username = profile [ConfigurationKeys .USERNAME_KEY ]
83+ authority = profile [ConfigurationKeys .AUTHORITY_KEY ]
84+ return username != _DEFAULT_VALUE and authority != _DEFAULT_VALUE and not profile_has_been_set ()
7585
7686
7787def _get_config_profile_from_parser (parser ):
7888 config_file_path = _get_config_file_path ()
7989 parser .read (config_file_path )
8090 config = parser [ConfigurationKeys .USER_SECTION ]
81- config .ignore_ssl_errors = config .getboolean (ConfigurationKeys .IGNORE_SSL_ERRORS_KEY )
8291 return config
8392
8493
94+ def _get_config_file_path ():
95+ path = u"{}config.cfg" .format (util .get_user_project_path ())
96+ if not os .path .exists (path ) or not _verify_config_file (path ):
97+ _create_new_config_file (path )
98+ return path
99+
100+
85101def _create_new_config_file (path ):
86102 config_parser = ConfigParser ()
87103 config_parser = _create_user_section (config_parser )
@@ -93,25 +109,28 @@ def _create_user_section(parser):
93109 keys = ConfigurationKeys
94110 parser .add_section (keys .USER_SECTION )
95111 parser [keys .USER_SECTION ] = {}
96- parser [keys .USER_SECTION ][keys .AUTHORITY_KEY ] = "null"
97- parser [keys .USER_SECTION ][keys .USERNAME_KEY ] = "null"
98- parser [keys .USER_SECTION ][keys .IGNORE_SSL_ERRORS_KEY ] = "False"
112+ parser [keys .USER_SECTION ][keys .AUTHORITY_KEY ] = _DEFAULT_VALUE
113+ parser [keys .USER_SECTION ][keys .USERNAME_KEY ] = _DEFAULT_VALUE
114+ parser [keys .USER_SECTION ][keys .IGNORE_SSL_ERRORS_KEY ] = u "False"
99115 return parser
100116
101117
102118def _create_internal_section (parser ):
103119 keys = ConfigurationKeys
104120 parser .add_section (keys .INTERNAL_SECTION )
105121 parser [keys .INTERNAL_SECTION ] = {}
106- parser [keys .INTERNAL_SECTION ][keys .HAS_SET_PROFILE_KEY ] = "False"
122+ parser [keys .INTERNAL_SECTION ][keys .HAS_SET_PROFILE_KEY ] = u "False"
107123 return parser
108124
109125
110126def _save (parser , key = None , path = None ):
111127 path = _get_config_file_path () if path is None else path
112- util .open_file (path , "w+" , lambda f : parser .write (f ))
128+ util .open_file (path , u "w+" , lambda f : parser .write (f ))
113129 if key is not None :
114- print ("'{}' has been successfully updated" .format (key ))
130+ if key == ConfigurationKeys .HAS_SET_PROFILE_KEY :
131+ print (u"You have completed setting up your profile!" )
132+ else :
133+ print (u"'{}' has been successfully updated" .format (key ))
115134
116135
117136def _verify_config_file (path ):
0 commit comments