Skip to content

Commit

Permalink
Merge 3a48e89 into ca0bb0c
Browse files Browse the repository at this point in the history
  • Loading branch information
krak3n committed Jul 10, 2013
2 parents ca0bb0c + 3a48e89 commit 7660966
Show file tree
Hide file tree
Showing 9 changed files with 366 additions and 230 deletions.
20 changes: 5 additions & 15 deletions src/facio/config.py
Expand Up @@ -10,15 +10,15 @@

from clint.textui.colored import yellow
from docopt import docopt
from facio import get_version
from facio.base import BaseFacio
from facio.exceptions import FacioException
from facio.state import state
from random import choice
from six.moves import configparser as ConfigParser
from six.moves import input
from textwrap import dedent

from facio import get_version
from facio.exceptions import FacioException


class CommandLineInterface(object):
"""
Expand Down Expand Up @@ -59,7 +59,8 @@ def validate_project_name(self, name):
if not re.match('^\w+$', name):
raise FacioException('Project names can only contain numbers '
'letters and underscores')
return True
else:
state.set_project_name(name)


class ConfigurationFile(BaseFacio):
Expand Down Expand Up @@ -108,17 +109,6 @@ def __init__(self, interface, config):
self.interface = interface
self.config = config

def get_project_name(self):
""" Get the project name from the command line interface.
:returns: str -- The project name
"""

try:
return self.interface.arguments['<project_name>']
except KeyError:
raise FacioException('Project name not defined.')

def get_template_path(self):
""" Obtain the template with from the command line interface or from
prompting the user to choose a template from the config file.
Expand Down
7 changes: 5 additions & 2 deletions src/facio/pipeline/__init__.py
Expand Up @@ -7,6 +7,7 @@
import yaml

from facio.base import BaseFacio
from facio.state import state
from importlib import import_module
from yaml.scanner import ScannerError

Expand Down Expand Up @@ -143,10 +144,12 @@ def run_before(self):
""" Run the before modules. """

for path in self.pipeline.get('before', []):
self.run_module(path)
result = self.run_module(path)
state.pipeline_save_call(path, result)

def run_after(self):
""" Run the after modules. """

for path in self.pipeline.get('after', []):
self.run_module(path)
result = self.run_module(path)
state.pipeline_save_call(path, result)
10 changes: 4 additions & 6 deletions src/facio/run.py
Expand Up @@ -9,8 +9,9 @@

from facio.base import BaseFacio
from facio.config import Settings, CommandLineInterface, ConfigurationFile
from facio.template import Template
from facio.pipeline import Pipeline
from facio.template import Template
from facio.state import state


class Run(BaseFacio):
Expand All @@ -25,21 +26,18 @@ def run(self):
parsed = config.read()

settings = Settings(interface, parsed)
state.update_context_variables(settings.get_variables())

template = Template(
settings.get_project_name(),
settings.get_template_path()
)

template.update_context_variables(settings.get_variables())
template.update_ignore_globs(settings.get_ignore_globs())

template.copy()

pipeline = Pipeline()
pipeline.load(os.path.join(
template.get_project_root(),
'.facio.pipeline.yml'
state.get_project_root(), '.facio.pipeline.yml'
))

if pipeline.has_before():
Expand Down
170 changes: 170 additions & 0 deletions src/facio/state.py
@@ -0,0 +1,170 @@
# -*- coding: utf-8 -*-

"""
.. module:: facio.state
:synopsis: Facio state module, for accessing and maintaining state during
the template generation process.
"""

import os

from facio.base import BaseFacio
from sh import pwd
from six.moves import builtins


class State(BaseFacio):

def __init__(self):
""" State is stored in a __facio__ super global set at the moment
this class is instantiated but only if not already set.
This class is basically a proxy class for interfacing with __facio__
super global variable. All state is set and retrieved from __facio__.
"""

try:
self.state = builtins.__facio__
except AttributeError:
builtins.__facio__ = self
self.state = builtins.__facio__

def set_project_name(self, name):
""" Set the project name to the state.
:param name: The project name from facio.config.CommandLineInterface
:type name: str
"""

self.update_context_variables({'PROJECT_NAME': name})
self.state.project_name = name

def get_project_name(self):
""" Return the project name stored in the state.
:returns: str
"""

return self.state.project_name

def get_working_directory(self):
""" Use the ``sh`` library to return the current working directory
using the unix command ``pwd``.
:returns: str
"""

return '{0}'.format(pwd()).strip()

def get_project_root(self):
""" Return the project root, which is the current working directory
plus the project name.
:returns: str
"""

return os.path.join(self.get_working_directory(),
self.get_project_name())

def update_context_variables(self, dictionary):
""" Update the context varaibles dict with new values.
** Usage: **
.. code-block:: python
from facio.state import state
dictionary = {
'bar': 'baz',
'fib': 'fab',
}
state.update_context_variables(dictionary)
:param dictionary: Dictionary of new key values
:type dictionary: dict
"""

try:
dict1 = self.state.context_variables
except AttributeError:
self.state.context_variables = {}
dict1 = self.state.context_variables
dict2 = dictionary

if isinstance(dict1, dict) and isinstance(dict2, dict):
dict1.update(dict2)
self.state.context_variables = dict1
else:
self.warning('Failed to update context variables with {0}'.format(
dict2))

def get_context_variables(self):
""" Returns the current context variables at time of call.
:retutns: dict
"""

try:
return self.state.context_variables
except AttributeError:
return {}

def get_context_variable(self, name):
""" Return a specific context variable value.
:param name: Context variable name
:type name: str
:returns: str or None -- None if name not found in var list
"""

variables = self.get_context_variables()
return variables.get(name, None)

def pipeline_get_call_result(self, module_path):
""" Returns a pipeline call result, else returns false if the module
path is not in the pipeline call list.
:param module_path: The python dotted path to the module
:type module_path: str
:returns: Call result
"""

try:
calls = self.state.pipeline_calls
except AttributeError:
calls = []

try:
module, result = [(m, r) for m, r in calls if m == module_path][0]
except IndexError:
return None

return result

def pipeline_save_call(self, module_path, result):
""" Saves a pipeline call to state
:param module_path: The python dotted path to the module
:type module_path: str
:param result: The result of the module run() function
:type result: Anything
:returns: list -- The call list or tuples
"""

try:
calls = self.state.pipeline_calls
except AttributeError:
calls = []

if not self.pipeline_get_call_result(module_path):
calls.append((module_path, result))
self.state.pipeline_calls = calls

return calls


state = State()

0 comments on commit 7660966

Please sign in to comment.