<commit_before>from flask import Blueprint, render_template, current_app, redirect, request, url_for, jsonify

import os
import geopandas
from shapely.geometry import shape
from cea.utilities.standardize_coordinates import get_geographic_coordinate_system

import cea.inputlocator
import cea.scripts
from . import worker

blueprint = Blueprint(
    'landing_blueprint',
    __name__,
    url_prefix='/landing',
    template_folder='templates',
    static_folder='static'
)

@blueprint.route('/welcome')
def index():
    return render_template('landing.html')

@blueprint.route('/create-project')
def route_create_project():
    cea_config = current_app.cea_config
    project = os.path.dirname(cea_config.project)
    # TODO: Check if directory already exists
    return render_template('new_project.html', project=project)

@blueprint.route('/create-scenario')
def route_create_scenario():
    # TODO: Could add some preprocessing steps
    return render_template('new_scenario.html')

@blueprint.route('/project-overview')
def route_project_overview():
    cea_config = current_app.cea_config
    project_path = cea_config.project
    project_name = os.path.basename(project_path)

    # Get the list of scenarios
    scenarios = get_scenarios(project_path)
    return render_template('project_overview.html', project_name=project_name, scenarios=scenarios)

@blueprint.route('/create-zone')
def route_create_zone():
    scenario = request.args['scenario']
    return render_template('project_map.html', scenario=scenario)

@blueprint.route('/create-poly', methods=['POST'])
def route_poly_creator():
    # Get polygon points and create .shp file
    data = request.get_json()
    poly = shape(data['geometry'])
    poly = geopandas.GeoDataFrame(crs=get_geographic_coordinate_system(), geometry=[poly])

    cea_config = current_app.cea_config
    # Save current scenario name
    temp = cea_config.scenario_name
    cea_config.scenario_name = request.args['scenario']
    scenario_path = cea_config.scenario
    locator = cea.inputlocator.InputLocator(scenario_path)
    poly_path = locator.get_site_polygon()

    poly.to_file(poly_path)
    print('site.shp file created at %s' % poly_path)

    # FIXME: THIS IS A HACK, refactor this! This emulates tool/start/script to run zone_helper.py
    ###########################################
    form = {
        'scenario': scenario_path,
        'height-ag': '',
        'floors-ag': '',
        'year-construction': 2000,
        'height-bg': 3,
        'floors-bg': 1,
        'occupancy - type': 'MULTI_RES'
    }

    kwargs = {}
    script = 'zone-helper'
    print('/start/%s' % script)
    parameters = [p for _, p in cea_config.matching_parameters(cea.scripts.by_name(script).parameters)]
    for parameter in parameters:
        print('%s: %s' % (parameter.name, form.get(parameter.name)))
        kwargs[parameter.name] = parameter.decode(form.get(parameter.name))
    current_app.workers[script] = worker.main(script, **kwargs)
    ##########################################
    cea_config.scenario_name = temp

    return jsonify(dict(redirect='/landing/project_overview'))

@blueprint.route('/create-project/save', methods=['POST'])
def route_create_project_save():
    # FIXME: Cannot create new project if current project in config does not exist
    cea_config = current_app.cea_config
    # FIXME: A scenario will created based on the current scenario of the config
    cea_config.scenario_name = 'baseline'
    cea_config.project = os.path.join(request.form.get('projectPath'), request.form.get('projectName'))

    # Make sure that the project folder exists
    try:
        os.makedirs(cea_config.project)
    except OSError as e:
        print(e.message)
    cea_config.save()
    return redirect(url_for('landing_blueprint.route_project_overview'))

@blueprint.route('/create-scenario/save', methods=['POST'])
def route_create_scenario_save():
    cea_config = current_app.cea_config
    scenario = request.form.get('scenarioName')

    try:
        os.makedirs(os.path.join(cea_config.project, scenario))
    except OSError as e:
        print(e.message)

    if request.form.get('create-zone') == 'on':
        return redirect(url_for('landing_blueprint.route_create_zone', scenario=scenario))
    else:
        return redirect(url_for('landing_blueprint.route_project_overview'))

@blueprint.route('/open-project')
def route_open_project():
    cea_config = current_app.cea_config
    project = cea_config.project
    return render_template('open_project.html', project=project)

@blueprint.route('/open-project/save', methods=['POST'])
def route_open_project_save():
    cea_config = current_app.cea_config
    project_path = request.form.get('projectPath')
    scenarios = get_scenarios(project_path)

    cea_config.scenario_name = scenarios[0]
    cea_config.project = project_path
    cea_config.save()
    return redirect(url_for('landing_blueprint.route_project_overview'))

@blueprint.route('/open-project/<scenario>')
def route_open_project_scenario(scenario):
    """Open project based on the scenario"""
    cea_config = current_app.cea_config
    # Make sure the scenario exists
    assert scenario in get_scenarios(cea_config.project)
    cea_config.scenario_name = scenario
    cea_config.save()
    return redirect(url_for('plots_blueprint.index'))

# FIXME: this should be refactored, as it is originally from tools/routes.py
@blueprint.route('/open-folder-dialog/<fqname>')
def route_open_folder_dialog(fqname):
    """Return html of folder structure for that parameter"""

    # these arguments are only set when called with the `navigate_to` function on an already open
    # folder dialog
    current_folder = request.args.get('current_folder')
    folder = request.args.get('folder')

    config = current_app.cea_config
    section, parameter_name = fqname.split(':')
    parameter = config.sections[section].parameters[parameter_name]

    if not current_folder:
        # first time calling, use current value of parameter for current folder
        current_folder = os.path.abspath(parameter.get())
        folder = None
    else:
        current_folder = os.path.abspath(os.path.join(current_folder, folder))

    if not os.path.exists(current_folder):
        # use home directory if it doesn't exist
        current_folder = os.path.expanduser('~')

    folders = []
    for entry in os.listdir(current_folder):
        if os.path.isdir(os.path.join(current_folder, entry)):
            folders.append(entry)

    current_folder = os.path.normpath(current_folder)
    breadcrumbs = current_folder.split(os.path.sep)

    return render_template('folder_listing.html', current_folder=current_folder,
                           folders=folders, title=parameter.help, fqname=fqname,
                           parameter_name=parameter.name, breadcrumbs=breadcrumbs)

# FIXME: this should be refactored, as it is originally from tools/routes.py
@blueprint.route('/open-file-dialog/<fqname>')
def route_open_file_dialog(fqname):
    """Return html of file structure for that parameter"""

    # these arguments are only set when called with the `navigate_to` function on an already open
    # file dialog
    current_folder = request.args.get('current_folder')
    folder = request.args.get('folder')

    config = current_app.cea_config
    section, parameter_name = fqname.split(':')
    parameter = config.sections[section].parameters[parameter_name]

    if not current_folder:
        # first time calling, use current value of parameter for current folder
        current_folder = os.path.dirname(parameter.get())
        folder = None
    else:
        current_folder = os.path.abspath(os.path.join(current_folder, folder))

    if not os.path.exists(current_folder):
        # use home directory if it doesn't exist
        current_folder = os.path.expanduser('~')

    folders = []
    files = []
    for entry in os.listdir(current_folder):
        if os.path.isdir(os.path.join(current_folder, entry)):
            folders.append(entry)
        else:
            ext = os.path.splitext(entry)[1]
            if parameter._extensions and ext and ext[1:] in parameter._extensions:
                files.append(entry)
            elif not parameter._extensions:
                # any file can be added
                files.append(entry)

    breadcrumbs = os.path.normpath(current_folder).split(os.path.sep)

    return render_template('file_listing.html', current_folder=current_folder,
                           folders=folders, files=files, title=parameter.help, fqname=fqname,
                           parameter_name=parameter.name, breadcrumbs=breadcrumbs)

def get_scenarios(path):
    """Return list of scenarios based on folder structure"""
    scenarios = []
    for directory in os.listdir(path):
        if os.path.isdir(os.path.join(path, directory)):
            scenarios.append(directory)
    return scenarios
<commit_msg>Update url correction<commit_after>from flask import Blueprint, render_template, current_app, redirect, request, url_for, jsonify

import os
import geopandas
from shapely.geometry import shape
from cea.utilities.standardize_coordinates import get_geographic_coordinate_system

import cea.inputlocator
import cea.scripts
from . import worker

blueprint = Blueprint(
    'landing_blueprint',
    __name__,
    url_prefix='/landing',
    template_folder='templates',
    static_folder='static'
)

@blueprint.route('/welcome')
def index():
    return render_template('landing.html')

@blueprint.route('/create-project')
def route_create_project():
    cea_config = current_app.cea_config
    project = os.path.dirname(cea_config.project)
    # TODO: Check if directory already exists
    return render_template('new_project.html', project=project)

@blueprint.route('/create-scenario')
def route_create_scenario():
    # TODO: Could add some preprocessing steps
    return render_template('new_scenario.html')

@blueprint.route('/project-overview')
def route_project_overview():
    cea_config = current_app.cea_config
    project_path = cea_config.project
    project_name = os.path.basename(project_path)

    # Get the list of scenarios
    scenarios = get_scenarios(project_path)
    return render_template('project_overview.html', project_name=project_name, scenarios=scenarios)

@blueprint.route('/create-zone')
def route_create_zone():
    scenario = request.args['scenario']
    return render_template('project_map.html', scenario=scenario)

@blueprint.route('/create-poly', methods=['POST'])
def route_poly_creator():
    # Get polygon points and create .shp file
    data = request.get_json()
    poly = shape(data['geometry'])
    poly = geopandas.GeoDataFrame(crs=get_geographic_coordinate_system(), geometry=[poly])

    cea_config = current_app.cea_config
    # Save current scenario name
    temp = cea_config.scenario_name
    cea_config.scenario_name = request.args['scenario']
    scenario_path = cea_config.scenario
    locator = cea.inputlocator.InputLocator(scenario_path)
    poly_path = locator.get_site_polygon()

    poly.to_file(poly_path)
    print('site.shp file created at %s' % poly_path)

    # FIXME: THIS IS A HACK, refactor this! This emulates tool/start/script to run zone_helper.py
    ###########################################
    form = {
        'scenario': scenario_path,
        'height-ag': '',
        'floors-ag': '',
        'year-construction': 2000,
        'height-bg': 3,
        'floors-bg': 1,
        'occupancy - type': 'MULTI_RES'
    }

    kwargs = {}
    script = 'zone-helper'
    print('/start/%s' % script)
    parameters = [p for _, p in cea_config.matching_parameters(cea.scripts.by_name(script).parameters)]
    for parameter in parameters:
        print('%s: %s' % (parameter.name, form.get(parameter.name)))
        kwargs[parameter.name] = parameter.decode(form.get(parameter.name))
    current_app.workers[script] = worker.main(script, **kwargs)
    ##########################################
    cea_config.scenario_name = temp

    return jsonify(dict(redirect='/landing/project-overview'))

@blueprint.route('/create-project/save', methods=['POST'])
def route_create_project_save():
    # FIXME: Cannot create new project if current project in config does not exist
    cea_config = current_app.cea_config
    # FIXME: A scenario will created based on the current scenario of the config
    cea_config.scenario_name = 'baseline'
    cea_config.project = os.path.join(request.form.get('projectPath'), request.form.get('projectName'))

    # Make sure that the project folder exists
    try:
        os.makedirs(cea_config.project)
    except OSError as e:
        print(e.message)
    cea_config.save()
    return redirect(url_for('landing_blueprint.route_project_overview'))

@blueprint.route('/create-scenario/save', methods=['POST'])
def route_create_scenario_save():
    cea_config = current_app.cea_config
    scenario = request.form.get('scenarioName')

    try:
        os.makedirs(os.path.join(cea_config.project, scenario))
    except OSError as e:
        print(e.message)

    if request.form.get('create-zone') == 'on':
        return redirect(url_for('landing_blueprint.route_create_zone', scenario=scenario))
    else:
        return redirect(url_for('landing_blueprint.route_project_overview'))

@blueprint.route('/open-project')
def route_open_project():
    cea_config = current_app.cea_config
    project = cea_config.project
    return render_template('open_project.html', project=project)

@blueprint.route('/open-project/save', methods=['POST'])
def route_open_project_save():
    cea_config = current_app.cea_config
    project_path = request.form.get('projectPath')
    scenarios = get_scenarios(project_path)

    cea_config.scenario_name = scenarios[0]
    cea_config.project = project_path
    cea_config.save()
    return redirect(url_for('landing_blueprint.route_project_overview'))

@blueprint.route('/open-project/<scenario>')
def route_open_project_scenario(scenario):
    """Open project based on the scenario"""
    cea_config = current_app.cea_config
    # Make sure the scenario exists
    assert scenario in get_scenarios(cea_config.project)
    cea_config.scenario_name = scenario
    cea_config.save()
    return redirect(url_for('plots_blueprint.index'))

# FIXME: this should be refactored, as it is originally from tools/routes.py
@blueprint.route('/open-folder-dialog/<fqname>')
def route_open_folder_dialog(fqname):
    """Return html of folder structure for that parameter"""

    # these arguments are only set when called with the `navigate_to` function on an already open
    # folder dialog
    current_folder = request.args.get('current_folder')
    folder = request.args.get('folder')

    config = current_app.cea_config
    section, parameter_name = fqname.split(':')
    parameter = config.sections[section].parameters[parameter_name]

    if not current_folder:
        # first time calling, use current value of parameter for current folder
        current_folder = os.path.abspath(parameter.get())
        folder = None
    else:
        current_folder = os.path.abspath(os.path.join(current_folder, folder))

    if not os.path.exists(current_folder):
        # use home directory if it doesn't exist
        current_folder = os.path.expanduser('~')

    folders = []
    for entry in os.listdir(current_folder):
        if os.path.isdir(os.path.join(current_folder, entry)):
            folders.append(entry)

    current_folder = os.path.normpath(current_folder)
    breadcrumbs = current_folder.split(os.path.sep)

    return render_template('folder_listing.html', current_folder=current_folder,
                           folders=folders, title=parameter.help, fqname=fqname,
                           parameter_name=parameter.name, breadcrumbs=breadcrumbs)

# FIXME: this should be refactored, as it is originally from tools/routes.py
@blueprint.route('/open-file-dialog/<fqname>')
def route_open_file_dialog(fqname):
    """Return html of file structure for that parameter"""

    # these arguments are only set when called with the `navigate_to` function on an already open
    # file dialog
    current_folder = request.args.get('current_folder')
    folder = request.args.get('folder')

    config = current_app.cea_config
    section, parameter_name = fqname.split(':')
    parameter = config.sections[section].parameters[parameter_name]

    if not current_folder:
        # first time calling, use current value of parameter for current folder
        current_folder = os.path.dirname(parameter.get())
        folder = None
    else:
        current_folder = os.path.abspath(os.path.join(current_folder, folder))

    if not os.path.exists(current_folder):
        # use home directory if it doesn't exist
        current_folder = os.path.expanduser('~')

    folders = []
    files = []
    for entry in os.listdir(current_folder):
        if os.path.isdir(os.path.join(current_folder, entry)):
            folders.append(entry)
        else:
            ext = os.path.splitext(entry)[1]
            if parameter._extensions and ext and ext[1:] in parameter._extensions:
                files.append(entry)
            elif not parameter._extensions:
                # any file can be added
                files.append(entry)

    breadcrumbs = os.path.normpath(current_folder).split(os.path.sep)

    return render_template('file_listing.html', current_folder=current_folder,
                           folders=folders, files=files, title=parameter.help, fqname=fqname,
                           parameter_name=parameter.name, breadcrumbs=breadcrumbs)

def get_scenarios(path):
    """Return list of scenarios based on folder structure"""
    scenarios = []
    for directory in os.listdir(path):
        if os.path.isdir(os.path.join(path, directory)):
            scenarios.append(directory)
    return scenarios
<|endoftext|>


<commit_before>import django_filters

from content.filters import IntervalOrderFilterSet
from content.generic.views import GenericObjectDetail, GenericObjectList
from chart.filters import ChartFilterSet
from chart.models import Chart

class ObjectList(GenericObjectList):
    def get_extra_context(self, *args, **kwargs):
        extra_context = super(ObjectList, self).get_extra_context(*args, **kwargs)
        added_context = {'title': 'Charts'}
        if extra_context:
            extra_context.update(
                added_context,
            )
        else:
            extra_context = added_context

        return extra_context

    def get_filterset(self, request, queryset):
        return None

    def get_paginate_by(self):
        return 12

    def get_queryset(self):
        return Chart.permitted.all()

object_list = ObjectList()

class ObjectDetail(GenericObjectList):
    def get_queryset(self, slug):
        return Chart.permitted.get(slug=slug).chartentries.all().order_by('current_position')

    def get_extra_context(self, *args, **kwargs):
        extra_context = super(ObjectDetail, self).get_extra_context(*args, **kwargs)
        added_context = {'title': 'Chart'}
        if extra_context:
            extra_context.update(
                added_context,
            )
        else:
            extra_context = added_context
        return extra_context

    def get_filterset(self, request, queryset):
        return None

object_detail = ObjectDetail()
<commit_msg>removed filter import<commit_after>import django_filters

from content.filters import IntervalOrderFilterSet
from content.generic.views import GenericObjectDetail, GenericObjectList
from chart.models import Chart

class ObjectList(GenericObjectList):
    def get_extra_context(self, *args, **kwargs):
        extra_context = super(ObjectList, self).get_extra_context(*args, **kwargs)
        added_context = {'title': 'Charts'}
        if extra_context:
            extra_context.update(
                added_context,
            )
        else:
            extra_context = added_context

        return extra_context

    def get_filterset(self, request, queryset):
        return None

    def get_paginate_by(self):
        return 12

    def get_queryset(self):
        return Chart.permitted.all()

object_list = ObjectList()

class ObjectDetail(GenericObjectList):
    def get_queryset(self, slug):
        return Chart.permitted.get(slug=slug).chartentries.all().order_by('current_position')

    def get_extra_context(self, *args, **kwargs):
        extra_context = super(ObjectDetail, self).get_extra_context(*args, **kwargs)
        added_context = {'title': 'Chart'}
        if extra_context:
            extra_context.update(
                added_context,
            )
        else:
            extra_context = added_context
        return extra_context

    def get_filterset(self, request, queryset):
        return None

object_detail = ObjectDetail()
<|endoftext|>


<commit_before># Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt

from __future__ import unicode_literals
import frappe
from frappe.model.document import Document
from frappe.website.utils import cleanup_page_name
from frappe.website.render import clear_cache
from frappe.modules import get_module_name
from frappe.search.website_search import update_index_for_path, remove_document_from_index

class WebsiteGenerator(Document):
	website = frappe._dict()

	def __init__(self, *args, **kwargs):
		self.route = None
		super(WebsiteGenerator, self).__init__(*args, **kwargs)

	def get_website_properties(self, key=None, default=None):
		out = getattr(self, '_website', None) or getattr(self, 'website', None) or {}
		if not isinstance(out, dict):
			# website may be a property too, so ignore
			out = {}
		if key:
			return out.get(key, default)
		else:
			return out

	def autoname(self):
		if not self.name and self.meta.autoname != "hash":
			self.name = self.scrubbed_title()

	def onload(self):
		self.get("__onload").update({
			"is_website_generator": True,
			"published": self.is_website_published()
		})

	def validate(self):
		self.set_route()

	def set_route(self):
		if self.is_website_published() and not self.route:
			self.route = self.make_route()

		if self.route:
			self.route = self.route.strip('/.')[:139]

	def make_route(self):
		'''Returns the default route. If `route` is specified in DocType it will be
		route/title'''
		from_title = self.scrubbed_title()
		if self.meta.route:
			return self.meta.route + '/' + from_title
		else:
			return from_title

	def scrubbed_title(self):
		return self.scrub(self.get(self.get_title_field()))

	def get_title_field(self):
		'''return title field from website properties or meta.title_field'''
		title_field = self.get_website_properties('page_title_field')
		if not title_field:
			if self.meta.title_field:
				title_field = self.meta.title_field
			elif self.meta.has_field('title'):
				title_field = 'title'
			else:
				title_field = 'name'

		return title_field

	def clear_cache(self):
		super(WebsiteGenerator, self).clear_cache()
		clear_cache(self.route)

	def scrub(self, text):
		return cleanup_page_name(text).replace('_', '-')

	def get_parents(self, context):
		'''Return breadcrumbs'''
		pass

	def on_update(self):
		self.send_indexing_request()
		self.remove_old_route_from_index()

	def on_change(self):
		# Update the index on change
		# On change is triggered last in the event lifecycle
		self.update_website_search_index()

	def on_trash(self):
		self.clear_cache()
		self.send_indexing_request('URL_DELETED')
		# On deleting the doc, remove the page from the web_routes index
		if self.allow_website_search_indexing():
			remove_document_from_index(self.route)

	def is_website_published(self):
		"""Return true if published in website"""
		if self.get_condition_field():
			return self.get(self.get_condition_field()) and True or False
		else:
			return True

	def get_condition_field(self):
		condition_field = self.get_website_properties('condition_field')
		if not condition_field:
			if self.meta.is_published_field:
				condition_field = self.meta.is_published_field

		return condition_field

	def get_page_info(self):
		route = frappe._dict()
		route.update({
			"doc": self,
			"page_or_generator": "Generator",
			"ref_doctype":self.doctype,
			"idx": self.idx,
			"docname": self.name,
			"controller": get_module_name(self.doctype, self.meta.module),
		})

		route.update(self.get_website_properties())

		if not route.page_title:
			route.page_title = self.get(self.get_title_field())

		return route

	def send_indexing_request(self, operation_type='URL_UPDATED'):
		"""Send indexing request on update/trash operation."""

		if frappe.db.get_single_value('Website Settings', 'enable_google_indexing') \
			and self.is_website_published() and self.meta.allow_guest_to_view:

			url = frappe.utils.get_url(self.route)
			frappe.enqueue('frappe.website.doctype.website_settings.google_indexing.publish_site', \
				url=url, operation_type=operation_type)

	# Change the field value in doctype
	# Override this method to disable indexing
	def allow_website_search_indexing(self):
		return self.meta.index_web_pages_for_search

	def remove_old_route_from_index(self):
		"""Remove page from the website index if the route has changed."""
		if self.allow_website_search_indexing():
			return
		old_doc = self.get_doc_before_save()
		# Check if the route is changed
		if old_doc and old_doc.route != self.route:
			# Remove the route from index if the route has changed
			remove_document_from_index("web_routes", old_doc.route)

	def update_website_search_index(self):
		"""
			Update the full test index executed on document change event.
			- remove document from index if document is unpublished
			- update index otherwise
		"""
		if not self.allow_website_search_indexing():
			return

		if self.is_website_published():
			frappe.enqueue(update_index_for_path, path=self.route)
		elif self.route:
			# If the website is not published
			remove_document_from_index("web_routes", self.route)
<commit_msg>feat: do not update search index in tests<commit_after># Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt

from __future__ import unicode_literals
import frappe
from frappe.model.document import Document
from frappe.website.utils import cleanup_page_name
from frappe.website.render import clear_cache
from frappe.modules import get_module_name
from frappe.search.website_search import update_index_for_path, remove_document_from_index

class WebsiteGenerator(Document):
	website = frappe._dict()

	def __init__(self, *args, **kwargs):
		self.route = None
		super(WebsiteGenerator, self).__init__(*args, **kwargs)

	def get_website_properties(self, key=None, default=None):
		out = getattr(self, '_website', None) or getattr(self, 'website', None) or {}
		if not isinstance(out, dict):
			# website may be a property too, so ignore
			out = {}
		if key:
			return out.get(key, default)
		else:
			return out

	def autoname(self):
		if not self.name and self.meta.autoname != "hash":
			self.name = self.scrubbed_title()

	def onload(self):
		self.get("__onload").update({
			"is_website_generator": True,
			"published": self.is_website_published()
		})

	def validate(self):
		self.set_route()

	def set_route(self):
		if self.is_website_published() and not self.route:
			self.route = self.make_route()

		if self.route:
			self.route = self.route.strip('/.')[:139]

	def make_route(self):
		'''Returns the default route. If `route` is specified in DocType it will be
		route/title'''
		from_title = self.scrubbed_title()
		if self.meta.route:
			return self.meta.route + '/' + from_title
		else:
			return from_title

	def scrubbed_title(self):
		return self.scrub(self.get(self.get_title_field()))

	def get_title_field(self):
		'''return title field from website properties or meta.title_field'''
		title_field = self.get_website_properties('page_title_field')
		if not title_field:
			if self.meta.title_field:
				title_field = self.meta.title_field
			elif self.meta.has_field('title'):
				title_field = 'title'
			else:
				title_field = 'name'

		return title_field

	def clear_cache(self):
		super(WebsiteGenerator, self).clear_cache()
		clear_cache(self.route)

	def scrub(self, text):
		return cleanup_page_name(text).replace('_', '-')

	def get_parents(self, context):
		'''Return breadcrumbs'''
		pass

	def on_update(self):
		self.send_indexing_request()
		self.remove_old_route_from_index()

	def on_change(self):
		# Update the index on change
		# On change is triggered last in the event lifecycle
		self.update_website_search_index()

	def on_trash(self):
		self.clear_cache()
		self.send_indexing_request('URL_DELETED')
		# On deleting the doc, remove the page from the web_routes index
		if self.allow_website_search_indexing():
			remove_document_from_index(self.route)

	def is_website_published(self):
		"""Return true if published in website"""
		if self.get_condition_field():
			return self.get(self.get_condition_field()) and True or False
		else:
			return True

	def get_condition_field(self):
		condition_field = self.get_website_properties('condition_field')
		if not condition_field:
			if self.meta.is_published_field:
				condition_field = self.meta.is_published_field

		return condition_field

	def get_page_info(self):
		route = frappe._dict()
		route.update({
			"doc": self,
			"page_or_generator": "Generator",
			"ref_doctype":self.doctype,
			"idx": self.idx,
			"docname": self.name,
			"controller": get_module_name(self.doctype, self.meta.module),
		})

		route.update(self.get_website_properties())

		if not route.page_title:
			route.page_title = self.get(self.get_title_field())

		return route

	def send_indexing_request(self, operation_type='URL_UPDATED'):
		"""Send indexing request on update/trash operation."""

		if frappe.db.get_single_value('Website Settings', 'enable_google_indexing') \
			and self.is_website_published() and self.meta.allow_guest_to_view:

			url = frappe.utils.get_url(self.route)
			frappe.enqueue('frappe.website.doctype.website_settings.google_indexing.publish_site', \
				url=url, operation_type=operation_type)

	# Change the field value in doctype
	# Override this method to disable indexing
	def allow_website_search_indexing(self):
		return self.meta.index_web_pages_for_search

	def remove_old_route_from_index(self):
		"""Remove page from the website index if the route has changed."""
		if self.allow_website_search_indexing() or frappe.flags.in_test:
			return
		old_doc = self.get_doc_before_save()
		# Check if the route is changed
		if old_doc and old_doc.route != self.route:
			# Remove the route from index if the route has changed
			remove_document_from_index("web_routes", old_doc.route)

	def update_website_search_index(self):
		"""
			Update the full test index executed on document change event.
			- remove document from index if document is unpublished
			- update index otherwise
		"""
		if not self.allow_website_search_indexing() or frappe.flags.in_test:
			return

		if self.is_website_published():
			frappe.enqueue(update_index_for_path, path=self.route)
		elif self.route:
			# If the website is not published
			remove_document_from_index("web_routes", self.route)
<|endoftext|>


<commit_before># Copyright (c) 2013 Yubico AB
# All rights reserved.
#
#   Redistribution and use in source and binary forms, with or
#   without modification, are permitted provided that the following
#   conditions are met:
#
#    1. Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#    2. Redistributions in binary form must reproduce the above
#       copyright notice, this list of conditions and the following
#       disclaimer in the documentation and/or other materials provided
#       with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

from __future__ import print_function

import os
try:
    import hidraw as hid  # Prefer hidraw
except ImportError:
    import hid
from time import time
from u2flib_host.device import U2FDevice
from u2flib_host.yubicommon.compat import byte2int, int2byte
from u2flib_host import exc

DEVICES = [
    (0x1050, 0x0200),  # Gnubby
    (0x1050, 0x0113),  # YubiKey NEO U2F
    (0x1050, 0x0114),  # YubiKey NEO OTP+U2F
    (0x1050, 0x0115),  # YubiKey NEO U2F+CCID
    (0x1050, 0x0116),  # YubiKey NEO OTP+U2F+CCID
    (0x1050, 0x0120),  # Security Key by Yubico
    (0x1050, 0x0410),  # YubiKey Plus
    (0x1050, 0x0402),  # YubiKey 4 U2F
    (0x1050, 0x0403),  # YubiKey 4 OTP+U2F
    (0x1050, 0x0406),  # YubiKey 4 U2F+CCID
    (0x1050, 0x0407),  # YubiKey 4 OTP+U2F+CCID
    (0x2581, 0xf1d0),  # Plug-Up U2F Security Key
]
HID_RPT_SIZE = 64

TYPE_INIT = 0x80
U2F_VENDOR_FIRST = 0x40

# USB Commands
CMD_INIT = 0x06
CMD_WINK = 0x08
CMD_PING = 0x01
CMD_APDU = 0x03
CMD_LOCK = 0x04
U2FHID_YUBIKEY_DEVICE_CONFIG = U2F_VENDOR_FIRST

STAT_ERR = 0xbf

def list_devices(dev_class=None):
    dev_class = dev_class or HIDDevice
    devices = []
    for d in hid.enumerate(0, 0):
        usage_page = d['usage_page']
        if usage_page == 0xf1d0 and d['usage'] == 1:
            devices.append(dev_class(d['path']))
        # Usage page doesn't work on Linux
        elif (d['vendor_id'], d['product_id']) in DEVICES:
            device = HIDDevice(d['path'])
            try:
                device.open()
                device.close()
                devices.append(dev_class(d['path']))
            except exc.DeviceError:
                pass
    return devices

def _read_timeout(dev, size, timeout=2.0):
    timeout += time()
    while time() < timeout:
        resp = dev.read(size)
        if resp:
            return resp
    return []

class U2FHIDError(Exception):
    def __init__(self, code):
        super(Exception, self).__init__("U2FHIDError: 0x%02x" % code)
        self.code = code

class HIDDevice(U2FDevice):

    """
    U2FDevice implementation using the HID transport.
    """

    def __init__(self, path):
        self.path = path
        self.cid = b"\xff\xff\xff\xff"

    def open(self):
        self.handle = hid.device()
        self.handle.open_path(self.path)
        self.handle.set_nonblocking(True)
        self.init()

    def close(self):
        if hasattr(self, 'handle'):
            self.handle.close()
            del self.handle

    def init(self):
        nonce = os.urandom(8)
        resp = self.call(CMD_INIT, nonce)
        while resp[:8] != nonce:
            print("Wrong nonce, read again...")
            resp = self._read_resp(self.cid, CMD_INIT)
        self.cid = resp[8:12]

    def set_mode(self, mode):
        data = mode + b"\x0f\x00\x00"
        self.call(U2FHID_YUBIKEY_DEVICE_CONFIG, data)

    def _do_send_apdu(self, apdu_data):
        return self.call(CMD_APDU, apdu_data)

    def wink(self):
        self.call(CMD_WINK)

    def ping(self, msg=b'Hello U2F'):
        resp = self.call(CMD_PING, msg)
        if resp != msg:
            raise exc.DeviceError("Incorrect PING readback")
        return resp

    def lock(self, lock_time=10):
        self.call(CMD_LOCK, chr(lock_time))

    def _send_req(self, cid, cmd, data):
        size = len(data)
        bc_l = int2byte(size & 0xff)
        bc_h = int2byte(size >> 8 & 0xff)
        payload = cid + int2byte(TYPE_INIT | cmd) + bc_h + bc_l + \
            data[:HID_RPT_SIZE - 7]
        payload += b'\0' * (HID_RPT_SIZE - len(payload))
        self.handle.write([0] + [byte2int(c) for c in payload])
        data = data[HID_RPT_SIZE - 7:]
        seq = 0
        while len(data) > 0:
            payload = cid + int2byte(0x7f & seq) + data[:HID_RPT_SIZE - 5]
            payload += b'\0' * (HID_RPT_SIZE - len(payload))
            self.handle.write([0] + [byte2int(c) for c in payload])
            data = data[HID_RPT_SIZE - 5:]
            seq += 1

    def _read_resp(self, cid, cmd):
        resp = b'.'
        header = cid + int2byte(TYPE_INIT | cmd)
        while resp and resp[:5] != header:
            resp_vals = _read_timeout(self.handle, HID_RPT_SIZE)
            resp = b''.join(int2byte(v) for v in resp_vals)
            if resp[:5] == cid + int2byte(STAT_ERR):
                raise U2FHIDError(byte2int(resp[7]))

        if not resp:
            raise exc.DeviceError("Invalid response from device!")

        data_len = (byte2int(resp[5]) << 8) + byte2int(resp[6])
        data = resp[7:min(7 + data_len, HID_RPT_SIZE)]
        data_len -= len(data)

        seq = 0
        while data_len > 0:
            resp_vals = _read_timeout(self.handle, HID_RPT_SIZE)
            resp = b''.join(int2byte(v) for v in resp_vals)
            if resp[:4] != cid:
                raise exc.DeviceError("Wrong CID from device!")
            if byte2int(resp[4:5]) != seq & 0x7f:
                raise exc.DeviceError("Wrong SEQ from device!")
            seq += 1
            new_data = resp[5:min(5 + data_len, HID_RPT_SIZE)]
            data_len -= len(new_data)
            data += new_data
        return data

    def call(self, cmd, data=b''):
        if isinstance(data, int):
            data = int2byte(data)

        self._send_req(self.cid, cmd, data)
        return self._read_resp(self.cid, cmd)
<commit_msg>Remove unneeded chr()<commit_after># Copyright (c) 2013 Yubico AB
# All rights reserved.
#
#   Redistribution and use in source and binary forms, with or
#   without modification, are permitted provided that the following
#   conditions are met:
#
#    1. Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#    2. Redistributions in binary form must reproduce the above
#       copyright notice, this list of conditions and the following
#       disclaimer in the documentation and/or other materials provided
#       with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

from __future__ import print_function

import os
try:
    import hidraw as hid  # Prefer hidraw
except ImportError:
    import hid
from time import time
from u2flib_host.device import U2FDevice
from u2flib_host.yubicommon.compat import byte2int, int2byte
from u2flib_host import exc

DEVICES = [
    (0x1050, 0x0200),  # Gnubby
    (0x1050, 0x0113),  # YubiKey NEO U2F
    (0x1050, 0x0114),  # YubiKey NEO OTP+U2F
    (0x1050, 0x0115),  # YubiKey NEO U2F+CCID
    (0x1050, 0x0116),  # YubiKey NEO OTP+U2F+CCID
    (0x1050, 0x0120),  # Security Key by Yubico
    (0x1050, 0x0410),  # YubiKey Plus
    (0x1050, 0x0402),  # YubiKey 4 U2F
    (0x1050, 0x0403),  # YubiKey 4 OTP+U2F
    (0x1050, 0x0406),  # YubiKey 4 U2F+CCID
    (0x1050, 0x0407),  # YubiKey 4 OTP+U2F+CCID
    (0x2581, 0xf1d0),  # Plug-Up U2F Security Key
]
HID_RPT_SIZE = 64

TYPE_INIT = 0x80
U2F_VENDOR_FIRST = 0x40

# USB Commands
CMD_INIT = 0x06
CMD_WINK = 0x08
CMD_PING = 0x01
CMD_APDU = 0x03
CMD_LOCK = 0x04
U2FHID_YUBIKEY_DEVICE_CONFIG = U2F_VENDOR_FIRST

STAT_ERR = 0xbf

def list_devices(dev_class=None):
    dev_class = dev_class or HIDDevice
    devices = []
    for d in hid.enumerate(0, 0):
        usage_page = d['usage_page']
        if usage_page == 0xf1d0 and d['usage'] == 1:
            devices.append(dev_class(d['path']))
        # Usage page doesn't work on Linux
        elif (d['vendor_id'], d['product_id']) in DEVICES:
            device = HIDDevice(d['path'])
            try:
                device.open()
                device.close()
                devices.append(dev_class(d['path']))
            except exc.DeviceError:
                pass
    return devices

def _read_timeout(dev, size, timeout=2.0):
    timeout += time()
    while time() < timeout:
        resp = dev.read(size)
        if resp:
            return resp
    return []

class U2FHIDError(Exception):
    def __init__(self, code):
        super(Exception, self).__init__("U2FHIDError: 0x%02x" % code)
        self.code = code

class HIDDevice(U2FDevice):

    """
    U2FDevice implementation using the HID transport.
    """

    def __init__(self, path):
        self.path = path
        self.cid = b"\xff\xff\xff\xff"

    def open(self):
        self.handle = hid.device()
        self.handle.open_path(self.path)
        self.handle.set_nonblocking(True)
        self.init()

    def close(self):
        if hasattr(self, 'handle'):
            self.handle.close()
            del self.handle

    def init(self):
        nonce = os.urandom(8)
        resp = self.call(CMD_INIT, nonce)
        while resp[:8] != nonce:
            print("Wrong nonce, read again...")
            resp = self._read_resp(self.cid, CMD_INIT)
        self.cid = resp[8:12]

    def set_mode(self, mode):
        data = mode + b"\x0f\x00\x00"
        self.call(U2FHID_YUBIKEY_DEVICE_CONFIG, data)

    def _do_send_apdu(self, apdu_data):
        return self.call(CMD_APDU, apdu_data)

    def wink(self):
        self.call(CMD_WINK)

    def ping(self, msg=b'Hello U2F'):
        resp = self.call(CMD_PING, msg)
        if resp != msg:
            raise exc.DeviceError("Incorrect PING readback")
        return resp

    def lock(self, lock_time=10):
        self.call(CMD_LOCK, lock_time)

    def _send_req(self, cid, cmd, data):
        size = len(data)
        bc_l = int2byte(size & 0xff)
        bc_h = int2byte(size >> 8 & 0xff)
        payload = cid + int2byte(TYPE_INIT | cmd) + bc_h + bc_l + \
            data[:HID_RPT_SIZE - 7]
        payload += b'\0' * (HID_RPT_SIZE - len(payload))
        self.handle.write([0] + [byte2int(c) for c in payload])
        data = data[HID_RPT_SIZE - 7:]
        seq = 0
        while len(data) > 0:
            payload = cid + int2byte(0x7f & seq) + data[:HID_RPT_SIZE - 5]
            payload += b'\0' * (HID_RPT_SIZE - len(payload))
            self.handle.write([0] + [byte2int(c) for c in payload])
            data = data[HID_RPT_SIZE - 5:]
            seq += 1

    def _read_resp(self, cid, cmd):
        resp = b'.'
        header = cid + int2byte(TYPE_INIT | cmd)
        while resp and resp[:5] != header:
            resp_vals = _read_timeout(self.handle, HID_RPT_SIZE)
            resp = b''.join(int2byte(v) for v in resp_vals)
            if resp[:5] == cid + int2byte(STAT_ERR):
                raise U2FHIDError(byte2int(resp[7]))

        if not resp:
            raise exc.DeviceError("Invalid response from device!")

        data_len = (byte2int(resp[5]) << 8) + byte2int(resp[6])
        data = resp[7:min(7 + data_len, HID_RPT_SIZE)]
        data_len -= len(data)

        seq = 0
        while data_len > 0:
            resp_vals = _read_timeout(self.handle, HID_RPT_SIZE)
            resp = b''.join(int2byte(v) for v in resp_vals)
            if resp[:4] != cid:
                raise exc.DeviceError("Wrong CID from device!")
            if byte2int(resp[4:5]) != seq & 0x7f:
                raise exc.DeviceError("Wrong SEQ from device!")
            seq += 1
            new_data = resp[5:min(5 + data_len, HID_RPT_SIZE)]
            data_len -= len(new_data)
            data += new_data
        return data

    def call(self, cmd, data=b''):
        if isinstance(data, int):
            data = int2byte(data)

        self._send_req(self.cid, cmd, data)
        return self._read_resp(self.cid, cmd)
<|endoftext|>


<commit_before>from warnings import catch_warnings, warn
from tempfile import gettempdir
from os import unlink
from os.path import join, split
from unittest import TestCase, TestLoader, TextTestRunner, skipIf
from functools import partial
from pickle import load, dump
import sys

if __name__ == "__main__":
    from cobra import io
    from cobra.test import data_directory
else:
    from .. import io
    from . import data_directory

libraries = ["scipy", "libsbml", "cPickle", "jsonschema"]
for library in libraries:
    try:
        exec("import %s" % library)
    except ImportError:
        exec("%s = None" % library)

with open(join(data_directory, "mini.pickle"), "rb") as infile:
    mini_model = load(infile)

class TestCobraIO(object):
    def compare_models(self, model1, model2):
        self.assertEqual(len(model1.reactions),
                         len(model2.reactions))
        self.assertEqual(len(model1.metabolites),
                         len(model2.metabolites))
        for attr in ("id", "name", "lower_bound", "upper_bound", "gene_reaction_rule"):
            self.assertEqual(getattr(model1.reactions[0], attr),
                             getattr(model2.reactions[0], attr))
            self.assertEqual(getattr(model1.reactions[10], attr),
                             getattr(model2.reactions[10], attr))
            self.assertEqual(getattr(model1.reactions[-1], attr),
                             getattr(model2.reactions[-1], attr))
        for attr in ("id", "name", "compartment", "formula", "charge"):
            self.assertEqual(getattr(model1.metabolites[0], attr),
                             getattr(model2.metabolites[0], attr))
            self.assertEqual(getattr(model1.metabolites[10], attr),
                             getattr(model2.metabolites[10], attr))
            self.assertEqual(getattr(model1.metabolites[-1], attr),
                             getattr(model2.metabolites[-1], attr))
        self.assertEqual(len(model1.reactions[0].metabolites),
                         len(model2.reactions[0].metabolites))
        self.assertEqual(len(model1.reactions[14].metabolites),
                         len(model2.reactions[14].metabolites))
        self.assertEqual(len(model1.reactions[-1].metabolites),
                         len(model2.reactions[-1].metabolites))
        self.assertEqual(len(model1.genes), len(model2.genes))
        # ensure they have the same solution max
        model1.optimize()
        model2.optimize()
        self.assertAlmostEqual(model1.solution.f, model2.solution.f,
                               places=3)
        self.extra_comparisons(model1, model2)

    def extra_comparisons(self, model1, model2):
        # Overriding this prevents these extra comparisons. For example, mat
        # will need to skip this.
        self.assertEqual(model1.compartments, model2.compartments)
        self.assertEqual(model1.metabolites[4].annotation,
                         model2.metabolites[4].annotation)
        self.assertEqual(model1.reactions[4].annotation,
                         model2.reactions[4].annotation)
        self.assertEqual(model1.genes[4].annotation,
                         model2.genes[4].annotation)
        for attr in ("id", "name"):
            self.assertEqual(getattr(model1.genes[0], attr),
                             getattr(model2.genes[0], attr))
            self.assertEqual(getattr(model1.genes[10], attr),
                             getattr(model2.genes[10], attr))
            self.assertEqual(getattr(model1.genes[-1], attr),
                             getattr(model2.genes[-1], attr))
        return

    def test_read(self):
        read_model = self.read_function(self.test_file)
        self.compare_models(self.test_model, read_model)

    def test_read_nonexistent(self):
        # make sure that an error is raised when given a nonexistent file
        self.assertRaises(IOError, self.read_function, "fake_file")

    def test_write(self):
        test_output_filename = join(gettempdir(), split(self.test_file)[-1])
        self.write_function(self.test_model, test_output_filename)
        reread_model = self.read_function(test_output_filename)
        self.compare_models(self.test_model, reread_model)
        self.validate(test_output_filename)
        unlink(test_output_filename)

    def test_write_empty(self):
        test_output_filename = join(gettempdir(), split(self.test_file)[-1])
        m = self.test_model.copy()
        m.remove_reactions(list(m.reactions))
        self.write_function(m, test_output_filename)
        reread_model = self.read_function(test_output_filename)
        self.assertEqual(len(reread_model.reactions), 0)
        self.assertEqual(len(reread_model.metabolites), len(m.metabolites))
        unlink(test_output_filename)

    def validate(self, filename):
        # overload if a validator exists
        None

class TestCobraIOSBMLfbc2(TestCase, TestCobraIO):
    def setUp(self):
        self.test_model = mini_model
        self.test_file = join(data_directory, "mini_fbc2.xml")
        self.read_function = io.read_sbml_model
        self.write_function = io.write_sbml_model

    def validate(self, filename):
        io.sbml3.validate_sbml_model(filename)

class TestCobraIOSBMLfbc2Gz(TestCobraIOSBMLfbc2):
    def setUp(self):
        self.test_model = mini_model
        self.test_file = join(data_directory, "mini_fbc2.xml.gz")
        self.read_function = io.read_sbml_model
        self.write_function = io.write_sbml_model

class TestCobraIOSBMLfbc2Bz2(TestCobraIOSBMLfbc2):
    def setUp(self):
        self.test_model = mini_model
        self.test_file = join(data_directory, "mini_fbc2.xml.bz2")
        self.read_function = io.read_sbml_model
        self.write_function = io.write_sbml_model

class TestCobraSBMLValidation(TestCase):
    def test_bad_valiation(self):
        for i in range(3):
            filename = join(data_directory, "invalid%d.xml" % i)
            m, errors = io.sbml3.validate_sbml_model(filename)
            self.assertTrue(len(errors) >= 1)

@skipIf(not libsbml, "libsbml required")
class TestCobraIOSBMLfbc1(TestCase, TestCobraIO):
    def setUp(self):
        self.test_model = mini_model
        self.test_file = join(data_directory, "mini_fbc1.xml")
        self.read_function = io.read_sbml_model
        self.write_function = partial(io.write_legacy_sbml,
                                      use_fbc_package=True)

    def extra_comparisons(self, model1, model2):
        None

@skipIf(not libsbml, "libsbml required")
class TestCobraIOSBMLcobra(TestCase, TestCobraIO):
    def setUp(self):
        self.test_model = mini_model
        self.test_file = join(data_directory, "mini_cobra.xml")
        self.read_function = io.read_sbml_model
        self.write_function = partial(io.write_sbml_model,
                                      use_fbc_package=False)

    def extra_comparisons(self, model1, model2):
        None

@skipIf(not scipy, "scipy required")
class TestCobraIOmat(TestCase, TestCobraIO):
    def setUp(self):
        self.test_model = mini_model
        self.test_file = join(data_directory, "mini.mat")
        self.read_function = io.load_matlab_model
        self.write_function = io.save_matlab_model

    def extra_comparisons(self, model1, model2):
        # MAT does not store gene names
        None

class TestCobraIOjson(TestCase, TestCobraIO):
    def setUp(self):
        self.test_model = mini_model
        self.test_file = join(data_directory, "mini.json")
        self.read_function = io.load_json_model
        self.write_function = io.save_json_model

    def validate(self, filename):
        with open(filename, "r") as infile:
            loaded = io.json.json.load(infile)
        if jsonschema is None:
            warn("jsonschema not installed")
            return
        jsonschema.validate(loaded, io.json.json_schema)

class TestCobraIOPickle(TestCase, TestCobraIO):
    def setUp(self):
        self.test_model = mini_model
        self.test_file = join(data_directory, "mini.pickle")
        self.load = load
        self.dump = dump

        def read_function(filename):
            with open(filename, "rb") as infile:
                return self.load(infile)

        def write_function(model, filename):
            with open(filename, "wb") as outfile:
                self.dump(model, outfile)

        self.read_function = read_function
        self.write_function = write_function

@skipIf(not cPickle, "cPickle required")
class TestCobraIOcPickle(TestCobraIOPickle):
    def setUp(self):
        TestCobraIOPickle.setUp(self)
        self.load = cPickle.load
        self.dump = cPickle.dump

# make a test suite to run all of the tests
loader = TestLoader()
suite = loader.loadTestsFromModule(sys.modules[__name__])

def test_all():
    TextTestRunner(verbosity=2).run(suite)

if __name__ == "__main__":
    test_all()
<commit_msg>ensure objective_coefficient saved during IO<commit_after>from warnings import catch_warnings, warn
from tempfile import gettempdir
from os import unlink
from os.path import join, split
from unittest import TestCase, TestLoader, TextTestRunner, skipIf
from functools import partial
from pickle import load, dump
import sys

if __name__ == "__main__":
    from cobra import io
    from cobra.test import data_directory
else:
    from .. import io
    from . import data_directory

libraries = ["scipy", "libsbml", "cPickle", "jsonschema"]
for library in libraries:
    try:
        exec("import %s" % library)
    except ImportError:
        exec("%s = None" % library)

with open(join(data_directory, "mini.pickle"), "rb") as infile:
    mini_model = load(infile)

class TestCobraIO(object):
    def compare_models(self, model1, model2):
        self.assertEqual(len(model1.reactions),
                         len(model2.reactions))
        self.assertEqual(len(model1.metabolites),
                         len(model2.metabolites))
        for attr in ("id", "name", "lower_bound", "upper_bound",
                     "objective_coefficient", "gene_reaction_rule"):
            self.assertEqual(getattr(model1.reactions[0], attr),
                             getattr(model2.reactions[0], attr))
            self.assertEqual(getattr(model1.reactions[10], attr),
                             getattr(model2.reactions[10], attr))
            self.assertEqual(getattr(model1.reactions[-1], attr),
                             getattr(model2.reactions[-1], attr))
        for attr in ("id", "name", "compartment", "formula", "charge"):
            self.assertEqual(getattr(model1.metabolites[0], attr),
                             getattr(model2.metabolites[0], attr))
            self.assertEqual(getattr(model1.metabolites[10], attr),
                             getattr(model2.metabolites[10], attr))
            self.assertEqual(getattr(model1.metabolites[-1], attr),
                             getattr(model2.metabolites[-1], attr))
        self.assertEqual(len(model1.reactions[0].metabolites),
                         len(model2.reactions[0].metabolites))
        self.assertEqual(len(model1.reactions[14].metabolites),
                         len(model2.reactions[14].metabolites))
        self.assertEqual(len(model1.reactions[-1].metabolites),
                         len(model2.reactions[-1].metabolites))
        self.assertEqual(len(model1.genes), len(model2.genes))
        # ensure they have the same solution max
        model1.optimize()
        model2.optimize()
        self.assertAlmostEqual(model1.solution.f, model2.solution.f,
                               places=3)
        self.extra_comparisons(model1, model2)

    def extra_comparisons(self, model1, model2):
        # Overriding this prevents these extra comparisons. For example, mat
        # will need to skip this.
        self.assertEqual(model1.compartments, model2.compartments)
        self.assertEqual(model1.metabolites[4].annotation,
                         model2.metabolites[4].annotation)
        self.assertEqual(model1.reactions[4].annotation,
                         model2.reactions[4].annotation)
        self.assertEqual(model1.genes[4].annotation,
                         model2.genes[4].annotation)
        for attr in ("id", "name"):
            self.assertEqual(getattr(model1.genes[0], attr),
                             getattr(model2.genes[0], attr))
            self.assertEqual(getattr(model1.genes[10], attr),
                             getattr(model2.genes[10], attr))
            self.assertEqual(getattr(model1.genes[-1], attr),
                             getattr(model2.genes[-1], attr))
        return

    def test_read(self):
        read_model = self.read_function(self.test_file)
        self.compare_models(self.test_model, read_model)

    def test_read_nonexistent(self):
        # make sure that an error is raised when given a nonexistent file
        self.assertRaises(IOError, self.read_function, "fake_file")

    def test_write(self):
        test_output_filename = join(gettempdir(), split(self.test_file)[-1])
        self.write_function(self.test_model, test_output_filename)
        reread_model = self.read_function(test_output_filename)
        self.compare_models(self.test_model, reread_model)
        self.validate(test_output_filename)
        unlink(test_output_filename)

    def test_write_empty(self):
        test_output_filename = join(gettempdir(), split(self.test_file)[-1])
        m = self.test_model.copy()
        m.remove_reactions(list(m.reactions))
        self.write_function(m, test_output_filename)
        reread_model = self.read_function(test_output_filename)
        self.assertEqual(len(reread_model.reactions), 0)
        self.assertEqual(len(reread_model.metabolites), len(m.metabolites))
        unlink(test_output_filename)

    def validate(self, filename):
        # overload if a validator exists
        None

class TestCobraIOSBMLfbc2(TestCase, TestCobraIO):
    def setUp(self):
        self.test_model = mini_model
        self.test_file = join(data_directory, "mini_fbc2.xml")
        self.read_function = io.read_sbml_model
        self.write_function = io.write_sbml_model

    def validate(self, filename):
        io.sbml3.validate_sbml_model(filename)

class TestCobraIOSBMLfbc2Gz(TestCobraIOSBMLfbc2):
    def setUp(self):
        self.test_model = mini_model
        self.test_file = join(data_directory, "mini_fbc2.xml.gz")
        self.read_function = io.read_sbml_model
        self.write_function = io.write_sbml_model

class TestCobraIOSBMLfbc2Bz2(TestCobraIOSBMLfbc2):
    def setUp(self):
        self.test_model = mini_model
        self.test_file = join(data_directory, "mini_fbc2.xml.bz2")
        self.read_function = io.read_sbml_model
        self.write_function = io.write_sbml_model

class TestCobraSBMLValidation(TestCase):
    def test_bad_valiation(self):
        for i in range(3):
            filename = join(data_directory, "invalid%d.xml" % i)
            m, errors = io.sbml3.validate_sbml_model(filename)
            self.assertTrue(len(errors) >= 1)

@skipIf(not libsbml, "libsbml required")
class TestCobraIOSBMLfbc1(TestCase, TestCobraIO):
    def setUp(self):
        self.test_model = mini_model
        self.test_file = join(data_directory, "mini_fbc1.xml")
        self.read_function = io.read_sbml_model
        self.write_function = partial(io.write_legacy_sbml,
                                      use_fbc_package=True)

    def extra_comparisons(self, model1, model2):
        None

@skipIf(not libsbml, "libsbml required")
class TestCobraIOSBMLcobra(TestCase, TestCobraIO):
    def setUp(self):
        self.test_model = mini_model
        self.test_file = join(data_directory, "mini_cobra.xml")
        self.read_function = io.read_sbml_model
        self.write_function = partial(io.write_sbml_model,
                                      use_fbc_package=False)

    def extra_comparisons(self, model1, model2):
        None

@skipIf(not scipy, "scipy required")
class TestCobraIOmat(TestCase, TestCobraIO):
    def setUp(self):
        self.test_model = mini_model
        self.test_file = join(data_directory, "mini.mat")
        self.read_function = io.load_matlab_model
        self.write_function = io.save_matlab_model

    def extra_comparisons(self, model1, model2):
        # MAT does not store gene names
        None

class TestCobraIOjson(TestCase, TestCobraIO):
    def setUp(self):
        self.test_model = mini_model
        self.test_file = join(data_directory, "mini.json")
        self.read_function = io.load_json_model
        self.write_function = io.save_json_model

    def validate(self, filename):
        with open(filename, "r") as infile:
            loaded = io.json.json.load(infile)
        if jsonschema is None:
            warn("jsonschema not installed")
            return
        jsonschema.validate(loaded, io.json.json_schema)

class TestCobraIOPickle(TestCase, TestCobraIO):
    def setUp(self):
        self.test_model = mini_model
        self.test_file = join(data_directory, "mini.pickle")
        self.load = load
        self.dump = dump

        def read_function(filename):
            with open(filename, "rb") as infile:
                return self.load(infile)

        def write_function(model, filename):
            with open(filename, "wb") as outfile:
                self.dump(model, outfile)

        self.read_function = read_function
        self.write_function = write_function

@skipIf(not cPickle, "cPickle required")
class TestCobraIOcPickle(TestCobraIOPickle):
    def setUp(self):
        TestCobraIOPickle.setUp(self)
        self.load = cPickle.load
        self.dump = cPickle.dump

# make a test suite to run all of the tests
loader = TestLoader()
suite = loader.loadTestsFromModule(sys.modules[__name__])

def test_all():
    TextTestRunner(verbosity=2).run(suite)

if __name__ == "__main__":
    test_all()
<|endoftext|>


<commit_before># -*- scanner.py -*-
# -*- MIT License (c) 2014 David Leonard -*-
# -*- drksephy.github.io -*-

#################
#     TODO      #
#################

# - [done] Scan <target program>
# - [    ] Tokenize keywords
# - [    ] Convert code using Classes

import sys

class scanner(object):

    def __init__(self, curr_row, curr_col, curr_token, curr_val, tokens):
        self.curr_row   = curr_row
        self.curr_col   = curr_col
        self.curr_token = curr_token
        self.curr_val   = curr_val
        self.tokens     = tokens

def scan(source):
    # Reads <source program> and builds tokens.

    # Variables to assist with tokenization
    # row/col of current character
    curr_row = 1
    curr_col = 1

    text = open(source, 'r').read().splitlines()
    for line in text:
        curr_col = 1
        for char in line:

            # Treat all ascii chars <= 32 as spaces
            if to_ascii(char) == 32:
                print "row: " + str(curr_row) + " , " + "col: " + str(curr_col) + " is: " + char
            # Check if char: ! " # $ % & ' ( ) * + , - . / : ; < = > ? @

            if ((to_ascii(char) > 32 and to_ascii(char) < 47) or (to_ascii(char) > 57 and to_ascii(char) < 65)):
                print "row: " + str(curr_row) + " , " + "col: " + str(curr_col) + " is: " + char

            # Check if char: 0 1 2 3 4 5 6 7 8 9
            if to_ascii(char) > 47 and to_ascii(char) < 58:
                print "row: " + str(curr_row) + " , " + "col: " + str(curr_col) + " is: " + char

            # Check if char is uppercase
            if (to_ascii(char) > 64 and to_ascii(char) < 91):
                x = build_string(char)
                # print "row: " + str(curr_row) + " , " + "col: " + str(curr_col) + " is: " + to_lower(char)

            # Check if char is lowercase
            if to_ascii(char) > 96 and to_ascii(char) < 123:
                # build_string(char)
                print "row: " + str(curr_row) + " , " + "col: " + str(curr_col) + " is: " + char

            # Check if char: [ \ ] ^ _ `
            if to_ascii(char) > 90 and to_ascii(char) < 97:
                print "row: " + str(curr_row) + " , " + "col: " + str(curr_col) + " is: " + char

            # Check if char: { | } ~ DEL
            if to_ascii(char) > 122 and to_ascii(char) < 128:
                print "row: " + str(curr_row) + " , " + "col: " + str(curr_col) + " is: " + char
            curr_col += 1
        curr_row += 1
    print tokens

###################################
#         HELPER FUNCTIONS        #
###################################

def lookup(table, key):
    # Reads token value from corresponding table
    return table[key]

def to_ascii(string):
    # Returns ascii value of a character
    return ord(string)

def to_lower(string):
    # Returns lowercase string
    return string.lower()

def to_upper(string):
    # Returns uppercase string
    return string.upper()

KEYWORDS = {
    'BEGIN'     : 'TK_BEGIN',
    'BREAK'     : 'TK_BREAK',
    'CONST'     : 'TK_CONST',
    'DO'        : 'TK_DO',
    'DOWNTO'    : 'TK_DOWNTO',
    'ELSE'      : 'TK_ELSE',
    'END'       : 'TK_END',
    'END.'      : 'TK_END_CODE',
    'FOR'       : 'TK_FOR',
    'FUNCTION'  : 'TK_FUNCTION',
    'IDENTIFIER': 'TK_IDENTIFIER',
    'IF'        : 'TK_IF',
    'LABEL'     : 'TK_LABEL',
    'PROGRAM'   : 'TK_PROGRAM',
    'REPEAT'    : 'TK_REPEAT',
    'STRING'    : 'TK_STRING',
    'THEN'      : 'TK_THEN',
    'TO'        : 'TK_TO',
    'TYPE'      : 'TK_TYPE',
    'VAR'       : 'TK_VAR',
    'WHILE'     : 'TK_WHILE'
}

OPERATORS = {
    '+'         : 'TK_PLUS',
    '-'         : 'TK_MINUS',
    '*'         : 'TK_MULT',
    '/'         : 'TK_DIV_FLOAT',
    'DIV'       : 'TK_DIV',
    'MOD'       : 'TK_MOD',
    ':'         : 'TK_COLON',
    '='         : 'TK_EQUALS',
    ':='        : 'TK_ASSIGNMENT',
    '>'         : 'TK_GREATER',
    '<'         : 'TK_LESS',
    '>='        : 'TK_GREATER_EQUALS',
    '<='        : 'TK_LESS_EQUALS',
    'AND'       : 'TK_AND',
    'OR'        : 'TK_OR',
    'NOT'       : 'TK_NOT'
}<commit_msg>scanner.py: Moving helper functions in class methods<commit_after># -*- scanner.py -*-
# -*- MIT License (c) 2014 David Leonard -*-
# -*- drksephy.github.io -*-

#################
#     TODO      #
#################

# - [done] Scan <target program>
# - [    ] Tokenize keywords
# - [    ] Convert code using Classes

import sys

class scanner(object):

    def __init__(self, curr_row, curr_col, curr_token, curr_val, tokens):
        self.curr_row   = curr_row
        self.curr_col   = curr_col
        self.curr_token = curr_token
        self.curr_val   = curr_val
        self.tokens     = tokens

    def lookup(self, table, key):
        return self.table[key]

    def to_ascii(self, char):
        return ord(char)

    def to_lower(char):
        return char.lower()

    def to_upper(char):
        return char.upper()

def scan(source):
    # Reads <source program> and builds tokens.

    # Variables to assist with tokenization
    # row/col of current character
    curr_row = 1
    curr_col = 1

    text = open(source, 'r').read().splitlines()
    for line in text:
        curr_col = 1
        for char in line:

            # Treat all ascii chars <= 32 as spaces
            if to_ascii(char) == 32:
                print "row: " + str(curr_row) + " , " + "col: " + str(curr_col) + " is: " + char
            # Check if char: ! " # $ % & ' ( ) * + , - . / : ; < = > ? @

            if ((to_ascii(char) > 32 and to_ascii(char) < 47) or (to_ascii(char) > 57 and to_ascii(char) < 65)):
                print "row: " + str(curr_row) + " , " + "col: " + str(curr_col) + " is: " + char

            # Check if char: 0 1 2 3 4 5 6 7 8 9
            if to_ascii(char) > 47 and to_ascii(char) < 58:
                print "row: " + str(curr_row) + " , " + "col: " + str(curr_col) + " is: " + char

            # Check if char is uppercase
            if (to_ascii(char) > 64 and to_ascii(char) < 91):
                x = build_string(char)
                # print "row: " + str(curr_row) + " , " + "col: " + str(curr_col) + " is: " + to_lower(char)

            # Check if char is lowercase
            if to_ascii(char) > 96 and to_ascii(char) < 123:
                # build_string(char)
                print "row: " + str(curr_row) + " , " + "col: " + str(curr_col) + " is: " + char

            # Check if char: [ \ ] ^ _ `
            if to_ascii(char) > 90 and to_ascii(char) < 97:
                print "row: " + str(curr_row) + " , " + "col: " + str(curr_col) + " is: " + char

            # Check if char: { | } ~ DEL
            if to_ascii(char) > 122 and to_ascii(char) < 128:
                print "row: " + str(curr_row) + " , " + "col: " + str(curr_col) + " is: " + char
            curr_col += 1
        curr_row += 1
    print tokens

KEYWORDS = {
    'BEGIN'     : 'TK_BEGIN',
    'BREAK'     : 'TK_BREAK',
    'CONST'     : 'TK_CONST',
    'DO'        : 'TK_DO',
    'DOWNTO'    : 'TK_DOWNTO',
    'ELSE'      : 'TK_ELSE',
    'END'       : 'TK_END',
    'END.'      : 'TK_END_CODE',
    'FOR'       : 'TK_FOR',
    'FUNCTION'  : 'TK_FUNCTION',
    'IDENTIFIER': 'TK_IDENTIFIER',
    'IF'        : 'TK_IF',
    'LABEL'     : 'TK_LABEL',
    'PROGRAM'   : 'TK_PROGRAM',
    'REPEAT'    : 'TK_REPEAT',
    'STRING'    : 'TK_STRING',
    'THEN'      : 'TK_THEN',
    'TO'        : 'TK_TO',
    'TYPE'      : 'TK_TYPE',
    'VAR'       : 'TK_VAR',
    'WHILE'     : 'TK_WHILE'
}

OPERATORS = {
    '+'         : 'TK_PLUS',
    '-'         : 'TK_MINUS',
    '*'         : 'TK_MULT',
    '/'         : 'TK_DIV_FLOAT',
    'DIV'       : 'TK_DIV',
    'MOD'       : 'TK_MOD',
    ':'         : 'TK_COLON',
    '='         : 'TK_EQUALS',
    ':='        : 'TK_ASSIGNMENT',
    '>'         : 'TK_GREATER',
    '<'         : 'TK_LESS',
    '>='        : 'TK_GREATER_EQUALS',
    '<='        : 'TK_LESS_EQUALS',
    'AND'       : 'TK_AND',
    'OR'        : 'TK_OR',
    'NOT'       : 'TK_NOT'
}<|endoftext|>


<commit_before>#!/usr/bin/env python
###########################################################
#               WARNING: Generated code!                  #
#              **************************                 #
# Manual changes may get lost if file is generated again. #
# Only code inside the [MANUAL] tags will be kept.        #
###########################################################

import roslib; roslib.load_manifest('behavior_action_pick')
from flexbe_core import Behavior, Autonomy, OperatableStateMachine, ConcurrencyContainer, PriorityContainer, Logger
from behavior_action_get_entity_pose.action_get_entity_pose_sm import Action_get_entity_poseSM
from sara_flexbe_states.gen_gripper_pose import GenGripperPose
from sara_flexbe_states.move_arm_pose import MoveArmPose
from sara_flexbe_states.move_arm_named_pose import MoveArmNamedPose
from behavior_check_reachability.check_reachability_sm import Check_reachabilitySM
# Additional imports can be added inside the following tags
# [MANUAL_IMPORT]

# [/MANUAL_IMPORT]

'''
Created on Fri Oct 20 2017
@author: Philippe La Madeleine
'''
class Action_pickSM(Behavior):
    '''
    Try to pick an object
    '''

    def __init__(self):
        super(Action_pickSM, self).__init__()
        self.name = 'Action_pick'

        # parameters of this behavior

        # references to used behaviors
        self.add_behavior(Action_get_entity_poseSM, 'get_pose_1')
        self.add_behavior(Action_get_entity_poseSM, 'get_pose_2')
        self.add_behavior(Action_get_entity_poseSM, 'approach/Action_get_entity_pose')
        self.add_behavior(Action_get_entity_poseSM, 'approach/Action_get_entity_pose_2')
        self.add_behavior(Action_get_entity_poseSM, 'approach/Action_get_entity_pose_3')
        self.add_behavior(Check_reachabilitySM, 'Check_reachability')
        self.add_behavior(Action_get_entity_poseSM, 'Container/Action_get_entity_pose')
        self.add_behavior(Action_get_entity_poseSM, 'Container/Action_get_entity_pose_2')
        self.add_behavior(Action_get_entity_poseSM, 'Container/Action_get_entity_pose_3')

        # Additional initialization code can be added inside the following tags
        # [MANUAL_INIT]

        # [/MANUAL_INIT]

        # Behavior comments:

    def create(self):
        # x:597 y:593, x:287 y:320, x:275 y:449, x:292 y:221, x:450 y:146
        _state_machine = OperatableStateMachine(outcomes=['success', 'too far', 'unreachable', 'not seen', 'critical fail'], input_keys=['object'], output_keys=['grip_pose'])
        _state_machine.userdata.object = ""
        _state_machine.userdata.grip_pose = 0

        # Additional creation code can be added inside the following tags
        # [MANUAL_CREATE]

        # [/MANUAL_CREATE]

        # x:795 y:320, x:816 y:488
        _sm_container_0 = OperatableStateMachine(outcomes=['finished', 'failed'], input_keys=['object'], output_keys=['pose'])

        with _sm_container_0:
            # x:30 y:40
            OperatableStateMachine.add('Action_get_entity_pose',
                                        self.use_behavior(Action_get_entity_poseSM, 'Container/Action_get_entity_pose'),
                                        transitions={'found': 'gen1', 'not found': 'gen1'},
                                        autonomy={'found': Autonomy.Inherit, 'not found': Autonomy.Inherit},
                                        remapping={'name': 'object', 'pose': 'pose'})

            # x:296 y:282
            OperatableStateMachine.add('gen2',
                                        GenGripperPose(x=0, y=0, z=0, t=0.5),
                                        transitions={'done': 'move2'},
                                        autonomy={'done': Autonomy.Off},
                                        remapping={'pose_in': 'pose', 'pose_out': 'pose_out'})

            # x:297 y:441
            OperatableStateMachine.add('gen3',
                                        GenGripperPose(x=0, y=0, z=0, t=-0.5),
                                        transitions={'done': 'move3'},
                                        autonomy={'done': Autonomy.Off},
                                        remapping={'pose_in': 'pose', 'pose_out': 'pose_out'})

            # x:519 y:135
            OperatableStateMachine.add('move1',
                                        MoveArmPose(wait=True),
                                        transitions={'done': 'finished', 'failed': 'Action_get_entity_pose_2'},
                                        autonomy={'done': Autonomy.Off, 'failed': Autonomy.Off},
                                        remapping={'pose': 'pose_out'})

            # x:524 y:306
            OperatableStateMachine.add('move2',
                                        MoveArmPose(wait=True),
                                        transitions={'done': 'finished', 'failed': 'Action_get_entity_pose_3'},
                                        autonomy={'done': Autonomy.Off, 'failed': Autonomy.Off},
                                        remapping={'pose': 'pose_out'})

            # x:528 y:462
            OperatableStateMachine.add('move3',
                                        MoveArmPose(wait=True),
                                        transitions={'done': 'finished', 'failed': 'failed'},
                                        autonomy={'done': Autonomy.Off, 'failed': Autonomy.Off},
                                        remapping={'pose': 'pose_out'})

            # x:27 y:209
            OperatableStateMachine.add('Action_get_entity_pose_2',
                                        self.use_behavior(Action_get_entity_poseSM, 'Container/Action_get_entity_pose_2'),
                                        transitions={'found': 'gen2', 'not found': 'gen2'},
                                        autonomy={'found': Autonomy.Inherit, 'not found': Autonomy.Inherit},
                                        remapping={'name': 'object', 'pose': 'pose'})

            # x:35 y:381
            OperatableStateMachine.add('Action_get_entity_pose_3',
                                        self.use_behavior(Action_get_entity_poseSM, 'Container/Action_get_entity_pose_3'),
                                        transitions={'found': 'gen3', 'not found': 'gen3'},
                                        autonomy={'found': Autonomy.Inherit, 'not found': Autonomy.Inherit},
                                        remapping={'name': 'object', 'pose': 'pose'})

            # x:293 y:116
            OperatableStateMachine.add('gen1',
                                        GenGripperPose(x=0, y=0, z=0, t=0),
                                        transitions={'done': 'move1'},
                                        autonomy={'done': Autonomy.Off},
                                        remapping={'pose_in': 'pose', 'pose_out': 'pose_out'})

        # x:855 y:303, x:856 y:566
        _sm_approach_1 = OperatableStateMachine(outcomes=['finished', 'failed'], input_keys=['object'])

        with _sm_approach_1:
            # x:39 y:60
            OperatableStateMachine.add('Action_get_entity_pose',
                                        self.use_behavior(Action_get_entity_poseSM, 'approach/Action_get_entity_pose'),
                                        transitions={'found': 'gen1', 'not found': 'gen1'},
                                        autonomy={'found': Autonomy.Inherit, 'not found': Autonomy.Inherit},
                                        remapping={'name': 'object', 'pose': 'pose'})

            # x:331 y:112
            OperatableStateMachine.add('gen1',
                                        GenGripperPose(x=-0.1, y=0, z=0, t=0),
                                        transitions={'done': 'move1'},
                                        autonomy={'done': Autonomy.Off},
                                        remapping={'pose_in': 'pose', 'pose_out': 'pose_out'})

            # x:542 y:197
            OperatableStateMachine.add('move1',
                                        MoveArmPose(wait=True),
                                        transitions={'done': 'finished', 'failed': 'Action_get_entity_pose_2'},
                                        autonomy={'done': Autonomy.Off, 'failed': Autonomy.Off},
                                        remapping={'pose': 'pose_out'})

            # x:334 y:294
            OperatableStateMachine.add('gen2',
                                        GenGripperPose(x=-0.1, y=-0.2, z=0, t=0.5),
                                        transitions={'done': 'move2'},
                                        autonomy={'done': Autonomy.Off},
                                        remapping={'pose_in': 'pose', 'pose_out': 'pose_out'})

            # x:543 y:364
            OperatableStateMachine.add('move2',
                                        MoveArmPose(wait=True),
                                        transitions={'done': 'finished', 'failed': 'Action_get_entity_pose_3'},
                                        autonomy={'done': Autonomy.Off, 'failed': Autonomy.Off},
                                        remapping={'pose': 'pose_out'})

            # x:333 y:452
            OperatableStateMachine.add('gen3',
                                        GenGripperPose(x=-0.1, y=0.2, z=0, t=-0.5),
                                        transitions={'done': 'move3'},
                                        autonomy={'done': Autonomy.Off},
                                        remapping={'pose_in': 'pose', 'pose_out': 'pose_out'})

            # x:543 y:494
            OperatableStateMachine.add('move3',
                                        MoveArmPose(wait=True),
                                        transitions={'done': 'finished', 'failed': 'failed'},
                                        autonomy={'done': Autonomy.Off, 'failed': Autonomy.Off},
                                        remapping={'pose': 'pose_out'})

            # x:27 y:240
            OperatableStateMachine.add('Action_get_entity_pose_2',
                                        self.use_behavior(Action_get_entity_poseSM, 'approach/Action_get_entity_pose_2'),
                                        transitions={'found': 'gen2', 'not found': 'gen2'},
                                        autonomy={'found': Autonomy.Inherit, 'not found': Autonomy.Inherit},
                                        remapping={'name': 'object', 'pose': 'pose'})

            # x:31 y:413
            OperatableStateMachine.add('Action_get_entity_pose_3',
                                        self.use_behavior(Action_get_entity_poseSM, 'approach/Action_get_entity_pose_3'),
                                        transitions={'found': 'gen3', 'not found': 'gen3'},
                                        autonomy={'found': Autonomy.Inherit, 'not found': Autonomy.Inherit},
                                        remapping={'name': 'object', 'pose': 'pose'})

        with _state_machine:
            # x:44 y:32
            OperatableStateMachine.add('get_pose_1',
                                        self.use_behavior(Action_get_entity_poseSM, 'get_pose_1'),
                                        transitions={'found': 'PreGripPose', 'not found': 'PreGripPose'},
                                        autonomy={'found': Autonomy.Inherit, 'not found': Autonomy.Inherit},
                                        remapping={'name': 'object', 'pose': 'pose'})

            # x:32 y:210
            OperatableStateMachine.add('get_pose_2',
                                        self.use_behavior(Action_get_entity_poseSM, 'get_pose_2'),
                                        transitions={'found': 'Check_reachability', 'not found': 'not seen'},
                                        autonomy={'found': Autonomy.Inherit, 'not found': Autonomy.Inherit},
                                        remapping={'name': 'object', 'pose': 'pose'})

            # x:50 y:388
            OperatableStateMachine.add('approach',
                                        _sm_approach_1,
                                        transitions={'finished': 'Container', 'failed': 'unreachable'},
                                        autonomy={'finished': Autonomy.Inherit, 'failed': Autonomy.Inherit},
                                        remapping={'object': 'object'})

            # x:45 y:132
            OperatableStateMachine.add('PreGripPose',
                                        MoveArmNamedPose(pose_name="PreGripPose", wait=True),
                                        transitions={'done': 'get_pose_2', 'failed': 'critical fail'},
                                        autonomy={'done': Autonomy.Off, 'failed': Autonomy.Off})

            # x:36 y:298
            OperatableStateMachine.add('Check_reachability',
                                        self.use_behavior(Check_reachabilitySM, 'Check_reachability'),
                                        transitions={'ok': 'approach', 'too_far': 'too far'},
                                        autonomy={'ok': Autonomy.Inherit, 'too_far': Autonomy.Inherit},
                                        remapping={'pose': 'pose'})

            # x:53 y:495
            OperatableStateMachine.add('Container',
                                        _sm_container_0,
                                        transitions={'finished': 'gen up', 'failed': 'unreachable'},
                                        autonomy={'finished': Autonomy.Inherit, 'failed': Autonomy.Inherit},
                                        remapping={'object': 'object', 'pose': 'pose'})

            # x:52 y:585
            OperatableStateMachine.add('gen up',
                                        GenGripperPose(x=0, y=0, z=0.1, t=0),
                                        transitions={'done': 'move up'},
                                        autonomy={'done': Autonomy.Off},
                                        remapping={'pose_in': 'pose', 'pose_out': 'pose_out'})

            # x:231 y:577
            OperatableStateMachine.add('move up',
                                        MoveArmPose(wait=True),
                                        transitions={'done': 'move back', 'failed': 'move back'},
                                        autonomy={'done': Autonomy.Off, 'failed': Autonomy.Off},
                                        remapping={'pose': 'pose'})

            # x:398 y:576
            OperatableStateMachine.add('move back',
                                        MoveArmNamedPose(pose_name="PostGripPose", wait=True),
                                        transitions={'done': 'success', 'failed': 'critical fail'},
                                        autonomy={'done': Autonomy.Off, 'failed': Autonomy.Off})

        return _state_machine

    # Private functions can be added inside the following tags
    # [MANUAL_FUNC]

    # [/MANUAL_FUNC]
<commit_msg>update<commit_after>#!/usr/bin/env python
###########################################################
#               WARNING: Generated code!                  #
#              **************************                 #
# Manual changes may get lost if file is generated again. #
# Only code inside the [MANUAL] tags will be kept.        #
###########################################################

import roslib; roslib.load_manifest('behavior_action_pick')
from flexbe_core import Behavior, Autonomy, OperatableStateMachine, ConcurrencyContainer, PriorityContainer, Logger
from behavior_action_get_entity_pose.action_get_entity_pose_sm import Action_get_entity_poseSM
from sara_flexbe_states.gen_gripper_pose import GenGripperPose
from sara_flexbe_states.move_arm_pose import MoveArmPose
from sara_flexbe_states.move_arm_named_pose import MoveArmNamedPose
from behavior_check_reachability.check_reachability_sm import Check_reachabilitySM
from sara_flexbe_states.set_gripper_state import SetGripperState
# Additional imports can be added inside the following tags
# [MANUAL_IMPORT]

# [/MANUAL_IMPORT]

'''
Created on Fri Oct 20 2017
@author: Philippe La Madeleine
'''
class Action_pickSM(Behavior):
	'''
	Try to pick an object
	'''

	def __init__(self):
		super(Action_pickSM, self).__init__()
		self.name = 'Action_pick'

		# parameters of this behavior

		# references to used behaviors
		self.add_behavior(Action_get_entity_poseSM, 'get_pose_1')
		self.add_behavior(Action_get_entity_poseSM, 'get_pose_2')
		self.add_behavior(Action_get_entity_poseSM, 'approach/Action_get_entity_pose')
		self.add_behavior(Action_get_entity_poseSM, 'approach/Action_get_entity_pose_2')
		self.add_behavior(Action_get_entity_poseSM, 'approach/Action_get_entity_pose_3')
		self.add_behavior(Check_reachabilitySM, 'Check_reachability')

		# Additional initialization code can be added inside the following tags
		# [MANUAL_INIT]

        # [/MANUAL_INIT]

		# Behavior comments:

	def create(self):
		# x:885 y:552, x:294 y:303, x:517 y:477, x:292 y:221, x:529 y:139, x:524 y:543
		_state_machine = OperatableStateMachine(outcomes=['success', 'too far', 'unreachable', 'not seen', 'critical fail', 'missed'], input_keys=['object'], output_keys=['grip_pose'])
		_state_machine.userdata.object = "bottle"
		_state_machine.userdata.grip_pose = 0

		# Additional creation code can be added inside the following tags
		# [MANUAL_CREATE]

        # [/MANUAL_CREATE]

		# x:795 y:320, x:293 y:566
		_sm_get_0 = OperatableStateMachine(outcomes=['finished', 'failed'], input_keys=['pose'], output_keys=['pose'])

		with _sm_get_0:
			# x:293 y:116
			OperatableStateMachine.add('gen1',
										GenGripperPose(x=0.04, y=0, z=0, t=0),
										transitions={'done': 'move1'},
										autonomy={'done': Autonomy.Off},
										remapping={'pose_in': 'pose', 'pose_out': 'pose_out'})

			# x:297 y:441
			OperatableStateMachine.add('gen3',
										GenGripperPose(x=0.04, y=0, z=0, t=-0.5),
										transitions={'done': 'move3'},
										autonomy={'done': Autonomy.Off},
										remapping={'pose_in': 'pose', 'pose_out': 'pose_out'})

			# x:519 y:135
			OperatableStateMachine.add('move1',
										MoveArmPose(wait=True),
										transitions={'done': 'finished', 'failed': 'gen2'},
										autonomy={'done': Autonomy.Off, 'failed': Autonomy.Off},
										remapping={'pose': 'pose_out'})

			# x:524 y:306
			OperatableStateMachine.add('move2',
										MoveArmPose(wait=True),
										transitions={'done': 'finished', 'failed': 'gen3'},
										autonomy={'done': Autonomy.Off, 'failed': Autonomy.Off},
										remapping={'pose': 'pose_out'})

			# x:528 y:462
			OperatableStateMachine.add('move3',
										MoveArmPose(wait=True),
										transitions={'done': 'finished', 'failed': 'failed'},
										autonomy={'done': Autonomy.Off, 'failed': Autonomy.Off},
										remapping={'pose': 'pose_out'})

			# x:296 y:282
			OperatableStateMachine.add('gen2',
										GenGripperPose(x=0.04, y=0, z=0, t=0.5),
										transitions={'done': 'move2'},
										autonomy={'done': Autonomy.Off},
										remapping={'pose_in': 'pose', 'pose_out': 'pose_out'})

		# x:855 y:303, x:364 y:670
		_sm_approach_1 = OperatableStateMachine(outcomes=['finished', 'failed'], input_keys=['object', 'pose'], output_keys=['pose'])

		with _sm_approach_1:
			# x:39 y:60
			OperatableStateMachine.add('Action_get_entity_pose',
										self.use_behavior(Action_get_entity_poseSM, 'approach/Action_get_entity_pose'),
										transitions={'found': 'gen1', 'not found': 'failed'},
										autonomy={'found': Autonomy.Inherit, 'not found': Autonomy.Inherit},
										remapping={'name': 'object', 'pose': 'pose'})

			# x:331 y:112
			OperatableStateMachine.add('gen1',
										GenGripperPose(x=-0.1, y=0, z=0, t=0),
										transitions={'done': 'move1'},
										autonomy={'done': Autonomy.Off},
										remapping={'pose_in': 'pose', 'pose_out': 'pose_out'})

			# x:542 y:197
			OperatableStateMachine.add('move1',
										MoveArmPose(wait=True),
										transitions={'done': 'finished', 'failed': 'Action_get_entity_pose_2'},
										autonomy={'done': Autonomy.Off, 'failed': Autonomy.Off},
										remapping={'pose': 'pose_out'})

			# x:334 y:294
			OperatableStateMachine.add('gen2',
										GenGripperPose(x=-0.1, y=-0.1, z=0, t=0.5),
										transitions={'done': 'move2'},
										autonomy={'done': Autonomy.Off},
										remapping={'pose_in': 'pose', 'pose_out': 'pose_out'})

			# x:543 y:364
			OperatableStateMachine.add('move2',
										MoveArmPose(wait=True),
										transitions={'done': 'finished', 'failed': 'Action_get_entity_pose_3'},
										autonomy={'done': Autonomy.Off, 'failed': Autonomy.Off},
										remapping={'pose': 'pose_out'})

			# x:333 y:452
			OperatableStateMachine.add('gen3',
										GenGripperPose(x=-0.1, y=0.1, z=0, t=-0.5),
										transitions={'done': 'move3'},
										autonomy={'done': Autonomy.Off},
										remapping={'pose_in': 'pose', 'pose_out': 'pose_out'})

			# x:543 y:494
			OperatableStateMachine.add('move3',
										MoveArmPose(wait=True),
										transitions={'done': 'finished', 'failed': 'failed'},
										autonomy={'done': Autonomy.Off, 'failed': Autonomy.Off},
										remapping={'pose': 'pose_out'})

			# x:27 y:240
			OperatableStateMachine.add('Action_get_entity_pose_2',
										self.use_behavior(Action_get_entity_poseSM, 'approach/Action_get_entity_pose_2'),
										transitions={'found': 'gen2', 'not found': 'failed'},
										autonomy={'found': Autonomy.Inherit, 'not found': Autonomy.Inherit},
										remapping={'name': 'object', 'pose': 'pose'})

			# x:31 y:413
			OperatableStateMachine.add('Action_get_entity_pose_3',
										self.use_behavior(Action_get_entity_poseSM, 'approach/Action_get_entity_pose_3'),
										transitions={'found': 'gen3', 'not found': 'failed'},
										autonomy={'found': Autonomy.Inherit, 'not found': Autonomy.Inherit},
										remapping={'name': 'object', 'pose': 'pose'})

		with _state_machine:
			# x:44 y:32
			OperatableStateMachine.add('get_pose_1',
										self.use_behavior(Action_get_entity_poseSM, 'get_pose_1'),
										transitions={'found': 'PreGripPose', 'not found': 'PreGripPose'},
										autonomy={'found': Autonomy.Inherit, 'not found': Autonomy.Inherit},
										remapping={'name': 'object', 'pose': 'pose'})

			# x:32 y:210
			OperatableStateMachine.add('get_pose_2',
										self.use_behavior(Action_get_entity_poseSM, 'get_pose_2'),
										transitions={'found': 'Check_reachability', 'not found': 'not seen'},
										autonomy={'found': Autonomy.Inherit, 'not found': Autonomy.Inherit},
										remapping={'name': 'object', 'pose': 'pose'})

			# x:48 y:466
			OperatableStateMachine.add('approach',
										_sm_approach_1,
										transitions={'finished': 'Get', 'failed': 'back'},
										autonomy={'finished': Autonomy.Inherit, 'failed': Autonomy.Inherit},
										remapping={'object': 'object', 'pose': 'pose'})

			# x:45 y:132
			OperatableStateMachine.add('PreGripPose',
										MoveArmNamedPose(pose_name="PreGripPose", wait=True),
										transitions={'done': 'get_pose_2', 'failed': 'critical fail'},
										autonomy={'done': Autonomy.Off, 'failed': Autonomy.Off})

			# x:82 y:300
			OperatableStateMachine.add('Check_reachability',
										self.use_behavior(Check_reachabilitySM, 'Check_reachability'),
										transitions={'ok': 'open', 'too_far': 'too far'},
										autonomy={'ok': Autonomy.Inherit, 'too_far': Autonomy.Inherit},
										remapping={'pose': 'pose'})

			# x:44 y:557
			OperatableStateMachine.add('Get',
										_sm_get_0,
										transitions={'finished': 'gen up', 'failed': 'back2'},
										autonomy={'finished': Autonomy.Inherit, 'failed': Autonomy.Inherit},
										remapping={'pose': 'pose'})

			# x:45 y:654
			OperatableStateMachine.add('gen up',
										GenGripperPose(x=0, y=0, z=0.05, t=0),
										transitions={'done': 'close'},
										autonomy={'done': Autonomy.Off},
										remapping={'pose_in': 'pose', 'pose_out': 'pose_out'})

			# x:392 y:654
			OperatableStateMachine.add('move up',
										MoveArmPose(wait=True),
										transitions={'done': 'move back', 'failed': 'move back'},
										autonomy={'done': Autonomy.Off, 'failed': Autonomy.Off},
										remapping={'pose': 'pose_out'})

			# x:568 y:654
			OperatableStateMachine.add('move back',
										MoveArmNamedPose(pose_name="PostGripPose", wait=True),
										transitions={'done': 'success', 'failed': 'critical fail'},
										autonomy={'done': Autonomy.Off, 'failed': Autonomy.Off})

			# x:228 y:642
			OperatableStateMachine.add('close',
										SetGripperState(width=0, effort=0.00001),
										transitions={'object': 'move up', 'no_object': 'back3'},
										autonomy={'object': Autonomy.Off, 'no_object': Autonomy.Off},
										remapping={'object_size': 'object_size'})

			# x:52 y:390
			OperatableStateMachine.add('open',
										SetGripperState(width=0.14, effort=0),
										transitions={'object': 'approach', 'no_object': 'approach'},
										autonomy={'object': Autonomy.Off, 'no_object': Autonomy.Off},
										remapping={'object_size': 'object_size'})

			# x:221 y:455
			OperatableStateMachine.add('back',
										MoveArmNamedPose(pose_name="PreGripPose", wait=False),
										transitions={'done': 'unreachable', 'failed': 'critical fail'},
										autonomy={'done': Autonomy.Off, 'failed': Autonomy.Off})

			# x:221 y:512
			OperatableStateMachine.add('back2',
										MoveArmNamedPose(pose_name="PreGripPose", wait=False),
										transitions={'done': 'unreachable', 'failed': 'critical fail'},
										autonomy={'done': Autonomy.Off, 'failed': Autonomy.Off})

			# x:221 y:569
			OperatableStateMachine.add('back3',
										MoveArmNamedPose(pose_name="PreGripPose", wait=False),
										transitions={'done': 'missed', 'failed': 'critical fail'},
										autonomy={'done': Autonomy.Off, 'failed': Autonomy.Off})

		return _state_machine

	# Private functions can be added inside the following tags
	# [MANUAL_FUNC]

    # [/MANUAL_FUNC]
<|endoftext|>