Class: CreateProj::Creator::PythonCreator

Inherits:
CreateProj::Creator show all
Defined in:
lib/createproj/creator/python.rb

Overview

Class for creating Ruby project

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ PythonCreator

Returns a new instance of PythonCreator.



5
6
7
8
9
10
11
12
13
# File 'lib/createproj/creator/python.rb', line 5

def initialize(*args)
  super(*args)
  @precommit_template = 'lint-pre-commit'
  @precommit_options = { linter: 'pylint', file_ext: '.py' }
  @gitignore_files = %w(*.pyc)
  @packages_to_install = {
    'pylint' => '1.4.0'
  }
end

Instance Method Details

#check_bash_scriptObject

Check that a bash script



39
40
41
42
43
44
45
46
47
48
# File 'lib/createproj/creator/python.rb', line 39

def check_bash_script
  command = <<-eos
    If you want the virtual env to be automatically activated
    upon entering the directory, follow the instructions in
    this gist (https://gist.github.com/mattjmcnaughton/4d599b62b60b59f9229f).
  eos

  puts command unless File.exist?(File.join("#{Dir.home}",
                                            '.virtualenv_sourcer'))
end

#install_dependenciesObject

Installs dependencies in the new sandbox

install_dependencies #=> pylint written in Gemfile

Examples:

Write pylint to a requirements.txt and

prompt `pip install -r requirements.txt`

Returns:

  • Nothing



80
81
82
83
84
85
86
87
88
89
# File 'lib/createproj/creator/python.rb', line 80

def install_dependencies
  File.open('requirements.txt', 'w+') do |f|
    @packages_to_install.each do |k, v|
      f.write("#{k}==#{v}\n")
    end
  end

  command = 'Enter the directory and run pip install requirements.txt'
  puts command
end

#install_sandboxObject

TODO:

Automate creating the virtualenv

Create an virtualenv with the project name and creates file dictating virtualenv

Check that the user has installed a bash script that will automatically ‘workon ENV` when entering directory with a .venv file

Examples:

Create rvm sandbox for project named test

install_sandbox #=> Executes `mkvirtualenv NAME`, write .env file,
                    and check for bash script

Returns:

  • Nothing



64
65
66
67
68
69
70
71
# File 'lib/createproj/creator/python.rb', line 64

def install_sandbox
  # make system call to virtualenvwrapper to create the virtualenv
  command = "Run mkvirtualenv #{virtualenv_name}"

  puts command
  write_venv_file
  check_bash_script
end

#virtualenv_nameString

Creates a namespaced virtualenv name

Examples:

Get virtualenv name if project name is test

virtualenv_name #=> 'test-createproj'

Returns:

  • (String)

    that has unique integer name



21
22
23
# File 'lib/createproj/creator/python.rb', line 21

def virtualenv_name
  "#{@name}-createproj"
end

#write_venv_fileObject

Write a .venv file with the name of the virtualenv

Examples:

write_venv_file #=> `cat .venv` #=> NAME

Returns:

  • Nothing



31
32
33
34
35
36
# File 'lib/createproj/creator/python.rb', line 31

def write_venv_file
  # must have both .ruby-gemset and .ruby-version file
  File.open('.venv', 'w+') do |f|
    f.write(virtualenv_name)
  end
end