def ensure_local_plotly_files(): """Ensure that filesystem is setup/filled out in a valid way. If the config or credential files aren't filled out, then write them to the disk. """ if ensure_writable_plotly_dir(): for fn in [CREDENTIALS_FILE, CONFIG_FILE]: utils.ensure_file_exists(fn) contents = utils.load_json_dict(fn) contents_orig = contents.copy() for key, val in list(FILE_CONTENT[fn].items()): # TODO: removed type checking below, may want to revisit if key not in contents: contents[key] = val contents_keys = list(contents.keys()) for key in contents_keys: if key not in FILE_CONTENT[fn]: del contents[key] # save only if contents has changed. # This is to avoid .credentials or .config file to be overwritten randomly, # which we constantly keep experiencing # (sync issues? the file might be locked for writing by other process in file._permissions) if contents_orig.keys() != contents.keys(): utils.save_json_dict(fn, contents) else: warnings.warn("Looks like you don't have 'read-write' permission to " "your 'home' ('~') directory or to our '~/.plotly' " "directory. That means plotly's python api can't setup " "local configuration files. No problem though! You'll " "just have to sign-in using 'plotly.plotly.sign_in()'. " "For help with that: 'help(plotly.plotly.sign_in)'." "\nQuestions? Visit https://support.plot.ly")
def get_config_file(*args): """Return specified args from `~/.plotly/.config`. as tuple. Returns all if no arguments are specified. Example: get_config_file('plotly_domain') """ # Read config from file if possible config = utils.load_json_dict(CONFIG_FILE, *args) if not config: # Config could not be read, use defaults config = copy.copy(FILE_CONTENT[CONFIG_FILE]) return config
def get_credentials_file(*args): """Return specified args from `~/.plotly_credentials`. as dict. Returns all if no arguments are specified. Example: get_credentials_file('username') """ # Read credentials from file if possible credentials = utils.load_json_dict(CREDENTIALS_FILE, *args) if not credentials: # Credentials could not be read, use defaults credentials = copy.copy(FILE_CONTENT[CREDENTIALS_FILE]) return credentials
def stash_files(self): self._credentials = utils.load_json_dict(files.CREDENTIALS_FILE) self._config = utils.load_json_dict(files.CONFIG_FILE)