<commit_before># Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt

from __future__ import unicode_literals
import frappe, json
from frappe.utils.nestedset import get_root_of
from frappe.utils import cint
from erpnext.accounts.doctype.pos_profile.pos_profile import get_item_groups

from six import string_types

@frappe.whitelist()
def get_items(start, page_length, price_list, item_group, search_value="", pos_profile=None):
	data = dict()
	warehouse = ""
	display_items_in_stock = 0

	if pos_profile:
		warehouse, display_items_in_stock = frappe.db.get_value('POS Profile', pos_profile, ['warehouse', 'display_items_in_stock'])

	if not frappe.db.exists('Item Group', item_group):
		item_group = get_root_of('Item Group')

	if search_value:
		data = search_serial_or_batch_or_barcode_number(search_value)

	item_code = data.get("item_code") if data.get("item_code") else search_value
	serial_no = data.get("serial_no") if data.get("serial_no") else ""
	batch_no = data.get("batch_no") if data.get("batch_no") else ""
	barcode = data.get("barcode") if data.get("barcode") else ""

	condition = get_conditions(item_code, serial_no, batch_no, barcode)

	if pos_profile:
		condition += get_item_group_condition(pos_profile)

	lft, rgt = frappe.db.get_value('Item Group', item_group, ['lft', 'rgt'])
	# locate function is used to sort by closest match from the beginning of the value

	result = []

	items_data = frappe.db.sql(""" SELECT name as item_code,
			item_name, image as item_image, idx as idx,is_stock_item
		FROM
			`tabItem`
		WHERE
			disabled = 0 and has_variants = 0 and is_sales_item = 1
			and item_group in (select name from `tabItem Group` where lft >= {lft} and rgt <= {rgt})
			and {condition} order by idx desc limit {start}, {page_length}"""
		.format(
			start=start, page_length=page_length,
			lft=lft, rgt=rgt,
			condition=condition
		), as_dict=1)

	if items_data:
		items = [d.item_code for d in items_data]
		item_prices_data = frappe.get_all("Item Price",
			fields = ["item_code", "price_list_rate", "currency"],
			filters = {'price_list': price_list, 'item_code': ['in', items]})

		item_prices, bin_data = {}, {}
		for d in item_prices_data:
			item_prices[d.item_code] = d

		if display_items_in_stock:
			filters = {'actual_qty': [">", 0], 'item_code': ['in', items]}

			if warehouse:
				filters['warehouse'] = warehouse

			bin_data = frappe._dict(
				frappe.get_all("Bin", fields = ["item_code", "sum(actual_qty) as actual_qty"],
				filters = filters, group_by = "item_code")
			)

		for item in items_data:
			row = {}

			row.update(item)
			item_price = item_prices.get(item.item_code) or {}
			row.update({
				'price_list_rate': item_price.get('price_list_rate'),
				'currency': item_price.get('currency'),
				'actual_qty': bin_data.get('actual_qty')
			})

			result.append(row)

	res = {
		'items': result
	}

	if serial_no:
		res.update({
			'serial_no': serial_no
		})

	if batch_no:
		res.update({
			'batch_no': batch_no
		})

	if barcode:
		res.update({
			'barcode': barcode
		})

	return res

@frappe.whitelist()
def search_serial_or_batch_or_barcode_number(search_value):
	# search barcode no
	barcode_data = frappe.db.get_value('Item Barcode', {'barcode': search_value}, ['barcode', 'parent as item_code'], as_dict=True)
	if barcode_data:
		return barcode_data

	# search serial no
	serial_no_data = frappe.db.get_value('Serial No', search_value, ['name as serial_no', 'item_code'], as_dict=True)
	if serial_no_data:
		return serial_no_data

	# search batch no
	batch_no_data = frappe.db.get_value('Batch', search_value, ['name as batch_no', 'item as item_code'], as_dict=True)
	if batch_no_data:
		return batch_no_data

	return {}

def get_conditions(item_code, serial_no, batch_no, barcode):
	if serial_no or batch_no or barcode:
		return "name = {0}".format(frappe.db.escape(item_code))

	return """(name like {item_code}
		or item_name like {item_code})""".format(item_code = frappe.db.escape('%' + item_code + '%'))

def get_item_group_condition(pos_profile):
	cond = "and 1=1"
	item_groups = get_item_groups(pos_profile)
	if item_groups:
		cond = "and item_group in (%s)"%(', '.join(['%s']*len(item_groups)))

	return cond % tuple(item_groups)

def item_group_query(doctype, txt, searchfield, start, page_len, filters):
	item_groups = []
	cond = "1=1"
	pos_profile= filters.get('pos_profile')

	if pos_profile:
		item_groups = get_item_groups(pos_profile)

		if item_groups:
			cond = "name in (%s)"%(', '.join(['%s']*len(item_groups)))
			cond = cond % tuple(item_groups)

	return frappe.db.sql(""" select distinct name from `tabItem Group`
			where {condition} and (name like %(txt)s) limit {start}, {page_len}"""
		.format(condition = cond, start=start, page_len= page_len),
			{'txt': '%%%s%%' % txt})

@frappe.whitelist()
def get_pos_fields():
	return frappe.get_all("POS Field", fields=["label", "fieldname",
		"fieldtype", "default_value", "reqd", "read_only", "options"])<commit_msg>fix(pos): fix pos display item instock<commit_after># Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt

from __future__ import unicode_literals
import frappe, json
from frappe.utils.nestedset import get_root_of
from frappe.utils import cint
from erpnext.accounts.doctype.pos_profile.pos_profile import get_item_groups

from six import string_types

@frappe.whitelist()
def get_items(start, page_length, price_list, item_group, search_value="", pos_profile=None):
	data = dict()
	warehouse = ""
	display_items_in_stock = 0

	if pos_profile:
		warehouse, display_items_in_stock = frappe.db.get_value('POS Profile', pos_profile, ['warehouse', 'display_items_in_stock'])

	if not frappe.db.exists('Item Group', item_group):
		item_group = get_root_of('Item Group')

	if search_value:
		data = search_serial_or_batch_or_barcode_number(search_value)

	item_code = data.get("item_code") if data.get("item_code") else search_value
	serial_no = data.get("serial_no") if data.get("serial_no") else ""
	batch_no = data.get("batch_no") if data.get("batch_no") else ""
	barcode = data.get("barcode") if data.get("barcode") else ""

	condition = get_conditions(item_code, serial_no, batch_no, barcode)

	if pos_profile:
		condition += get_item_group_condition(pos_profile)

	lft, rgt = frappe.db.get_value('Item Group', item_group, ['lft', 'rgt'])
	# locate function is used to sort by closest match from the beginning of the value

	result = []

	items_data = frappe.db.sql(""" SELECT name as item_code,
			item_name, image as item_image, idx as idx,is_stock_item
		FROM
			`tabItem`
		WHERE
			disabled = 0 and has_variants = 0 and is_sales_item = 1
			and item_group in (select name from `tabItem Group` where lft >= {lft} and rgt <= {rgt})
			and {condition} order by idx desc limit {start}, {page_length}"""
		.format(
			start=start, page_length=page_length,
			lft=lft, rgt=rgt,
			condition=condition
		), as_dict=1)

	if items_data:
		items = [d.item_code for d in items_data]
		item_prices_data = frappe.get_all("Item Price",
			fields = ["item_code", "price_list_rate", "currency"],
			filters = {'price_list': price_list, 'item_code': ['in', items]})

		item_prices, bin_data = {}, {}
		for d in item_prices_data:
			item_prices[d.item_code] = d

		# prepare filter for bin query
		bin_filters = {'item_code': ['in', items]}
		if warehouse:
			bin_filters['warehouse'] = warehouse
		if display_items_in_stock:
			bin_filters['actual_qty'] = [">", 0]

		# query item bin
		bin_data = frappe.get_all(
			'Bin', fields=['item_code', 'sum(actual_qty) as actual_qty'],
			filters=bin_filters, group_by='item_code'
		)

		# convert list of dict into dict as {item_code: actual_qty}
		bin_dict = {}
		for b in bin_data:
			bin_dict[b.get('item_code')] = b.get('actual_qty')

		for item in items_data:
			item_code = item.item_code
			item_price = item_prices.get(item_code) or {}
			item_stock_qty = bin_dict.get(item_code)

			if display_items_in_stock and not item_stock_qty:
				pass
			else:
				row = {}
				row.update(item)
				row.update({
					'price_list_rate': item_price.get('price_list_rate'),
					'currency': item_price.get('currency'),
					'actual_qty': item_stock_qty,
				})
				result.append(row)

	res = {
		'items': result
	}

	if serial_no:
		res.update({
			'serial_no': serial_no
		})

	if batch_no:
		res.update({
			'batch_no': batch_no
		})

	if barcode:
		res.update({
			'barcode': barcode
		})

	return res

@frappe.whitelist()
def search_serial_or_batch_or_barcode_number(search_value):
	# search barcode no
	barcode_data = frappe.db.get_value('Item Barcode', {'barcode': search_value}, ['barcode', 'parent as item_code'], as_dict=True)
	if barcode_data:
		return barcode_data

	# search serial no
	serial_no_data = frappe.db.get_value('Serial No', search_value, ['name as serial_no', 'item_code'], as_dict=True)
	if serial_no_data:
		return serial_no_data

	# search batch no
	batch_no_data = frappe.db.get_value('Batch', search_value, ['name as batch_no', 'item as item_code'], as_dict=True)
	if batch_no_data:
		return batch_no_data

	return {}

def get_conditions(item_code, serial_no, batch_no, barcode):
	if serial_no or batch_no or barcode:
		return "name = {0}".format(frappe.db.escape(item_code))

	return """(name like {item_code}
		or item_name like {item_code})""".format(item_code = frappe.db.escape('%' + item_code + '%'))

def get_item_group_condition(pos_profile):
	cond = "and 1=1"
	item_groups = get_item_groups(pos_profile)
	if item_groups:
		cond = "and item_group in (%s)"%(', '.join(['%s']*len(item_groups)))

	return cond % tuple(item_groups)

def item_group_query(doctype, txt, searchfield, start, page_len, filters):
	item_groups = []
	cond = "1=1"
	pos_profile= filters.get('pos_profile')

	if pos_profile:
		item_groups = get_item_groups(pos_profile)

		if item_groups:
			cond = "name in (%s)"%(', '.join(['%s']*len(item_groups)))
			cond = cond % tuple(item_groups)

	return frappe.db.sql(""" select distinct name from `tabItem Group`
			where {condition} and (name like %(txt)s) limit {start}, {page_len}"""
		.format(condition = cond, start=start, page_len= page_len),
			{'txt': '%%%s%%' % txt})

@frappe.whitelist()
def get_pos_fields():
	return frappe.get_all("POS Field", fields=["label", "fieldname",
		"fieldtype", "default_value", "reqd", "read_only", "options"])<|endoftext|>


<commit_before>import StringIO
import requests
import xypath
import messytables
from hamcrest import equal_to, is_in
from orm import session, Value, DataSet, Indicator, send
import orm
import datetime
import re
import dl

indicator_list = """
SP.POP.TOTL
SP.POP.DPND.OL
SP.POP.DPND.YG
SH.DTH.IMRT
SM.POP.TOTL.ZS
AG.LND.TOTL.K2
SP.POP.GROW
EN.POP.DNST
EN.URB.MCTY
NY.GNP.MKTP.CD
SI.POV.DDAY
SP.DYN.AMRT.FE
SP.DYN.AMRT.MA
LP.LPI.OVRL.XQ
IS.ROD.TOTL.KM
IS.RRS.TOTL.KM
NY.GNP.MKTP.PP.CD
NY.GDP.PCAP.PP.CD
NY.GNP.PCAP.PP.CD
DT.ODA.ODAT.PC.ZS
IT.CEL.SETS.P2
IT.NET.USER.P2
NE.CON.PRVT.PP.KD
EG.ELC.ACCS.ZS
SI.POV.GINI""".strip().split('\n')

"""Value: dsID, region, indID, period, value, source, is_number
   DataSet: dsID, last_updated, last_scraped, name
   Indicator: indID, name, units
   """

dataset = {'dsID':'World Bank',
           'last_updated':None,
           'last_scraped':orm.now(),
           'name': 'World Bank'}

DataSet(**dataset).save()

def getcountrylist():
    for value in session.query(Value).filter(Value.indID == "m49-name").all():
        yield value.region

def getcountry(threeletter="PAK"):
    print threeletter
    baseurl = "http://api.worldbank.org/datafiles/%s_Country_MetaData_en_EXCEL.xls"
    value = {'dsID':'World Bank',
             'region':threeletter,
             'source':baseurl%threeletter,
             'is_number':True}

    fh = dl.grab(baseurl%threeletter, [404])
    if not fh: return
    messy = messytables.excel.XLSTableSet(fh)
    table = xypath.Table.from_messy(list(messy.tables)[0])
    indicators = table.filter(is_in(indicator_list))
    indname = indicators.shift(x=-1)
    if not len(indname) == len(indicator_list):
        print "missing indicators", [x.value for x in indname]

    code = table.filter(equal_to('Indicator Code'))

    years = code.fill(xypath.RIGHT)
    junction = indname.junction(years)
    for ind_cell, year_cell, value_cell in junction:
        vdict = dict(value)
        vdict['indID'] = ind_cell.value
        vdict['period'] = year_cell.value
        vdict['value'] = value_cell.value

        indicator = {'indID':vdict['indID']}
        nameunits = re.search('(.*)\((.*)\)',vdict['indID'])
        if nameunits:
            (indicator['name'], indicator['units'])=nameunits.groups()
        else:
            indicator['name']=vdict['indID']
            indicator['units']='uno'
        Indicator(**indicator).save()
        v = Value(**vdict)
        if not v.is_blank():
            v.save()
    print len(session.query(Value).filter(Value.dsID=='World Bank').all())
    session.commit()

for country in getcountrylist():
    getcountry(country)
<commit_msg>add inflation to world bank<commit_after>import StringIO
import requests
import xypath
import messytables
from hamcrest import equal_to, is_in
from orm import session, Value, DataSet, Indicator, send
import orm
import datetime
import re
import dl

indicator_list = """
SP.POP.TOTL
SP.POP.DPND.OL
SP.POP.DPND.YG
SH.DTH.IMRT
SM.POP.TOTL.ZS
AG.LND.TOTL.K2
SP.POP.GROW
EN.POP.DNST
EN.URB.MCTY
NY.GNP.MKTP.CD
SI.POV.DDAY
SP.DYN.AMRT.FE
SP.DYN.AMRT.MA
LP.LPI.OVRL.XQ
IS.ROD.TOTL.KM
IS.RRS.TOTL.KM
NY.GNP.MKTP.PP.CD
NY.GDP.PCAP.PP.CD
NY.GNP.PCAP.PP.CD
DT.ODA.ODAT.PC.ZS
IT.CEL.SETS.P2
IT.NET.USER.P2
NE.CON.PRVT.PP.KD
EG.ELC.ACCS.ZS
SI.POV.GINI
FP.CPI.TOTL.ZG
""".strip().split('\n')

"""Value: dsID, region, indID, period, value, source, is_number
   DataSet: dsID, last_updated, last_scraped, name
   Indicator: indID, name, units
   """

dataset = {'dsID':'World Bank',
           'last_updated':None,
           'last_scraped':orm.now(),
           'name': 'World Bank'}

DataSet(**dataset).save()

def getcountrylist():
    for value in session.query(Value).filter(Value.indID == "m49-name").all():
        yield value.region

def getcountry(threeletter="PAK"):
    print threeletter
    baseurl = "http://api.worldbank.org/datafiles/%s_Country_MetaData_en_EXCEL.xls"
    value = {'dsID':'World Bank',
             'region':threeletter,
             'source':baseurl%threeletter,
             'is_number':True}

    fh = dl.grab(baseurl%threeletter, [404])
    if not fh: return
    messy = messytables.excel.XLSTableSet(fh)
    table = xypath.Table.from_messy(list(messy.tables)[0])
    indicators = table.filter(is_in(indicator_list))
    indname = indicators.shift(x=-1)
    if not len(indname) == len(indicator_list):
        print "missing indicators", [x.value for x in indname]

    code = table.filter(equal_to('Indicator Code'))

    years = code.fill(xypath.RIGHT)
    junction = indname.junction(years)
    for ind_cell, year_cell, value_cell in junction:
        vdict = dict(value)
        vdict['indID'] = ind_cell.value
        vdict['period'] = year_cell.value
        vdict['value'] = value_cell.value

        indicator = {'indID':vdict['indID']}
        nameunits = re.search('(.*)\((.*)\)',vdict['indID'])
        if nameunits:
            (indicator['name'], indicator['units'])=nameunits.groups()
        else:
            indicator['name']=vdict['indID']
            indicator['units']='uno'
        Indicator(**indicator).save()
        v = Value(**vdict)
        if not v.is_blank():
            v.save()
    print len(session.query(Value).filter(Value.dsID=='World Bank').all())
    session.commit()

for country in getcountrylist():
    getcountry(country)
<|endoftext|>


<commit_before>from flask import current_app

from dominate import tags
from visitor import Visitor

class BaseRenderer(Visitor):
    def visit_object(self, node):
        if current_app.debug:
            return '<!-- no implementation in {} to render {} -->'.format(
                self.__class__.__name__, node.__class__.__name__,
            )
        return ''

class SimpleRenderer(BaseRenderer):
    def visit_Link(self, node):
        return tags.a(node.title, title=node.title, **node.attribs)

    def visit_Navbar(self, node):
        cont = tags.nav(_class='navbar')

        for item in node.items:
            cont.add(tags.li(self.visit(item)))

        return cont

    def visit_View(self, node):
        kwargs = {}
        if node.active:
            kwargs['_class'] = 'active'
        return tags.a(node.title,
                      href=node.get_url(),
                      title=node.title,
                      **kwargs)

    def visit_Subgroup(self, node):
        group = tags.ul()

        for item in node.items:
            group.add(tags.li(self.visit(item)))

        return group

    def visit_Separator(self, node):
        return tags.span(_class='separator')

    def visit_Label(self, node):
        return tags.span(node.title, _class='nav-label')
<commit_msg>Use dominate to render comments as well.<commit_after>from flask import current_app

from dominate import tags
from visitor import Visitor

class BaseRenderer(Visitor):
    def visit_object(self, node):
        if current_app.debug:
            return tags.comment(
                'no implementation in {} to render {}'.format(
                    self.__class__.__name__, node.__class__.__name__,
                ))
        return ''

class SimpleRenderer(BaseRenderer):
    def visit_Link(self, node):
        return tags.a(node.title, title=node.title, **node.attribs)

    def visit_Navbar(self, node):
        cont = tags.nav(_class='navbar')

        for item in node.items:
            cont.add(tags.li(self.visit(item)))

        return cont

    def visit_View(self, node):
        kwargs = {}
        if node.active:
            kwargs['_class'] = 'active'
        return tags.a(node.title,
                      href=node.get_url(),
                      title=node.title,
                      **kwargs)

    def visit_Subgroup(self, node):
        group = tags.ul()

        for item in node.items:
            group.add(tags.li(self.visit(item)))

        return group

    def visit_Separator(self, node):
        return tags.span(_class='separator')

    def visit_Label(self, node):
        return tags.span(node.title, _class='nav-label')
<|endoftext|>


<commit_before>from functools import wraps
import os
import json
from base64 import b64encode
import time as time_module
from copy import copy
import logging

from six.moves.urllib.parse import urlencode
from flask import request, session, redirect, url_for, g
from oauth2client.client import flow_from_clientsecrets, OAuth2WebServerFlow,\
    AccessTokenRefreshError
import httplib2
from itsdangerous import TimedJSONWebSignatureSerializer, SignatureExpired

__all__ = ['OpenIDConnect', 'MemoryCredentials']

logger = logging.getLogger(__name__)

class MemoryCredentials(dict):
    """
    Non-persistent local credentials store.
    Use this if you only have one app server, and don't mind making everyone
    log in again after a restart.
    """
    pass

class OpenIDConnect(object):
    """
    @see: https://developers.google.com/api-client-library/python/start/get_started
    @see: https://developers.google.com/api-client-library/python/samples/authorized_api_web_server_calendar.py
    """
    def __init__(self, app=None, credentials_store=None, http=None, time=None,
                 urandom=None):
        # set from app config in .init_app()
        self.callback_path = None
        self.flow = None
        self.cookie_serializer = None

        # optional, also set from app config
        self.google_apps_domain = None
        self.id_token_cookie_name = 'oidc_id_token'
        self.id_token_cookie_ttl = 7 * 86400  # one week
        # should ONLY be turned off for local debugging
        self.id_token_cookie_secure = True

        # stuff that we might want to override for tests
        self.http = http if http is not None else httplib2.Http()
        self.credentials_store = credentials_store\
            if credentials_store is not None\
            else MemoryCredentials()
        self.time = time if time is not None else time_module.time
        self.urandom = urandom if urandom is not None else os.urandom

        # get stuff from the app's config, which may override stuff set above
        if app is not None:
            self.init_app(app)

    def init_app(self, app):
        """
        Do setup that requires a Flask app.
        """
        # register callback route and cookie-setting decorator
        app.route('/oidc_callback')(self.oidc_callback)
        app.after_request(self.after_request)

        # load client_secrets.json
        self.flow = flow_from_clientsecrets(
            app.config['OIDC_CLIENT_SECRETS'],
            scope=['openid', 'email'])
        assert isinstance(self.flow, OAuth2WebServerFlow)

        # create a cookie signer using the Flask secret key
        self.cookie_serializer = TimedJSONWebSignatureSerializer(
            app.config['SECRET_KEY'])

        try:
            self.google_apps_domain = app.config['OIDC_GOOGLE_APPS_DOMAIN']
        except KeyError:
            pass

        try:
            self.id_token_cookie_name = app.config['OIDC_ID_TOKEN_COOKIE_NAME']
        except KeyError:
            pass

        try:
            self.id_token_cookie_ttl = app.config['OIDC_ID_TOKEN_COOKIE_TTL']
        except KeyError:
            pass

        try:
            self.id_token_cookie_secure =\
                app.config['OIDC_ID_TOKEN_COOKIE_SECURE']
        except KeyError:
            pass

        try:
            self.credentials_store = app.config['OIDC_CREDENTIALS_STORE']
        except KeyError:
            pass

    def get_cookie_id_token(self):
        try:
            id_token_cookie = request.cookies[self.id_token_cookie_name]
            return self.cookie_serializer.loads(id_token_cookie)
        except (KeyError, SignatureExpired):
            logger.debug("Missing or invalid ID token cookie", exc_info=True)
            return None

    def set_cookie_id_token(self, id_token):
        """
        Cooperates with @after_request to set a new ID token cookie.
        """
        g.oidc_id_token = id_token
        g.oidc_id_token_dirty = True

    def after_request(self, response):
        """
        Set a new ID token cookie if the ID token has changed.
        """
        if getattr(g, 'oidc_id_token_dirty', False):
            signed_id_token = self.cookie_serializer.dumps(g.oidc_id_token)
            response.set_cookie(
                self.id_token_cookie_name, signed_id_token,
                secure=self.id_token_cookie_secure,
                httponly=True,
                max_age=self.id_token_cookie_ttl)
        return response

    def authenticate_or_redirect(self):
        """
        Helper function suitable for @app.before_request and @check (below).
        Sets g.oidc_id_token to the ID token if the user has successfully
        authenticated, else returns a redirect object so they can go try
        to authenticate.
        :return: A redirect, or None if the user is authenticated.
        """
        # the auth callback and error pages don't need user to be authenticated
        if request.endpoint in frozenset(['oidc_callback', 'oidc_error']):
            return None

        # retrieve signed ID token cookie
        id_token = self.get_cookie_id_token()
        if id_token is None:
            return self.redirect_to_auth_server(request.url)

        # ID token expired
        # when Google is the IdP, this happens after one hour
        if self.time() >= id_token['exp']:
            # get credentials from store
            try:
                credentials = self.credentials_store[id_token['sub']]
            except KeyError:
                logger.debug("Expired ID token, credentials missing",
                             exc_info=True)
                return self.redirect_to_auth_server(request.url)

            # refresh and store credentials
            try:
                credentials.refresh(self.http)
                id_token = credentials.id_token
                self.credentials_store[id_token['sub']] = credentials
                self.set_cookie_id_token(id_token)
            except AccessTokenRefreshError:
                # Can't refresh. Wipe credentials and redirect user to IdP
                # for re-authentication.
                logger.debug("Expired ID token, can't refresh credentials",
                             exc_info=True)
                del self.credentials_store[id_token['sub']]
                return self.redirect_to_auth_server(request.url)

        # make ID token available to views
        g.oidc_id_token = id_token

        return None

    def check(self, view_func):
        """
        Use this to decorate view functions if only some of your app's views
        require authentication.
        """
        @wraps(view_func)
        def decorated(*args, **kwargs):
            response = self.authenticate_or_redirect()
            if response is not None:
                return response
            return view_func(*args, **kwargs)
        return decorated

    def flow_for_request(self):
        """
        Build a flow with the correct absolute callback URL for this request.
        :return:
        """
        flow = copy(self.flow)
        flow.redirect_uri = url_for('oidc_callback', _external=True)
        return flow

    def redirect_to_auth_server(self, destination):
        """
        Set a CSRF token in the session, and redirect to the IdP.
        :param destination: the page that the user was going to,
                            before we noticed they weren't logged in
        :return: a redirect response
        """
        csrf_token = b64encode(self.urandom(24)).decode('utf-8')
        session['oidc_csrf_token'] = csrf_token
        state = {
            'csrf_token': csrf_token,
            'destination': destination,
        }
        extra_params = {
            'state': json.dumps(state),
        }
        flow = self.flow_for_request()
        auth_url = '{url}&{extra_params}'.format(
            url=flow.step1_get_authorize_url(),
            extra_params=urlencode(extra_params))
        # if the user has an ID token, it's invalid, or we wouldn't be here
        self.set_cookie_id_token(None)
        return redirect(auth_url)

    def is_id_token_valid(self, id_token):
        """
        Check if `id_token` is a current ID token for this application,
        was issued by the Apps domain we expected,
        and that the email address has been verified.

        @see: http://openid.net/specs/openid-connect-core-1_0.html#IDTokenValidation
        """
        if not id_token:
            return False

        # TODO: step 2: check issuer

        if isinstance(id_token['aud'], list):
            # step 3 for audience list
            if self.flow.client_id not in id_token['aud']:
                return False
            # step 4
            if 'azp' not in id_token:
                return False
        else:
            # step 3 for single audience
            if id_token['aud'] != self.flow.client_id:
                return False

        # step 5
        if 'azp' in id_token and id_token['azp'] != self.flow.client_id:
            return False

        # steps 9, 10
        if not (id_token['iat'] <= self.time() < id_token['exp']):
            return False

        # (not required if using HTTPS?) step 11: check nonce

        # additional steps specific to our usage

        if id_token.get('hd') != self.google_apps_domain:
            return False

        if not id_token['email_verified']:
            return False

        return True

    WRONG_GOOGLE_APPS_DOMAIN = 'WRONG_GOOGLE_APPS_DOMAIN'

    def oidc_callback(self):
        """
        Exchange the auth code for actual credentials,
        then redirect to the originally requested page.
        """
        # retrieve session and callback variables
        try:
            session_csrf_token = session.pop('oidc_csrf_token')

            state = json.loads(request.args['state'])
            csrf_token = state['csrf_token']
            destination = state['destination']

            code = request.args['code']
        except (KeyError, ValueError):
            logger.debug("Can't retrieve CSRF token, state, or code",
                         exc_info=True)
            return self.oidc_error()

        # check callback CSRF token passed to IdP
        # against session CSRF token held by user
        if csrf_token != session_csrf_token:
            logger.debug("CSRF token mismatch")
            return self.oidc_error()

        # make a request to IdP to exchange the auth code for OAuth credentials
        flow = self.flow_for_request()
        credentials = flow.step2_exchange(code, http=self.http)
        id_token = credentials.id_token
        if not self.is_id_token_valid(id_token):
            logger.debug("Invalid ID token")
            if id_token.get('hd') != self.google_apps_domain:
                return self.oidc_error(
                    "You must log in with an account from the {0} domain."
                    .format(self.google_apps_domain),
                    self.WRONG_GOOGLE_APPS_DOMAIN)
            return self.oidc_error()

        # store credentials by subject
        # when Google is the IdP, the subject is their G+ account number
        self.credentials_store[id_token['sub']] = credentials

        # set a persistent signed cookie containing the ID token
        # and redirect to the final destination
        # TODO: validate redirect destination
        response = redirect(destination)
        self.set_cookie_id_token(id_token)
        return response

    def oidc_error(self, message='Not Authorized', code=None):
        return (message, 401, {
            'Content-Type': 'text/plain',
        })
<commit_msg>Adding redirects and google domain exceptions<commit_after>from functools import wraps
import os
import json
from base64 import b64encode
import time as time_module
from copy import copy
import logging

from six.moves.urllib.parse import urlencode
from flask import request, session, redirect, url_for, g
from oauth2client.client import flow_from_clientsecrets, OAuth2WebServerFlow,\
    AccessTokenRefreshError
import httplib2
from itsdangerous import TimedJSONWebSignatureSerializer, SignatureExpired

__all__ = ['OpenIDConnect', 'MemoryCredentials']

logger = logging.getLogger(__name__)

class MemoryCredentials(dict):
    """
    Non-persistent local credentials store.
    Use this if you only have one app server, and don't mind making everyone
    log in again after a restart.
    """
    pass

class OpenIDConnect(object):
    """
    @see: https://developers.google.com/api-client-library/python/start/get_started
    @see: https://developers.google.com/api-client-library/python/samples/authorized_api_web_server_calendar.py
    """
    def __init__(self, app=None, credentials_store=None, http=None, time=None,
                 urandom=None):
        # set from app config in .init_app()
        self.callback_path = None
        self.flow = None
        self.cookie_serializer = None

        # optional, also set from app config
        self.google_apps_domain = None
        self.id_token_cookie_name = 'oidc_id_token'
        self.id_token_cookie_ttl = 7 * 86400  # one week
        # should ONLY be turned off for local debugging
        self.id_token_cookie_secure = True

        # stuff that we might want to override for tests
        self.http = http if http is not None else httplib2.Http()
        self.credentials_store = credentials_store\
            if credentials_store is not None\
            else MemoryCredentials()
        self.time = time if time is not None else time_module.time
        self.urandom = urandom if urandom is not None else os.urandom

        # get stuff from the app's config, which may override stuff set above
        if app is not None:
            self.init_app(app)

    def init_app(self, app):
        """
        Do setup that requires a Flask app.
        """
        # register callback route and cookie-setting decorator
        app.route('/oidc_callback')(self.oidc_callback)
        app.after_request(self.after_request)

        # load client_secrets.json
        self.flow = flow_from_clientsecrets(
            app.config['OIDC_CLIENT_SECRETS'],
            scope=['openid', 'email'])
        assert isinstance(self.flow, OAuth2WebServerFlow)

        # create a cookie signer using the Flask secret key
        self.cookie_serializer = TimedJSONWebSignatureSerializer(
            app.config['SECRET_KEY'])

        try:
            self.google_apps_domain = app.config['OIDC_GOOGLE_APPS_DOMAIN']
        except KeyError:
            pass

        try:
            self.id_token_cookie_name = app.config['OIDC_ID_TOKEN_COOKIE_NAME']
        except KeyError:
            pass

        try:
            self.id_token_cookie_ttl = app.config['OIDC_ID_TOKEN_COOKIE_TTL']
        except KeyError:
            pass

        try:
            self.id_token_cookie_secure =\
                app.config['OIDC_ID_TOKEN_COOKIE_SECURE']
        except KeyError:
            pass

        try:
            self.credentials_store = app.config['OIDC_CREDENTIALS_STORE']
        except KeyError:
            pass

    def get_cookie_id_token(self):
        try:
            id_token_cookie = request.cookies[self.id_token_cookie_name]
            return self.cookie_serializer.loads(id_token_cookie)
        except (KeyError, SignatureExpired):
            logger.debug("Missing or invalid ID token cookie", exc_info=True)
            return None

    def set_cookie_id_token(self, id_token):
        """
        Cooperates with @after_request to set a new ID token cookie.
        """
        g.oidc_id_token = id_token
        g.oidc_id_token_dirty = True

    def after_request(self, response):
        """
        Set a new ID token cookie if the ID token has changed.
        """
        if getattr(g, 'oidc_id_token_dirty', False):
            signed_id_token = self.cookie_serializer.dumps(g.oidc_id_token)
            response.set_cookie(
                self.id_token_cookie_name, signed_id_token,
                secure=self.id_token_cookie_secure,
                httponly=True,
                max_age=self.id_token_cookie_ttl)
        return response

    def authenticate_or_redirect(self):
        """
        Helper function suitable for @app.before_request and @check (below).
        Sets g.oidc_id_token to the ID token if the user has successfully
        authenticated, else returns a redirect object so they can go try
        to authenticate.
        :return: A redirect, or None if the user is authenticated.
        """
        # the auth callback and error pages don't need user to be authenticated
        if request.endpoint in frozenset(['oidc_callback', 'oidc_error']):
            return None

        # retrieve signed ID token cookie
        id_token = self.get_cookie_id_token()
        if id_token is None:
            return self.redirect_to_auth_server(request.url)

        # ID token expired
        # when Google is the IdP, this happens after one hour
        if self.time() >= id_token['exp']:
            # get credentials from store
            try:
                credentials = self.credentials_store[id_token['sub']]
            except KeyError:
                logger.debug("Expired ID token, credentials missing",
                             exc_info=True)
                return self.redirect_to_auth_server(request.url)

            # refresh and store credentials
            try:
                credentials.refresh(self.http)
                id_token = credentials.id_token
                self.credentials_store[id_token['sub']] = credentials
                self.set_cookie_id_token(id_token)
            except AccessTokenRefreshError:
                # Can't refresh. Wipe credentials and redirect user to IdP
                # for re-authentication.
                logger.debug("Expired ID token, can't refresh credentials",
                             exc_info=True)
                del self.credentials_store[id_token['sub']]
                return self.redirect_to_auth_server(request.url)

        # make ID token available to views
        g.oidc_id_token = id_token

        return None

    def check(self, view_func):
        """
        Use this to decorate view functions if only some of your app's views
        require authentication.
        """
        @wraps(view_func)
        def decorated(*args, **kwargs):
            response = self.authenticate_or_redirect()
            if response is not None:
                return response
            return view_func(*args, **kwargs)
        return decorated

    def flow_for_request(self):
        """
        Build a flow with the correct absolute callback URL for this request.
        :return:
        """
        flow = copy(self.flow)
        flow.redirect_uri = url_for('oidc_callback', _external=True)
        return flow

    def redirect_to_auth_server(self, destination):
        """
        Set a CSRF token in the session, and redirect to the IdP.
        :param destination: the page that the user was going to,
                            before we noticed they weren't logged in
        :return: a redirect response
        """
        csrf_token = b64encode(self.urandom(24)).decode('utf-8')
        session['oidc_csrf_token'] = csrf_token
        state = {
            'csrf_token': csrf_token,
            'destination': destination,
        }
        extra_params = {
            'state': json.dumps(state),
        }
        flow = self.flow_for_request()
        auth_url = '{url}&{extra_params}'.format(
            url=flow.step1_get_authorize_url(),
            extra_params=urlencode(extra_params))
        # if the user has an ID token, it's invalid, or we wouldn't be here
        self.set_cookie_id_token(None)
        return redirect(auth_url)

    def is_id_token_valid(self, id_token):
        """
        Check if `id_token` is a current ID token for this application,
        was issued by the Apps domain we expected,
        and that the email address has been verified.

        @see: http://openid.net/specs/openid-connect-core-1_0.html#IDTokenValidation
        """
        if not id_token:
            return False

        # TODO: step 2: check issuer

        if isinstance(id_token['aud'], list):
            # step 3 for audience list
            if self.flow.client_id not in id_token['aud']:
                return False
            # step 4
            if 'azp' not in id_token:
                return False
        else:
            # step 3 for single audience
            if id_token['aud'] != self.flow.client_id:
                return False

        # step 5
        if 'azp' in id_token and id_token['azp'] != self.flow.client_id:
            return False

        # steps 9, 10
        if not (id_token['iat'] <= self.time() < id_token['exp']):
            return False

        # (not required if using HTTPS?) step 11: check nonce

        # additional steps specific to our usage

        if id_token.get('hd') != self.google_apps_domain:
            return False

        if not id_token['email_verified']:
            return False

        return True

    WRONG_GOOGLE_APPS_DOMAIN = 'WRONG_GOOGLE_APPS_DOMAIN'

    def oidc_callback(self):
        """
        Exchange the auth code for actual credentials,
        then redirect to the originally requested page.
        """
        # retrieve session and callback variables
        try:
            session_csrf_token = session.pop('oidc_csrf_token')

            state = json.loads(request.args['state'])
            csrf_token = state['csrf_token']
            destination = state['destination']

            code = request.args['code']
        except (KeyError, ValueError):
            logger.debug("Can't retrieve CSRF token, state, or code",
                         exc_info=True)
            return self.oidc_error()

        # check callback CSRF token passed to IdP
        # against session CSRF token held by user
        if csrf_token != session_csrf_token:
            logger.debug("CSRF token mismatch")
            return self.oidc_error()

        # make a request to IdP to exchange the auth code for OAuth credentials
        flow = self.flow_for_request()
        credentials = flow.step2_exchange(code, http=self.http)
        id_token = credentials.id_token
        if not self.is_id_token_valid(id_token):
            logger.debug("Invalid ID token")
            # None will allow all domains otherwise limit down to the specific domain.
            if self.google_apps_domain is not None:
                if not isinstance(self.google_apps_domain, list):
                    self.google_apps_domain = [self.google_apps_domain]
                for domain in self.google_apps_domain:
                    if id_token.get('hd') != domain:
                        return self.oidc_error(
                            "You must log in with an account from the {0} domain."
                            .format(self.google_apps_domain),
                            self.WRONG_GOOGLE_APPS_DOMAIN)
            return self.oidc_error()

        # store credentials by subject
        # when Google is the IdP, the subject is their G+ account number
        self.credentials_store[id_token['sub']] = credentials

        # set a persistent signed cookie containing the ID token
        # and redirect to the final destination
        # TODO: validate redirect destination
        response = redirect_to(destination)
        self.set_cookie_id_token(id_token)
        return response

    def oidc_error(self, message='Not Authorized', code=None):
        return (message, 401, {
            'Content-Type': 'text/plain',
        })

    def redirect_to(self, destination, *args, **kwargs):
        path = request.args.get('next') or \
            request.referrer or \
            destination
        return redirect(path)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
import libvirt
import re
import virtinst.util as util
from django.shortcuts import render_to_response
from django.http import Http404, HttpResponse, HttpResponseRedirect
from virtmgr.model.models import *

def index(request, host):

   	if not request.user.is_authenticated():
	   	return HttpResponseRedirect('/login')

 	usr_id = request.user.id
	kvm_host = Host.objects.get(user=usr_id,hostname=host)
	usr_name = request.user

	def creds(credentials, user_data):
		for credential in credentials:
			if credential[0] == libvirt.VIR_CRED_AUTHNAME:
				credential[4] = kvm_host.login
				if len(credential[4]) == 0:
					credential[4] = credential[3]
			elif credential[0] == libvirt.VIR_CRED_PASSPHRASE:
				credential[4] = kvm_host.passwd
			else:
				return -1
		return 0

  	def vm_conn():
  		flags = [libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_PASSPHRASE]
	   	auth = [flags, creds, None]
		uri = 'qemu+tcp://' + kvm_host.ipaddr + '/system'
	   	try:
		   	conn = libvirt.openAuth(uri, auth, 0)
		   	return conn
		except:
			print "Not connected"

	def get_all_vm():
		vname = {}
		for id in conn.listDomainsID():
			id = int(id)
			dom = conn.lookupByID(id)
			vname[dom.name()] = dom.info()[0]
		for id in conn.listDefinedDomains():
			dom = conn.lookupByName(id)
			vname[dom.name()] = dom.info()[0]
		return vname

	def get_all_stg():
		storages = []
		for name in conn.listStoragePools():
			storages.append(name)
		for name in conn.listDefinedStoragePools():
			storages.append(name)
		return storages

	def get_all_net():
		networks = []
		for name in conn.listNetworks():
			networks.append(name)
		for name in conn.listDefinedNetworks():
			networks.append(name)
		# Not support all distro but Fedora!!!
		#for ifcfg in conn.listInterfaces():
		#	if ifcfg != 'lo' and not re.findall("eth", ifcfg):
		#		networks.append(ifcfg)
		#for ifcfg in conn.listDefinedInterfaces():
		#	if ifcfg != 'lo' and not re.findall("eth", ifcfg):
		#		networks.append(ifcfg)
		return networks

	def get_arch():
		arch = conn.getInfo()[0]
		return arch

	def find_all_iso():
		iso = []
		for storage in storages:
			stg = conn.storagePoolLookupByName(storage)
			stg.refresh(0)
			for img in stg.listVolumes():
				if re.findall(".iso", img) or re.findall(".ISO", img):
					iso.append(img)
		return iso

	def find_all_img():
		disk = []
		for storage in storages:
			stg = conn.storagePoolLookupByName(storage)
			stg.refresh(0)
			for img in stg.listVolumes():
				if re.findall(".img", img) or re.findall(".IMG", img):
					disk.append(img)
		return disk

	def get_img_path(vol):
		for storage in storages:
			stg = conn.storagePoolLookupByName(storage)
			for img in stg.listVolumes():
				if vol == img:
					vl = stg.storageVolLookupByName(vol)
					return vl.path()

	def get_img_format(vol):
		for storage in storages:
			stg = conn.storagePoolLookupByName(storage)
			for img in stg.listVolumes():
				if vol == img:
					vl = stg.storageVolLookupByName(vol)
					xml = vl.XMLDesc(0)
					format = util.get_xml_path(xml, "/volume/target/format/@type")
					return format

	def get_cpus():
		try:
			info = conn.getInfo()[2]
			return info
		except:
			print 'Get info failed'

	def get_emulator():
		try:
			emulator = []
			xml = conn.getCapabilities()
			arch = conn.getInfo()[0]
			if arch == 'x86_64':
				emulator.append(util.get_xml_path(xml,"/capabilities/guest[1]/arch/emulator"))
				emulator.append(util.get_xml_path(xml,"/capabilities/guest[2]/arch/emulator"))
			else:
				emulator = util.get_xml_path(xml,"/capabilities/guest[1]/arch/@name")
			return emulator
		except:
			print 'Get emulator failed'

	def get_machine():
		try:
			xml = conn.getCapabilities()
			machine = util.get_xml_path(xml,"/capabilities/guest/arch/machine/@canonical")
			return machine
		except:
			print 'Get info failed'

	def add_vm(name, mem, cpus, arch, machine, emul, img_frmt, img, iso, bridge):
		if not img:
			img = ''
		memaloc = mem
		xml = """<domain type='kvm'>
				  <name>%s</name>
				  <memory>%s</memory>
				  <currentMemory>%s</currentMemory>
				  <vcpu>%s</vcpu>
				  <os>
				    <type arch='%s' machine='%s'>hvm</type>
				    <boot dev='hd'/>
				    <boot dev='cdrom'/>
				    <bootmenu enable='yes'/>
				  </os>
				  <features>
				    <acpi/>
				    <apic/>
				    <pae/>
				  </features>
				  <clock offset='utc'/>
				  <on_poweroff>destroy</on_poweroff>
				  <on_reboot>restart</on_reboot>
				  <on_crash>restart</on_crash>
				  <devices>""" % (name, mem, memaloc, cpus, arch, machine)

		if arch == 'x86_64':
			xml += """<emulator>%s</emulator>""" % (emul[1])
		else:
			xml += """<emulator>%s</emulator>""" % (emul[0])

		xml += """<disk type='file' device='disk'>
				      <driver name='qemu' type='%s'/>
				      <source file='%s'/>
				      <target dev='hda' bus='ide'/>
				      <address type='drive' controller='0' bus='0' unit='0'/>
				    </disk>
				    <disk type='file' device='cdrom'>
				      <driver name='qemu' type='raw'/>
				      <source file='%s'/>
				      <target dev='hdc' bus='ide'/>
				      <readonly/>
				      <address type='drive' controller='0' bus='1' unit='0'/>
				    </disk>
				    <controller type='ide' index='0'>
				      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
				    </controller>
				    """ % (img_frmt, img, iso)

		if re.findall("br", bridge):
			xml += """<interface type='bridge'>
					<source bridge='%s'/>""" % (bridge)
		else:
			xml += """<interface type='network'>
					<source network='%s'/>""" % (bridge)

		xml += """<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
				    </interface>
				    <input type='tablet' bus='usb'/>
				    <input type='mouse' bus='ps2'/>
				    <graphics type='vnc' port='-1' autoport='yes'/>
				    <video>
				      <model type='cirrus' vram='9216' heads='1'/>
				      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
				    </video>
				    <memballoon model='virtio'>
				      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
				    </memballoon>
				  </devices>
				</domain>"""
		conn.defineXML(xml)

	conn = vm_conn()

	if conn == None:
		return HttpResponseRedirect('/overview/' + host + '/')

	cores = get_cpus()
	all_vm = get_all_vm()
	storages = get_all_stg()
	all_iso = find_all_iso()
	all_img = find_all_img()
	bridge = get_all_net()
	arch = get_arch()
	emul = get_emulator()
	machine = get_machine()
	addmem = conn.getInfo()[1]

	# Core CPUS
	cpus = []
	for cpu in range(1,cores+1):
		cpus.append(cpu)

	if request.method == 'POST':
		name = request.POST.get('name','')
		setmem = request.POST.get('memory','')
		cpus = request.POST.get('cpus','')
		iso = request.POST.get('iso','')
		img = request.POST.get('img','')
		netbr = request.POST.get('bridge','')
		archvm = request.POST.get('arch','')
		setmem = int(setmem) * 1024
		hdd = get_img_path(img)
		cdrom = get_img_path(iso)
		hdd_frmt = get_img_format(img)
		errors = []
		if not img:
			errors.append(u'Образы HDD для виртуальной машины отсутствуют')
		if not name:
			errors.append(u'Введите название виртуальной машины')
		if not errors:
			add_vm(name, setmem, cpus, archvm, machine, emul, hdd_frmt, hdd, cdrom, netbr)
			return HttpResponseRedirect('/vm/' + host + '/' + name + '/')

	return render_to_response('newvm.html', locals())

def redir(request):
	if not request.user.is_authenticated():
		return HttpResponseRedirect('/')
	else:
		return HttpResponseRedirect('/dashboard/')<commit_msg>fix newvm hdd<commit_after># -*- coding: utf-8 -*-
import libvirt
import re
import virtinst.util as util
from django.shortcuts import render_to_response
from django.http import Http404, HttpResponse, HttpResponseRedirect
from virtmgr.model.models import *

def index(request, host):

   	if not request.user.is_authenticated():
	   	return HttpResponseRedirect('/login')

 	usr_id = request.user.id
	kvm_host = Host.objects.get(user=usr_id,hostname=host)
	usr_name = request.user

	def creds(credentials, user_data):
		for credential in credentials:
			if credential[0] == libvirt.VIR_CRED_AUTHNAME:
				credential[4] = kvm_host.login
				if len(credential[4]) == 0:
					credential[4] = credential[3]
			elif credential[0] == libvirt.VIR_CRED_PASSPHRASE:
				credential[4] = kvm_host.passwd
			else:
				return -1
		return 0

  	def vm_conn():
  		flags = [libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_PASSPHRASE]
	   	auth = [flags, creds, None]
		uri = 'qemu+tcp://' + kvm_host.ipaddr + '/system'
	   	try:
		   	conn = libvirt.openAuth(uri, auth, 0)
		   	return conn
		except:
			print "Not connected"

	def get_all_vm():
		vname = {}
		for id in conn.listDomainsID():
			id = int(id)
			dom = conn.lookupByID(id)
			vname[dom.name()] = dom.info()[0]
		for id in conn.listDefinedDomains():
			dom = conn.lookupByName(id)
			vname[dom.name()] = dom.info()[0]
		return vname

	def get_all_stg():
		storages = []
		for name in conn.listStoragePools():
			storages.append(name)
		for name in conn.listDefinedStoragePools():
			storages.append(name)
		return storages

	def get_all_net():
		networks = []
		for name in conn.listNetworks():
			networks.append(name)
		for name in conn.listDefinedNetworks():
			networks.append(name)
		# Not support all distro but Fedora!!!
		#for ifcfg in conn.listInterfaces():
		#	if ifcfg != 'lo' and not re.findall("eth", ifcfg):
		#		networks.append(ifcfg)
		#for ifcfg in conn.listDefinedInterfaces():
		#	if ifcfg != 'lo' and not re.findall("eth", ifcfg):
		#		networks.append(ifcfg)
		return networks

	def get_arch():
		arch = conn.getInfo()[0]
		return arch

	def find_all_iso():
		iso = []
		for storage in storages:
			stg = conn.storagePoolLookupByName(storage)
			stg.refresh(0)
			for img in stg.listVolumes():
				if re.findall(".iso", img) or re.findall(".ISO", img):
					iso.append(img)
		return iso

	def find_all_img():
		disk = []
		for storage in storages:
			stg = conn.storagePoolLookupByName(storage)
			stg.refresh(0)
			for img in stg.listVolumes():
				if re.findall(".img", img) or re.findall(".IMG", img):
					disk.append(img)
		return disk

	def get_img_path(vol):
		for storage in storages:
			stg = conn.storagePoolLookupByName(storage)
			for img in stg.listVolumes():
				if vol == img:
					vl = stg.storageVolLookupByName(vol)
					return vl.path()

	def get_img_format(vol):
		for storage in storages:
			stg = conn.storagePoolLookupByName(storage)
			for img in stg.listVolumes():
				if vol == img:
					vl = stg.storageVolLookupByName(vol)
					xml = vl.XMLDesc(0)
					format = util.get_xml_path(xml, "/volume/target/format/@type")
					return format

	def get_cpus():
		try:
			info = conn.getInfo()[2]
			return info
		except:
			print 'Get info failed'

	def get_emulator():
		try:
			emulator = []
			xml = conn.getCapabilities()
			arch = conn.getInfo()[0]
			if arch == 'x86_64':
				emulator.append(util.get_xml_path(xml,"/capabilities/guest[1]/arch/emulator"))
				emulator.append(util.get_xml_path(xml,"/capabilities/guest[2]/arch/emulator"))
			else:
				emulator = util.get_xml_path(xml,"/capabilities/guest[1]/arch/@name")
			return emulator
		except:
			print 'Get emulator failed'

	def get_machine():
		try:
			xml = conn.getCapabilities()
			machine = util.get_xml_path(xml,"/capabilities/guest/arch/machine/@canonical")
			return machine
		except:
			print 'Get info failed'

	def add_vm(name, mem, cpus, arch, machine, emul, img_frmt, img, iso, bridge):
		if not iso:
			iso = ''
		memaloc = mem
		xml = """<domain type='kvm'>
				  <name>%s</name>
				  <memory>%s</memory>
				  <currentMemory>%s</currentMemory>
				  <vcpu>%s</vcpu>
				  <os>
				    <type arch='%s' machine='%s'>hvm</type>
				    <boot dev='hd'/>
				    <boot dev='cdrom'/>
				    <bootmenu enable='yes'/>
				  </os>
				  <features>
				    <acpi/>
				    <apic/>
				    <pae/>
				  </features>
				  <clock offset='utc'/>
				  <on_poweroff>destroy</on_poweroff>
				  <on_reboot>restart</on_reboot>
				  <on_crash>restart</on_crash>
				  <devices>""" % (name, mem, memaloc, cpus, arch, machine)

		if arch == 'x86_64':
			xml += """<emulator>%s</emulator>""" % (emul[1])
		else:
			xml += """<emulator>%s</emulator>""" % (emul[0])

		xml += """<disk type='file' device='disk'>
				      <driver name='qemu' type='%s'/>
				      <source file='%s'/>
				      <target dev='hda' bus='ide'/>
				      <address type='drive' controller='0' bus='0' unit='0'/>
				    </disk>
				    <disk type='file' device='cdrom'>
				      <driver name='qemu' type='raw'/>
				      <source file='%s'/>
				      <target dev='hdc' bus='ide'/>
				      <readonly/>
				      <address type='drive' controller='0' bus='1' unit='0'/>
				    </disk>
				    <controller type='ide' index='0'>
				      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
				    </controller>
				    """ % (img_frmt, img, iso)

		if re.findall("br", bridge):
			xml += """<interface type='bridge'>
					<source bridge='%s'/>""" % (bridge)
		else:
			xml += """<interface type='network'>
					<source network='%s'/>""" % (bridge)

		xml += """<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
				    </interface>
				    <input type='tablet' bus='usb'/>
				    <input type='mouse' bus='ps2'/>
				    <graphics type='vnc' port='-1' autoport='yes'/>
				    <video>
				      <model type='cirrus' vram='9216' heads='1'/>
				      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
				    </video>
				    <memballoon model='virtio'>
				      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
				    </memballoon>
				  </devices>
				</domain>"""
		conn.defineXML(xml)

	conn = vm_conn()

	if conn == None:
		return HttpResponseRedirect('/overview/' + host + '/')

	cores = get_cpus()
	all_vm = get_all_vm()
	storages = get_all_stg()
	all_iso = find_all_iso()
	all_img = find_all_img()
	bridge = get_all_net()
	arch = get_arch()
	emul = get_emulator()
	machine = get_machine()
	addmem = conn.getInfo()[1]

	# Core CPUS
	cpus = []
	for cpu in range(1,cores+1):
		cpus.append(cpu)

	if request.method == 'POST':
		name = request.POST.get('name','')
		setmem = request.POST.get('memory','')
		cpus = request.POST.get('cpus','')
		iso = request.POST.get('iso','')
		img = request.POST.get('img','')
		netbr = request.POST.get('bridge','')
		archvm = request.POST.get('arch','')
		setmem = int(setmem) * 1024
		hdd = get_img_path(img)
		cdrom = get_img_path(iso)
		hdd_frmt = get_img_format(img)
		errors = []
		if not img:
			errors.append(u'Образы HDD для виртуальной машины отсутствуют')
		if not name:
			errors.append(u'Введите название виртуальной машины')
		if not errors:
			add_vm(name, setmem, cpus, archvm, machine, emul, hdd_frmt, hdd, cdrom, netbr)
			return HttpResponseRedirect('/vm/' + host + '/' + name + '/')

	return render_to_response('newvm.html', locals())

def redir(request):
	if not request.user.is_authenticated():
		return HttpResponseRedirect('/')
	else:
		return HttpResponseRedirect('/dashboard/')<|endoftext|>


<commit_before>#encoding: utf8
import itertools
import unittest
from nose.tools import *

import mapnik2
from binascii import unhexlify
from utilities import Todo

class FeatureTest(unittest.TestCase):
    def makeOne(self, *args, **kw):
        return mapnik2.Feature(*args, **kw)

    def test_default_constructor(self):
        f = self.makeOne(1)
        self.failUnless(f is not None)

    def test_python_extended_constructor(self):
        f = self.makeOne(1, 'POLYGON ((35 10, 10 20, 15 40, 45 45, 35 10),(20 30, 35 35, 30 20, 20 30))', foo="bar")
        self.failUnlessEqual(f['foo'], 'bar')
        self.failUnlessEqual(f.envelope(),mapnik2.Box2d(10.0,10.0,45.0,45.0))

    def test_set_get_properties(self):
        f = self.makeOne(1)
        counter = itertools.count(0)
        def test_val(expected):
            key = 'prop%d'%counter.next()
            try:
                f[key] = expected
            except TypeError:
                self.fail("%r (%s)"%(expected, type(expected)))
            self.failUnlessEqual(f[key], expected)
        for v in (1, True, 1.4, "foo", u"avión"):
            test_val(v)

    def test_add_wkt_geometry(self):
        def add_geom_wkt(wkt):
            f = self.makeOne(1)
            self.failUnlessEqual(len(f.geometries()), 0)
            f.add_geometries_from_wkt(wkt)
            self.failUnlessEqual(len(f.geometries()), 3)
            e = mapnik2.Box2d()
            self.failUnlessEqual(e.valid(), False)
            for g in f.geometries():
                if not e.valid():
                    e = g.envelope()
                else:
                    e +=g.envelope()

            self.failUnlessEqual(e, f.envelope())

        def add_geom_wkb(wkb):
            f = self.makeOne(1)
            self.failUnlessEqual(len(f.geometries()), 0)
            f.add_geometries_from_wkb(unhexlify(wkb))
            self.failUnlessEqual(len(f.geometries()), 1)
            e = mapnik2.Box2d()
            self.failUnlessEqual(e.valid(), False)
            for g in f.geometries():
                if not e.valid():
                    e = g.envelope()
                else:
                    e +=g.envelope()

            self.failUnlessEqual(e, f.envelope())

        def run() :
            add_geom_wkt('GEOMETRYCOLLECTION(POINT(4 6),LINESTRING(4 6,7 10),POLYGON ((30 10, 10 20, 20 40, 40 40, 30 10)))')
            add_geom_wkb('010300000001000000050000000000000000003e4000000000000024400000000000002440000000000000344000000000000034400000000000004440000000000000444000000000000044400000000000003e400000000000002440') # POLYGON ((30 10, 10 20, 20 40, 40 40, 30 10))
        run()

def test_feature_expression_evaluation():
    f = mapnik2.Feature(1)
    f['name'] = 'a'
    expr = mapnik2.Expression("[name]='a'")
    evaluated = expr.evaluate(f)
    eq_(evaluated,True)
    num_attributes = len(f)
    eq_(num_attributes,1)
    eq_(f.id(),1)

# https://github.com/mapnik/mapnik/issues/933
def test_feature_expression_evaluation_missing_attr():
    f = mapnik2.Feature(1)
    f['name'] = 'a'
    expr = mapnik2.Expression("[fielddoesnotexist]='a'")
    evaluated = expr.evaluate(f)
    eq_(evaluated,True)
    num_attributes = len(f)
    eq_(num_attributes,1)
    eq_(f.id(),1)

# https://github.com/mapnik/mapnik/issues/934
def test_feature_expression_evaluation_attr_with_spaces():
    f = mapnik2.Feature(1)
    f['name with space'] = 'a'
    expr = mapnik2.Expression("[name with space]='a'")
    eq_(expr.evaluate(f),True)

if __name__ == "__main__":
    [eval(run)() for run in dir() if 'test_' in run]
<commit_msg>more robust tests for feature expressions - refs #933 and #944<commit_after>#encoding: utf8
import itertools
import unittest
from nose.tools import *

import mapnik2
from binascii import unhexlify
from utilities import Todo

class FeatureTest(unittest.TestCase):
    def makeOne(self, *args, **kw):
        return mapnik2.Feature(*args, **kw)

    def test_default_constructor(self):
        f = self.makeOne(1)
        self.failUnless(f is not None)

    def test_python_extended_constructor(self):
        f = self.makeOne(1, 'POLYGON ((35 10, 10 20, 15 40, 45 45, 35 10),(20 30, 35 35, 30 20, 20 30))', foo="bar")
        self.failUnlessEqual(f['foo'], 'bar')
        self.failUnlessEqual(f.envelope(),mapnik2.Box2d(10.0,10.0,45.0,45.0))

    def test_set_get_properties(self):
        f = self.makeOne(1)
        counter = itertools.count(0)
        def test_val(expected):
            key = 'prop%d'%counter.next()
            try:
                f[key] = expected
            except TypeError:
                self.fail("%r (%s)"%(expected, type(expected)))
            self.failUnlessEqual(f[key], expected)
        for v in (1, True, 1.4, "foo", u"avión"):
            test_val(v)

    def test_add_wkt_geometry(self):
        def add_geom_wkt(wkt):
            f = self.makeOne(1)
            self.failUnlessEqual(len(f.geometries()), 0)
            f.add_geometries_from_wkt(wkt)
            self.failUnlessEqual(len(f.geometries()), 3)
            e = mapnik2.Box2d()
            self.failUnlessEqual(e.valid(), False)
            for g in f.geometries():
                if not e.valid():
                    e = g.envelope()
                else:
                    e +=g.envelope()

            self.failUnlessEqual(e, f.envelope())

        def add_geom_wkb(wkb):
            f = self.makeOne(1)
            self.failUnlessEqual(len(f.geometries()), 0)
            f.add_geometries_from_wkb(unhexlify(wkb))
            self.failUnlessEqual(len(f.geometries()), 1)
            e = mapnik2.Box2d()
            self.failUnlessEqual(e.valid(), False)
            for g in f.geometries():
                if not e.valid():
                    e = g.envelope()
                else:
                    e +=g.envelope()

            self.failUnlessEqual(e, f.envelope())

        def run() :
            add_geom_wkt('GEOMETRYCOLLECTION(POINT(4 6),LINESTRING(4 6,7 10),POLYGON ((30 10, 10 20, 20 40, 40 40, 30 10)))')
            add_geom_wkb('010300000001000000050000000000000000003e4000000000000024400000000000002440000000000000344000000000000034400000000000004440000000000000444000000000000044400000000000003e400000000000002440') # POLYGON ((30 10, 10 20, 20 40, 40 40, 30 10))
        run()

def test_feature_expression_evaluation():
    f = mapnik2.Feature(1)
    f['name'] = 'a'
    eq_(f['name'],u'a')
    expr = mapnik2.Expression("[name]='a'")
    evaluated = expr.evaluate(f)
    eq_(evaluated,True)
    num_attributes = len(f)
    eq_(num_attributes,1)
    eq_(f.id(),1)

# https://github.com/mapnik/mapnik/issues/933
def test_feature_expression_evaluation_missing_attr():
    f = mapnik2.Feature(1)
    f['name'] = u'a'
    eq_(f['name'],u'a')
    expr = mapnik2.Expression("[fielddoesnotexist]='a'")
    evaluated = expr.evaluate(f)
    eq_(evaluated,False)
    num_attributes = len(f)
    eq_(num_attributes,1)
    eq_(f.id(),1)

# https://github.com/mapnik/mapnik/issues/934
def test_feature_expression_evaluation_attr_with_spaces():
    f = mapnik2.Feature(1)
    f['name with space'] = u'a'
    eq_(f['name with space'],u'a')
    expr = mapnik2.Expression("[name with space]='a'")
    eq_(str(expr),"[name with space]='a'")
    eq_(expr.evaluate(f),True)

if __name__ == "__main__":
    [eval(run)() for run in dir() if 'test_' in run]
<|endoftext|>


<commit_before>from __future__ import unicode_literals, division, absolute_import
from builtins import *  # pylint: disable=unused-import, redefined-builtin

import logging

from flask_restplus import inputs

from flexget.plugin import get_plugins, get_plugin_by_name, DependencyError
from flexget.api import api, APIResource, BadRequest

log = logging.getLogger('plugins')

plugins_api = api.namespace('plugins', description='Get Flexget plugins')

class ObjectsContainer(object):
    phase_object = {
        'type': 'object',
        'properties': {
            'phase': {'type': 'string'},
            'priority': {'type': 'integer'}
        }
    }

    plugin_object = {
        'type': 'object',
        'properties': {
            'name': {'type': 'string'},
            'api_ver': {'type': 'integer'},
            'builtin': {'type': 'boolean'},
            'category': {'type': 'string'},
            'contexts': {'type': 'array', 'items': {'type': 'string'}},
            'debug': {'type': 'boolean'},
            'groups': {'type': 'array', 'items': {'type': 'string'}},
            'phase_handlers': {'type': 'array', 'items': phase_object}
        }
    }

    plugin_list_reply = {
        'type': 'object',
        'properties': {
            'plugin_list': {'type': 'array', 'items': plugin_object},
            'number_of_plugins': {'type': 'integer'}
        }
    }

plugin_schema = api.schema('plugin_object', ObjectsContainer.plugin_object)
plugin_list_reply_schema = api.schema('plugin_list_reply', ObjectsContainer.plugin_list_reply)

plugin_parser = api.parser()
plugin_parser.add_argument('include_schema', type=inputs.boolean, default=False,
                           help='Include plugin schema. This will increase response size')

plugins_parser = plugin_parser.copy()

plugins_parser.add_argument('group', case_sensitive=False, help='Show plugins belonging to this group')
plugins_parser.add_argument('phase', case_sensitive=False, help='Show plugins that act on this phase')

def plugin_to_dict(plugin):
    return {
        'name': plugin.name,
        'api_ver': plugin.api_ver,
        'builtin': plugin.builtin,
        'category': plugin.category,
        'contexts': plugin.contexts,
        'debug': plugin.debug,
        'groups': plugin.groups,
        'phase_handlers': [dict(phase=handler, priority=event.priority) for handler, event in
                           plugin.phase_handlers.items()]
    }

@plugins_api.route('/')
class PluginsAPI(APIResource):
    @api.response(200, model=plugin_list_reply_schema)
    @api.response(BadRequest)
    @api.doc(parser=plugins_parser)
    def get(self, session=None):
        """ Get list of registered plugins """
        args = plugins_parser.parse_args()
        plugin_list = []
        try:
            for plugin in get_plugins(phase=args['phase'], group=args['group']):
                p = plugin_to_dict(plugin)
                if args['include_schema']:
                    p['schema'] = plugin.schema
                plugin_list.append(p)
        except ValueError as e:
            raise BadRequest(str(e))
        return {'plugin_list': plugin_list,
                'number_of_plugins': len(plugin_list)}

@plugins_api.route('/<string:plugin_name>/')
class PluginAPI(APIResource):
    @api.response(BadRequest)
    @api.response(200, model=plugin_schema)
    @api.doc(parser=plugin_parser, params={'plugin_name': 'Name of the plugin to return'})
    def get(self, plugin_name, session=None):
        """ Return plugin data by name"""
        args = plugin_parser.parse_args()
        try:
            plugin = get_plugin_by_name(plugin_name, issued_by='plugins API')
        except DependencyError as e:
            raise BadRequest(e.message)
        p = plugin_to_dict(plugin)
        if args['include_schema']:
            p['schema'] = plugin.schema
        return p
<commit_msg>Fixed plugins test<commit_after>from __future__ import unicode_literals, division, absolute_import
from builtins import *  # pylint: disable=unused-import, redefined-builtin

import logging

from flask_restplus import inputs

from flexget.plugin import get_plugins, get_plugin_by_name, DependencyError
from flexget.api import api, APIResource, BadRequest

log = logging.getLogger('plugins')

plugins_api = api.namespace('plugins', description='Get Flexget plugins')

class ObjectsContainer(object):
    phase_object = {
        'type': 'object',
        'properties': {
            'phase': {'type': 'string'},
            'priority': {'type': 'integer'}
        }
    }

    plugin_object = {
        'type': 'object',
        'properties': {
            'name': {'type': 'string'},
            'api_ver': {'type': 'integer'},
            'builtin': {'type': 'boolean'},
            'category': {'type': ['string', 'null']},
            'contexts': {'type': 'array', 'items': {'type': 'string'}},
            'debug': {'type': 'boolean'},
            'groups': {'type': 'array', 'items': {'type': 'string'}},
            'phase_handlers': {'type': 'array', 'items': phase_object}
        }
    }

    plugin_list_reply = {
        'type': 'object',
        'properties': {
            'plugin_list': {'type': 'array', 'items': plugin_object},
            'number_of_plugins': {'type': 'integer'}
        }
    }

plugin_schema = api.schema('plugin_object', ObjectsContainer.plugin_object)
plugin_list_reply_schema = api.schema('plugin_list_reply', ObjectsContainer.plugin_list_reply)

plugin_parser = api.parser()
plugin_parser.add_argument('include_schema', type=inputs.boolean, default=False,
                           help='Include plugin schema. This will increase response size')

plugins_parser = plugin_parser.copy()

plugins_parser.add_argument('group', case_sensitive=False, help='Show plugins belonging to this group')
plugins_parser.add_argument('phase', case_sensitive=False, help='Show plugins that act on this phase')

def plugin_to_dict(plugin):
    return {
        'name': plugin.name,
        'api_ver': plugin.api_ver,
        'builtin': plugin.builtin,
        'category': plugin.category,
        'contexts': plugin.contexts,
        'debug': plugin.debug,
        'groups': plugin.groups,
        'phase_handlers': [dict(phase=handler, priority=event.priority) for handler, event in
                           plugin.phase_handlers.items()]
    }

@plugins_api.route('/')
class PluginsAPI(APIResource):
    @api.response(200, model=plugin_list_reply_schema)
    @api.response(BadRequest)
    @api.doc(parser=plugins_parser)
    def get(self, session=None):
        """ Get list of registered plugins """
        args = plugins_parser.parse_args()
        plugin_list = []
        try:
            for plugin in get_plugins(phase=args['phase'], group=args['group']):
                p = plugin_to_dict(plugin)
                if args['include_schema']:
                    p['schema'] = plugin.schema
                plugin_list.append(p)
        except ValueError as e:
            raise BadRequest(str(e))
        return {'plugin_list': plugin_list,
                'number_of_plugins': len(plugin_list)}

@plugins_api.route('/<string:plugin_name>/')
class PluginAPI(APIResource):
    @api.response(BadRequest)
    @api.response(200, model=plugin_schema)
    @api.doc(parser=plugin_parser, params={'plugin_name': 'Name of the plugin to return'})
    def get(self, plugin_name, session=None):
        """ Return plugin data by name"""
        args = plugin_parser.parse_args()
        try:
            plugin = get_plugin_by_name(plugin_name, issued_by='plugins API')
        except DependencyError as e:
            raise BadRequest(e.message)
        p = plugin_to_dict(plugin)
        if args['include_schema']:
            p['schema'] = plugin.schema
        return p
<|endoftext|>


<commit_before>""" Version module.

    Allows to define the version number uniquely.
"""

__version__ = "0.5"
<commit_msg>Changed version number<commit_after>""" Version module.

    Allows to define the version number uniquely.
"""

__version__ = "0.6"
<|endoftext|>


<commit_before>#!/usr/bin/python3.4
'''
This is the flight controller program for a high-altitude balloon
payload. The process herein allows for easy adding and removing of
sensors, and simple, clear user interface on launch day.

Russell Jeffery
14 July 2017
'''

from fl_objects_2 import *

def main():
    #------------------------------------------------------------------
    '''
    This is the body of the program.
    '''
    #------------------------------------------------------------------

    #Here, the launch function awaits input from the user.
    trigger_pin = 22
    comfort_led = 29
    launch(trigger_pin, comfort_led)

    #This next bit is the startup procedure.
    try:
        ###############################################################
        '''
        If you are in the business of adding or removing sensors, you
        are in the right place!

        --Variables:    Define any variables that you will need for
        several sensors.
        --Sensors:      Instantiate the sensor objects.
        --Queue:        Add the sensors to the start/write/stop queue.

        '''

        #Variables.
        Vref = 5.46

        #Sensors.

        #Queue.
        sensors = []

        ###############################################################

    except KeyboardInterrupt:
        print('flight_controller_2.py was terminated by the user.')

    finally:
        #Stop all the sensors.

        GPIO.cleanup()

main()
<commit_msg>Updated the flight controller<commit_after>#!/usr/bin/python3.4
'''
This is the flight controller program for a high-altitude balloon
payload. The process herein allows for easy adding and removing of
sensors, and simple, clear user interface on launch day.

Russell Jeffery
14 July 2017
'''

from fl_objects_2 import *

def main():
    #------------------------------------------------------------------
    '''
    This is the body of the program.
    '''
    #------------------------------------------------------------------

    #Here, the launch function awaits input from the user.
    trigger_pin = 22
    comfort_led = 29
    launch(trigger_pin, comfort_led)

    #This next bit is the startup procedure.
    try:
        ###############################################################
        '''
        If you are in the business of adding or removing sensors, you
        are in the right place!

        --Variables:    Define any variables that you will need for several sensors.
        --Sensors:      Instantiate the sensor objects.
        --Queue:        Add the sensors to the start/write/stop queue.

        '''

        #Variables.
        Vref = 5.46

        #Sensors.

        #Queue.
        sensors = []

        ###############################################################

        #Start all the sensors with their identical ".start()" methods.
        for sensor in sensors:
            try:
                sensor.start()
            except:
                sensors.remove(sensor)
                print(sensor.name, 'failed to start. It was kicked out of the queue.')
            finally:
                pass

        #This is the main loop that is going to be running for most of the flight.
        flying = True
        while flying:

            #The following checks for a button push.
            flying = not landing(trigger_pin)

    except KeyboardInterrupt:
        print('flight_controller_2.py was terminated by the user.')

    finally:
        #Shut all the sensors down.
        for sensor in sensors:
            sensor.stop()

        GPIO.cleanup()
        print('Payload was recovered safely at', asctime())

main()
<|endoftext|>


<commit_before>#!/usr/bin/python -OO
#
# This file is part of Archivematica.
#
# Copyright 2010-2012 Artefactual Systems Inc. <http://artefactual.com>
#
# Archivematica is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Archivematica is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Archivematica.    If not, see <http://www.gnu.org/licenses/>.

# @package Archivematica
# @subpackage archivematicaClientScript
# @author Joseph Perry <joseph@artefactual.com>
# @version svn: $Id$

#/src/dashboard/src/main/models.py

import os
import sys
import csv
import traceback
sys.path.append("/usr/lib/archivematica/archivematicaCommon")
from sharedVariablesAcrossModules import sharedVariablesAcrossModules

simpleMetadataCSVkey = []
simpleMetadataCSV = {}
compoundMetadataCSVkey = []
compoundMetadataCSV = {}

CSVMetadata=(simpleMetadataCSVkey, simpleMetadataCSV, compoundMetadataCSVkey, compoundMetadataCSV)

def parseMetadata(SIPPath):
    ret = ({},{})
    transfersPath = os.path.join(SIPPath, "objects", "metadata", "transfers")
    for transfer in os.listdir(transfersPath):
        metadataCSVFilePath = os.path.join(transfersPath, transfer, "metadata", "metadata.csv")
        if os.path.isfile(metadataCSVFilePath):
            try:
                parseMetadtaCSV(metadataCSVFilePath)
            except Exception as inst:
                print >>sys.stderr, type(inst)     # the exception instance
                print >>sys.stderr, inst.args
                print >>sys.stderr, "error parsing: ", metadataCSVFilePath
                traceback.print_exc(file=sys.stdout)
                sharedVariablesAcrossModules.globalErrorCount +=1

def parseMetadtaCSV(metadataCSVFilePath):
    with open(metadataCSVFilePath, 'rb') as f:
        reader = csv.reader(f)
        firstRow = True
        type = ""
        for row in reader:
            if firstRow: #header row
                type = row[0].lower()
                if type == "filename":
                    CSVMetadata[0].extend(row)
                elif type == "parts":
                    CSVMetadata[2].extend(row)
                else:
                    print >>sys.stderr, "error parsing: ", metadataCSVFilePath
                    print >>sys.stderr, "unsupported: ", type
                    sharedVariablesAcrossModules.globalErrorCount +=1
                    return
                firstRow = False

            else: #data row
                if type == "filename":
                    simpleMetadataCSV[row[0]] = row
                elif type == "parts":
                    directory = row[0]
                    if directory.endswith("/"):
                        directory = directory[:-1]
                    compoundMetadataCSV[directory] = row

    <commit_msg>Correcting metadata.csv path mapping.<commit_after>#!/usr/bin/python -OO
#
# This file is part of Archivematica.
#
# Copyright 2010-2012 Artefactual Systems Inc. <http://artefactual.com>
#
# Archivematica is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Archivematica is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Archivematica.    If not, see <http://www.gnu.org/licenses/>.

# @package Archivematica
# @subpackage archivematicaClientScript
# @author Joseph Perry <joseph@artefactual.com>
# @version svn: $Id$

#/src/dashboard/src/main/models.py

import os
import sys
import csv
import traceback
sys.path.append("/usr/lib/archivematica/archivematicaCommon")
from sharedVariablesAcrossModules import sharedVariablesAcrossModules

simpleMetadataCSVkey = []
simpleMetadataCSV = {}
compoundMetadataCSVkey = []
compoundMetadataCSV = {}

CSVMetadata=(simpleMetadataCSVkey, simpleMetadataCSV, compoundMetadataCSVkey, compoundMetadataCSV)

def parseMetadata(SIPPath):
    ret = ({},{})
    transfersPath = os.path.join(SIPPath, "objects", "metadata", "transfers")
    for transfer in os.listdir(transfersPath):
        metadataCSVFilePath = os.path.join(transfersPath, transfer, "metadata.csv")
        if os.path.isfile(metadataCSVFilePath):
            try:
                parseMetadtaCSV(metadataCSVFilePath)
            except Exception as inst:
                print >>sys.stderr, type(inst)     # the exception instance
                print >>sys.stderr, inst.args
                print >>sys.stderr, "error parsing: ", metadataCSVFilePath
                traceback.print_exc(file=sys.stdout)
                sharedVariablesAcrossModules.globalErrorCount +=1

def parseMetadtaCSV(metadataCSVFilePath):
    with open(metadataCSVFilePath, 'rb') as f:
        reader = csv.reader(f)
        firstRow = True
        type = ""
        for row in reader:
            if firstRow: #header row
                type = row[0].lower()
                if type == "filename":
                    CSVMetadata[0].extend(row)
                elif type == "parts":
                    CSVMetadata[2].extend(row)
                else:
                    print >>sys.stderr, "error parsing: ", metadataCSVFilePath
                    print >>sys.stderr, "unsupported: ", type
                    sharedVariablesAcrossModules.globalErrorCount +=1
                    return
                firstRow = False

            else: #data row
                if type == "filename":
                    simpleMetadataCSV[row[0]] = row
                elif type == "parts":
                    directory = row[0]
                    if directory.endswith("/"):
                        directory = directory[:-1]
                    compoundMetadataCSV[directory] = row

    <|endoftext|>