<commit_before>#!/usr/bin/python3

import os, json, datetime, sys, math, numbers
from os.path import join as pjoin

SKIP_META_LINES = 6
# FLO2D cells to longitude, latitude mapping file location
CADPTS_DAT_FILE = 'META_FLO2D/CADPTS.DAT'
CWD = os.getcwd()
CADPTS_DAT_FILE_PATH = pjoin(CWD, CADPTS_DAT_FILE)

def getWaterLevelGrid(lines) :
    waterLevels = []
    for line in lines[6:] :
        if line == '\n' :
            break
        v = line.split()
        # Get flood level (Elevation)
        # waterLevels.append('%s %s' % (v[0], v[1]))
        # Get flood depth (Depth)
        waterLevels.append('%s %s' % (v[0], v[2]))

    return waterLevels

def getGridBoudary(gap=250.0) :
    "longitude  -> x : larger value"
    "latitude   -> y : smaller value"

    long_min = 1000000000.0
    lat_min = 1000000000.0
    long_max = 0.0
    lat_max = 0.0

    with open(CADPTS_DAT_FILE_PATH) as f:
        lines = f.readlines()
        for line in lines :
            values = line.split()
            long_min = min(long_min, float(values[1]))
            lat_min = min(lat_min, float(values[2]))

            long_max = max(long_max, float(values[1]))
            lat_max = max(lat_max, float(values[2]))

    return {
        'long_min': long_min,
        'lat_min': lat_min,
        'long_max': long_max,
        'lat_max': lat_max
    }

def getCellGrid(boudary, gap=250.0) :
    CellMap = {}

    cols = int(math.ceil((boudary['long_max'] - boudary['long_min']) / gap)) + 1
    rows = int(math.ceil((boudary['lat_max'] - boudary['lat_min']) / gap)) + 1

    with open(CADPTS_DAT_FILE_PATH) as f:
        lines = f.readlines()
        for line in lines :
            v = line.split()
            i = int((float(v[1]) - boudary['long_min']) / gap)
            j = int((float(v[2]) - boudary['lat_min']) / gap)
            if not isinstance(i, numbers.Integral) or not isinstance(j, numbers.Integral) :
                print('### WARNING i: %d, j: %d, cols: %d, rows: %d' % (i, j, cols, rows))
            if (i >= cols or j >= rows) :
                print('### WARNING i: %d, j: %d, cols: %d, rows: %d' % (i, j, cols, rows))
            if i >= 0 or j >= 0 :
                CellMap[int(v[0])] = (i, rows - j -1)

    return CellMap

def getEsriGrid(waterLevels, boudary, CellMap, gap=250.0, missingVal=-9) :
    "Esri GRID format : https://en.wikipedia.org/wiki/Esri_grid"
    "ncols         4"
    "nrows         6"
    "xllcorner     0.0"
    "yllcorner     0.0"
    "cellsize      50.0"
    "NODATA_value  -9999"
    "-9999 -9999 5 2"
    "-9999 20 100 36"
    "3 8 35 10"
    "32 42 50 6"
    "88 75 27 9"
    "13 5 1 -9999"

    EsriGrid = []

    cols = int(math.ceil((boudary['long_max'] - boudary['long_min']) / gap)) + 1
    rows = int(math.ceil((boudary['lat_max'] - boudary['lat_min']) / gap)) + 1
    #print('>>>>>  cols: %d, rows: %d' % (cols, rows))

    Grid = [[missingVal for x in range(cols)] for y in range(rows)]

    for level in waterLevels :
        v = level.split()
        i, j = CellMap[int(v[0])]
        if (i >= cols or j >= rows) :
            print('i: %d, j: %d, cols: %d, rows: %d' % (i, j, cols, rows))
            print(boudary)
        Grid[j][i] = float(v[1])

    EsriGrid.append('%s\t%s\n' % ('ncols', cols))
    EsriGrid.append('%s\t%s\n' % ('nrows', rows))
    EsriGrid.append('%s\t%s\n' % ('xllcorner', boudary['long_min'] - 125))
    EsriGrid.append('%s\t%s\n' % ('yllcorner', boudary['lat_min'] - 125))
    EsriGrid.append('%s\t%s\n' % ('cellsize', gap))
    EsriGrid.append('%s\t%s\n' % ('NODATA_value', missingVal))

    for j in range(0, rows) :
        arr = []
        for i in range(0, cols) :
            arr.append(Grid[j][i])

        EsriGrid.append('%s\n' % (' '.join(str(x) for x in arr)))

    return EsriGrid

def getWaterLevelOfChannels(lines, channels=[]) :
    ''' Get Water Levels of given set of channels
    '''
    waterLevels = {}
    for line in lines[6:] :
        if line == '\n' :
            break
        v = line.split()
        if int(v[0]) in channels :
            # Get flood level (Elevation)
            # waterLevels.append((v[0], v[1]))
            # Get flood depth (Depth)
            waterLevels[int(v[0])] = v[2]

    return waterLevels

<commit_msg>Extracted FLO2D WaterLevel Elevation on flood plain<commit_after>#!/usr/bin/python3

import os, json, datetime, sys, math, numbers
from os.path import join as pjoin

SKIP_META_LINES = 6
# FLO2D cells to longitude, latitude mapping file location
CADPTS_DAT_FILE = 'META_FLO2D/CADPTS.DAT'
CWD = os.getcwd()
CADPTS_DAT_FILE_PATH = pjoin(CWD, CADPTS_DAT_FILE)

def getWaterLevelGrid(lines) :
    waterLevels = []
    for line in lines[6:] :
        if line == '\n' :
            break
        v = line.split()
        # Get flood level (Elevation)
        # waterLevels.append('%s %s' % (v[0], v[1]))
        # Get flood depth (Depth)
        waterLevels.append('%s %s' % (v[0], v[2]))

    return waterLevels

def getGridBoudary(gap=250.0) :
    "longitude  -> x : larger value"
    "latitude   -> y : smaller value"

    long_min = 1000000000.0
    lat_min = 1000000000.0
    long_max = 0.0
    lat_max = 0.0

    with open(CADPTS_DAT_FILE_PATH) as f:
        lines = f.readlines()
        for line in lines :
            values = line.split()
            long_min = min(long_min, float(values[1]))
            lat_min = min(lat_min, float(values[2]))

            long_max = max(long_max, float(values[1]))
            lat_max = max(lat_max, float(values[2]))

    return {
        'long_min': long_min,
        'lat_min': lat_min,
        'long_max': long_max,
        'lat_max': lat_max
    }

def getCellGrid(boudary, gap=250.0) :
    CellMap = {}

    cols = int(math.ceil((boudary['long_max'] - boudary['long_min']) / gap)) + 1
    rows = int(math.ceil((boudary['lat_max'] - boudary['lat_min']) / gap)) + 1

    with open(CADPTS_DAT_FILE_PATH) as f:
        lines = f.readlines()
        for line in lines :
            v = line.split()
            i = int((float(v[1]) - boudary['long_min']) / gap)
            j = int((float(v[2]) - boudary['lat_min']) / gap)
            if not isinstance(i, numbers.Integral) or not isinstance(j, numbers.Integral) :
                print('### WARNING i: %d, j: %d, cols: %d, rows: %d' % (i, j, cols, rows))
            if (i >= cols or j >= rows) :
                print('### WARNING i: %d, j: %d, cols: %d, rows: %d' % (i, j, cols, rows))
            if i >= 0 or j >= 0 :
                CellMap[int(v[0])] = (i, rows - j -1)

    return CellMap

def getEsriGrid(waterLevels, boudary, CellMap, gap=250.0, missingVal=-9) :
    "Esri GRID format : https://en.wikipedia.org/wiki/Esri_grid"
    "ncols         4"
    "nrows         6"
    "xllcorner     0.0"
    "yllcorner     0.0"
    "cellsize      50.0"
    "NODATA_value  -9999"
    "-9999 -9999 5 2"
    "-9999 20 100 36"
    "3 8 35 10"
    "32 42 50 6"
    "88 75 27 9"
    "13 5 1 -9999"

    EsriGrid = []

    cols = int(math.ceil((boudary['long_max'] - boudary['long_min']) / gap)) + 1
    rows = int(math.ceil((boudary['lat_max'] - boudary['lat_min']) / gap)) + 1
    #print('>>>>>  cols: %d, rows: %d' % (cols, rows))

    Grid = [[missingVal for x in range(cols)] for y in range(rows)]

    for level in waterLevels :
        v = level.split()
        i, j = CellMap[int(v[0])]
        if (i >= cols or j >= rows) :
            print('i: %d, j: %d, cols: %d, rows: %d' % (i, j, cols, rows))
            print(boudary)
        Grid[j][i] = float(v[1])

    EsriGrid.append('%s\t%s\n' % ('ncols', cols))
    EsriGrid.append('%s\t%s\n' % ('nrows', rows))
    EsriGrid.append('%s\t%s\n' % ('xllcorner', boudary['long_min'] - 125))
    EsriGrid.append('%s\t%s\n' % ('yllcorner', boudary['lat_min'] - 125))
    EsriGrid.append('%s\t%s\n' % ('cellsize', gap))
    EsriGrid.append('%s\t%s\n' % ('NODATA_value', missingVal))

    for j in range(0, rows) :
        arr = []
        for i in range(0, cols) :
            arr.append(Grid[j][i])

        EsriGrid.append('%s\n' % (' '.join(str(x) for x in arr)))

    return EsriGrid

def getWaterLevelOfChannels(lines, channels=[]) :
    ''' Get Water Levels of given set of channels
    '''
    waterLevels = {}
    for line in lines[6:] :
        if line == '\n' :
            break
        v = line.split()
        if int(v[0]) in channels :
            # Get flood level (Elevation)
            waterLevels[int(v[0])] = v[1]
            # Get flood depth (Depth)
            # waterLevels[int(v[0])] = v[2]

    return waterLevels

<|endoftext|>


<commit_before>"""
Given an absolute path for a file (Unix-style), simplify it.

For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"
click to show corner cases.

Corner Cases:
Did you consider the case where path = "/../"?
In this case, you should return "/".
Another corner case is the path might contain multiple slashes '/' together, such as "/home//foo/".
In this case, you should ignore redundant slashes and return "/home/foo".
"""

class Solution:
    # @param path, a string
    # @return a string
    def simplifyPath(self, path):
        path_list = path.strip('/').split('/')
        ret = []
        jump = 0
        for p in path_list[::-1]:
            if p == '.' or p == '':
                continue
            elif p == '..':
                jump += 1
            else:                       # p is a valid path
                if jump > 0:
                    jump -= 1
                    continue
                else:
                    ret.insert(0, p)
        return '/'+'/'.join(ret)
    # Note:
    # 1. Remove dup '/', if using split(), // will become '', remove it
    # 2. Keep in mind those two [::-1]
    # 3. Don't forget to attach the first '/'
<commit_msg>Refactored Simplify Path<commit_after>"""
Given an absolute path for a file (Unix-style), simplify it.

For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"
click to show corner cases.

Corner Cases:
Did you consider the case where path = "/../"?
In this case, you should return "/".
Another corner case is the path might contain multiple slashes '/' together, such as "/home//foo/".
In this case, you should ignore redundant slashes and return "/home/foo".
"""

class Solution:
    # @param path, a string
    # @return a string
    def simplifyPath(self, path):
        path_list = path.strip('/').split('/')
        ret = []
        jump = 0
        for p in path_list[::-1]:
            if p == '.' or p == '':
                continue
            elif p == '..':
                jump += 1
            else:                       # p is a valid path
                if jump > 0:
                    jump -= 1
                else:
                    ret.insert(0, p)
        return '/'+'/'.join(ret)
    # Note:
    # 1. Remove dup '/', if using split(), // will become '', remove it
    # 2. Keep in mind those two [::-1]
    # 3. Don't forget to attach the first '/'
<|endoftext|>


<commit_before>from __future__ import print_function, division, absolute_import
from fontTools.misc.py23 import *
import logging

# add a do-nothing handler to the libary's top-level logger, to avoid
# "no handlers could be found" error if client doesn't configure logging
log = logging.getLogger(__name__)
log.addHandler(logging.NullHandler())

version = "3.0"
<commit_msg>[fontTools.__init__] export 'configLogger' in the top-level module to make it easier for clients to import<commit_after>from __future__ import print_function, division, absolute_import
from fontTools.misc.py23 import *
import logging

# add a do-nothing handler to the libary's top-level logger, to avoid
# "no handlers could be found" error if client doesn't configure logging
log = logging.getLogger(__name__)
log.addHandler(logging.NullHandler())

# clients may call this to configure logging with a predefined handler and format
from fontTools.misc.loggingTools import configLogger

version = "3.0"

__all__ = ["version", "log", "configLogger"]
<|endoftext|>


<commit_before>'''
ARCHES - a program developed to inventory and manage immovable cultural heritage.
Copyright (C) 2013 J. Paul Getty Trust and World Monuments Fund

This program 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.

This program 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 Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''

import re
from django.template import RequestContext
from django.shortcuts import render_to_response, redirect
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.cache import never_cache
from django.contrib.auth import authenticate, login, logout
from django.core.urlresolvers import reverse
from django.conf import settings
from django.db import transaction
from django.db import connection
from arches.app.models import models
from arches.app.models.resource import Resource
from arches.app.models.concept import Concept
from django.utils.translation import ugettext as _
from arches.app.utils.betterJSONSerializer import JSONSerializer, JSONDeserializer
from arches.app.utils.JSONResponse import JSONResponse
from arches.app.models.entity import Entity
from arches.app.search.search_engine_factory import SearchEngineFactory
from arches.app.search.elasticsearch_dsl_builder import Query, Terms, Bool, Match

@csrf_exempt
def resource_manager(request, resourcetypeid='', form_id='', resourceid=''):

    if resourceid != '':
        resource = Resource(resourceid)
    elif resourcetypeid != '':
        resource = Resource({'entitytypeid': resourcetypeid})

    form = resource.get_form(form_id)

    if request.method == 'POST':
        # get the values from the form and pass to the resource
        data = JSONDeserializer().deserialize(request.POST.get('formdata', {}))
        form.update(data)

        with transaction.atomic():
            if resourceid != '':
                resource.delete_index()
            resource.save(user=request.user)
            resource.index()
            resourceid = resource.entityid

            return redirect('resource_manager', resourcetypeid=resourcetypeid, form_id=form_id, resourceid=resourceid)

    return render_to_response('resource-manager.htm', {
            'form': form,
            'formdata': JSONSerializer().serialize(form.data),
            'form_template': 'views/forms/' + form_id + '.htm',
            'form_id': form_id,
            'resourcetypeid': resourcetypeid,
            'resourceid': resourceid,
            'main_script': 'resource-manager',
            'active_page': 'ResourceManger',
            'resource': resource,
            'resource_name': resource.get_primary_name(),
            'resource_type_name': resource.get_type_name(),
            'form_groups': resource.form_groups
        },
        context_instance=RequestContext(request))

def related_resoures(request, resourceid):
    return JSONResponse(get_related_resources(resourceid), indent=4)

def get_related_resources(resourceid):
    ret = {
        'resource_relationships': [],
        'related_resources': []
    }
    terms = []
    se = SearchEngineFactory().create()

    query = Query(se)
    terms.append(Terms(field='entityid1', terms=resourceid).dsl)
    terms.append(Terms(field='entityid2', terms=resourceid).dsl)
    query.add_filter(terms, operator='or')
    resource_relations = query.search(index='resource_relations', doc_type='all')

    entityids = set()
    for relation in resource_relations['hits']['hits']:
        ret['resource_relationships'].append(relation['_source'])
        entityids.add(relation['_source']['entityid1'])
        entityids.add(relation['_source']['entityid2'])
    if len(entityids) > 0:
        entityids.remove(resourceid)

    related_resources = se.search(index='entity', doc_type='_all', id=list(entityids))
    if related_resources:
        for resource in related_resources['docs']:
            ret['related_resources'].append(resource['_source'])

    return JSONResponse(ret)

def report(request, resourceid):
    lang = request.GET.get('lang', settings.LANGUAGE_CODE)
    se = SearchEngineFactory().create()
    report_info = se.search(index='resource', id=resourceid)
    report_info['source'] = report_info['_source']
    report_info['type'] = report_info['_type']
    report_info['source']['graph'] = report_info['source']['graph'][0]
    del report_info['_source']
    del report_info['_type']

    related_resource_info = get_related_resources(resourceid)

    #return JSONResponse(report_info, indent=4)

    def get_evaluation_path(valueid):
        value = models.Values.objects.get(pk=valueid)
        concept_graph = Concept().get(id=value.conceptid_id, include_subconcepts=False,
            include_parentconcepts=True, include_relatedconcepts=False, up_depth_limit=None, lang=lang)

        paths = []
        for path in concept_graph.get_paths(lang=lang)[0]:
            if path['label'] != 'Arches' and path['label'] != 'Evaluation Criteria Type':
                paths.append(path['label'])
        return '; '.join(paths)

    concept_label_ids = set()
    uuid_regex = re.compile('[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}')
    # gather together all uuid's referenced in the resource graph
    def crawl(items):
        for item in items:
            for key in item:
                if isinstance(item[key], list):
                    crawl(item[key])
                else:
                    if uuid_regex.match(item[key]):
                        if key == 'EVALUATION_CRITERIA_TYPE_E55__value':
                            item[key] = get_evaluation_path(item[key])
                        concept_label_ids.add(item[key])

    crawl([report_info['source']['graph']])

    # get all the concept labels from the uuid's
    concept_labels = se.search(index='concept_labels', id=list(concept_label_ids))

    #print concept_labels
    temp = {}

    # convert all labels to their localized prefLabel
    for concept_label in concept_labels['docs']:
        #temp[concept_label['_id']] = concept_label
        if concept_label['found']:
            # the resource graph already referenced the preferred label in the desired language
            if concept_label['_source']['type'] == 'prefLabel' and concept_label['_source']['language'] == lang:
                temp[concept_label['_id']] = concept_label['_source']
            else:
                # the resource graph referenced a non-preferred label or a label not in our target language, so we need to get the right label
                query = Query(se)
                terms = Terms(field='conceptid', terms=[concept_label['_source']['conceptid']])
                match = Match(field='type', query='preflabel', type='phrase')
                query.add_filter(terms)
                query.add_query(match)
                preflabels = query.search(index='concept_labels')['hits']['hits']
                for preflabel in preflabels:
                    # get the label in the preferred language, otherwise get the label in the default language
                    if preflabel['_source']['language'] == lang:
                        temp[concept_label['_id']] = preflabel['_source']
                        break
                    if preflabel['_source']['language'] == settings.LANGUAGE_CODE:
                        temp[concept_label['_id']] = preflabel['_source']

    # replace the uuid's in the resource graph with their preferred and localized label
    def crawl_again(items):
        for item in items:
            for key in item:
                if isinstance(item[key], list):
                    crawl_again(item[key])
                else:
                    if uuid_regex.match(item[key]):
                        try:
                            item[key] = temp[item[key]]['value']
                        except:
                            pass

    crawl_again([report_info['source']['graph']])

    #return JSONResponse(report_info, indent=4)

    return render_to_response('resource-report.htm', {
            'resourceid': resourceid,
            'report_template': 'views/reports/' + report_info['type'] + '.htm',
            'report_info': report_info,
            'main_script': 'resource-report',
            'active_page': 'ResourceReport'
        },
        context_instance=RequestContext(request))

def map_layers(request, entitytypeid='all', get_centroids=False):
    data = []
    bbox = request.GET.get('bbox', '')
    limit = request.GET.get('limit', settings.MAP_LAYER_FEATURE_LIMIT)
    entityids = request.GET.get('entityid', '')
    geojson_collection = {
      "type": "FeatureCollection",
      "features": []
    }

    se = SearchEngineFactory().create()
    query = Query(se, limit=limit)

    args = { 'index': 'maplayers' }
    if entitytypeid != 'all':
        args['doc_type'] = entitytypeid
    if entityids != '':
        for entityid in entityids.split(','):
            geojson_collection['features'].append(se.search(index='maplayers', id=entityid)['_source'])
        return JSONResponse(geojson_collection)

    data = query.search(**args)

    for item in data['hits']['hits']:
        if get_centroids:
            item['_source']['properties']['geometry_collection'] = item['_source']['geometry']
            item['_source']['geometry'] = item['_source']['properties']['centroid']
        else:
            item['_source']['properties'].pop('extent', None)
        item['_source']['properties'].pop('centroid', None)
        geojson_collection['features'].append(item['_source'])

    return JSONResponse(geojson_collection)

def edit_history(request, resourceid=''):
    ret = []
    current = None
    index = -1
    start = request.GET.get('start', 0)
    limit = request.GET.get('limit', 10)
    if resourceid != '':
        dates = models.EditLog.objects.filter(resourceid = resourceid).values_list('timestamp', flat=True).order_by('-timestamp').distinct('timestamp')[start:limit]
        # dates = models.EditLog.objects.datetimes('timestamp', 'second', order='DESC')
        for date in dates:
            #ret[str(date)] = models.EditLog.objects.filter(resourceid = self.resource.entityid, timestamp = date)
            print str(date)

        for log in models.EditLog.objects.filter(resourceid = resourceid, timestamp__in = dates).values().order_by('-timestamp', 'attributeentitytypeid'):
            if str(log['timestamp']) != current:
                current = str(log['timestamp'])
                ret.append({'date':str(log['timestamp'].date()), 'time': str(log['timestamp'].time().replace(microsecond=0).isoformat()), 'log': []})
                index = index + 1

            ret[index]['log'].append(log)

    return JSONResponse(ret, indent=4)

class ResourceForm(object):
    def __init__(self, resource):
        # here is where we can create the basic format for the form data
        info = self.get_info()
        self.id = info['id']
        self.name = info['name']
        self.icon = info['icon']
        self.resource = resource
        if self.resource == None:
            self.schema = None
        else:
            self.schema = Entity.get_mapping_schema(self.resource.entitytypeid)

        self.data = {
            "domains": {},
            "defaults": {}
        }
        self.load()

    @staticmethod
    def get_info():
        return {
            'id': '',
            'icon': '',
            'name': '',
            'class': ResourceForm
        }

	def update(self, data):
		# update resource w/ post data
		return

    def load(self):
        # retrieves the data from the server
        return

    def get_nodes(self, entitytypeid):
        ret = []
        entities = self.resource.find_entities_by_type_id(entitytypeid)
        for entity in entities:
            data = {}

            for entity in entity.flatten():
                #data[entity.entitytypeid] = self.encode_entity(entity)
                data = dict(data.items() + self.encode_entity(entity).items())
            ret.append(data)
        return ret

    def update_nodes(self, entitytypeid, data):
        for entity in self.resource.find_entities_by_type_id(entitytypeid):
            self.resource.child_entities.remove(entity)

        if self.schema == None:
            self.schema = Entity.get_mapping_schema(self.resource.entitytypeid)
        for value in data[entitytypeid.replace('.', '_')]:
            baseentity = None
            for newentity in self.decode_data_item(value):
                entity = Entity()
                entity.create_from_mapping(self.resource.entitytypeid, self.schema[newentity['entitytypeid']]['steps'], newentity['entitytypeid'], newentity['value'], newentity['entityid'])

                if baseentity == None:
                    baseentity = entity
                else:
                    baseentity.merge(entity)

            self.resource.merge_at(baseentity, self.resource.entitytypeid)

    def update_node(self, entitytypeid, data):
        if self.schema == None:
            self.schema = Entity.get_mapping_schema(self.resource.entitytypeid)
        nodes = self.resource.find_entities_by_type_id(entitytypeid)
        node_data = self.decode_data_item(data[entitytypeid.replace('.', '_')])[0]

        if len(nodes) == 0:
            entity = Entity()
            entity.create_from_mapping(self.resource.entitytypeid, self.schema[entitytypeid]['steps'], entitytypeid, node_data['value'], node_data['entityid'])
            self.resource.merge_at(entity, self.resource.entitytypeid)
        else:
            nodes[0].value = node_data['value']

    def encode_entity(self, entity):
        def enc(entity, attr):
            return '%s__%s' % (entity.entitytypeid.replace('.', '_'), attr)

        ret = {}
        for key, value in entity.__dict__.items():
            if not key.startswith("__"):
                ret[enc(entity, key)] = value
        return ret

    def decode_data_item(self, entity):
        def dec(item):
            # item = "NAME_E41__entitytypeid"
            val = item.split('__')
            if len(val) != 2:
                return False
            entitytypeid = val[0]
            propertyname = val[1]

            v = entitytypeid.split('_')
            entitytypeid = '%s.%s' % ('_'.join(v[:-1]), v[-1])
            return (entitytypeid, propertyname)

        ret = {}
        for key, value in entity.iteritems():
            r = dec(key)
            if r:
                if r[0] not in ret:
                    ret[r[0]] = {}
                ret[r[0]][r[1]] = value

        ret2 = []
        for key, value in ret.iteritems():
            value['entitytypeid'] = key
            ret2.append(value)
        return ret2
<commit_msg>fixing regression in maplayers service optimization<commit_after>'''
ARCHES - a program developed to inventory and manage immovable cultural heritage.
Copyright (C) 2013 J. Paul Getty Trust and World Monuments Fund

This program 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.

This program 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 Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''

import re
from django.template import RequestContext
from django.shortcuts import render_to_response, redirect
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.cache import never_cache
from django.contrib.auth import authenticate, login, logout
from django.core.urlresolvers import reverse
from django.conf import settings
from django.db import transaction
from django.db import connection
from arches.app.models import models
from arches.app.models.resource import Resource
from arches.app.models.concept import Concept
from django.utils.translation import ugettext as _
from arches.app.utils.betterJSONSerializer import JSONSerializer, JSONDeserializer
from arches.app.utils.JSONResponse import JSONResponse
from arches.app.models.entity import Entity
from arches.app.search.search_engine_factory import SearchEngineFactory
from arches.app.search.elasticsearch_dsl_builder import Query, Terms, Bool, Match

@csrf_exempt
def resource_manager(request, resourcetypeid='', form_id='', resourceid=''):

    if resourceid != '':
        resource = Resource(resourceid)
    elif resourcetypeid != '':
        resource = Resource({'entitytypeid': resourcetypeid})

    form = resource.get_form(form_id)

    if request.method == 'POST':
        # get the values from the form and pass to the resource
        data = JSONDeserializer().deserialize(request.POST.get('formdata', {}))
        form.update(data)

        with transaction.atomic():
            if resourceid != '':
                resource.delete_index()
            resource.save(user=request.user)
            resource.index()
            resourceid = resource.entityid

            return redirect('resource_manager', resourcetypeid=resourcetypeid, form_id=form_id, resourceid=resourceid)

    return render_to_response('resource-manager.htm', {
            'form': form,
            'formdata': JSONSerializer().serialize(form.data),
            'form_template': 'views/forms/' + form_id + '.htm',
            'form_id': form_id,
            'resourcetypeid': resourcetypeid,
            'resourceid': resourceid,
            'main_script': 'resource-manager',
            'active_page': 'ResourceManger',
            'resource': resource,
            'resource_name': resource.get_primary_name(),
            'resource_type_name': resource.get_type_name(),
            'form_groups': resource.form_groups
        },
        context_instance=RequestContext(request))

def related_resoures(request, resourceid):
    return JSONResponse(get_related_resources(resourceid), indent=4)

def get_related_resources(resourceid):
    ret = {
        'resource_relationships': [],
        'related_resources': []
    }
    terms = []
    se = SearchEngineFactory().create()

    query = Query(se)
    terms.append(Terms(field='entityid1', terms=resourceid).dsl)
    terms.append(Terms(field='entityid2', terms=resourceid).dsl)
    query.add_filter(terms, operator='or')
    resource_relations = query.search(index='resource_relations', doc_type='all')

    entityids = set()
    for relation in resource_relations['hits']['hits']:
        ret['resource_relationships'].append(relation['_source'])
        entityids.add(relation['_source']['entityid1'])
        entityids.add(relation['_source']['entityid2'])
    if len(entityids) > 0:
        entityids.remove(resourceid)

    related_resources = se.search(index='entity', doc_type='_all', id=list(entityids))
    if related_resources:
        for resource in related_resources['docs']:
            ret['related_resources'].append(resource['_source'])

    return JSONResponse(ret)

def report(request, resourceid):
    lang = request.GET.get('lang', settings.LANGUAGE_CODE)
    se = SearchEngineFactory().create()
    report_info = se.search(index='resource', id=resourceid)
    report_info['source'] = report_info['_source']
    report_info['type'] = report_info['_type']
    report_info['source']['graph'] = report_info['source']['graph'][0]
    del report_info['_source']
    del report_info['_type']

    related_resource_info = get_related_resources(resourceid)

    #return JSONResponse(report_info, indent=4)

    def get_evaluation_path(valueid):
        value = models.Values.objects.get(pk=valueid)
        concept_graph = Concept().get(id=value.conceptid_id, include_subconcepts=False,
            include_parentconcepts=True, include_relatedconcepts=False, up_depth_limit=None, lang=lang)

        paths = []
        for path in concept_graph.get_paths(lang=lang)[0]:
            if path['label'] != 'Arches' and path['label'] != 'Evaluation Criteria Type':
                paths.append(path['label'])
        return '; '.join(paths)

    concept_label_ids = set()
    uuid_regex = re.compile('[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}')
    # gather together all uuid's referenced in the resource graph
    def crawl(items):
        for item in items:
            for key in item:
                if isinstance(item[key], list):
                    crawl(item[key])
                else:
                    if uuid_regex.match(item[key]):
                        if key == 'EVALUATION_CRITERIA_TYPE_E55__value':
                            item[key] = get_evaluation_path(item[key])
                        concept_label_ids.add(item[key])

    crawl([report_info['source']['graph']])

    # get all the concept labels from the uuid's
    concept_labels = se.search(index='concept_labels', id=list(concept_label_ids))

    #print concept_labels
    temp = {}

    # convert all labels to their localized prefLabel
    for concept_label in concept_labels['docs']:
        #temp[concept_label['_id']] = concept_label
        if concept_label['found']:
            # the resource graph already referenced the preferred label in the desired language
            if concept_label['_source']['type'] == 'prefLabel' and concept_label['_source']['language'] == lang:
                temp[concept_label['_id']] = concept_label['_source']
            else:
                # the resource graph referenced a non-preferred label or a label not in our target language, so we need to get the right label
                query = Query(se)
                terms = Terms(field='conceptid', terms=[concept_label['_source']['conceptid']])
                match = Match(field='type', query='preflabel', type='phrase')
                query.add_filter(terms)
                query.add_query(match)
                preflabels = query.search(index='concept_labels')['hits']['hits']
                for preflabel in preflabels:
                    # get the label in the preferred language, otherwise get the label in the default language
                    if preflabel['_source']['language'] == lang:
                        temp[concept_label['_id']] = preflabel['_source']
                        break
                    if preflabel['_source']['language'] == settings.LANGUAGE_CODE:
                        temp[concept_label['_id']] = preflabel['_source']

    # replace the uuid's in the resource graph with their preferred and localized label
    def crawl_again(items):
        for item in items:
            for key in item:
                if isinstance(item[key], list):
                    crawl_again(item[key])
                else:
                    if uuid_regex.match(item[key]):
                        try:
                            item[key] = temp[item[key]]['value']
                        except:
                            pass

    crawl_again([report_info['source']['graph']])

    #return JSONResponse(report_info, indent=4)

    return render_to_response('resource-report.htm', {
            'resourceid': resourceid,
            'report_template': 'views/reports/' + report_info['type'] + '.htm',
            'report_info': report_info,
            'main_script': 'resource-report',
            'active_page': 'ResourceReport'
        },
        context_instance=RequestContext(request))

def map_layers(request, entitytypeid='all', get_centroids=False):
    data = []
    bbox = request.GET.get('bbox', '')
    limit = request.GET.get('limit', settings.MAP_LAYER_FEATURE_LIMIT)
    entityids = request.GET.get('entityid', '')
    geojson_collection = {
      "type": "FeatureCollection",
      "features": []
    }

    se = SearchEngineFactory().create()
    query = Query(se, limit=limit)

    args = { 'index': 'maplayers' }
    if entitytypeid != 'all':
        args['doc_type'] = entitytypeid
    if entityids != '':
        for entityid in entityids.split(','):
            geojson_collection['features'].append(se.search(index='maplayers', id=entityid)['_source'])
        return JSONResponse(geojson_collection)

    data = query.search(**args)

    for item in data['hits']['hits']:
        if get_centroids:
            item['_source']['geometry'] = item['_source']['properties']['centroid']
            item['_source'].pop('properties', None)
        else:
            item['_source']['properties'].pop('extent', None)
            item['_source']['properties'].pop('centroid', None)
        geojson_collection['features'].append(item['_source'])

    return JSONResponse(geojson_collection)

def edit_history(request, resourceid=''):
    ret = []
    current = None
    index = -1
    start = request.GET.get('start', 0)
    limit = request.GET.get('limit', 10)
    if resourceid != '':
        dates = models.EditLog.objects.filter(resourceid = resourceid).values_list('timestamp', flat=True).order_by('-timestamp').distinct('timestamp')[start:limit]
        # dates = models.EditLog.objects.datetimes('timestamp', 'second', order='DESC')
        for date in dates:
            #ret[str(date)] = models.EditLog.objects.filter(resourceid = self.resource.entityid, timestamp = date)
            print str(date)

        for log in models.EditLog.objects.filter(resourceid = resourceid, timestamp__in = dates).values().order_by('-timestamp', 'attributeentitytypeid'):
            if str(log['timestamp']) != current:
                current = str(log['timestamp'])
                ret.append({'date':str(log['timestamp'].date()), 'time': str(log['timestamp'].time().replace(microsecond=0).isoformat()), 'log': []})
                index = index + 1

            ret[index]['log'].append(log)

    return JSONResponse(ret, indent=4)

class ResourceForm(object):
    def __init__(self, resource):
        # here is where we can create the basic format for the form data
        info = self.get_info()
        self.id = info['id']
        self.name = info['name']
        self.icon = info['icon']
        self.resource = resource
        if self.resource == None:
            self.schema = None
        else:
            self.schema = Entity.get_mapping_schema(self.resource.entitytypeid)

        self.data = {
            "domains": {},
            "defaults": {}
        }
        self.load()

    @staticmethod
    def get_info():
        return {
            'id': '',
            'icon': '',
            'name': '',
            'class': ResourceForm
        }

	def update(self, data):
		# update resource w/ post data
		return

    def load(self):
        # retrieves the data from the server
        return

    def get_nodes(self, entitytypeid):
        ret = []
        entities = self.resource.find_entities_by_type_id(entitytypeid)
        for entity in entities:
            data = {}

            for entity in entity.flatten():
                #data[entity.entitytypeid] = self.encode_entity(entity)
                data = dict(data.items() + self.encode_entity(entity).items())
            ret.append(data)
        return ret

    def update_nodes(self, entitytypeid, data):
        for entity in self.resource.find_entities_by_type_id(entitytypeid):
            self.resource.child_entities.remove(entity)

        if self.schema == None:
            self.schema = Entity.get_mapping_schema(self.resource.entitytypeid)
        for value in data[entitytypeid.replace('.', '_')]:
            baseentity = None
            for newentity in self.decode_data_item(value):
                entity = Entity()
                entity.create_from_mapping(self.resource.entitytypeid, self.schema[newentity['entitytypeid']]['steps'], newentity['entitytypeid'], newentity['value'], newentity['entityid'])

                if baseentity == None:
                    baseentity = entity
                else:
                    baseentity.merge(entity)

            self.resource.merge_at(baseentity, self.resource.entitytypeid)

    def update_node(self, entitytypeid, data):
        if self.schema == None:
            self.schema = Entity.get_mapping_schema(self.resource.entitytypeid)
        nodes = self.resource.find_entities_by_type_id(entitytypeid)
        node_data = self.decode_data_item(data[entitytypeid.replace('.', '_')])[0]

        if len(nodes) == 0:
            entity = Entity()
            entity.create_from_mapping(self.resource.entitytypeid, self.schema[entitytypeid]['steps'], entitytypeid, node_data['value'], node_data['entityid'])
            self.resource.merge_at(entity, self.resource.entitytypeid)
        else:
            nodes[0].value = node_data['value']

    def encode_entity(self, entity):
        def enc(entity, attr):
            return '%s__%s' % (entity.entitytypeid.replace('.', '_'), attr)

        ret = {}
        for key, value in entity.__dict__.items():
            if not key.startswith("__"):
                ret[enc(entity, key)] = value
        return ret

    def decode_data_item(self, entity):
        def dec(item):
            # item = "NAME_E41__entitytypeid"
            val = item.split('__')
            if len(val) != 2:
                return False
            entitytypeid = val[0]
            propertyname = val[1]

            v = entitytypeid.split('_')
            entitytypeid = '%s.%s' % ('_'.join(v[:-1]), v[-1])
            return (entitytypeid, propertyname)

        ret = {}
        for key, value in entity.iteritems():
            r = dec(key)
            if r:
                if r[0] not in ret:
                    ret[r[0]] = {}
                ret[r[0]][r[1]] = value

        ret2 = []
        for key, value in ret.iteritems():
            value['entitytypeid'] = key
            ret2.append(value)
        return ret2
<|endoftext|>


<commit_before>import unittest
from test import test_support

import os, resource

# This test is checking a few specific problem spots with the resource module.

class ResourceTest(unittest.TestCase):

    def test_args(self):
        self.assertRaises(TypeError, resource.getrlimit)
        self.assertRaises(TypeError, resource.getrlimit, 42, 42)
        self.assertRaises(TypeError, resource.setrlimit)
        self.assertRaises(TypeError, resource.setrlimit, 42, 42, 42)

    def test_fsize_ismax(self):

        try:
            (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
        except AttributeError:
            pass
        else:
            # RLIMIT_FSIZE should be RLIM_INFINITY, which will be a really big
            # number on a platform with large file support.  On these platforms,
            # we need to test that the get/setrlimit functions properly convert
            # the number to a C long long and that the conversion doesn't raise
            # an error.
            self.assertEqual(resource.RLIM_INFINITY, max)
            resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))

    def test_fsize_enforced(self):
        try:
            (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
        except AttributeError:
            pass
        else:
            # Check to see what happens when the RLIMIT_FSIZE is small.  Some
            # versions of Python were terminated by an uncaught SIGXFSZ, but
            # pythonrun.c has been fixed to ignore that exception.  If so, the
            # write() should return EFBIG when the limit is exceeded.

            # At least one platform has an unlimited RLIMIT_FSIZE and attempts
            # to change it raise ValueError instead.
            try:
                try:
                    resource.setrlimit(resource.RLIMIT_FSIZE, (1024, max))
                    limit_set = True
                except ValueError:
                    limit_set = False
                f = open(test_support.TESTFN, "wb")
                try:
                    f.write("X" * 1024)
                    try:
                        f.write("Y")
                        f.flush()
                    except IOError:
                        if not limit_set:
                            raise
                    if limit_set:
                        # Close will attempt to flush the byte we wrote
                        # Restore limit first to avoid getting a spurious error
                        resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
                finally:
                    f.close()
                    os.unlink(test_support.TESTFN)
            finally:
                if limit_set:
                    resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))

    def test_fsize_toobig(self):
        # Be sure that setrlimit is checking for really large values
        too_big = 10**50
        try:
            (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
        except AttributeError:
            pass
        else:
            try:
                resource.setrlimit(resource.RLIMIT_FSIZE, (too_big, max))
            except (OverflowError, ValueError):
                pass
            try:
                resource.setrlimit(resource.RLIMIT_FSIZE, (max, too_big))
            except (OverflowError, ValueError):
                pass

    def test_getrusage(self):
        self.assertRaises(TypeError, resource.getrusage)
        self.assertRaises(TypeError, resource.getrusage, 42, 42)
        usageself = resource.getrusage(resource.RUSAGE_SELF)
        usagechildren = resource.getrusage(resource.RUSAGE_CHILDREN)
        # May not be available on all systems.
        try:
            usageboth = resource.getrusage(resource.RUSAGE_BOTH)
        except (ValueError, AttributeError):
            pass

def test_main(verbose=None):
    test_support.run_unittest(ResourceTest)

if __name__ == "__main__":
    test_main()
<commit_msg>str/bytes strictness.<commit_after>import unittest
from test import test_support

import os, resource

# This test is checking a few specific problem spots with the resource module.

class ResourceTest(unittest.TestCase):

    def test_args(self):
        self.assertRaises(TypeError, resource.getrlimit)
        self.assertRaises(TypeError, resource.getrlimit, 42, 42)
        self.assertRaises(TypeError, resource.setrlimit)
        self.assertRaises(TypeError, resource.setrlimit, 42, 42, 42)

    def test_fsize_ismax(self):

        try:
            (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
        except AttributeError:
            pass
        else:
            # RLIMIT_FSIZE should be RLIM_INFINITY, which will be a really big
            # number on a platform with large file support.  On these platforms,
            # we need to test that the get/setrlimit functions properly convert
            # the number to a C long long and that the conversion doesn't raise
            # an error.
            self.assertEqual(resource.RLIM_INFINITY, max)
            resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))

    def test_fsize_enforced(self):
        try:
            (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
        except AttributeError:
            pass
        else:
            # Check to see what happens when the RLIMIT_FSIZE is small.  Some
            # versions of Python were terminated by an uncaught SIGXFSZ, but
            # pythonrun.c has been fixed to ignore that exception.  If so, the
            # write() should return EFBIG when the limit is exceeded.

            # At least one platform has an unlimited RLIMIT_FSIZE and attempts
            # to change it raise ValueError instead.
            try:
                try:
                    resource.setrlimit(resource.RLIMIT_FSIZE, (1024, max))
                    limit_set = True
                except ValueError:
                    limit_set = False
                f = open(test_support.TESTFN, "wb")
                try:
                    f.write(b"X" * 1024)
                    try:
                        f.write(b"Y")
                        f.flush()
                    except IOError:
                        if not limit_set:
                            raise
                    if limit_set:
                        # Close will attempt to flush the byte we wrote
                        # Restore limit first to avoid getting a spurious error
                        resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
                finally:
                    f.close()
                    os.unlink(test_support.TESTFN)
            finally:
                if limit_set:
                    resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))

    def test_fsize_toobig(self):
        # Be sure that setrlimit is checking for really large values
        too_big = 10**50
        try:
            (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
        except AttributeError:
            pass
        else:
            try:
                resource.setrlimit(resource.RLIMIT_FSIZE, (too_big, max))
            except (OverflowError, ValueError):
                pass
            try:
                resource.setrlimit(resource.RLIMIT_FSIZE, (max, too_big))
            except (OverflowError, ValueError):
                pass

    def test_getrusage(self):
        self.assertRaises(TypeError, resource.getrusage)
        self.assertRaises(TypeError, resource.getrusage, 42, 42)
        usageself = resource.getrusage(resource.RUSAGE_SELF)
        usagechildren = resource.getrusage(resource.RUSAGE_CHILDREN)
        # May not be available on all systems.
        try:
            usageboth = resource.getrusage(resource.RUSAGE_BOTH)
        except (ValueError, AttributeError):
            pass

def test_main(verbose=None):
    test_support.run_unittest(ResourceTest)

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


<commit_before>""" Test functions for basic module

"""

import unittest
import scipy.limits as limits
from scipy.scipy_test import assert_array_equal, assert_equal
from scipy.scipy_test import assert_almost_equal, assert_array_almost_equal
from scipy import *

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

class test_roots(unittest.TestCase):
    def check_basic(self):
        a1 = [1,-4,4]
        a2 = [4,-16,16]
        a3 = [1,5,6]
        assert_array_almost_equal(roots(a1),[2,2],11)
        assert_array_almost_equal(roots(a2),[2,2],11)
        assert_array_almost_equal(sort(roots(a3)),sort([-3,-2]),11)

    def check_inverse(self):
        a = rand(5)
        assert_array_almost_equal(sort(roots(poly(a))),sort(a),11)

class test_factorial(unittest.TestCase):
    def check_basic(self):
        for k in range(0,13):
            assert_equal(factorial(k),product(grid[1:k+1]))
        self.failUnlessRaises(ValueError, factorial, -10)

    def check_exact(self):
        resdict = {4:24L,10:3628800L,15:1307674368000L,
                   19:121645100408832000L,
                   100:93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000L}
        for key in resdict.keys():
            assert_equal(factorial(key,exact=1),resdict[key])

class test_comb(unittest.TestCase):
    def check_basic(self):
        for N in range(0,11):
            for k in range(0,N+1):
                ans = product(grid[N-k+1:N+1]) / product(grid[1:k+1])
                assert_almost_equal(comb(N,k),ans,9)
        self.failUnlessRaises(ValueError, comb, -10,1)
        self.failUnlessRaises(ValueError, comb, 10,-1)
        self.failUnlessRaises(ValueError, comb, -10,-3)
        self.failUnlessRaises(ValueError, comb, 10,11)

    def check_exact(self):
        resdict = {(10,2):45L, (10,5):252L,
                   (1000,20):339482811302457603895512614793686020778700L,
                   (1000,975):47641862536236518640933948075167736642053976275040L
                   }
        for key in resdict.keys():
            assert_equal(comb(key[0],key[1],exact=1),resdict[key])

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

def test_suite():
    suites = []
    suites.append( unittest.makeSuite(test_roots,'check_') )
    suites.append( unittest.makeSuite(test_factorial,'check_') )
    suites.append( unittest.makeSuite(test_comb,'check_') )
    total_suite = unittest.TestSuite(suites)
    return total_suite

def test():
    all_tests = test_suite()
    runner = unittest.TextTestRunner()
    runner.run(all_tests)
    return runner

if __name__ == "__main__":
    test()
<commit_msg>Bug fix in xplt.<commit_after>""" Test functions for basic module

"""

import unittest
import scipy.limits as limits
from scipy.scipy_test import assert_array_equal, assert_equal
from scipy.scipy_test import assert_almost_equal, assert_array_almost_equal
from scipy import *

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

class test_roots(unittest.TestCase):
    def check_basic(self):
        a1 = [1,-4,4]
        a2 = [4,-16,16]
        a3 = [1,5,6]
        assert_array_almost_equal(roots(a1),[2,2],11)
        assert_array_almost_equal(roots(a2),[2,2],11)
        assert_array_almost_equal(sort(roots(a3)),sort([-3,-2]),7)

    def check_inverse(self):
        a = rand(5)
        assert_array_almost_equal(sort(roots(poly(a))),sort(a),5)

class test_factorial(unittest.TestCase):
    def check_basic(self):
        for k in range(0,13):
            assert_equal(factorial(k),product(grid[1:k+1]))
        self.failUnlessRaises(ValueError, factorial, -10)

    def check_exact(self):
        resdict = {4:24L,10:3628800L,15:1307674368000L,
                   19:121645100408832000L,
                   100:93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000L}
        for key in resdict.keys():
            assert_equal(factorial(key,exact=1),resdict[key])

class test_comb(unittest.TestCase):
    def check_basic(self):
        for N in range(0,11):
            for k in range(0,N+1):
                ans = product(grid[N-k+1:N+1]) / product(grid[1:k+1])
                assert_almost_equal(comb(N,k),ans,9)
        self.failUnlessRaises(ValueError, comb, -10,1)
        self.failUnlessRaises(ValueError, comb, 10,-1)
        self.failUnlessRaises(ValueError, comb, -10,-3)
        self.failUnlessRaises(ValueError, comb, 10,11)

    def check_exact(self):
        resdict = {(10,2):45L, (10,5):252L,
                   (1000,20):339482811302457603895512614793686020778700L,
                   (1000,975):47641862536236518640933948075167736642053976275040L
                   }
        for key in resdict.keys():
            assert_equal(comb(key[0],key[1],exact=1),resdict[key])

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

def test_suite():
    suites = []
    suites.append( unittest.makeSuite(test_roots,'check_') )
    suites.append( unittest.makeSuite(test_factorial,'check_') )
    suites.append( unittest.makeSuite(test_comb,'check_') )
    total_suite = unittest.TestSuite(suites)
    return total_suite

def test():
    all_tests = test_suite()
    runner = unittest.TextTestRunner()
    runner.run(all_tests)
    return runner

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


<commit_before>from django import forms
from django.utils.translation import ugettext_lazy as _
from aristotle_favourites.models import Folder
from aristotle_mdr.forms.bulk_actions import BulkActionForm
from aristotle_mdr.perms import user_can_view

class FolderForm(forms.ModelForm):
    class Meta:
        model = Folder
        exclude = ['slug','owner', 'items']

class BulkAddToFolder(BulkChangeFromFolder):
    action_text = _('Add to folder...')
    classes = 'fa-folder'
    items_label = _('Add the following items to the folder')

    folder = forms.ModelChoiceField(
        queryset=Folder.objects.all(),
        label=_("Folder to move to"),
        #widget=autocomplete_light.ChoiceWidget('Autocomplete_concept')
    )

    def make_changes(self):
        folder = self.cleaned_data['folder']
        # self.queryset = folder.items.all()
        items = self.cleaned_data['items']
        bad_items = [str(i.id) for i in items if not user_can_view(self.user, i)]
        items = items.visible(self.user)
        folder.items.add(*items)

        return _(
            "%(num_items)s items added to the folder '%(folder_name)s'. \n"
            "Some items failed, they had the id's: %(bad_ids)s"
        ) % {
            'num_items': len(items),
            'bad_ids': ",".join(bad_items)
            'folder_name': folder.name,
        }

class BulkRemoveFromFolder(BulkChangeFromFolder):
    action_text = _('Add to folder...')
    classes = 'fa-folder-o'
    items_label = _('Add the following items to the folder')

    def make_changes(self):
        folder = self.cleaned_data['folder']
        items = self.cleaned_data['items']
        folder.items.remove(*items)

        return _(
            "%(num_items)s items removed from the folder '%(folder_name)s'."
        ) % {
            'num_items': len(items),
            'folder_name': folder.name,
        }

class BulkChangeFromFolder(BulkActionForm):
    classes = 'fa-folder-o'
    confirm_page = 'aristotle_favourites/actions/bulk_add.html'

    folder = forms.ModelChoiceField(
        queryset=Folder.objects.all(),
        label=_("Folder to move to"),
        #widget=autocomplete_light.ChoiceWidget('Autocomplete_concept')
    )

    def __init__(self, *args, **kwargs):
        super(BulkAddToFolder, self).__init__(*args, **kwargs)
        # self.queryset = MDR._concept.objects.none()
        self.fields['folder']=forms.ModelChoiceField(
            queryset=self.user.favourite_folders.all(),
            label=_("Folder to move to"),
        )
<commit_msg>fix syntax error<commit_after>from django import forms
from django.utils.translation import ugettext_lazy as _
from aristotle_favourites.models import Folder
from aristotle_mdr.forms.bulk_actions import BulkActionForm
from aristotle_mdr.perms import user_can_view

class FolderForm(forms.ModelForm):
    class Meta:
        model = Folder
        exclude = ['slug','owner', 'items']

class BulkChangeFromFolder(BulkActionForm):
    classes = 'fa-folder-o'
    confirm_page = 'aristotle_favourites/actions/bulk_add.html'

    folder = forms.ModelChoiceField(
        queryset=Folder.objects.all(),
        label=_("Folder to move to"),
        #widget=autocomplete_light.ChoiceWidget('Autocomplete_concept')
    )

    def __init__(self, *args, **kwargs):
        super(BulkAddToFolder, self).__init__(*args, **kwargs)
        # self.queryset = MDR._concept.objects.none()
        self.fields['folder']=forms.ModelChoiceField(
            queryset=self.user.favourite_folders.all(),
            label=_("Folder to move to"),
        )

class BulkAddToFolder(BulkChangeFromFolder):
    action_text = _('Add to folder...')
    classes = 'fa-folder'
    items_label = _('Add the following items to the folder')

    folder = forms.ModelChoiceField(
        queryset=Folder.objects.all(),
        label=_("Folder to move to"),
        #widget=autocomplete_light.ChoiceWidget('Autocomplete_concept')
    )

    def make_changes(self):
        folder = self.cleaned_data['folder']
        # self.queryset = folder.items.all()
        items = self.cleaned_data['items']
        bad_items = [str(i.id) for i in items if not user_can_view(self.user, i)]
        items = items.visible(self.user)
        folder.items.add(*items)

        return _(
            "%(num_items)s items added to the folder '%(folder_name)s'. \n"
            "Some items failed, they had the id's: %(bad_ids)s"
        ) % {
            'num_items': len(items),
            'bad_ids': ",".join(bad_items),
            'folder_name': folder.name,
        }

class BulkRemoveFromFolder(BulkChangeFromFolder):
    action_text = _('Add to folder...')
    classes = 'fa-folder-o'
    items_label = _('Add the following items to the folder')

    def make_changes(self):
        folder = self.cleaned_data['folder']
        items = self.cleaned_data['items']
        folder.items.remove(*items)

        return _(
            "%(num_items)s items removed from the folder '%(folder_name)s'."
        ) % {
            'num_items': len(items),
            'folder_name': folder.name,
        }
<|endoftext|>


<commit_before>import time, sys, uuid
from bitmovin import Bitmovin, Encoding, S3Output, H264CodecConfiguration, \
    AACCodecConfiguration, H264Profile, StreamInput, SelectionMode, Stream, EncodingOutput, ACLEntry, ACLPermission, \
    FMP4Muxing, MuxingStream, CloudRegion, DashManifest, VideoAdaptationSet, AudioAdaptationSet, Period, \
    FMP4Representation, FMP4RepresentationType, LiveStreamConfiguration, LiveDashManifest, GenericS3Output, TSMuxing, LiveHlsManifest, HlsManifest, AudioMedia, VariantStream
from bitmovin.errors import BitmovinApiError

ERROR_CODE_LIVE_STREAM_NOT_READY = 2023
LIVE_STREAM_INFORMATION_FETCH_RETRY_INTERVAL = 5
LIVE_STREAM_INFORMATION_FETCH_MAX_RETRIES = 60

API_KEY = '<YOUR_API_KEY>'
STREAM_KEY = '<YOUR_STREAM_KEY>'
ENCODER_VERSION = 'STABLE'
CLOUD_REGION = CloudRegion.KUBERNETES
INFRASTRUCTURE_ID = '<YOUR_INFRASTRUCTURE_ID>'

GENERIC_S3_OUTPUT_ACCESSKEY = '<YOUR_GENERIC_S3_ACCESS_KEY>'
GENERIC_S3_OUTPUT_SECRETKEY = '<YOUR_GENERIC_S3_OUTPUT_SECRETKEY>'
GENERIC_S3_OUTPUT_BUCKETNAME = '<YOUR_GENERIC_S3_OUTPUT_BUCKETNAME>'
GENERIC_S3_OUTPUT_HOST = '<YOUR_GENERIC_S3_HOST>'
GENERIC_S3_OUTPUT_PORT = 8080 # YOUR_GENERIC_S3_PORT

OUTPUT_BASE_PATH = 'your/output/base/path/{}/'.format(uuid.uuid4())

class NoRtmpInputAvailableError(Exception):
    def __init__(self, message):
        super(message)

def main():
    bitmovin = Bitmovin(api_key=API_KEY)
    resource_response = bitmovin.inputs.RTMP.list()

    if not resource_response or \
       not resource_response.resource or \
       not isinstance(resource_response.resource, list) or \
       len(resource_response.resource) < 1:
        print('Could not find any RTMP inputs. Please contact support.')
        sys.exit(3)

    rtmp_input_id = resource_response.resource[0].id
    print("Found RTMP input with ID '{}'".format(rtmp_input_id))

    rtmp_input = bitmovin.inputs.RTMP.retrieve(id_=rtmp_input_id).resource

    output = GenericS3Output(access_key=GENERIC_S3_OUTPUT_ACCESSKEY,
                         secret_key=GENERIC_S3_OUTPUT_SECRETKEY,
                         bucket_name=GENERIC_S3_OUTPUT_BUCKETNAME,
                         host=GENERIC_S3_OUTPUT_HOST,
                         port=GENERIC_S3_OUTPUT_PORT,
                         name='Sample Generic S3 Output')
    output = bitmovin.outputs.GenericS3.create(output).resource

    encoding = Encoding(name='example python hls live stream encoding',
                        encoder_version=ENCODER_VERSION,
                        cloud_region=CLOUD_REGION,
                        infrastructure_id=INFRASTRUCTURE_ID)
    encoding = bitmovin.encodings.Encoding.create(encoding).resource

    video_codec_configuration_1080p = H264CodecConfiguration(name='example_video_codec_configuration_1080p',
                                                             bitrate=4800000,
                                                             rate=25.0,
                                                             width=1920,
                                                             height=1080,
                                                             profile=H264Profile.HIGH)
    video_codec_configuration_1080p = bitmovin.codecConfigurations.H264.create(video_codec_configuration_1080p).resource

    video_codec_configuration_720p = H264CodecConfiguration(name='example_video_codec_configuration_720p',
                                                            bitrate=2400000,
                                                            rate=25.0,
                                                            width=1280,
                                                            height=720,
                                                            profile=H264Profile.HIGH)
    video_codec_configuration_720p = bitmovin.codecConfigurations.H264.create(video_codec_configuration_720p).resource

    video_codec_configuration_360p = H264CodecConfiguration(name='example_video_codec_configuration_720p',
                                                            bitrate=800000,
                                                            rate=25.0,
                                                            width=640,
                                                            height=360,
                                                            profile=H264Profile.HIGH)
    video_codec_configuration_360p = bitmovin.codecConfigurations.H264.create(video_codec_configuration_360p).resource

    audio_codec_configuration = AACCodecConfiguration(name='example_audio_codec_configuration_english',
                                                      bitrate=128000,
                                                      rate=48000)
    audio_codec_configuration = bitmovin.codecConfigurations.AAC.create(audio_codec_configuration).resource

    video_input_stream = StreamInput(input_id=rtmp_input.id,
                                     input_path='live',
                                     position=0,
                                     selection_mode=SelectionMode.AUTO)
    audio_input_stream = StreamInput(input_id=rtmp_input.id,
                                     input_path='live',
                                     position=1,
                                     selection_mode=SelectionMode.AUTO)

    video_stream_1080p = Stream(codec_configuration_id=video_codec_configuration_1080p.id,
                                input_streams=[video_input_stream], name='Sample Stream 1080p')
    video_stream_1080p = bitmovin.encodings.Stream.create(object_=video_stream_1080p,
                                                          encoding_id=encoding.id).resource

    video_stream_720p = Stream(codec_configuration_id=video_codec_configuration_720p.id,
                               input_streams=[video_input_stream], name='Sample Stream 720p')
    video_stream_720p = bitmovin.encodings.Stream.create(object_=video_stream_720p,
                                                         encoding_id=encoding.id).resource

    video_stream_360p = Stream(codec_configuration_id=video_codec_configuration_360p.id,
                               input_streams=[video_input_stream], name='Sample Stream 360p')
    video_stream_360p = bitmovin.encodings.Stream.create(object_=video_stream_360p,
                                                         encoding_id=encoding.id).resource

    audio_stream = Stream(codec_configuration_id=audio_codec_configuration.id,
                          input_streams=[audio_input_stream], name='Sample Stream AUDIO')
    audio_stream = bitmovin.encodings.Stream.create(object_=audio_stream,
                                                    encoding_id=encoding.id).resource

    video_muxing_stream_1080p = MuxingStream(video_stream_1080p.id)
    video_muxing_stream_720p = MuxingStream(video_stream_720p.id)
    video_muxing_stream_360p = MuxingStream(video_stream_360p.id)
    audio_muxing_stream = MuxingStream(audio_stream.id)

    acl_entry = ACLEntry(permission=ACLPermission.PUBLIC_READ)

    video_muxing_1080p_output = EncodingOutput(output_id=output.id,
                                               output_path=OUTPUT_BASE_PATH + 'video/1080p',
                                               acl=[acl_entry])
    video_muxing_1080p = TSMuxing(segment_length=4,
                                    segment_naming='seg_%number%.ts',
                                    streams=[video_muxing_stream_1080p],
                                    outputs=[video_muxing_1080p_output],
                                    name='Sample Muxing 1080p')
    video_muxing_1080p = bitmovin.encodings.Muxing.TS.create(object_=video_muxing_1080p,
                                                               encoding_id=encoding.id).resource
    video_muxing_720p_output = EncodingOutput(output_id=output.id,
                                              output_path=OUTPUT_BASE_PATH + 'video/720p',
                                              acl=[acl_entry])
    video_muxing_720p = TSMuxing(segment_length=4,
                                   segment_naming='seg_%number%.m4s',
                                   streams=[video_muxing_stream_720p],
                                   outputs=[video_muxing_720p_output],
                                   name='Sample Muxing 720p')
    video_muxing_720p = bitmovin.encodings.Muxing.TS.create(object_=video_muxing_720p,
                                                              encoding_id=encoding.id).resource

    video_muxing_360p_output = EncodingOutput(output_id=output.id,
                                              output_path=OUTPUT_BASE_PATH + 'video/360p',
                                              acl=[acl_entry])
    video_muxing_360p = TSMuxing(segment_length=4,
                                   segment_naming='seg_%number%.m4s',
                                   streams=[video_muxing_stream_360p],
                                   outputs=[video_muxing_360p_output],
                                   name='Sample Muxing 360p')
    video_muxing_360p = bitmovin.encodings.Muxing.TS.create(object_=video_muxing_360p,
                                                              encoding_id=encoding.id).resource

    audio_muxing_output = EncodingOutput(output_id=output.id,
                                              output_path=OUTPUT_BASE_PATH + 'audio/128k',
                                              acl=[acl_entry])
    audio_muxing = TSMuxing(segment_length=4,
                              segment_naming='seg_%number%.m4s',
                              streams=[audio_muxing_stream],
                              outputs=[audio_muxing_output],
                              name='Sample Muxing AUDIO')
    audio_muxing = bitmovin.encodings.Muxing.TS.create(object_=audio_muxing,
                                                         encoding_id=encoding.id).resource

    #### Manifest #####################################################################################################

    manifest_output = EncodingOutput(output_id=output.id,
                                     output_path=OUTPUT_BASE_PATH,
                                     acl=[acl_entry])
    hls_manifest = HlsManifest(manifest_name='example_live_manifest.m3u8',
                                 outputs=[manifest_output],
                                 name='Sample HLS Manifest Live',
                                 description='Bitmovin Python Test Live HLS Manifest')
    hls_manifest = bitmovin.manifests.HLS.create(hls_manifest).resource

    audio_media = AudioMedia(name='Sample Audio Media',
                             group_id='audio_group',
                             segment_path="audio/128k/",
                             encoding_id=encoding.id,
                             stream_id=audio_stream.id,
                             muxing_id=audio_muxing.id,
                             language='en',
                             uri='audiomedia.m3u8',
                             autoselect=False)

    audio_media = bitmovin.manifests.HLS.AudioMedia.create(manifest_id=hls_manifest.id, object_=audio_media).resource

    variant_stream_1080p = VariantStream(audio=audio_media.groupId,
                                         closed_captions='NONE',
                                         segment_path="video/1080p/",
                                         uri='video_1080p.m3u8',
                                         encoding_id=encoding.id,
                                         stream_id=video_stream_1080p.id,
                                         muxing_id=video_muxing_1080p.id)

    variant_stream_1080p = bitmovin.manifests.HLS.VariantStream.create(manifest_id=hls_manifest.id,
                                                                       object_=variant_stream_1080p)

    variant_stream_720p = VariantStream(audio=audio_media.groupId,
                                        closed_captions='NONE',
                                        segment_path="video/720p/",
                                        uri='video_720p.m3u8',
                                        encoding_id=encoding.id,
                                        stream_id=video_stream_720p.id,
                                        muxing_id=video_muxing_720p.id)

    variant_stream_720p = bitmovin.manifests.HLS.VariantStream.create(manifest_id=hls_manifest.id,
                                                                      object_=variant_stream_720p)

    variant_stream_360p = VariantStream(audio=audio_media.groupId,
                                        closed_captions='NONE',
                                        segment_path="video/360p/",
                                        uri='video_360p.m3u8',
                                        encoding_id=encoding.id,
                                        stream_id=video_stream_360p.id,
                                        muxing_id=video_muxing_360p.id)

    variant_stream_360p = bitmovin.manifests.HLS.VariantStream.create(manifest_id=hls_manifest.id,
                                                                      object_=variant_stream_360p)

    bitmovin.manifests.HLS.start(manifest_id=hls_manifest.id)

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

    live_hls_manifest = LiveHlsManifest(manifest_id=hls_manifest.id)

    live_stream_configuration = LiveStreamConfiguration(
        stream_key=STREAM_KEY,
        live_hls_manifests=[live_hls_manifest]
    )

    resource_response = bitmovin.encodings.Encoding.start_live(
        encoding_id=encoding.id,
        live_stream_configuration=live_stream_configuration
    )

    bitmovin.encodings.Encoding.wait_until_running(encoding_id=resource_response.resource.id)

    live_details = None
    retry = 0

    while retry < LIVE_STREAM_INFORMATION_FETCH_MAX_RETRIES:
        try:
            live_details = bitmovin.encodings.Encoding.retrieve_live(encoding_id=resource_response.resource.id)
            break
        except BitmovinApiError as bitmovin_api_error:
            if bitmovin_api_error.response.data.code != ERROR_CODE_LIVE_STREAM_NOT_READY:
                sys.exit(2)
            print("Live stream is not ready yet. Retrying to fetch live stream information in {} seconds...".format(
                LIVE_STREAM_INFORMATION_FETCH_RETRY_INTERVAL
            ))
            retry += 1
            time.sleep(LIVE_STREAM_INFORMATION_FETCH_RETRY_INTERVAL)

    if live_details is not None:
        print_live_stream_details(encoding_id=encoding.id, live_stream_details=live_details.resource)
    else:
        print("Unable to fetch live stream details!")
        sys.exit(1)

def print_live_stream_details(encoding_id, live_stream_details):
    rtmp_service="enc-{}-bitcodin-rtmp".format(encoding_id)
    node_port_patch_command="kubectl patch service {} -p '{{ \"spec\": {{ \"type\": \"NodePort\" }} }}'".format(rtmp_service)
    query_node_port="kubectl get service {} -o json | jq '.spec.ports[] | select(.name==\"rtmp-ingest-point\").nodePort'".format(rtmp_service)
    node_port_patch_custom_id="kubectl patch service {} -p '{{ \"spec\": {{ \"type\": \"NodePort\" \"nodePort\": \"30001\" }} }}'".format(rtmp_service)
    load_balancer="kubectl patch service {} -p '{{ \"spec\": {{ \"type\": \"LoadBalancer\" }} }}'".format(rtmp_service)
    load_balancer_ip_and_port="kubectl get service {} -o json | jq -r '.status.loadBalancer.ingress[] | .ip'".format(rtmp_service)

    print('\n-----------------------------------------------------')
    print('Live Stream set up successfully: \n')
    print('Encoding ID ... {}'.format(encoding_id))
    print('Encoder IP .... {}'.format(live_stream_details.encoderIp))
    print('Stream Key .... {}'.format(live_stream_details.streamKey))
    print('')
    print('Stream URL: ... {}'.format('rtmp://<public-ip>:<port>/live'.format(live_stream_details.encoderIp)))
    print('-----------------------------------------------------')
    print('You can now configure the RTMP ingest point for access outside of your infrastructure.')
    print('Configure node port access ............................ {}'.format(node_port_patch_command))
    print('Configure node port access (custom port) .............. {}'.format(node_port_patch_custom_id))
    print('Find the node port .................................... {}'.format(query_node_port))
    print('Configure public load balancer ........................ {}'.format(load_balancer))
    print('Find out the ip of the load balancer (use port 1935)... {}'.format(load_balancer_ip_and_port))
    print('\n')

if __name__ == '__main__':
    main()
<commit_msg>added timeshift for hls manifest in on-premise livestream<commit_after>import time, sys, uuid
from bitmovin import Bitmovin, Encoding, S3Output, H264CodecConfiguration, \
    AACCodecConfiguration, H264Profile, StreamInput, SelectionMode, Stream, EncodingOutput, ACLEntry, ACLPermission, \
    FMP4Muxing, MuxingStream, CloudRegion, DashManifest, VideoAdaptationSet, AudioAdaptationSet, Period, \
    FMP4Representation, FMP4RepresentationType, LiveStreamConfiguration, LiveDashManifest, GenericS3Output, TSMuxing, LiveHlsManifest, HlsManifest, AudioMedia, VariantStream
from bitmovin.errors import BitmovinApiError

ERROR_CODE_LIVE_STREAM_NOT_READY = 2023
LIVE_STREAM_INFORMATION_FETCH_RETRY_INTERVAL = 5
LIVE_STREAM_INFORMATION_FETCH_MAX_RETRIES = 60

API_KEY = '<YOUR_API_KEY>'
STREAM_KEY = '<YOUR_STREAM_KEY>'
ENCODER_VERSION = 'STABLE'
CLOUD_REGION = CloudRegion.KUBERNETES
INFRASTRUCTURE_ID = '<YOUR_INFRASTRUCTURE_ID>'

GENERIC_S3_OUTPUT_ACCESSKEY = '<YOUR_GENERIC_S3_ACCESS_KEY>'
GENERIC_S3_OUTPUT_SECRETKEY = '<YOUR_GENERIC_S3_OUTPUT_SECRETKEY>'
GENERIC_S3_OUTPUT_BUCKETNAME = '<YOUR_GENERIC_S3_OUTPUT_BUCKETNAME>'
GENERIC_S3_OUTPUT_HOST = '<YOUR_GENERIC_S3_HOST>'
GENERIC_S3_OUTPUT_PORT = 8080 # YOUR_GENERIC_S3_PORT

OUTPUT_BASE_PATH = 'your/output/base/path/{}/'.format(uuid.uuid4())

class NoRtmpInputAvailableError(Exception):
    def __init__(self, message):
        super(message)

def main():
    bitmovin = Bitmovin(api_key=API_KEY)
    resource_response = bitmovin.inputs.RTMP.list()

    if not resource_response or \
       not resource_response.resource or \
       not isinstance(resource_response.resource, list) or \
       len(resource_response.resource) < 1:
        print('Could not find any RTMP inputs. Please contact support.')
        sys.exit(3)

    rtmp_input_id = resource_response.resource[0].id
    print("Found RTMP input with ID '{}'".format(rtmp_input_id))

    rtmp_input = bitmovin.inputs.RTMP.retrieve(id_=rtmp_input_id).resource

    output = GenericS3Output(access_key=GENERIC_S3_OUTPUT_ACCESSKEY,
                         secret_key=GENERIC_S3_OUTPUT_SECRETKEY,
                         bucket_name=GENERIC_S3_OUTPUT_BUCKETNAME,
                         host=GENERIC_S3_OUTPUT_HOST,
                         port=GENERIC_S3_OUTPUT_PORT,
                         name='Sample Generic S3 Output')
    output = bitmovin.outputs.GenericS3.create(output).resource

    encoding = Encoding(name='example python hls live stream encoding',
                        encoder_version=ENCODER_VERSION,
                        cloud_region=CLOUD_REGION,
                        infrastructure_id=INFRASTRUCTURE_ID)
    encoding = bitmovin.encodings.Encoding.create(encoding).resource

    video_codec_configuration_1080p = H264CodecConfiguration(name='example_video_codec_configuration_1080p',
                                                             bitrate=4800000,
                                                             rate=25.0,
                                                             width=1920,
                                                             height=1080,
                                                             profile=H264Profile.HIGH)
    video_codec_configuration_1080p = bitmovin.codecConfigurations.H264.create(video_codec_configuration_1080p).resource

    video_codec_configuration_720p = H264CodecConfiguration(name='example_video_codec_configuration_720p',
                                                            bitrate=2400000,
                                                            rate=25.0,
                                                            width=1280,
                                                            height=720,
                                                            profile=H264Profile.HIGH)
    video_codec_configuration_720p = bitmovin.codecConfigurations.H264.create(video_codec_configuration_720p).resource

    video_codec_configuration_360p = H264CodecConfiguration(name='example_video_codec_configuration_720p',
                                                            bitrate=800000,
                                                            rate=25.0,
                                                            width=640,
                                                            height=360,
                                                            profile=H264Profile.HIGH)
    video_codec_configuration_360p = bitmovin.codecConfigurations.H264.create(video_codec_configuration_360p).resource

    audio_codec_configuration = AACCodecConfiguration(name='example_audio_codec_configuration_english',
                                                      bitrate=128000,
                                                      rate=48000)
    audio_codec_configuration = bitmovin.codecConfigurations.AAC.create(audio_codec_configuration).resource

    video_input_stream = StreamInput(input_id=rtmp_input.id,
                                     input_path='live',
                                     position=0,
                                     selection_mode=SelectionMode.AUTO)
    audio_input_stream = StreamInput(input_id=rtmp_input.id,
                                     input_path='live',
                                     position=1,
                                     selection_mode=SelectionMode.AUTO)

    video_stream_1080p = Stream(codec_configuration_id=video_codec_configuration_1080p.id,
                                input_streams=[video_input_stream], name='Sample Stream 1080p')
    video_stream_1080p = bitmovin.encodings.Stream.create(object_=video_stream_1080p,
                                                          encoding_id=encoding.id).resource

    video_stream_720p = Stream(codec_configuration_id=video_codec_configuration_720p.id,
                               input_streams=[video_input_stream], name='Sample Stream 720p')
    video_stream_720p = bitmovin.encodings.Stream.create(object_=video_stream_720p,
                                                         encoding_id=encoding.id).resource

    video_stream_360p = Stream(codec_configuration_id=video_codec_configuration_360p.id,
                               input_streams=[video_input_stream], name='Sample Stream 360p')
    video_stream_360p = bitmovin.encodings.Stream.create(object_=video_stream_360p,
                                                         encoding_id=encoding.id).resource

    audio_stream = Stream(codec_configuration_id=audio_codec_configuration.id,
                          input_streams=[audio_input_stream], name='Sample Stream AUDIO')
    audio_stream = bitmovin.encodings.Stream.create(object_=audio_stream,
                                                    encoding_id=encoding.id).resource

    video_muxing_stream_1080p = MuxingStream(video_stream_1080p.id)
    video_muxing_stream_720p = MuxingStream(video_stream_720p.id)
    video_muxing_stream_360p = MuxingStream(video_stream_360p.id)
    audio_muxing_stream = MuxingStream(audio_stream.id)

    acl_entry = ACLEntry(permission=ACLPermission.PUBLIC_READ)

    video_muxing_1080p_output = EncodingOutput(output_id=output.id,
                                               output_path=OUTPUT_BASE_PATH + 'video/1080p',
                                               acl=[acl_entry])
    video_muxing_1080p = TSMuxing(segment_length=4,
                                    segment_naming='seg_%number%.ts',
                                    streams=[video_muxing_stream_1080p],
                                    outputs=[video_muxing_1080p_output],
                                    name='Sample Muxing 1080p')
    video_muxing_1080p = bitmovin.encodings.Muxing.TS.create(object_=video_muxing_1080p,
                                                               encoding_id=encoding.id).resource
    video_muxing_720p_output = EncodingOutput(output_id=output.id,
                                              output_path=OUTPUT_BASE_PATH + 'video/720p',
                                              acl=[acl_entry])
    video_muxing_720p = TSMuxing(segment_length=4,
                                   segment_naming='seg_%number%.m4s',
                                   streams=[video_muxing_stream_720p],
                                   outputs=[video_muxing_720p_output],
                                   name='Sample Muxing 720p')
    video_muxing_720p = bitmovin.encodings.Muxing.TS.create(object_=video_muxing_720p,
                                                              encoding_id=encoding.id).resource

    video_muxing_360p_output = EncodingOutput(output_id=output.id,
                                              output_path=OUTPUT_BASE_PATH + 'video/360p',
                                              acl=[acl_entry])
    video_muxing_360p = TSMuxing(segment_length=4,
                                   segment_naming='seg_%number%.m4s',
                                   streams=[video_muxing_stream_360p],
                                   outputs=[video_muxing_360p_output],
                                   name='Sample Muxing 360p')
    video_muxing_360p = bitmovin.encodings.Muxing.TS.create(object_=video_muxing_360p,
                                                              encoding_id=encoding.id).resource

    audio_muxing_output = EncodingOutput(output_id=output.id,
                                              output_path=OUTPUT_BASE_PATH + 'audio/128k',
                                              acl=[acl_entry])
    audio_muxing = TSMuxing(segment_length=4,
                              segment_naming='seg_%number%.m4s',
                              streams=[audio_muxing_stream],
                              outputs=[audio_muxing_output],
                              name='Sample Muxing AUDIO')
    audio_muxing = bitmovin.encodings.Muxing.TS.create(object_=audio_muxing,
                                                         encoding_id=encoding.id).resource

    #### Manifest #####################################################################################################

    manifest_output = EncodingOutput(output_id=output.id,
                                     output_path=OUTPUT_BASE_PATH,
                                     acl=[acl_entry])
    hls_manifest = HlsManifest(manifest_name='example_live_manifest.m3u8',
                                 outputs=[manifest_output],
                                 name='Sample HLS Manifest Live',
                                 description='Bitmovin Python Test Live HLS Manifest')
    hls_manifest = bitmovin.manifests.HLS.create(hls_manifest).resource

    audio_media = AudioMedia(name='Sample Audio Media',
                             group_id='audio_group',
                             segment_path="audio/128k/",
                             encoding_id=encoding.id,
                             stream_id=audio_stream.id,
                             muxing_id=audio_muxing.id,
                             language='en',
                             uri='audiomedia.m3u8',
                             autoselect=False)

    audio_media = bitmovin.manifests.HLS.AudioMedia.create(manifest_id=hls_manifest.id, object_=audio_media).resource

    variant_stream_1080p = VariantStream(audio=audio_media.groupId,
                                         closed_captions='NONE',
                                         segment_path="video/1080p/",
                                         uri='video_1080p.m3u8',
                                         encoding_id=encoding.id,
                                         stream_id=video_stream_1080p.id,
                                         muxing_id=video_muxing_1080p.id)

    variant_stream_1080p = bitmovin.manifests.HLS.VariantStream.create(manifest_id=hls_manifest.id,
                                                                       object_=variant_stream_1080p)

    variant_stream_720p = VariantStream(audio=audio_media.groupId,
                                        closed_captions='NONE',
                                        segment_path="video/720p/",
                                        uri='video_720p.m3u8',
                                        encoding_id=encoding.id,
                                        stream_id=video_stream_720p.id,
                                        muxing_id=video_muxing_720p.id)

    variant_stream_720p = bitmovin.manifests.HLS.VariantStream.create(manifest_id=hls_manifest.id,
                                                                      object_=variant_stream_720p)

    variant_stream_360p = VariantStream(audio=audio_media.groupId,
                                        closed_captions='NONE',
                                        segment_path="video/360p/",
                                        uri='video_360p.m3u8',
                                        encoding_id=encoding.id,
                                        stream_id=video_stream_360p.id,
                                        muxing_id=video_muxing_360p.id)

    variant_stream_360p = bitmovin.manifests.HLS.VariantStream.create(manifest_id=hls_manifest.id,
                                                                      object_=variant_stream_360p)

    bitmovin.manifests.HLS.start(manifest_id=hls_manifest.id)

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

    live_hls_manifest = LiveHlsManifest(manifest_id=hls_manifest.id,
                                        time_shift=60)

    live_stream_configuration = LiveStreamConfiguration(
        stream_key=STREAM_KEY,
        live_hls_manifests=[live_hls_manifest],
    )

    resource_response = bitmovin.encodings.Encoding.start_live(
        encoding_id=encoding.id,
        live_stream_configuration=live_stream_configuration
    )

    bitmovin.encodings.Encoding.wait_until_running(encoding_id=resource_response.resource.id)

    live_details = None
    retry = 0

    while retry < LIVE_STREAM_INFORMATION_FETCH_MAX_RETRIES:
        try:
            live_details = bitmovin.encodings.Encoding.retrieve_live(encoding_id=resource_response.resource.id)
            break
        except BitmovinApiError as bitmovin_api_error:
            if bitmovin_api_error.response.data.code != ERROR_CODE_LIVE_STREAM_NOT_READY:
                sys.exit(2)
            print("Live stream is not ready yet. Retrying to fetch live stream information in {} seconds...".format(
                LIVE_STREAM_INFORMATION_FETCH_RETRY_INTERVAL
            ))
            retry += 1
            time.sleep(LIVE_STREAM_INFORMATION_FETCH_RETRY_INTERVAL)

    if live_details is not None:
        print_live_stream_details(encoding_id=encoding.id, live_stream_details=live_details.resource)
    else:
        print("Unable to fetch live stream details!")
        sys.exit(1)

def print_live_stream_details(encoding_id, live_stream_details):
    rtmp_service="enc-{}-bitcodin-rtmp".format(encoding_id)
    node_port_patch_command="kubectl patch service {} -p '{{ \"spec\": {{ \"type\": \"NodePort\" }} }}'".format(rtmp_service)
    query_node_port="kubectl get service {} -o json | jq '.spec.ports[] | select(.name==\"rtmp-ingest-point\").nodePort'".format(rtmp_service)
    node_port_patch_custom_id="kubectl patch service {} -p '{{ \"spec\": {{ \"type\": \"NodePort\" \"nodePort\": \"30001\" }} }}'".format(rtmp_service)
    load_balancer="kubectl patch service {} -p '{{ \"spec\": {{ \"type\": \"LoadBalancer\" }} }}'".format(rtmp_service)
    load_balancer_ip_and_port="kubectl get service {} -o json | jq -r '.status.loadBalancer.ingress[] | .ip'".format(rtmp_service)

    print('\n-----------------------------------------------------')
    print('Live Stream set up successfully: \n')
    print('Encoding ID ... {}'.format(encoding_id))
    print('Encoder IP .... {}'.format(live_stream_details.encoderIp))
    print('Stream Key .... {}'.format(live_stream_details.streamKey))
    print('')
    print('Stream URL: ... {}'.format('rtmp://<public-ip>:<port>/live'.format(live_stream_details.encoderIp)))
    print('-----------------------------------------------------')
    print('You can now configure the RTMP ingest point for access outside of your infrastructure.')
    print('Configure node port access ............................ {}'.format(node_port_patch_command))
    print('Configure node port access (custom port) .............. {}'.format(node_port_patch_custom_id))
    print('Find the node port .................................... {}'.format(query_node_port))
    print('Configure public load balancer ........................ {}'.format(load_balancer))
    print('Find out the ip of the load balancer (use port 1935)... {}'.format(load_balancer_ip_and_port))
    print('\n')

if __name__ == '__main__':
    main()
<|endoftext|>