def get_template_path_from_project(): """Get current template path when in a project""" if not configurationhandler.project_config: configurationhandler.loadConfig() return os.path.abspath( tools.get_template_directory( configurationhandler.project_config['template']))
def get_current_name(path=None): '''return project name if we are in a project''' configurationhandler.loadConfig(can_stop=False, config_file_path=path) try: return configurationhandler.project_config['project'] except KeyError: return None
def copy_dirs_from_template(dirs = ["."], extra_substitutions = []): if not configurationhandler.project_config: configurationhandler.loadConfig() project_name = configurationhandler.project_config['project'] pathname = get_template_path_from_project() abs_path_project_root = os.path.join(pathname, 'project_root') py_name = python_name(project_name) sentence_name, camel_case_name = conventional_names(project_name) substitutions = [("project_name",project_name), ("camel_case_name",camel_case_name), ("python_name",py_name), ("sentence_name",sentence_name),] + extra_substitutions for top_dir in dirs: full_top_dir = os.path.join(abs_path_project_root, top_dir) for root, subdirs, files in os.walk(full_top_dir): try: relative_dir = root.split('project_root/')[1] except: relative_dir = "" # python dir should be replace by py_name (project "pythonified" # name) if os.path.basename(relative_dir).startswith('python'): relative_dir = relative_dir.replace('python', py_name) for directory in subdirs: if os.path.basename(directory).startswith('python'): directory = directory.replace('python', py_name) if not os.path.exists(os.path.join(relative_dir, directory)): os.mkdir(os.path.join(relative_dir, directory)) for filename in files: file_from_template(root, filename, relative_dir, substitutions, overwrite = True)
def update_version_in_project_file(new_version, template_name): """Update version in .quickly file""" configurationhandler.loadConfig() if configurationhandler.project_config['template'] == template_name: configurationhandler.project_config['version'] = new_version else: configurationhandler.project_config['version_%s' % template_name] = new_version configurationhandler.saveConfig()
def run_command(command_name, template='builtins', path=None, *args): '''run a command from a template''' if not path: path = os.getcwd() configurationhandler.loadConfig(can_stop=False, config_file_path=path) try: project_template = configurationhandler.project_config['template'] except KeyError: project_template = None if template != 'builtins': project_template = template command = commands.get_all_commands()[template][command_name] return_code = command.launch(path, list(args), project_template) return return_code
def get_commands_in_context(context_path=None): """seek for available commands in current context (extern call)""" opt_template = None if configurationhandler.loadConfig(can_stop=False, config_file_path=context_path) == 0: try: opt_template = configurationhandler.project_config['template'] except KeyError: pass return(_get_commands_in_context(opt_template, context_path))
def get_completion_in_context(argv, context_path=None): """seek for available completion (command, template…) : return tuples with list of available commands and origin (default or template) """ if context_path is None: context_path = os.getcwd() else: context_path = os.path.abspath(context_path) available_completion = [] # get available templates after option if needed if argv[-2] in ("-t", "--template"): available_completion.extend( [template for template in commands.get_all_templates()]) else: # treat commands and try to get the template from the project directory if we are in a project (if not already provided by the -t option) (opt_command, opt_template) = process_command_line(argv[3:]) if not opt_template and configurationhandler.loadConfig( can_stop=False, config_file_path=context_path) == 0: try: opt_template = configurationhandler.project_config['template'] except KeyError: pass # if no command yet, check for available command if len(opt_command) == 1: available_completion.extend([ command.name for command in _get_commands_in_context( opt_template, context_path) ]) else: # ask for the command what she needs (it automatically handle the case of command followed by template and command followed by command) for command in commands.get_commands_by_criteria( name=opt_command[0] ): # as 1: is '' or the begining of a word available_completion.extend( command.shell_completion(opt_template, opt_command[1:])) # dash option completion. Some commands have dash args, but we also have them. So we take what command thought # should be the completions and add our own. We also strip anything that doesn't start with a dash, since many # completion functions are naive and just give all possible matches, even if they don't match. if argv[-1].startswith("-"): available_completion.extend([ "-h", "--help", "-t", "--template", "--staging", "--verbose", "--version" ]) available_completion = [ arg for arg in available_completion if arg.startswith('-') ] # remove duplicates and sort completion = list(set(available_completion)) completion.sort() return (completion)
def get_commands_in_context(context_path=None): """seek for available commands in current context (extern call)""" opt_template = None if configurationhandler.loadConfig(can_stop=False, config_file_path=context_path) == 0: try: opt_template = configurationhandler.project_config['template'] except KeyError: pass return (_get_commands_in_context(opt_template, context_path))
def upgrade(): # change .quickly file before getting template access return_code = configurationhandler.loadConfig(can_stop=False) if return_code == 0: # rename ubuntu-project in ubuntu-application if configurationhandler.project_config['template'] == "ubuntu-project": configurationhandler.project_config['template'] = "ubuntu-application" configurationhandler.saveConfig()
def upgrade(): # change .quickly file before getting template access return_code = configurationhandler.loadConfig(can_stop=False) if return_code == 0: # rename ubuntu-project in ubuntu-application if configurationhandler.project_config['template'] == "ubuntu-project": configurationhandler.project_config[ 'template'] = "ubuntu-application" configurationhandler.saveConfig()
def copy_setup_py_from_template(extra_substitutions=[]): if not configurationhandler.project_config: configurationhandler.loadConfig() project_name = configurationhandler.project_config['project'] pathname = get_template_path_from_project() abs_path_project_root = os.path.join(pathname, 'project_root') py_name = python_name(project_name) sentence_name, camel_case_name = conventional_names(project_name) substitutions = [ ("project_name", project_name), ("camel_case_name", camel_case_name), ("python_name", py_name), ("sentence_name", sentence_name), ] + extra_substitutions template_setup = os.path.join(abs_path_project_root, 'setup.py') project_setup = 'setup.py' # Grab quickly-owned bits in our original and copy them to user's version start_marker = "###################### DO NOT TOUCH THIS (HEAD TO THE SECOND PART) ######################" end_marker = "##################################################################################" try: with file(template_setup, 'r') as template_fd: template_contents = '' for line in template_fd.readlines(): if line == end_marker + '\n': break elif template_contents: template_contents = template_contents + line elif line == start_marker + '\n': template_contents = line for s in substitutions: pattern, sub = s template_contents = template_contents.replace(pattern, sub) update_file_content(project_setup, start_marker, end_marker, template_contents) except Exception as e: print(_("Failed to update setup.py:\n%s" % e)) sys.exit(4)
def get_project_and_template_versions(template_name): """Return project and template version""" # take template version. Default is current Quickly version template_version = quicklyconfig.__version__ template_path = tools.get_template_directory(template_name) file_command_parameters = file( os.path.join(template_path, 'commandsconfig'), 'rb') for line in file_command_parameters: fields = line.split( '#' )[0] # Suppress commentary after the value in configuration file and in full line fields = fields.split('=') # Separate variable from value # normally, we have two fields in "fields" if len(fields) == 2: targeted_property = fields[0].strip() value = fields[1].strip() if targeted_property == 'TEMPLATE_VERSION': template_version = value break # get current project version for this template. Default is no migration (ie take current version) configurationhandler.loadConfig() # if this project corresponding natively to this template if configurationhandler.project_config['template'] == template_name: try: project_version = configurationhandler.project_config['version'] except KeyError: # it was called format in quickly 0.2.x project_version = configurationhandler.project_config['format'] configurationhandler.project_config['version'] = project_version del configurationhandler.project_config['format'] configurationhandler.saveConfig() else: try: project_version = configurationhandler.project_config[ 'version_%s' % template_name] except KeyError: # initialize with an empty project version to force first upgrade project_version = '' return (project_version, template_version)
def copy_dirs_from_template(dirs=["."], extra_substitutions=[]): if not configurationhandler.project_config: configurationhandler.loadConfig() project_name = configurationhandler.project_config['project'] pathname = get_template_path_from_project() abs_path_project_root = os.path.join(pathname, 'project_root') py_name = python_name(project_name) sentence_name, camel_case_name = conventional_names(project_name) substitutions = [ ("project_name", project_name), ("camel_case_name", camel_case_name), ("python_name", py_name), ("sentence_name", sentence_name), ] + extra_substitutions for top_dir in dirs: full_top_dir = os.path.join(abs_path_project_root, top_dir) for root, subdirs, files in os.walk(full_top_dir): try: relative_dir = root.split('project_root/')[1] except: relative_dir = "" # python dir should be replace by py_name (project "pythonified" # name) if os.path.basename(relative_dir).startswith('python'): relative_dir = relative_dir.replace('python', py_name) for directory in subdirs: if os.path.basename(directory).startswith('python'): directory = directory.replace('python', py_name) if not os.path.exists(os.path.join(relative_dir, directory)): os.mkdir(os.path.join(relative_dir, directory)) for filename in files: file_from_template(root, filename, relative_dir, substitutions, overwrite=True)
def copy_setup_py_from_template(extra_substitutions = []): if not configurationhandler.project_config: configurationhandler.loadConfig() project_name = configurationhandler.project_config['project'] pathname = get_template_path_from_project() abs_path_project_root = os.path.join(pathname, 'project_root') py_name = python_name(project_name) sentence_name, camel_case_name = conventional_names(project_name) substitutions = [("project_name",project_name), ("camel_case_name",camel_case_name), ("python_name",py_name), ("sentence_name",sentence_name),] + extra_substitutions template_setup = os.path.join(abs_path_project_root, 'setup.py') project_setup = 'setup.py' # Grab quickly-owned bits in our original and copy them to user's version start_marker = "###################### DO NOT TOUCH THIS (HEAD TO THE SECOND PART) ######################" end_marker = "##################################################################################" try: with file(template_setup, 'r') as template_fd: template_contents = '' for line in template_fd.readlines(): if line == end_marker + '\n': break elif template_contents: template_contents = template_contents + line elif line == start_marker + '\n': template_contents = line for s in substitutions: pattern, sub = s template_contents = template_contents.replace(pattern, sub) update_file_content(project_setup, start_marker, end_marker, template_contents) except Exception as e: print(_("Failed to update setup.py:\n%s" % e)) sys.exit(4)
def get_project_and_template_versions(template_name): """Return project and template version""" # take template version. Default is current Quickly version template_version = quicklyconfig.__version__ template_path = tools.get_template_directory(template_name) file_command_parameters = file(os.path.join(template_path, 'commandsconfig'), 'rb') for line in file_command_parameters: fields = line.split('#')[0] # Suppress commentary after the value in configuration file and in full line fields = fields.split('=') # Separate variable from value # normally, we have two fields in "fields" if len(fields) == 2: targeted_property = fields[0].strip() value = fields[1].strip() if targeted_property == 'TEMPLATE_VERSION': template_version = value break # get current project version for this template. Default is no migration (ie take current version) configurationhandler.loadConfig() # if this project corresponding natively to this template if configurationhandler.project_config['template'] == template_name: try: project_version = configurationhandler.project_config['version'] except KeyError: # it was called format in quickly 0.2.x project_version = configurationhandler.project_config['format'] configurationhandler.project_config['version'] = project_version del configurationhandler.project_config['format'] configurationhandler.saveConfig() else: try: project_version = configurationhandler.project_config['version_%s' % template_name] except KeyError: # initialize with an empty project version to force first upgrade project_version = '' return (project_version, template_version)
def get_completion_in_context(argv, context_path=None): """seek for available completion (command, example…) : return tuples with list of available commands and origin (default or example) """ if context_path is None: context_path = os.getcwd() else: context_path = os.path.abspath(context_path) available_completion = [] # get available templates after option if needed if argv[-2] in ("-e", "--example"): available_completion.extend(['example_argument']) else: # treat commands and try to get the template from the project directory if we are in a project (if not already provided by the -t option) (opt_command, opt_example) = process_command_line(argv[3:]) if not opt_example and configurationhandler.loadConfig(can_stop=False, config_file_path=context_path) == 0: try: opt_example = configurationhandler.project_config['example'] except KeyError: pass # if no command yet, check for available command if len(opt_command) == 1: available_completion.extend([command.name for command in _get_commands_in_context(opt_example, context_path)]) else: # ask for the command what she needs (it automatically handle the case of command followed by template and command followed by command) for command in commands.get_commands_by_criteria(name=opt_command[0]): # as 1: is '' or the begining of a word available_completion.extend(command.shell_completion(opt_example, opt_command[1:])) # dash option completion. Some commands have dash args, but we also have them. So we take what command thought # should be the completions and add our own. We also strip anything that doesn't start with a dash, since many # completion functions are naive and just give all possible matches, even if they don't match. if argv[-1].startswith("-"): available_completion.extend(["-h", "--help", "-e", "--example", "--verbose", "--version"]) available_completion = [arg for arg in available_completion if arg.startswith('-')] # remove duplicates and sort completion = list(set(available_completion)) completion.sort() return (completion)
def get_template_path_from_project(): """Get current template path when in a project""" if not configurationhandler.project_config: configurationhandler.loadConfig() return os.path.abspath(tools.get_template_directory(configurationhandler.project_config['template']))