<commit_before>from django.test import SimpleTestCase, Client, TransactionTestCase
from django.core.urlresolvers import reverse
from decimal import *
from .helper import parse_json_product_list, product_list_to_json
from .models import Product, Cash

class HelperTestCase(SimpleTestCase):
    product_list = []
    def setUp(self):
        self.product_list.append(Product(product_stockApplies = False, product_name="one", product_price=Decimal(12)))
        self.product_list.append(Product(product_stockApplies = False, product_name="two", product_price=Decimal(4)))
        self.product_list.append(Product(product_stockApplies = False, product_name="three", product_price=Decimal(0)))
        self.product_list.append(Product(product_stockApplies = False, product_name="four", product_price=Decimal(5000)))

    def test_json_parsing(self):
        json = product_list_to_json(self.product_list)
        test_list = parse_json_product_list(json)
        # The Decimal is necessary due to some Django magic
        self.assertEqual(test_list[0].product_price, Decimal(self.product_list[0].product_price))
        self.assertEqual(test_list[1].product_price, Decimal(self.product_list[1].product_price))
        self.assertEqual(test_list[2].product_price, Decimal(self.product_list[2].product_price))
        self.assertEqual(test_list[3].product_price, Decimal(self.product_list[3].product_price))

class CashTestCase(TransactionTestCase):
    def test_cash_set(self):
        client = Client()
        response = client.get(reverse('cash', args=[5]))
        self.assertEqual(response.status_code, 200)

        cash, _ = Cash.objects.get_or_create(id=0)
        self.assertEqual(cash.cash_amount, 5)

    def test_cash_reset(self):
        cash, _ = Cash.objects.get_or_create(id=0)
        cash.cash_amount = 5
        cash.save()

        client = Client()
        response = client.get(reverse('cash', args=[0]))
        self.assertEqual(response.status_code, 200)

        cash, _ = Cash.objects.get_or_create(id=0)
        self.assertEqual(cash.cash_amount, 0)
<commit_msg>Better name<commit_after>from django.test import SimpleTestCase, Client, TransactionTestCase
from django.core.urlresolvers import reverse
from decimal import *
from .helper import parse_json_product_list, product_list_to_json
from .models import Product, Cash

class HelperTestCase(SimpleTestCase):
    product_list = []
    def setUp(self):
        self.product_list.append(Product(product_stockApplies = False, product_name="one", product_price=Decimal(12)))
        self.product_list.append(Product(product_stockApplies = False, product_name="two", product_price=Decimal(4)))
        self.product_list.append(Product(product_stockApplies = False, product_name="three", product_price=Decimal(0)))
        self.product_list.append(Product(product_stockApplies = False, product_name="four", product_price=Decimal(5000)))

    def test_json_parsing(self):
        json = product_list_to_json(self.product_list)
        test_list = parse_json_product_list(json)
        # The Decimal is necessary due to some Django magic
        self.assertEqual(test_list[0].product_price, Decimal(self.product_list[0].product_price))
        self.assertEqual(test_list[1].product_price, Decimal(self.product_list[1].product_price))
        self.assertEqual(test_list[2].product_price, Decimal(self.product_list[2].product_price))
        self.assertEqual(test_list[3].product_price, Decimal(self.product_list[3].product_price))

class CashViewTestCase(TransactionTestCase):
    def test_cash_set(self):
        client = Client()
        response = client.get(reverse('cash', args=[5]))
        self.assertEqual(response.status_code, 200)

        cash, _ = Cash.objects.get_or_create(id=0)
        self.assertEqual(cash.cash_amount, 5)

    def test_cash_reset(self):
        cash, _ = Cash.objects.get_or_create(id=0)
        cash.cash_amount = 5
        cash.save()

        client = Client()
        response = client.get(reverse('cash', args=[0]))
        self.assertEqual(response.status_code, 200)

        cash, _ = Cash.objects.get_or_create(id=0)
        self.assertEqual(cash.cash_amount, 0)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

import os
import os.path
import re

from toolbox2.action import Action, ActionException
from toolbox2.worker.ffprobe import FFprobeWorker
from toolbox2.worker.ffmpeg import FFmpegWorker

class AVInfo(object):

    RES_SD_PAL      = '720x576'
    RES_SD_PAL_VBI  = '720x608'
    RES_SD_NTSC     = '720x480'
    RES_SD_NTSC_VBI = '720x512'
    RES_HD          = '1920x1080'
    RES_HD_1280     = '1280x1080'
    RES_HD_1440     = '1440x1080'

    FPS_PAL         = [25, 50]
    FPS_NTSC        = [29.97, 30, 59.94, 60]
    FPS_FILM        = [23.97, 23.98, 24]

    def __init__(self, data):
        self.data = data
        self.audio_format = None
        self.video_res = None
        self.video_has_vbi = False
        self.video_fps = 0
        self.video_dar = 0
        self.timecode = '00:00:00:00'
        self.video_streams = []
        self.audio_streams = []
        self.data_streams = []
        self.format = data['format']

        for stream in data['streams']:
            if stream['codec_type'] == 'video':
                self.video_streams.append(stream)
            elif stream['codec_type'] == 'audio':
                self.audio_streams.append(stream)
            else:
                self.data_streams.append(stream)

        self._init_res()
        self._init_fps()
        self._init_dar()
        self._init_timecode()
        self._init_audio_format()

    def _init_res(self):
        if self.video_streams:
            self.video_res = '%sx%s' % (self.video_streams[0]['width'],
                                        self.video_streams[0]['height'])

        if self.video_res in [self.RES_SD_PAL_VBI, self.RES_SD_NTSC_VBI]:
            self.video_has_vbi = True

    def _init_fps(self):
        if not self.video_streams:
            return

        match = re.match('(\d+)/(\d+)', self.video_streams[0]['r_frame_rate'])
        if match:
            (num, den) = match.groups()
            self.video_fps = round(float(num) / float(den), 2)

    def _init_dar(self):
        if not self.video_streams:
            return

        if 'display_aspect_ratio' in self.video_streams[0]:
            self.video_dar = self.video_streams[0]['display_aspect_ratio']

    def _init_timecode(self):
        self.timecode = '00:00:00:00'
        metadata = self.format.get('tags', {})
        if 'timecode' in metadata:
            self.timecode = metadata['timecode']
        elif 'timecode_at_mark_in' in metadata:
            self.timecode = metadata['timecode_at_mark_in']
        elif len(self.video_streams) > 0 and 'timecode' in self.video_streams[0]:
            self.timecode = self.video_streams[0]['timecode']
        else:
            for stream in self.data_streams:
                stream_metadata = stream.get('tags', {})
                if 'timecode' in stream_metadata:
                    self.timecode = stream_metadata['timecode']
                    break

    def _init_audio_format(self):
        if not self.audio_streams:
            return

        match = re.match('pcm_(.*)', self.audio_streams[0]['codec_name'])
        if match:
            self.audio_format = match.groups()[0]

    def video_has_VBI(self):
        return self.video_has_vbi

    def video_is_SD_PAL(self):
        return self.video_res in [self.RES_SD_PAL, self.RES_SD_PAL_VBI]

    def video_is_SD_NTSC(self):
        return self.video_res in [self.RES_SD_NTSC, self.RES_SD_NTSC_VBI]

    def video_is_HD(self):
        return self.video_res in [self.RES_HD, self.RES_HD_1280, self.RES_HD_1440]

    def video_is_SD(self):
        return not self.video_is_HD()

    def __repr__(self):
        return 'AVInfo (video_res=%s, video_has_vbi=%s, timecode=%s)' % (self.video_res, self.video_has_vbi, self.timecode)

class AVInfoActionException(ActionException):
    pass

class AVInfoAction(Action):
    """
    Extract audio/video information from media files using ffprobe/ffmpeg.
    """

    name = 'avinfo_extract'
    engine = 'ffmpeg'
    category = 'extract'
    description = 'audio/video information extract tool'
    required_params = {}

    def __init__(self, log, base_dir, _id, params=None, resources=None):
        Action.__init__(self, log, base_dir, _id, params, resources)
        self.input_file = None
        self.thumbnail = None
        self.probe_worker = None
        self.probe2_worker = None
        self.ffmpeg_worker = None

        if not os.path.isdir(self.tmp_dir):
            os.makedirs(self.tmp_dir)

        self.do_thumbnail = self.params.get('thumbnail', False)
        self.do_count_frames = self.params.get('count_frames', False)
        self.do_count_packets = self.params.get('count_packets', False)

    def _check(self):
        pass

    def _setup(self):

        self.input_file = self.get_input_resource(1).get('path')
        self.thumbnail = os.path.join(self.tmp_dir, 'thumbnail.jpg')

        self.probe_worker = self._new_worker(FFprobeWorker)
        self.probe_worker.add_input_file(self.input_file)
        self.workers.append(self.probe_worker)

        if self.do_thumbnail:
            self.ffmpeg_worker = self._new_worker(FFmpegWorker)
            self.ffmpeg_worker.add_input_file(self.input_file)
            self.ffmpeg_worker.add_output_file(self.thumbnail)
            self.ffmpeg_worker.make_thumbnail()
            self.workers.append(self.ffmpeg_worker)
            self.add_output_resource('thumbnail', self.thumbnail)

        if self.do_count_frames or self.do_count_packets:
            self.probe2_worker = self._new_worker(FFprobeWorker)
            self.probe2_worker.add_input_file(self.input_file)
            if self.do_count_packets:
                self.probe2_worker.count_packets()
            if self.do_count_frames:
                self.probe2_worker.count_frames()
            self.workers.append(self.probe2_worker)

    def _callback(self, worker, user_callback):
        if worker in [self.probe_worker, self.probe2_worker]:
            self.update_metadata(worker.metadata)
        Action._callback(self, worker, user_callback)

    def _execute(self, callback):
        has_video_streams = False
        self.worker_count = len(self.workers)
        for self.worker_idx, worker in enumerate(self.workers):

            # If no video streams are detected, skip thumbnail
            if not has_video_streams and worker == self.ffmpeg_worker:
                continue

            self._execute_worker(worker, callback)
            if worker == self.probe_worker:
                has_video_streams = self.probe_worker.metadata['format']['nb_video_streams'] > 0
            elif worker == self.ffmpeg_worker:
                self.update_metadata({
                    'thumbnail': self.thumbnail,
                })

        self.progress = 100
        self._callback(self.workers[-1], callback)

    def _finalize(self):
        pass

    def run(self, callback=None):
        Action.run(self, callback)
        return AVInfo(self.get_metadata())

<commit_msg>Remove unused _check method in avinfo extract action<commit_after># -*- coding: utf-8 -*-

import os
import os.path
import re

from toolbox2.action import Action, ActionException
from toolbox2.worker.ffprobe import FFprobeWorker
from toolbox2.worker.ffmpeg import FFmpegWorker

class AVInfo(object):

    RES_SD_PAL      = '720x576'
    RES_SD_PAL_VBI  = '720x608'
    RES_SD_NTSC     = '720x480'
    RES_SD_NTSC_VBI = '720x512'
    RES_HD          = '1920x1080'
    RES_HD_1280     = '1280x1080'
    RES_HD_1440     = '1440x1080'

    FPS_PAL         = [25, 50]
    FPS_NTSC        = [29.97, 30, 59.94, 60]
    FPS_FILM        = [23.97, 23.98, 24]

    def __init__(self, data):
        self.data = data
        self.audio_format = None
        self.video_res = None
        self.video_has_vbi = False
        self.video_fps = 0
        self.video_dar = 0
        self.timecode = '00:00:00:00'
        self.video_streams = []
        self.audio_streams = []
        self.data_streams = []
        self.format = data['format']

        for stream in data['streams']:
            if stream['codec_type'] == 'video':
                self.video_streams.append(stream)
            elif stream['codec_type'] == 'audio':
                self.audio_streams.append(stream)
            else:
                self.data_streams.append(stream)

        self._init_res()
        self._init_fps()
        self._init_dar()
        self._init_timecode()
        self._init_audio_format()

    def _init_res(self):
        if self.video_streams:
            self.video_res = '%sx%s' % (self.video_streams[0]['width'],
                                        self.video_streams[0]['height'])

        if self.video_res in [self.RES_SD_PAL_VBI, self.RES_SD_NTSC_VBI]:
            self.video_has_vbi = True

    def _init_fps(self):
        if not self.video_streams:
            return

        match = re.match('(\d+)/(\d+)', self.video_streams[0]['r_frame_rate'])
        if match:
            (num, den) = match.groups()
            self.video_fps = round(float(num) / float(den), 2)

    def _init_dar(self):
        if not self.video_streams:
            return

        if 'display_aspect_ratio' in self.video_streams[0]:
            self.video_dar = self.video_streams[0]['display_aspect_ratio']

    def _init_timecode(self):
        self.timecode = '00:00:00:00'
        metadata = self.format.get('tags', {})
        if 'timecode' in metadata:
            self.timecode = metadata['timecode']
        elif 'timecode_at_mark_in' in metadata:
            self.timecode = metadata['timecode_at_mark_in']
        elif len(self.video_streams) > 0 and 'timecode' in self.video_streams[0]:
            self.timecode = self.video_streams[0]['timecode']
        else:
            for stream in self.data_streams:
                stream_metadata = stream.get('tags', {})
                if 'timecode' in stream_metadata:
                    self.timecode = stream_metadata['timecode']
                    break

    def _init_audio_format(self):
        if not self.audio_streams:
            return

        match = re.match('pcm_(.*)', self.audio_streams[0]['codec_name'])
        if match:
            self.audio_format = match.groups()[0]

    def video_has_VBI(self):
        return self.video_has_vbi

    def video_is_SD_PAL(self):
        return self.video_res in [self.RES_SD_PAL, self.RES_SD_PAL_VBI]

    def video_is_SD_NTSC(self):
        return self.video_res in [self.RES_SD_NTSC, self.RES_SD_NTSC_VBI]

    def video_is_HD(self):
        return self.video_res in [self.RES_HD, self.RES_HD_1280, self.RES_HD_1440]

    def video_is_SD(self):
        return not self.video_is_HD()

    def __repr__(self):
        return 'AVInfo (video_res=%s, video_has_vbi=%s, timecode=%s)' % (self.video_res, self.video_has_vbi, self.timecode)

class AVInfoActionException(ActionException):
    pass

class AVInfoAction(Action):
    """
    Extract audio/video information from media files using ffprobe/ffmpeg.
    """

    name = 'avinfo_extract'
    engine = 'ffmpeg'
    category = 'extract'
    description = 'audio/video information extract tool'
    required_params = {}

    def __init__(self, log, base_dir, _id, params=None, resources=None):
        Action.__init__(self, log, base_dir, _id, params, resources)
        self.input_file = None
        self.thumbnail = None
        self.probe_worker = None
        self.probe2_worker = None
        self.ffmpeg_worker = None

        if not os.path.isdir(self.tmp_dir):
            os.makedirs(self.tmp_dir)

        self.do_thumbnail = self.params.get('thumbnail', False)
        self.do_count_frames = self.params.get('count_frames', False)
        self.do_count_packets = self.params.get('count_packets', False)

    def _setup(self):

        self.input_file = self.get_input_resource(1).get('path')
        self.thumbnail = os.path.join(self.tmp_dir, 'thumbnail.jpg')

        self.probe_worker = self._new_worker(FFprobeWorker)
        self.probe_worker.add_input_file(self.input_file)
        self.workers.append(self.probe_worker)

        if self.do_thumbnail:
            self.ffmpeg_worker = self._new_worker(FFmpegWorker)
            self.ffmpeg_worker.add_input_file(self.input_file)
            self.ffmpeg_worker.add_output_file(self.thumbnail)
            self.ffmpeg_worker.make_thumbnail()
            self.workers.append(self.ffmpeg_worker)
            self.add_output_resource('thumbnail', self.thumbnail)

        if self.do_count_frames or self.do_count_packets:
            self.probe2_worker = self._new_worker(FFprobeWorker)
            self.probe2_worker.add_input_file(self.input_file)
            if self.do_count_packets:
                self.probe2_worker.count_packets()
            if self.do_count_frames:
                self.probe2_worker.count_frames()
            self.workers.append(self.probe2_worker)

    def _callback(self, worker, user_callback):
        if worker in [self.probe_worker, self.probe2_worker]:
            self.update_metadata(worker.metadata)
        Action._callback(self, worker, user_callback)

    def _execute(self, callback):
        has_video_streams = False
        self.worker_count = len(self.workers)
        for self.worker_idx, worker in enumerate(self.workers):

            # If no video streams are detected, skip thumbnail
            if not has_video_streams and worker == self.ffmpeg_worker:
                continue

            self._execute_worker(worker, callback)
            if worker == self.probe_worker:
                has_video_streams = self.probe_worker.metadata['format']['nb_video_streams'] > 0
            elif worker == self.ffmpeg_worker:
                self.update_metadata({
                    'thumbnail': self.thumbnail,
                })

        self.progress = 100
        self._callback(self.workers[-1], callback)

    def _finalize(self):
        pass

    def run(self, callback=None):
        Action.run(self, callback)
        return AVInfo(self.get_metadata())

<|endoftext|>


<commit_before>#!/usr/bin/env python

from Tkinter import *
from ttk import *
import Tkconstants, tkFileDialog, tkSimpleDialog
import boto3
from os.path import expanduser
import os
import thread
import ConfigParser

class Uploader(Frame):
    def __init__(self, root, filename):
        Frame.__init__(self, root)

        self._filename = filename
        self._size = float(os.path.getsize(filename))
        self._seen_so_far = 0

        self._thread_should_exit = False

        Label(self, text="{}".format(filename)).grid(row=0, column=0, sticky=W, padx=10, pady=2)
        self.progress = Progressbar(self, orient='horizontal', mode='determinate')
        self.progress.grid(row=0, column=1, padx=10, pady=2)

        self.percent_label = Label(self, text="?", font="Sans 8")
        self.percent_label.grid(row=0, column=2, padx=4, pady=2)

        self.cancel_button = Button(self, text="Cancel", command=self.cancel_transfer)
        self.cancel_button.grid(row=0, column=3, padx=10, pady=2)

    def update_progress(self, bytes_amount):
        self._seen_so_far += bytes_amount
        percentage = (self._seen_so_far / self._size) * 100

        self.percent = int(percentage)
        self.progress["value"] = self.percent
        self.percent_label["text"] = "{}%".format(self.percent)
        if self._thread_should_exit:
            self.cancel_button["text"] = "Clear"
            thread.exit()

        if self.percent >= 100:
            self.cancel_button["text"] = "Clear"

    def _do_transfer(self, aws_access_key_id, aws_secret_access_key, filename, bucket, key):
        aws_session = boto3.session.Session(aws_access_key_id, aws_secret_access_key)
        cacert = os.environ.get('AWS_CACERT', None)
        s3 = aws_session.client('s3', verify=cacert)
        s3.upload_file(filename, bucket, key, Callback=self.update_progress)

    def do_transfer(self, *args):
        thread.start_new_thread(self._do_transfer, args)

    def cancel_transfer(self):
        if self.cancel_button["text"] == "Cancel":
            self._thread_should_exit = True
        elif self.cancel_button["text"] == "Clear":
            self.grid_forget()

class S3FileUploader(Frame):

    def __init__(self, root, config_file):
        Frame.__init__(self, root)

        r = self.r = 0
        self.error_row = r
        padx = 10
        pady = 2

        r+=1
        Label(self, text="S3 Uploader", font="Sans 16 bold").grid(row=r, columnspan=2, pady=pady)

        r+=1
        Label(self, text="AWS AccessKeyID:").grid(row=r, sticky=W, padx=padx, pady=pady)
        self.aws_access_key_id = StringVar(self)
        Entry(self, textvariable=self.aws_access_key_id).grid(row=r, column=1, sticky=W, padx=padx, pady=pady)

        r+=1
        Label(self, text="AWS AccessSecretKey:").grid(row=r, sticky=W, padx=padx, pady=pady)
        self.aws_secret_access_key = StringVar(self)
        Entry(self, textvariable=self.aws_secret_access_key).grid(row=r, column=1, sticky=W, padx=padx, pady=pady)

        r+=1
        Label(self, text="File:").grid(row=r, sticky=W, padx=padx, pady=pady)
        self.file_button = Button(self, text='Choose Local File...', command=self.askopenfilename)
        self.file_button.grid(row=r, column=1, sticky=W, padx=padx, pady=pady)

        r+=1
        Label(self, text="S3 Bucket:").grid(row=r, sticky=W, padx=padx, pady=pady)
        self.s3_bucket = StringVar(self)
        self.s3_bucket.set("public")
        self.s3_bucket_list = OptionMenu(self, self.s3_bucket, "org.cdfx.foobar", "secured", "secured", "public")
        self.s3_bucket_list.grid(row=r, column=1, sticky=W, padx=padx, pady=pady)

        r+=1
        Label(self, text="S3 Filename:").grid(row=r, sticky=W, padx=padx, pady=pady)
        self.s3_name = StringVar(self)
        Entry(self, textvariable=self.s3_name).grid(row=r, column=1, sticky=W, padx=padx, pady=pady)

        r+=1
        Button(self, text='START UPLOAD', command=self.upload_to_s3).grid(row=r, columnspan=2, padx=padx, pady=pady)

        r+=1
        Separator(self, orient="horizontal").grid(row=r, columnspan=2, padx=padx, pady=pady)

        self.r = r

        self.config_file = config_file
        self.load_config(config_file)
        self.filename = None

    def prompt_load_config(self):
        initialdir = self.config_file if self.config_file else "~/"
        config_filename = tkFileDialog.askopenfilename(parent=self, initialdir=expanduser(initialdir), title="Choose config file") #, filetypes=[('cfg files', '.cfg')])
        if config_filename:
            self.load_config(config_filename)

    def load_config(self, config_file):
        config = ConfigParser.ConfigParser()
        config_file_handle = expanduser(config_file)
        config.read(config_file_handle)
        try:
            aws_access_key_id = config.get("s3_uploader", "aws_access_key_id")
            self.aws_access_key_id.set(aws_access_key_id)

            aws_secret_access_key = config.get("s3_uploader", "aws_secret_access_key")
            self.aws_secret_access_key.set(aws_secret_access_key)

            s3_buckets = config.get("s3_uploader", "s3_buckets")
            s3_buckets = s3_buckets.split(',')
            self.s3_bucket_list.set_menu(s3_buckets[0], *(s3_buckets))

            self.config_file = config_file
        except ConfigParser.NoSectionError:
            print "Failed to read Config File, missing section 's3_uploader'"

    def askopenfilename(self):
        # define options for opening or saving a file
        self.file_opt = options = {}
        #options['defaultextension'] = '' #'.csv'
        #options['filetypes'] = [('all files', '.*'), ('text files', '.csv')]
        options['initialdir'] = expanduser("~")
        #options['initialfile'] = ''
        options['parent'] = self
        options['title'] = 'Choose File to upload'

        # get filename
        self.filename = tkFileDialog.askopenfilename(**self.file_opt)

        # open file on your own
        if self.filename:
            self.file_button["text"] = self.filename

    def show_errors(self, errors):

        self.errors = Frame(self)
        for error in errors:
            Label(self.errors, text=error, style='Error.TLabel').pack(fill=BOTH)

        self.errors.grid(row=self.error_row, columnspan=2, pady=20)

    def upload_to_s3(self):
        if hasattr(self, 'errors'):
            self.errors.grid_forget()

        aws_access_key_id = self.aws_access_key_id.get()
        aws_secret_access_key = self.aws_secret_access_key.get()
        filename = self.filename
        bucket = self.s3_bucket.get()
        name = self.s3_name.get()

        errors = []
        if not aws_access_key_id:
            errors.append(u"\u26A0 AWS Access Key ID is required")
        if not aws_secret_access_key:
            errors.append(u"\u26A0 AWS Access Secret Key is required")
        if not filename:
            errors.append(u"\u26A0 A File must be chosen for upload")
        if not bucket:
            errors.append(u"\u26A0 Choose an S3 bucket to upload the file to")
        if not name:
            errors.append(u"\u26A0 Enter a S3 filename to upload the file as")

        if errors:
            self.show_errors(errors)
            return

        self.r += 1
        up = Uploader(self, filename)
        up.grid(row=self.r, columnspan=2, sticky=NSEW)

        up.do_transfer(aws_access_key_id, aws_secret_access_key, filename, bucket, name)

class AboutWin(tkSimpleDialog.Dialog):
    def __init__(self, root):
        self._parent = root

    def show(self):
        tkSimpleDialog.Dialog.__init__(self, self._parent, "About S3 Uploader")

    def body(self, master):
        fr = Frame(master)
        Label(fr, text="S3 Uploader", font="Sans 14 bold").pack(padx=50)
        Label(fr, text="by Charles Daniel", font="Sans 10").pack(padx=50)
        Button(fr, text="OK", command=self.destroy).pack(padx=50, pady=10)
        fr.pack()

    def destroy(self):
        tkSimpleDialog.Dialog.destroy(self)

    def buttonbox(self):
        pass

def main(config_file):
    root = Tk()
    s = Style(root)
    s.configure('Error.TLabel', foreground='red', background='yellow')
    s.configure('.', padx=10, pady=10)

    root.title("S3 File Uploader")
    root['background'] = '#ececec'

    about_win = AboutWin(root)
    s3uploader_win = S3FileUploader(root, config_file)
    s3uploader_win.pack()

    menubar = Menu(root)
    filemenu = Menu(menubar, tearoff=0)
    filemenu.add_command(label="About", command=about_win.show)
    filemenu.add_separator()
    filemenu.add_command(label="Open Config...", command=s3uploader_win.prompt_load_config)
    filemenu.add_separator()
    filemenu.add_command(label="Exit", command=root.quit)
    menubar.add_cascade(label="S3Uploader", menu=filemenu)
    root.config(menu=menubar)

    root.mainloop()

if __name__=='__main__':
    main("~/s3_uploader.cfg")
<commit_msg>Report errors in transfers<commit_after>#!/usr/bin/env python

from Tkinter import *
from ttk import *
import Tkconstants, tkFileDialog, tkSimpleDialog
import boto3
import botocore
from os.path import expanduser
import os
import thread
import ConfigParser

class Uploader(Frame):
    def __init__(self, root, filename, bucket_name, s3_filename):
        Frame.__init__(self, root)

        self._filename = filename
        self._bucket_name = bucket_name
        self._s3_filename = s3_filename
        self._size = float(os.path.getsize(filename))
        self._seen_so_far = 0

        self._thread_should_exit = False

        Label(self, text="{}/{}".format(bucket_name, s3_filename)).grid(row=0, column=0, sticky=W, padx=10, pady=2)
        self.progress = Progressbar(self, orient='horizontal', mode='determinate')
        self.progress.grid(row=0, column=1, padx=10, pady=2)

        self.percent_label = Label(self, text="?", font="Sans 8")
        self.percent_label.grid(row=0, column=2, padx=4, pady=2)

        self.cancel_button = Button(self, text="Cancel", command=self.cancel_transfer)
        self.cancel_button.grid(row=0, column=3, padx=10, pady=2)

    def update_progress(self, bytes_amount):
        self._seen_so_far += bytes_amount
        percentage = (self._seen_so_far / self._size) * 100

        self.percent = int(percentage)
        self.progress["value"] = self.percent
        self.percent_label["text"] = "{}%".format(self.percent)
        if self._thread_should_exit:
            self.cancel_button["text"] = "Clear"
            thread.exit()

        if self.percent >= 100:
            self.cancel_button["text"] = "Clear"

    def _do_transfer(self, aws_access_key_id, aws_secret_access_key):
        try:
            aws_session = boto3.session.Session(aws_access_key_id, aws_secret_access_key)
            cacert = os.environ.get('AWS_CACERT', None)
            s3 = aws_session.client('s3', verify=cacert)
            s3.upload_file(self._filename, self._bucket_name, self._s3_filename, Callback=self.update_progress)
        except botocore.exceptions.ClientError as e:
            print "ERROR ", e.message
            self.show_error([e.message])

    def show_error(self, errors):
        self.errors = Frame(self)
        for error in errors:
            Label(self.errors, text=error, style='Error.TLabel', font="Sans 8").pack(fill=BOTH)

        self.errors.grid(row=1, columnspan=4, padx=10)
        self.cancel_button["text"] = "Clear"

    def do_transfer(self, *args):
        thread.start_new_thread(self._do_transfer, args)

    def cancel_transfer(self):
        if self.cancel_button["text"] == "Cancel":
            self._thread_should_exit = True
        elif self.cancel_button["text"] == "Clear":
            self.grid_forget()

class S3FileUploader(Frame):

    def __init__(self, root, config_file):
        Frame.__init__(self, root)

        r = self.r = 0
        self.error_row = r
        padx = 10
        pady = 2

        r+=1
        Label(self, text="S3 Uploader", font="Sans 16 bold").grid(row=r, columnspan=2, pady=pady)

        r+=1
        Label(self, text="AWS AccessKeyID:").grid(row=r, sticky=W, padx=padx, pady=pady)
        self.aws_access_key_id = StringVar(self)
        Entry(self, textvariable=self.aws_access_key_id).grid(row=r, column=1, sticky=W, padx=padx, pady=pady)

        r+=1
        Label(self, text="AWS AccessSecretKey:").grid(row=r, sticky=W, padx=padx, pady=pady)
        self.aws_secret_access_key = StringVar(self)
        Entry(self, textvariable=self.aws_secret_access_key).grid(row=r, column=1, sticky=W, padx=padx, pady=pady)

        r+=1
        Label(self, text="File:").grid(row=r, sticky=W, padx=padx, pady=pady)
        self.file_button = Button(self, text='Choose Local File...', command=self.askopenfilename)
        self.file_button.grid(row=r, column=1, sticky=W, padx=padx, pady=pady)

        r+=1
        Label(self, text="S3 Bucket:").grid(row=r, sticky=W, padx=padx, pady=pady)
        self.s3_bucket = StringVar(self)
        self.s3_bucket_list = OptionMenu(self, self.s3_bucket)
        self.s3_bucket_list.grid(row=r, column=1, sticky=W, padx=padx, pady=pady)

        r+=1
        Label(self, text="S3 Filename:").grid(row=r, sticky=W, padx=padx, pady=pady)
        self.s3_name = StringVar(self)
        Entry(self, textvariable=self.s3_name).grid(row=r, column=1, sticky=W, padx=padx, pady=pady)

        r+=1
        Button(self, text='START UPLOAD', command=self.upload_to_s3).grid(row=r, columnspan=2, padx=padx, pady=pady)

        r+=1
        Separator(self, orient="horizontal").grid(row=r, columnspan=2, padx=padx, pady=pady)

        self.r = r

        self.errors = None
        self.config_file = config_file
        self.load_config(config_file)
        self.filename = None

    def prompt_load_config(self):
        initialdir = self.config_file if self.config_file else "~/"
        config_filename = tkFileDialog.askopenfilename(parent=self, initialdir=expanduser(initialdir), title="Choose config file") #, filetypes=[('cfg files', '.cfg')])
        if config_filename:
            self.load_config(config_filename)

    def load_config(self, config_file):
        self.clear_errors()
        config = ConfigParser.ConfigParser()
        config_file_handle = expanduser(config_file)
        config.read(config_file_handle)
        try:
            aws_access_key_id = config.get("s3_uploader", "aws_access_key_id")
            self.aws_access_key_id.set(aws_access_key_id)

            aws_secret_access_key = config.get("s3_uploader", "aws_secret_access_key")
            self.aws_secret_access_key.set(aws_secret_access_key)

            s3_buckets = config.get("s3_uploader", "s3_buckets")
            s3_buckets = s3_buckets.split(',')
            self.s3_bucket_list.set_menu(s3_buckets[0], *(s3_buckets))

            self.config_file = config_file
        except ConfigParser.NoSectionError:
            print "Failed to read Config File, missing section 's3_uploader'"
            self.show_errors([u"\u26A0 Failed to read Config File, missing section 's3_uploader'"])

    def askopenfilename(self):
        # define options for opening or saving a file
        self.file_opt = options = {}
        #options['defaultextension'] = '' #'.csv'
        #options['filetypes'] = [('all files', '.*'), ('text files', '.csv')]
        options['initialdir'] = expanduser("~")
        #options['initialfile'] = ''
        options['parent'] = self
        options['title'] = 'Choose File to upload'

        # get filename
        self.filename = tkFileDialog.askopenfilename(**self.file_opt)

        # open file on your own
        if self.filename:
            self.file_button["text"] = self.filename

    def show_errors(self, errors):
        self.errors = Frame(self)
        for error in errors:
            Label(self.errors, text=error, style='Error.TLabel').pack(fill=BOTH)

        self.errors.grid(row=self.error_row, columnspan=2, pady=20)

    def clear_errors(self):
        if self.errors:
            self.errors.destroy()
            self.errors = None

    def upload_to_s3(self):
        self.clear_errors()

        aws_access_key_id = self.aws_access_key_id.get()
        aws_secret_access_key = self.aws_secret_access_key.get()
        filename = self.filename
        bucket = self.s3_bucket.get()
        name = self.s3_name.get()

        errors = []
        if not aws_access_key_id:
            errors.append(u"\u26A0 AWS Access Key ID is required")
        if not aws_secret_access_key:
            errors.append(u"\u26A0 AWS Access Secret Key is required")
        if not filename:
            errors.append(u"\u26A0 A File must be chosen for upload")
        if not bucket:
            errors.append(u"\u26A0 Choose an S3 bucket to upload the file to")
        if not name:
            errors.append(u"\u26A0 Enter a S3 filename to upload the file as")

        if errors:
            self.show_errors(errors)
            return

        self.r += 1
        up = Uploader(self, filename, bucket, name)
        up.grid(row=self.r, columnspan=2, sticky=NSEW)

        up.do_transfer(aws_access_key_id, aws_secret_access_key)

class AboutWin(tkSimpleDialog.Dialog):
    def __init__(self, root):
        self._parent = root

    def show(self):
        tkSimpleDialog.Dialog.__init__(self, self._parent, "About S3 Uploader")

    def body(self, master):
        fr = Frame(master)
        Label(fr, text="S3 Uploader", font="Sans 14 bold").pack(padx=50)
        Label(fr, text="by Charles Daniel", font="Sans 10").pack(padx=50)
        Button(fr, text="OK", command=self.destroy).pack(padx=50, pady=10)
        fr.pack()

    def destroy(self):
        tkSimpleDialog.Dialog.destroy(self)

    def buttonbox(self):
        pass

def main(config_file):
    root = Tk()
    s = Style(root)
    s.configure('Error.TLabel', foreground='red', background='yellow')
    s.configure('.', padx=10, pady=10)

    root.title("S3 File Uploader")
    root['background'] = '#ececec'

    about_win = AboutWin(root)
    s3uploader_win = S3FileUploader(root, config_file)
    s3uploader_win.pack()

    menubar = Menu(root)
    filemenu = Menu(menubar, tearoff=0)
    filemenu.add_command(label="About", command=about_win.show)
    filemenu.add_separator()
    filemenu.add_command(label="Open Config...", command=s3uploader_win.prompt_load_config)
    filemenu.add_separator()
    filemenu.add_command(label="Exit", command=root.quit)
    menubar.add_cascade(label="S3Uploader", menu=filemenu)
    root.config(menu=menubar)

    root.mainloop()

if __name__=='__main__':
    main("~/s3_uploader.cfg")
<|endoftext|>


<commit_before>"""Support for Freebox devices (Freebox v6 and Freebox mini 4K)."""
from __future__ import annotations

import logging
from typing import Any

from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
    DATA_RATE_KILOBYTES_PER_SECOND,
    DEVICE_CLASS_TEMPERATURE,
    TEMP_CELSIUS,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import DeviceInfo
import homeassistant.util.dt as dt_util

from .const import CALL_SENSORS, CONNECTION_SENSORS, DISK_PARTITION_SENSORS, DOMAIN
from .router import FreeboxRouter

_LOGGER = logging.getLogger(__name__)

async def async_setup_entry(
    hass: HomeAssistant, entry: ConfigEntry, async_add_entities
) -> None:
    """Set up the sensors."""
    router = hass.data[DOMAIN][entry.unique_id]
    entities = []

    _LOGGER.debug(
        "%s - %s - %s temperature sensors",
        router.name,
        router.mac,
        len(router.sensors_temperature),
    )
    entities = [
        FreeboxSensor(
            router,
            SensorEntityDescription(
                key=sensor_name,
                name=f"Freebox {sensor_name}",
                native_unit_of_measurement=TEMP_CELSIUS,
                device_class=DEVICE_CLASS_TEMPERATURE,
            ),
        )
        for sensor_name in router.sensors_temperature
    ]

    entities.extend(
        [FreeboxSensor(router, description) for description in CONNECTION_SENSORS]
    )
    entities.extend(
        [FreeboxCallSensor(router, description) for description in CALL_SENSORS]
    )

    _LOGGER.debug("%s - %s - %s disk(s)", router.name, router.mac, len(router.disks))
    entities.extend(
        FreeboxDiskSensor(router, disk, partition, description)
        for disk in router.disks.values()
        for partition in disk["partitions"]
        for description in DISK_PARTITION_SENSORS
    )

    async_add_entities(entities, True)

class FreeboxSensor(SensorEntity):
    """Representation of a Freebox sensor."""

    _attr_should_poll = False

    def __init__(
        self, router: FreeboxRouter, description: SensorEntityDescription
    ) -> None:
        """Initialize a Freebox sensor."""
        self.entity_description = description
        self._router = router
        self._attr_unique_id = f"{router.mac} {description.name}"

    @callback
    def async_update_state(self) -> None:
        """Update the Freebox sensor."""
        state = self._router.sensors[self.entity_description.key]
        if self.native_unit_of_measurement == DATA_RATE_KILOBYTES_PER_SECOND:
            self._attr_native_value = round(state / 1000, 2)
        else:
            self._attr_native_value = state

    @property
    def device_info(self) -> DeviceInfo:
        """Return the device information."""
        return self._router.device_info

    @callback
    def async_on_demand_update(self):
        """Update state."""
        self.async_update_state()
        self.async_write_ha_state()

    async def async_added_to_hass(self):
        """Register state update callback."""
        self.async_update_state()
        self.async_on_remove(
            async_dispatcher_connect(
                self.hass,
                self._router.signal_sensor_update,
                self.async_on_demand_update,
            )
        )

class FreeboxCallSensor(FreeboxSensor):
    """Representation of a Freebox call sensor."""

    def __init__(
        self, router: FreeboxRouter, description: SensorEntityDescription
    ) -> None:
        """Initialize a Freebox call sensor."""
        super().__init__(router, description)
        self._call_list_for_type = []

    @callback
    def async_update_state(self) -> None:
        """Update the Freebox call sensor."""
        self._call_list_for_type = []
        if self._router.call_list:
            for call in self._router.call_list:
                if not call["new"]:
                    continue
                if self.entity_description.key == call["type"]:
                    self._call_list_for_type.append(call)

        self._attr_native_value = len(self._call_list_for_type)

    @property
    def extra_state_attributes(self) -> dict[str, Any]:
        """Return device specific state attributes."""
        return {
            dt_util.utc_from_timestamp(call["datetime"]).isoformat(): call["name"]
            for call in self._call_list_for_type
        }

class FreeboxDiskSensor(FreeboxSensor):
    """Representation of a Freebox disk sensor."""

    def __init__(
        self,
        router: FreeboxRouter,
        disk: dict[str, Any],
        partition: dict[str, Any],
        description: SensorEntityDescription,
    ) -> None:
        """Initialize a Freebox disk sensor."""
        super().__init__(router, description)
        self._disk = disk
        self._partition = partition
        self._attr_name = f"{partition['label']} {description.name}"
        self._unique_id = f"{self._router.mac} {description.key} {self._disk['id']} {self._partition['id']}"

    @property
    def device_info(self) -> DeviceInfo:
        """Return the device information."""
        return DeviceInfo(
            identifiers={(DOMAIN, self._disk["id"])},
            model=self._disk["model"],
            name=f"Disk {self._disk['id']}",
            sw_version=self._disk["firmware"],
            via_device=(
                DOMAIN,
                self._router.mac,
            ),
        )

    @callback
    def async_update_state(self) -> None:
        """Update the Freebox disk sensor."""
        self._attr_native_value = round(
            self._partition["free_bytes"] * 100 / self._partition["total_bytes"], 2
        )
<commit_msg>Fix ZeroDivisionError on freebox/sensor (#57077)<commit_after>"""Support for Freebox devices (Freebox v6 and Freebox mini 4K)."""
from __future__ import annotations

import logging
from typing import Any

from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
    DATA_RATE_KILOBYTES_PER_SECOND,
    DEVICE_CLASS_TEMPERATURE,
    TEMP_CELSIUS,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import DeviceInfo
import homeassistant.util.dt as dt_util

from .const import CALL_SENSORS, CONNECTION_SENSORS, DISK_PARTITION_SENSORS, DOMAIN
from .router import FreeboxRouter

_LOGGER = logging.getLogger(__name__)

async def async_setup_entry(
    hass: HomeAssistant, entry: ConfigEntry, async_add_entities
) -> None:
    """Set up the sensors."""
    router = hass.data[DOMAIN][entry.unique_id]
    entities = []

    _LOGGER.debug(
        "%s - %s - %s temperature sensors",
        router.name,
        router.mac,
        len(router.sensors_temperature),
    )
    entities = [
        FreeboxSensor(
            router,
            SensorEntityDescription(
                key=sensor_name,
                name=f"Freebox {sensor_name}",
                native_unit_of_measurement=TEMP_CELSIUS,
                device_class=DEVICE_CLASS_TEMPERATURE,
            ),
        )
        for sensor_name in router.sensors_temperature
    ]

    entities.extend(
        [FreeboxSensor(router, description) for description in CONNECTION_SENSORS]
    )
    entities.extend(
        [FreeboxCallSensor(router, description) for description in CALL_SENSORS]
    )

    _LOGGER.debug("%s - %s - %s disk(s)", router.name, router.mac, len(router.disks))
    entities.extend(
        FreeboxDiskSensor(router, disk, partition, description)
        for disk in router.disks.values()
        for partition in disk["partitions"]
        for description in DISK_PARTITION_SENSORS
    )

    async_add_entities(entities, True)

class FreeboxSensor(SensorEntity):
    """Representation of a Freebox sensor."""

    _attr_should_poll = False

    def __init__(
        self, router: FreeboxRouter, description: SensorEntityDescription
    ) -> None:
        """Initialize a Freebox sensor."""
        self.entity_description = description
        self._router = router
        self._attr_unique_id = f"{router.mac} {description.name}"

    @callback
    def async_update_state(self) -> None:
        """Update the Freebox sensor."""
        state = self._router.sensors[self.entity_description.key]
        if self.native_unit_of_measurement == DATA_RATE_KILOBYTES_PER_SECOND:
            self._attr_native_value = round(state / 1000, 2)
        else:
            self._attr_native_value = state

    @property
    def device_info(self) -> DeviceInfo:
        """Return the device information."""
        return self._router.device_info

    @callback
    def async_on_demand_update(self):
        """Update state."""
        self.async_update_state()
        self.async_write_ha_state()

    async def async_added_to_hass(self):
        """Register state update callback."""
        self.async_update_state()
        self.async_on_remove(
            async_dispatcher_connect(
                self.hass,
                self._router.signal_sensor_update,
                self.async_on_demand_update,
            )
        )

class FreeboxCallSensor(FreeboxSensor):
    """Representation of a Freebox call sensor."""

    def __init__(
        self, router: FreeboxRouter, description: SensorEntityDescription
    ) -> None:
        """Initialize a Freebox call sensor."""
        super().__init__(router, description)
        self._call_list_for_type = []

    @callback
    def async_update_state(self) -> None:
        """Update the Freebox call sensor."""
        self._call_list_for_type = []
        if self._router.call_list:
            for call in self._router.call_list:
                if not call["new"]:
                    continue
                if self.entity_description.key == call["type"]:
                    self._call_list_for_type.append(call)

        self._attr_native_value = len(self._call_list_for_type)

    @property
    def extra_state_attributes(self) -> dict[str, Any]:
        """Return device specific state attributes."""
        return {
            dt_util.utc_from_timestamp(call["datetime"]).isoformat(): call["name"]
            for call in self._call_list_for_type
        }

class FreeboxDiskSensor(FreeboxSensor):
    """Representation of a Freebox disk sensor."""

    def __init__(
        self,
        router: FreeboxRouter,
        disk: dict[str, Any],
        partition: dict[str, Any],
        description: SensorEntityDescription,
    ) -> None:
        """Initialize a Freebox disk sensor."""
        super().__init__(router, description)
        self._disk = disk
        self._partition = partition
        self._attr_name = f"{partition['label']} {description.name}"
        self._unique_id = f"{self._router.mac} {description.key} {self._disk['id']} {self._partition['id']}"

    @property
    def device_info(self) -> DeviceInfo:
        """Return the device information."""
        return DeviceInfo(
            identifiers={(DOMAIN, self._disk["id"])},
            model=self._disk["model"],
            name=f"Disk {self._disk['id']}",
            sw_version=self._disk["firmware"],
            via_device=(
                DOMAIN,
                self._router.mac,
            ),
        )

    @callback
    def async_update_state(self) -> None:
        """Update the Freebox disk sensor."""
        value = None
        if self._partition.get("total_bytes"):
            value = round(
                self._partition["free_bytes"] * 100 / self._partition["total_bytes"], 2
            )
        self._attr_native_value = value
<|endoftext|>


<commit_before>"""Experimental code for new features."""
<commit_msg>Issue #111: added one-liner summary to niftynet.contrib module<commit_after>"""

.. module:: niftynet.contrib
   :synopsis: Experimental code for new features.

"""
<|endoftext|>


<commit_before>import sys
import os
import re

import sorter
import shell
from gitFunctions import Commiter
import shouter

class RTCInitializer:
    @staticmethod
    def initialize(config):
        RTCInitializer.loginandcollectstreamuuid(config)
        workspace = WorkspaceHandler(config)
        if config.useexistingworkspace:
            shouter.shout("Use existing workspace to start migration")
            workspace.load()
        else:
            workspace.createandload(config.streamuuid, config.initialcomponentbaselines)

    @staticmethod
    def loginandcollectstreamuuid(config):
        shell.execute("%s login -r %s -u %s -P %s" % (config.scmcommand, config.repo, config.user, config.password))
        config.collectstreamuuid()

class WorkspaceHandler:
    def __init__(self, config):
        self.config = config
        self.workspace = config.workspace
        self.repo = config.repo
        self.scmcommand = config.scmcommand

    def createandload(self, stream, componentbaselineentries=[]):
        shell.execute("%s create workspace -r %s -s %s %s" % (self.scmcommand, self.repo, stream, self.workspace))
        if componentbaselineentries:
            self.setcomponentstobaseline(componentbaselineentries, stream)
        else:
            self.setcomponentstobaseline(ImportHandler(self.config).determineinitialbaseline(stream),
                                         stream)
        self.load()

    def load(self):
        command = "%s load -r %s %s --force" % (self.scmcommand, self.repo, self.workspace)
        shouter.shout("Start (re)loading current workspace: " + command)
        shell.execute(command)
        shouter.shout("Load of workspace finished")

    def setcomponentstobaseline(self, componentbaselineentries, streamuuid):
        for entry in componentbaselineentries:
            shouter.shout("Set component '%s'(%s) to baseline '%s' (%s)" % (entry.componentname, entry.component,
                                                                            entry.baselinename, entry.baseline))

            replacecommand = "%s set component -r %s -b %s %s stream %s %s --overwrite-uncommitted" % \
                             (self.scmcommand, self.repo, entry.baseline, self.workspace, streamuuid, entry.component)
            shell.execute(replacecommand)

    def setnewflowtargets(self, streamuuid):
        shouter.shout("Set new Flowtargets")
        if not self.hasflowtarget(streamuuid):
            shell.execute("%s add flowtarget -r %s %s %s" % (self.scmcommand, self.repo, self.workspace, streamuuid))

        command = "%s set flowtarget -r %s %s --default --current %s" % (self.scmcommand, self.repo, self.workspace, streamuuid)
        shell.execute(command)

    def hasflowtarget(self, streamuuid):
        command = "%s --show-uuid y --show-alias n list flowtargets -r %s %s" % (self.scmcommand, self.repo, self.workspace)
        flowtargetlines = shell.getoutput(command)
        for flowtargetline in flowtargetlines:
            splittedinformationline = flowtargetline.split("\"")
            uuidpart = splittedinformationline[0].split(" ")
            flowtargetuuid = uuidpart[0].strip()[1:-1]
            if streamuuid in flowtargetuuid:
                return True
        return False

class Changes:

    latest_accept_command = ""

    @staticmethod
    def discard(config, *changeentries):
        idstodiscard = Changes._collectids(changeentries)
        shell.execute(config.scmcommand + " discard -w " + config.workspace + " -r " + config.repo + " -o" + idstodiscard)

    @staticmethod
    def accept(config, logpath, *changeentries):
        for changeEntry in changeentries:
            shouter.shout("Accepting: " + changeEntry.tostring())
        revisions = Changes._collectids(changeentries)
        Changes.latest_accept_command = config.scmcommand + " accept -v -o -r " + config.repo + " -t " + \
                                        config.workspace + " --changes" + revisions
        return shell.execute(Changes.latest_accept_command, logpath, "a")

    @staticmethod
    def _collectids(changeentries):
        ids = ""
        for changeentry in changeentries:
            ids += " " + changeentry.revision
        return ids

    @staticmethod
    def arereleatedmergechangesets(changeentry1, changeentry2):
        if changeentry1 and changeentry2:
            if changeentry1.author == changeentry2.author or "merge" in changeentry2.comment.lower():
                return True
        return False

    @staticmethod
    def tostring(*changes):
        logmessage = "Changes: \n"
        for change in changes:
            logmessage += change.tostring() + "\n"
        shouter.shout(logmessage)

class ImportHandler:
    def __init__(self, config):
        self.config = config
        self.acceptlogpath = config.getlogpath("accept.txt")

    def getcomponentbaselineentriesfromstream(self, stream):
        filename = self.config.getlogpath("StreamComponents_" + stream + ".txt")
        command = "%s --show-alias n --show-uuid y list components -v -m 30 -r %s %s" % (self.config.scmcommand,
                                                                                self.config.repo, stream)
        shell.execute(command, filename)
        componentbaselinesentries = []
        skippedfirstrow = False
        islinewithcomponent = 2
        component = ""
        baseline = ""
        componentname = ""
        baselinename = ""
        with open(filename, 'r', encoding=shell.encoding) as file:
            for line in file:
                cleanedline = line.strip()
                if cleanedline:
                    if not skippedfirstrow:
                        skippedfirstrow = True
                        continue
                    splittedinformationline = line.split("\"")
                    uuidpart = splittedinformationline[0].split(" ")
                    if islinewithcomponent % 2 is 0:
                        component = uuidpart[3].strip()[1:-1]
                        componentname = splittedinformationline[1]
                    else:
                        baseline = uuidpart[5].strip()[1:-1]
                        baselinename = splittedinformationline[1]

                    if baseline and component:
                        componentbaselinesentries.append(
                            ComponentBaseLineEntry(component, baseline, componentname, baselinename))
                        baseline = ""
                        component = ""
                        componentname = ""
                        baselinename = ""
                    islinewithcomponent += 1
        return componentbaselinesentries

    def determineinitialbaseline(self, stream):
        regex = "\(_\w+\)"
        pattern = re.compile(regex)
        config = self.config
        componentbaselinesentries = self.getcomponentbaselineentriesfromstream(stream)
        for entry in componentbaselinesentries:
            shouter.shout("Determine initial baseline of " + entry.componentname)
            # use always scm, lscm fails when specifying maximum over 10k
            command = "scm --show-alias n --show-uuid y list baselines --components %s -r %s -u %s -p %s -m 20000" % \
                      (entry.component, config.user, config.password, config.repo)
            baselineslines = shell.getoutput(command)
            baselineslines.reverse()  # reverse to have earliest baseline on top

            for baselineline in baselineslines:
                matcher = pattern.search(baselineline)
                if matcher:
                    matchedstring = matcher.group()
                    uuid = matchedstring[1:-1]
                    entry.baseline = uuid
                    entry.baselinename = "Automatically detected initial baseline"
                    break
        return componentbaselinesentries

    def acceptchangesintoworkspace(self, changeentries):
        amountofchanges = len(changeentries)
        shouter.shoutwithdate("Start accepting %s changesets" % amountofchanges)
        amountofacceptedchanges = 0
        changestoskip = 0
        reloaded = False
        for changeEntry in changeentries:
            amountofacceptedchanges += 1
            if changestoskip > 0:
                shouter.shout("Skipping " + changeEntry.tostring())
                changestoskip -= 1
                continue
            acceptedsuccesfully = Changes.accept(self.config, self.acceptlogpath,
                                                 changeEntry) is 0
            if not acceptedsuccesfully:
                shouter.shout("Change wasnt succesfully accepted into workspace")
                changestoskip = self.retryacceptincludingnextchangesets(changeEntry, changeentries)
            elif not reloaded:
                if self.is_reloading_necessary():
                    WorkspaceHandler(self.config).load()
                reloaded = True
            shouter.shout("Accepted change %s/%s into working directory" % (amountofacceptedchanges, amountofchanges))
            Commiter.addandcommit(changeEntry)

    @staticmethod
    def is_reloading_necessary():
        return shell.execute("git diff --exit-code") is 0

    @staticmethod
    def collect_changes_to_accept_to_avoid_conflicts(changewhichcantacceptedallone, changes):
        changestoaccept = [changewhichcantacceptedallone]
        nextchange = ImportHandler.getnextchangeset(changewhichcantacceptedallone, changes)

        while True:
            if Changes.arereleatedmergechangesets(changewhichcantacceptedallone, nextchange):
                changestoaccept.append(nextchange)
                nextchange = ImportHandler.getnextchangeset(nextchange, changes)
            else:
                break
        return changestoaccept

    def retryacceptincludingnextchangesets(self, change, changes):
        changestoskip = 0
        changestoaccept = ImportHandler.collect_changes_to_accept_to_avoid_conflicts(change, changes)
        amountofchangestoaccept = len(changestoaccept)

        if amountofchangestoaccept > 1:
            Changes.tostring(*changestoaccept)
            if self.config.useautomaticconflictresolution or self.is_user_agreeing_to_accept_next_change(change):
                for index in range(1, amountofchangestoaccept):
                    toaccept = changestoaccept[0:index + 1]  # accept least possible amount of changes
                    if Changes.accept(self.config, self.acceptlogpath, *toaccept) is 0:
                        changestoskip = len(toaccept) - 1  # initialchange shouldnt be skipped
                        break
                    else:
                        Changes.discard(self.config, *toaccept)  # revert initial state
        return changestoskip

    def is_user_agreeing_to_accept_next_change(self, change):
        messagetoask = "Press Y for accepting following changes, press N to skip"
        if not self.config.useautomaticconflictresolution:
            while True:
                answer = input(messagetoask).lower()
                if answer == "y":
                    return True
                elif answer == "n":
                    shouter.shout("Last executed command: \n" + Changes.latest_accept_command)
                    shouter.shout("Apropriate git commit command \n" + Commiter.getcommitcommand(change))
                    reallycontinue = "Do you want to continue? Y for continue, any key for abort"
                    if input(reallycontinue).lower() == "y":
                        return False
                    else:
                        sys.exit("Please check the output/log and rerun program with resume")
                else:
                    shouter.shout("Please answer with Y/N, input was " + answer)

    @staticmethod
    def getnextchangeset(currentchangeentry, changeentries):
        nextchangeentry = None
        nextindex = changeentries.index(currentchangeentry) + 1
        has_next_changeset = nextindex is not len(changeentries)
        if has_next_changeset:
            nextchangeentry = changeentries[nextindex]
        return nextchangeentry

    def getchangeentriesofstreamcomponents(self, componentbaselineentries):
        missingchangeentries = {}
        shouter.shout("Start collecting changeentries")
        changeentriesbycomponentbaselineentry = {}
        for componentBaseLineEntry in componentbaselineentries:
            changeentries = self.getchangeentriesofbaseline(componentBaseLineEntry.baseline)
            for changeentry in changeentries:
                missingchangeentries[changeentry.revision] = changeentry
        return missingchangeentries

    def readhistory(self, componentbaselineentries, streamname):
        if not self.config.useprovidedhistory:
            warning = "Warning - UseProvidedHistory is set to false, merge-conflicts are more likely to happen. \n " \
                      "For more information see https://github.com/WtfJoke/rtc2git/wiki/Getting-your-History-Files"
            shouter.shout(warning)
            return None
        historyuuids = {}
        shouter.shout("Start reading history files")
        for componentBaseLineEntry in componentbaselineentries:
            history = self.gethistory(componentBaseLineEntry.componentname, streamname)
            historyuuids[componentBaseLineEntry.component] = history
        return historyuuids

    @staticmethod
    def getchangeentriestoaccept(missingchangeentries, history):
        changeentriestoaccept = []
        if history:
            historywithchangeentryobject = {}
            for key in history.keys():
                currentuuids = history.get(key)
                changeentries = []
                for uuid in currentuuids:
                    changeentry = missingchangeentries.get(uuid)
                    if changeentry:
                        changeentries.append(changeentry)
                historywithchangeentryobject[key] = changeentries
            changeentriestoaccept = sorter.tosortedlist(historywithchangeentryobject)
        else:
            changeentriestoaccept.extend(missingchangeentries.values())
            # simple sort by date - same as returned by compare command
            changeentriestoaccept.sort(key=lambda change: change.date)
        return changeentriestoaccept

    @staticmethod
    def getchangeentriesfromfile(outputfilename):
        informationseparator = "@@"
        numberofexpectedinformationseparators = 5
        changeentries = []

        with open(outputfilename, 'r', encoding=shell.encoding) as file:
            currentline = ""
            currentinformationpresent = 0
            for line in file:
                cleanedline = line.strip()
                if cleanedline:
                    currentinformationpresent += cleanedline.count(informationseparator)
                    if currentline:
                        currentline += os.linesep
                    currentline += cleanedline
                    if currentinformationpresent >= numberofexpectedinformationseparators:
                        splittedlines = currentline.split(informationseparator)
                        revisionwithbrackets = splittedlines[0].strip()
                        revision = revisionwithbrackets[1:-1]
                        author = splittedlines[1].strip()
                        email = splittedlines[2].strip()
                        comment = splittedlines[3].strip()
                        date = splittedlines[4].strip()

                        changeentries.append(ChangeEntry(revision, author, email, date, comment))

                        currentinformationpresent = 0
                        currentline = ""
        return changeentries

    @staticmethod
    def getsimplehistoryfromfile(outputfilename):
        revisions = []
        if not os.path.isfile(outputfilename):
            shouter.shout("History file not found: " + outputfilename)
            shouter.shout("Skipping this part of history")
            return revisions

        with open(outputfilename, 'r', encoding=shell.encoding) as file:
            for line in file:
                revisions.append(line.strip())
        revisions.reverse()  # to begin by the oldest
        return revisions

    def getchangeentriesofbaseline(self, baselinetocompare):
        return self.getchangeentriesbytypeandvalue("baseline", baselinetocompare)

    def getchangeentriesofstream(self, streamtocompare):
        shouter.shout("Start collecting changes since baseline creation")
        missingchangeentries = {}
        changeentries = self.getchangeentriesbytypeandvalue("stream", streamtocompare)
        for changeentry in changeentries:
            missingchangeentries[changeentry.revision] = changeentry
        return missingchangeentries

    def getchangeentriesbytypeandvalue(self, comparetype, value):
        dateformat = "yyyy-MM-dd HH:mm:ss"
        outputfilename = self.config.getlogpath("Compare_" + comparetype + "_" + value + ".txt")
        comparecommand = "%s --show-alias n --show-uuid y compare ws %s %s %s -r %s -I sw -C @@{name}@@{email}@@ --flow-directions i -D @@\"%s\"@@" \
                         % (self.config.scmcommand, self.config.workspace, comparetype, value, self.config.repo, dateformat)
        shell.execute(comparecommand, outputfilename)
        return ImportHandler.getchangeentriesfromfile(outputfilename)

    def gethistory(self, componentname, streamname):
        outputfilename = self.config.gethistorypath("History_%s_%s.txt" % (componentname, streamname))
        return ImportHandler.getsimplehistoryfromfile(outputfilename)

class ChangeEntry:
    def __init__(self, revision, author, email, date, comment):
        self.revision = revision
        self.author = author
        self.email = email
        self.date = date
        self.comment = comment

    def getgitauthor(self):
        authorrepresentation = "%s <%s>" % (self.author, self.email)
        return shell.quote(authorrepresentation)

    def tostring(self):
        return self.comment + " (Date: " + self.date + ", Author: " + self.author + ", Revision: " + self.revision + ")"

class ComponentBaseLineEntry:
    def __init__(self, component, baseline, componentname, baselinename):
        self.component = component
        self.baseline = baseline
        self.componentname = componentname
        self.baselinename = baselinename
<commit_msg>show some progress by printing a message for collecting changes<commit_after>import sys
import os
import re

import sorter
import shell
from gitFunctions import Commiter
import shouter

class RTCInitializer:
    @staticmethod
    def initialize(config):
        RTCInitializer.loginandcollectstreamuuid(config)
        workspace = WorkspaceHandler(config)
        if config.useexistingworkspace:
            shouter.shout("Use existing workspace to start migration")
            workspace.load()
        else:
            workspace.createandload(config.streamuuid, config.initialcomponentbaselines)

    @staticmethod
    def loginandcollectstreamuuid(config):
        shell.execute("%s login -r %s -u %s -P %s" % (config.scmcommand, config.repo, config.user, config.password))
        config.collectstreamuuid()

class WorkspaceHandler:
    def __init__(self, config):
        self.config = config
        self.workspace = config.workspace
        self.repo = config.repo
        self.scmcommand = config.scmcommand

    def createandload(self, stream, componentbaselineentries=[]):
        shell.execute("%s create workspace -r %s -s %s %s" % (self.scmcommand, self.repo, stream, self.workspace))
        if componentbaselineentries:
            self.setcomponentstobaseline(componentbaselineentries, stream)
        else:
            self.setcomponentstobaseline(ImportHandler(self.config).determineinitialbaseline(stream),
                                         stream)
        self.load()

    def load(self):
        command = "%s load -r %s %s --force" % (self.scmcommand, self.repo, self.workspace)
        shouter.shout("Start (re)loading current workspace: " + command)
        shell.execute(command)
        shouter.shout("Load of workspace finished")

    def setcomponentstobaseline(self, componentbaselineentries, streamuuid):
        for entry in componentbaselineentries:
            shouter.shout("Set component '%s'(%s) to baseline '%s' (%s)" % (entry.componentname, entry.component,
                                                                            entry.baselinename, entry.baseline))

            replacecommand = "%s set component -r %s -b %s %s stream %s %s --overwrite-uncommitted" % \
                             (self.scmcommand, self.repo, entry.baseline, self.workspace, streamuuid, entry.component)
            shell.execute(replacecommand)

    def setnewflowtargets(self, streamuuid):
        shouter.shout("Set new Flowtargets")
        if not self.hasflowtarget(streamuuid):
            shell.execute("%s add flowtarget -r %s %s %s" % (self.scmcommand, self.repo, self.workspace, streamuuid))

        command = "%s set flowtarget -r %s %s --default --current %s" % (self.scmcommand, self.repo, self.workspace, streamuuid)
        shell.execute(command)

    def hasflowtarget(self, streamuuid):
        command = "%s --show-uuid y --show-alias n list flowtargets -r %s %s" % (self.scmcommand, self.repo, self.workspace)
        flowtargetlines = shell.getoutput(command)
        for flowtargetline in flowtargetlines:
            splittedinformationline = flowtargetline.split("\"")
            uuidpart = splittedinformationline[0].split(" ")
            flowtargetuuid = uuidpart[0].strip()[1:-1]
            if streamuuid in flowtargetuuid:
                return True
        return False

class Changes:

    latest_accept_command = ""

    @staticmethod
    def discard(config, *changeentries):
        idstodiscard = Changes._collectids(changeentries)
        shell.execute(config.scmcommand + " discard -w " + config.workspace + " -r " + config.repo + " -o" + idstodiscard)

    @staticmethod
    def accept(config, logpath, *changeentries):
        for changeEntry in changeentries:
            shouter.shout("Accepting: " + changeEntry.tostring())
        revisions = Changes._collectids(changeentries)
        Changes.latest_accept_command = config.scmcommand + " accept -v -o -r " + config.repo + " -t " + \
                                        config.workspace + " --changes" + revisions
        return shell.execute(Changes.latest_accept_command, logpath, "a")

    @staticmethod
    def _collectids(changeentries):
        ids = ""
        for changeentry in changeentries:
            ids += " " + changeentry.revision
        return ids

    @staticmethod
    def arereleatedmergechangesets(changeentry1, changeentry2):
        if changeentry1 and changeentry2:
            if changeentry1.author == changeentry2.author or "merge" in changeentry2.comment.lower():
                return True
        return False

    @staticmethod
    def tostring(*changes):
        logmessage = "Changes: \n"
        for change in changes:
            logmessage += change.tostring() + "\n"
        shouter.shout(logmessage)

class ImportHandler:
    def __init__(self, config):
        self.config = config
        self.acceptlogpath = config.getlogpath("accept.txt")

    def getcomponentbaselineentriesfromstream(self, stream):
        filename = self.config.getlogpath("StreamComponents_" + stream + ".txt")
        command = "%s --show-alias n --show-uuid y list components -v -m 30 -r %s %s" % (self.config.scmcommand,
                                                                                self.config.repo, stream)
        shell.execute(command, filename)
        componentbaselinesentries = []
        skippedfirstrow = False
        islinewithcomponent = 2
        component = ""
        baseline = ""
        componentname = ""
        baselinename = ""
        with open(filename, 'r', encoding=shell.encoding) as file:
            for line in file:
                cleanedline = line.strip()
                if cleanedline:
                    if not skippedfirstrow:
                        skippedfirstrow = True
                        continue
                    splittedinformationline = line.split("\"")
                    uuidpart = splittedinformationline[0].split(" ")
                    if islinewithcomponent % 2 is 0:
                        component = uuidpart[3].strip()[1:-1]
                        componentname = splittedinformationline[1]
                    else:
                        baseline = uuidpart[5].strip()[1:-1]
                        baselinename = splittedinformationline[1]

                    if baseline and component:
                        componentbaselinesentries.append(
                            ComponentBaseLineEntry(component, baseline, componentname, baselinename))
                        baseline = ""
                        component = ""
                        componentname = ""
                        baselinename = ""
                    islinewithcomponent += 1
        return componentbaselinesentries

    def determineinitialbaseline(self, stream):
        regex = "\(_\w+\)"
        pattern = re.compile(regex)
        config = self.config
        componentbaselinesentries = self.getcomponentbaselineentriesfromstream(stream)
        for entry in componentbaselinesentries:
            shouter.shout("Determine initial baseline of " + entry.componentname)
            # use always scm, lscm fails when specifying maximum over 10k
            command = "scm --show-alias n --show-uuid y list baselines --components %s -r %s -u %s -p %s -m 20000" % \
                      (entry.component, config.user, config.password, config.repo)
            baselineslines = shell.getoutput(command)
            baselineslines.reverse()  # reverse to have earliest baseline on top

            for baselineline in baselineslines:
                matcher = pattern.search(baselineline)
                if matcher:
                    matchedstring = matcher.group()
                    uuid = matchedstring[1:-1]
                    entry.baseline = uuid
                    entry.baselinename = "Automatically detected initial baseline"
                    break
        return componentbaselinesentries

    def acceptchangesintoworkspace(self, changeentries):
        amountofchanges = len(changeentries)
        shouter.shoutwithdate("Start accepting %s changesets" % amountofchanges)
        amountofacceptedchanges = 0
        changestoskip = 0
        reloaded = False
        for changeEntry in changeentries:
            amountofacceptedchanges += 1
            if changestoskip > 0:
                shouter.shout("Skipping " + changeEntry.tostring())
                changestoskip -= 1
                continue
            acceptedsuccesfully = Changes.accept(self.config, self.acceptlogpath,
                                                 changeEntry) is 0
            if not acceptedsuccesfully:
                shouter.shout("Change wasnt succesfully accepted into workspace")
                changestoskip = self.retryacceptincludingnextchangesets(changeEntry, changeentries)
            elif not reloaded:
                if self.is_reloading_necessary():
                    WorkspaceHandler(self.config).load()
                reloaded = True
            shouter.shout("Accepted change %s/%s into working directory" % (amountofacceptedchanges, amountofchanges))
            Commiter.addandcommit(changeEntry)

    @staticmethod
    def is_reloading_necessary():
        return shell.execute("git diff --exit-code") is 0

    @staticmethod
    def collect_changes_to_accept_to_avoid_conflicts(changewhichcantacceptedallone, changes):
        changestoaccept = [changewhichcantacceptedallone]
        nextchange = ImportHandler.getnextchangeset(changewhichcantacceptedallone, changes)

        while True:
            if Changes.arereleatedmergechangesets(changewhichcantacceptedallone, nextchange):
                changestoaccept.append(nextchange)
                nextchange = ImportHandler.getnextchangeset(nextchange, changes)
            else:
                break
        return changestoaccept

    def retryacceptincludingnextchangesets(self, change, changes):
        changestoskip = 0
        changestoaccept = ImportHandler.collect_changes_to_accept_to_avoid_conflicts(change, changes)
        amountofchangestoaccept = len(changestoaccept)

        if amountofchangestoaccept > 1:
            Changes.tostring(*changestoaccept)
            if self.config.useautomaticconflictresolution or self.is_user_agreeing_to_accept_next_change(change):
                for index in range(1, amountofchangestoaccept):
                    toaccept = changestoaccept[0:index + 1]  # accept least possible amount of changes
                    if Changes.accept(self.config, self.acceptlogpath, *toaccept) is 0:
                        changestoskip = len(toaccept) - 1  # initialchange shouldnt be skipped
                        break
                    else:
                        Changes.discard(self.config, *toaccept)  # revert initial state
        return changestoskip

    def is_user_agreeing_to_accept_next_change(self, change):
        messagetoask = "Press Y for accepting following changes, press N to skip"
        if not self.config.useautomaticconflictresolution:
            while True:
                answer = input(messagetoask).lower()
                if answer == "y":
                    return True
                elif answer == "n":
                    shouter.shout("Last executed command: \n" + Changes.latest_accept_command)
                    shouter.shout("Apropriate git commit command \n" + Commiter.getcommitcommand(change))
                    reallycontinue = "Do you want to continue? Y for continue, any key for abort"
                    if input(reallycontinue).lower() == "y":
                        return False
                    else:
                        sys.exit("Please check the output/log and rerun program with resume")
                else:
                    shouter.shout("Please answer with Y/N, input was " + answer)

    @staticmethod
    def getnextchangeset(currentchangeentry, changeentries):
        nextchangeentry = None
        nextindex = changeentries.index(currentchangeentry) + 1
        has_next_changeset = nextindex is not len(changeentries)
        if has_next_changeset:
            nextchangeentry = changeentries[nextindex]
        return nextchangeentry

    def getchangeentriesofstreamcomponents(self, componentbaselineentries):
        missingchangeentries = {}
        shouter.shout("Start collecting changeentries")
        changeentriesbycomponentbaselineentry = {}
        for componentBaseLineEntry in componentbaselineentries:
            changeentries = self.getchangeentriesofbaseline(componentBaseLineEntry.baseline)
            for changeentry in changeentries:
                missingchangeentries[changeentry.revision] = changeentry
        return missingchangeentries

    def readhistory(self, componentbaselineentries, streamname):
        if not self.config.useprovidedhistory:
            warning = "Warning - UseProvidedHistory is set to false, merge-conflicts are more likely to happen. \n " \
                      "For more information see https://github.com/WtfJoke/rtc2git/wiki/Getting-your-History-Files"
            shouter.shout(warning)
            return None
        historyuuids = {}
        shouter.shout("Start reading history files")
        for componentBaseLineEntry in componentbaselineentries:
            history = self.gethistory(componentBaseLineEntry.componentname, streamname)
            historyuuids[componentBaseLineEntry.component] = history
        return historyuuids

    @staticmethod
    def getchangeentriestoaccept(missingchangeentries, history):
        changeentriestoaccept = []
        if history:
            historywithchangeentryobject = {}
            for key in history.keys():
                currentuuids = history.get(key)
                changeentries = []
                for uuid in currentuuids:
                    changeentry = missingchangeentries.get(uuid)
                    if changeentry:
                        changeentries.append(changeentry)
                historywithchangeentryobject[key] = changeentries
            changeentriestoaccept = sorter.tosortedlist(historywithchangeentryobject)
        else:
            changeentriestoaccept.extend(missingchangeentries.values())
            # simple sort by date - same as returned by compare command
            changeentriestoaccept.sort(key=lambda change: change.date)
        return changeentriestoaccept

    @staticmethod
    def getchangeentriesfromfile(outputfilename):
        informationseparator = "@@"
        numberofexpectedinformationseparators = 5
        changeentries = []

        with open(outputfilename, 'r', encoding=shell.encoding) as file:
            currentline = ""
            currentinformationpresent = 0
            for line in file:
                cleanedline = line.strip()
                if cleanedline:
                    currentinformationpresent += cleanedline.count(informationseparator)
                    if currentline:
                        currentline += os.linesep
                    currentline += cleanedline
                    if currentinformationpresent >= numberofexpectedinformationseparators:
                        splittedlines = currentline.split(informationseparator)
                        revisionwithbrackets = splittedlines[0].strip()
                        revision = revisionwithbrackets[1:-1]
                        author = splittedlines[1].strip()
                        email = splittedlines[2].strip()
                        comment = splittedlines[3].strip()
                        date = splittedlines[4].strip()

                        changeentries.append(ChangeEntry(revision, author, email, date, comment))

                        currentinformationpresent = 0
                        currentline = ""
        return changeentries

    @staticmethod
    def getsimplehistoryfromfile(outputfilename):
        revisions = []
        if not os.path.isfile(outputfilename):
            shouter.shout("History file not found: " + outputfilename)
            shouter.shout("Skipping this part of history")
            return revisions

        with open(outputfilename, 'r', encoding=shell.encoding) as file:
            for line in file:
                revisions.append(line.strip())
        revisions.reverse()  # to begin by the oldest
        return revisions

    def getchangeentriesofbaseline(self, baselinetocompare):
        return self.getchangeentriesbytypeandvalue("baseline", baselinetocompare)

    def getchangeentriesofstream(self, streamtocompare):
        shouter.shout("Start collecting changes since baseline creation")
        missingchangeentries = {}
        changeentries = self.getchangeentriesbytypeandvalue("stream", streamtocompare)
        for changeentry in changeentries:
            missingchangeentries[changeentry.revision] = changeentry
        return missingchangeentries

    def getchangeentriesbytypeandvalue(self, comparetype, value):
        shouter.shout("Collect changes of " + comparetype + " " + value)
        dateformat = "yyyy-MM-dd HH:mm:ss"
        outputfilename = self.config.getlogpath("Compare_" + comparetype + "_" + value + ".txt")
        comparecommand = "%s --show-alias n --show-uuid y compare ws %s %s %s -r %s -I sw -C @@{name}@@{email}@@ --flow-directions i -D @@\"%s\"@@" \
                         % (self.config.scmcommand, self.config.workspace, comparetype, value, self.config.repo, dateformat)
        shell.execute(comparecommand, outputfilename)
        return ImportHandler.getchangeentriesfromfile(outputfilename)

    def gethistory(self, componentname, streamname):
        outputfilename = self.config.gethistorypath("History_%s_%s.txt" % (componentname, streamname))
        return ImportHandler.getsimplehistoryfromfile(outputfilename)

class ChangeEntry:
    def __init__(self, revision, author, email, date, comment):
        self.revision = revision
        self.author = author
        self.email = email
        self.date = date
        self.comment = comment

    def getgitauthor(self):
        authorrepresentation = "%s <%s>" % (self.author, self.email)
        return shell.quote(authorrepresentation)

    def tostring(self):
        return self.comment + " (Date: " + self.date + ", Author: " + self.author + ", Revision: " + self.revision + ")"

class ComponentBaseLineEntry:
    def __init__(self, component, baseline, componentname, baselinename):
        self.component = component
        self.baseline = baseline
        self.componentname = componentname
        self.baselinename = baselinename
<|endoftext|>


<commit_before>import os
import json
from websocket import handle_websocket
from flask import Flask, request, Response
from flask import render_template, send_from_directory, url_for

app = Flask(__name__)
app.config.from_object('angular_flask.settings')
app.url_map.strict_slashes = False

def my_app(environ, start_response):
    path = environ["PATH_INFO"]
    if path == "/":
        return app(environ, start_response)
    elif path == "/websocket":
        handle_websocket(environ["wsgi.websocket"])
    else:
        return app(environ, start_response)

import angular_flask.core
import angular_flask.models
import angular_flask.controllers
<commit_msg>Part one of moving websocket.py to controllers.py<commit_after>import os
import json
from websocket import handle_websocket
from flask import Flask, request, Response
from flask import render_template, send_from_directory, url_for

app = Flask(__name__)
app.config.from_object('angular_flask.settings')
app.url_map.strict_slashes = False

import angular_flask.core
import angular_flask.models
import angular_flask.controllers

def my_app(environ, start_response):
    path = environ["PATH_INFO"]
    if path == "/":
        return app(environ, start_response)
    elif path == "/websocket":
        angular_flask.controllers.handle_websocket(environ["wsgi.websocket"])
    else:
        return app(environ, start_response)

<|endoftext|>


<commit_before>import .radiation_field import Radiation

__all__ = ['Radiation', ]
<commit_msg>Fixed typo.<commit_after>from .radiation_field import Radiation

__all__ = ['Radiation', ]
<|endoftext|>


<commit_before>from flask import Flask, jsonify, abort, request, make_response, url_for
from flask_restful import reqparse, Resource, Api
from flask.ext.cors import CORS
import requests
import json
import os
import uuid
import getpass
import datetime

app = Flask(__name__, static_url_path = "")
CORS(app) # required for Cross-origin Request Sharing
api = Api(app)

myElasticServerIp = os.environ.get('ES_SERVER_IP', 'localhost')
myIndexName = os.environ.get('ES_INDEX_NAME', 'bestbuy-products')

api_base_url = '/api/v1'
es_base_url = {
    'products': 'http://' + myElasticServerIp + ':9200/' + myIndexName + '/product',
}

parser = reqparse.RequestParser()

class Healthz(Resource):
    def get(self):
        healthInfo ="""{
  "ES_SERVER_IP" : "{0}",
  "ES_INDEX_NAME" : "{1}",
  "STATUS" : "OK"
}"""
        return healthInfo.format(myElasticServerIp,myIndexName)

api.add_resource(Healthz, api_base_url+'/healthz')

class Search(Resource):

    def get(self):
        startTime = datetime.datetime.today()
        # parse the query: ?q=[something]
        parser.add_argument('q')
        query_string = parser.parse_args()
        # base search URL
        url = es_base_url['products']+'/_search'
        # Query Elasticsearch
        query_alnum = ''.join(e for e in query_string['q'] if (e.isalnum() or e.isspace()) )
        query = {
            "query": {
                "match": {
                    "_all": {
                        "query": query_alnum,
                        "operator": "and"
                    }
                }
            },
            "size": 20,
            "sort" : [
            "onSale",
            "bestSellingRank",
            "_score"
            ]
        }
        resp = requests.post(url, data=json.dumps(query))
        data = resp.json()

        # Build an array of results
        products = []
        for hit in data['hits']['hits']:
            product = hit['_source']
            product['id'] = hit['_id']
            products.append(product['name'])

        #Request Logging
        f = open('/tmp/__init__.log', 'a')
        f.write("\n")
        f.write("\nrequest.url: " + request.url)
        f.write("\nurl: "+url)
        f.write("\nquery: "+json.dumps(query))
        f.write('\n' + '\n'.join(products).encode('utf-8') )
        f.write('\nQuery Processed in: ' + str((datetime.datetime.today() - startTime).microseconds / 1000) + ' milliseconds')
        f.close()
        return products
api.add_resource(Search, api_base_url+'/search')
<commit_msg>Updated<commit_after>from flask import Flask, jsonify, abort, request, make_response, url_for
from flask_restful import reqparse, Resource, Api
from flask.ext.cors import CORS
import requests
import json
import os
import uuid
import getpass
import datetime

app = Flask(__name__, static_url_path = "")
CORS(app) # required for Cross-origin Request Sharing
api = Api(app)

myElasticServerIp = os.environ.get('ES_SERVER_IP', 'localhost')
myIndexName = os.environ.get('ES_INDEX_NAME', 'bestbuy-products')

api_base_url = '/api/v1'
es_base_url = {
    'products': 'http://' + myElasticServerIp + ':9200/' + myIndexName + '/product',
}

parser = reqparse.RequestParser()

class Healthz(Resource):
    def get(self):
        healthInfo ="""{
  "ES_SERVER_IP" : "{0}",
  "ES_INDEX_NAME" : "{1}",
  "STATUS" : "OK"
}"""
        return healthInfo.format(myElasticServerIp,myIndexName), 200, {'Content-Type': 'text/css; charset=utf-8'}

api.add_resource(Healthz, api_base_url+'/healthz')

class Search(Resource):

    def get(self):
        startTime = datetime.datetime.today()
        # parse the query: ?q=[something]
        parser.add_argument('q')
        query_string = parser.parse_args()
        # base search URL
        url = es_base_url['products']+'/_search'
        # Query Elasticsearch
        query_alnum = ''.join(e for e in query_string['q'] if (e.isalnum() or e.isspace()) )
        query = {
            "query": {
                "match": {
                    "_all": {
                        "query": query_alnum,
                        "operator": "and"
                    }
                }
            },
            "size": 20,
            "sort" : [
            "onSale",
            "bestSellingRank",
            "_score"
            ]
        }
        resp = requests.post(url, data=json.dumps(query))
        data = resp.json()

        # Build an array of results
        products = []
        for hit in data['hits']['hits']:
            product = hit['_source']
            product['id'] = hit['_id']
            products.append(product['name'])

        #Request Logging
        f = open('/tmp/__init__.log', 'a')
        f.write("\n")
        f.write("\nrequest.url: " + request.url)
        f.write("\nurl: "+url)
        f.write("\nquery: "+json.dumps(query))
        f.write('\n' + '\n'.join(products).encode('utf-8') )
        f.write('\nQuery Processed in: ' + str((datetime.datetime.today() - startTime).microseconds / 1000) + ' milliseconds')
        f.close()
        return products
api.add_resource(Search, api_base_url+'/search')
<|endoftext|>