def _set_agents(self, agent_types): for host, settings in self._settings.items(): if host not in agent_types: raise ConfigurationError("Host " + host + " not yet supported") host_agents = self._host_agents[host] = {} local_agents = self._local_agents[host] = {} index_file = self._index_file.format(host=host) if not self.default_host: self._default_host = host for user in settings[self.USERS]: if self.SSH not in user: user[self.SSH] = False gist_user = GistUser(**user) username = gist_user.username if host not in self._default_users: self._default_users[host] = username user_local_base = self.get_local_host_base(host, username) File.mkdir(user_local_base) self.set_local_dir(host, username, user_local_base) # dynamically load agent class agent_type = Type.get_type(agent_types[host]) agent = agent_type(gist_user=gist_user) host_agents[username] = agent local_agent_type = Type.get_type(agent_types[self.LOCAL]) local_agent = local_agent_type(remote_agent=agent, local_base=user_local_base, index_file=index_file) local_agents[username] = local_agent if host not in self._default_users: raise ConfigurationError("Please set at least one user at " + host)
def __init__(self, config): self._local_dirs = {} self._host_agents = {} self._local_agents = {} self.default_host = None self._default_users = {} try: agent_types = self._read_config(config) self._set_agents(agent_types) Gist.init(config[self.GIST]) File.init(config[self.FILE]) except KeyError as e: raise ConfigurationError(e)
def __init__(self, gist, file_entry, path=None): assert isinstance(gist, Gist), gist assert isinstance(file_entry, dict), file_entry self._gist = gist self._path = path self._name = file_entry['name'] self._url = file_entry['url'] self._size = file_entry['size'] self._content_type = file_entry['type'] self._binary = File.is_binary(self._name, self._content_type) self._content = None
def _read_config(self, config): local_base = config[self.LOCAL_BASE] if local_base[0] == '$': base_var = local_base[1:] local_base = os.getenv(base_var) if local_base is None: raise ConfigurationError("Environment variable " + base_var + "(for local directory) is not set") self._local_base = local_base = expanduser(local_base) self._metadata_base = join(local_base, config[self.METADATA_BASE]) File.mkdir(self._metadata_base) self._index_file = join(self._metadata_base, config[self.INDEX_FILE]) commit = config[self.COMMIT] self._commit_command = commit[self.COMMAND] self._commit_message = commit[self.MESSAGE] self._exact = config[self.EXACT] self._settings = config[self.REPOS] self._default_description = config[self.DEFAULT_DESC] self._default_filename = config[self.DEFAULT_FILENAME] if not self._settings: raise ConfigurationError(self.REPOS + " has empty setting") return config[self.AGENTS]
def create_gist(self, paths, **args): host = args[self.HOST] if host is None: host = self._default_host agents = self.get_agents(host) username = args[self.USER] if username is None: username = self._default_users[host] # at least 1 agent = self._get_agent(agents, username, self._exact) public = args[self.PUBLIC] or False desc = args[self.DESC] or self._default_description filename = args[self.FILE_NAME] or self._default_filename is_binary = args[self.BINARY] files = File.file_map(paths, filename, is_binary) if is_binary: # create placeholder file first original_files = files filename = next(iter(files)) files = File.dummy_file_map(filename) gist = agent.create_gist(files, desc, public) if is_binary: # clone and push local_dir = self._download_gist(gist, **args) File.copy_files(original_files, local_dir) self._upload_gist(gist, **args) return gist
def update(congist, args): """Update description and/or file name/content for filtered gists/files.""" if args.new_desc: for gist in congist.get_gists(**vars(args)): if args.force or _confirm(gist, "update description"): gist.set_description(args.new_desc) return if args.new_name or args.new_content != '': content = None if args.new_content != '': content = File.read(args.new_content) for f in congist.get_files(**vars(args)): f.update(args.new_name, content) else: raise ParameterError("Please specify one of -D, -N, -C")
def main(): # read command args args = parser.parse_args() if args.subcommand is None: parser.print_help() return # load system config cfg_file = join(dirname(abspath(__file__)), "../congist.yml") with open(cfg_file, 'r') as sys_file: sys_config = yaml.load(sys_file, yaml.SafeLoader) user_config_path = File.config_path(sys_config['user_cfg_path']) # load user config # TODO: if user_config_path does not exist, create a template with open(user_config_path, 'r') as user_file: # override order: sys config -> user config -> command args user_config = yaml.load(user_file, yaml.SafeLoader) config = {**sys_config, **user_config} for key, value in vars(args).items(): if value is not None: config[key] = value args.function(Congist(config), args)
def get_content(self, gist_file): assert isinstance(gist_file, GistFile), gist_file return File.read(gist_file.path, gist_file.binary)