<commit_before># Select sunspot coordinates from results-raw file
# csv format : image_num, image_time, x, y, intensity
import os
import csv
import math

rawdata_dir = 'rawdata_to_select/'
selected_dir = 'selected/'
filelist = os.listdir(rawdata_dir)

rawdata = []
zerorow = []
image = []
size = 1024

height = 1024
width = 1024
# radius_cut is used for removing dark background - so as to classify sunspots.
radius_cut = 440 # cutting radius
# real radius of sun - used for calculation.
radius_real = 460
halfheight = int(height/2)
halfwidth = int(width/2)

# Calculation of latitude and longitude of sunspot on sun
def latitude(x):
    return math.asin( (halfheight - x) / radius_real )
def longitude(x,y):
    return math.asin( (y - halfwidth) / (radius_real * math.cos(latitude(x))) )

# DFS range. Need to determine this taxi radius.
dx = [0,1,1,1,0,-1,-1,-1]
dy = [1,1,0,-1,-1,-1,0,1]

x_pixel_sum = 0
y_pixel_sum = 0
num_pixel = 0

total_num_of_row = 0

def dfs(x,y): # DFS(Depth First Search)
    global x_pixel_sum
    global y_pixel_sum
    global num_pixel
    image[x][y] = -1 # mark as already visited.
    x_pixel_sum += x
    y_pixel_sum += y

    num_pixel += 1
    for i in range(0,8):
        if(image[x+dx[i]][y+dy[i]] == 1):
            dfs(x+dx[i],y+dy[i])

for num in range(0,len(filelist)): # process all files in rawdata_dir
    print ('Processing ' + filelist[num] + '...')
    f = open(rawdata_dir + filelist[num], 'r')
    result_raw = open(selected_dir + 'selected-raw_' + filelist[0], 'w')
    result = open(selected_dir + 'selected_' + filelist[0], 'w')
    csvReader = csv.reader(f)
    for row in csvReader:
        rawdata.append(row)
        total_num_of_row += 1
    f.close()
    total_num_of_image = int(rawdata[len(rawdata)-1][0]) + 1
    print('Total ' + str(total_num_of_image) + ' images detected.')

    # to make zero array
    #for i in range(0,size):
    #    zerorow.append(0)

    num_row = 0
    for image_num in range(0,total_num_of_image):
        print('Image ' + str(image_num) + '...')

        # Make zero(blank) array
        #for i in range(0,size):
        #    image.append(zerorow)
        image = [[0 for a in range(size)] for b in range(size)]

        # Make image array to perform DFS
        while(int(rawdata[num_row][0]) == image_num):
            current_time = float(rawdata[num_row][1])
            x = int(rawdata[num_row][2])
            y = int(rawdata[num_row][3])
            image[x][y] = 1
            num_row += 1
            if(num_row == total_num_of_row):
                break

        print('DFS...')
        for a in range(0,size):
            for b in range(0,size):
                if(image[a][b] == 1):
                    x_pixel_sum = 0
                    y_pixel_sum = 0
                    num_pixel = 0
                    dfs(a,b)
                    x_average = x_pixel_sum / num_pixel
                    y_average = y_pixel_sum / num_pixel
                    latit = latitude(x_average)
                    longi = longitude(x_average, y_average)

                    result_raw.write(str(image_num) + ',' + str(current_time) + ',' + str(format(x_average, '.1f')) + ',' + str(format(y_average, '.1f')) + '\n')
                    result.write(str(image_num) + ',' + str(current_time) + ',' + str(format(latit, '.1f')) + ',' + str(format(longi, '.1f')) + '\n')
<commit_msg>selectdata.py 코드 정리<commit_after># Select sunspot coordinates from results-raw file
# csv format : image_num, image_time, x, y, intensity
import os
import csv
import math

rawdata_dir = 'rawdata_to_select/'
selected_dir = 'selected/'
filelist = os.listdir(rawdata_dir)

# 2D array declaration
rawdata = []
zerorow = []
image = []

size = 1024
height = 1024
width = 1024
# radius_cut is used for removing dark background - so as to classify sunspots.
radius_cut = 440 # cutting radius
# real radius of sun - used for calculation.
radius_real = 460
halfheight = int(height/2)
halfwidth = int(width/2)

# Calculation of latitude and longitude of sunspot on sun
def latitude(x):
    return math.asin( (halfheight - x) / radius_real )
def longitude(x,y):
    return math.asin( (y - halfwidth) / (radius_real * math.cos(latitude(x))) )

#######################################################
#### DFS range. Need to determine this taxi radius.####
dx = [0,1,1,1,0,-1,-1,-1]
dy = [1,1,0,-1,-1,-1,0,1]
#######################################################

# temporary variables
x_pixel_sum = 0
y_pixel_sum = 0
num_pixel = 0
total_num_of_row = 0

def dfs(x,y): # DFS(Depth First Search)
    global x_pixel_sum
    global y_pixel_sum
    global num_pixel
    image[x][y] = -1 # mark as already visited.
    x_pixel_sum += x
    y_pixel_sum += y

    num_pixel += 1
    for i in range(0,len(dx)):
        if(image[x+dx[i]][y+dy[i]] == 1):
            dfs(x+dx[i],y+dy[i])

for num in range(0,len(filelist)): # process all files in rawdata_dir
    print ('Processing ' + filelist[num] + '...')
    f = open(rawdata_dir + filelist[num], 'r')
    result_raw = open(selected_dir + 'selected-raw_' + filelist[0], 'w')
    result = open(selected_dir + 'selected_' + filelist[0], 'w')
    csvReader = csv.reader(f)
    for row in csvReader:
        rawdata.append(row)
        total_num_of_row += 1
    f.close()
    total_num_of_image = int(rawdata[len(rawdata)-1][0]) + 1
    print('Total ' + str(total_num_of_image) + ' images detected.')

    num_row = 0
    for image_num in range(0,total_num_of_image):
        print('Image ' + str(image_num) + '...')

        image = [[0 for a in range(size)] for b in range(size)]

        # Make image array to perform DFS
        while(int(rawdata[num_row][0]) == image_num):
            current_time = float(rawdata[num_row][1])
            x = int(rawdata[num_row][2])
            y = int(rawdata[num_row][3])
            image[x][y] = 1
            num_row += 1
            if(num_row == total_num_of_row):
                break

        print('DFS...')
        for a in range(0,size):
            for b in range(0,size):
                if(image[a][b] == 1):
                    x_pixel_sum = 0
                    y_pixel_sum = 0
                    num_pixel = 0
                    dfs(a,b)
                    x_average = x_pixel_sum / num_pixel
                    y_average = y_pixel_sum / num_pixel
                    latit = latitude(x_average)
                    longi = longitude(x_average, y_average)

                    result_raw.write(str(image_num) + ',' + str(current_time) + ',' + str(format(x_average, '.1f')) + ',' + str(format(y_average, '.1f')) + '\n')
                    result.write(str(image_num) + ',' + str(current_time) + ',' + str(format(latit, '.1f')) + ',' + str(format(longi, '.1f')) + '\n')
<|endoftext|>


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

import os
import csv
import numpy

from nose.plugins.attrib import attr as noseattr

from qa_tests import risk
from tests.utils import helpers

from openquake.engine.db import models

# FIXME(lp). This is a regression testing. Data has not been validated
# by an alternative reliable implemantation

class EventBasedRiskCase2TestCase(risk.BaseRiskQATestCase):
    cfg = os.path.join(os.path.dirname(__file__), 'job.ini')

    EXPECTED_LOSS_CURVE_XML = """<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">\n  <lossCurves investigationTime="50.0" sourceModelTreePath="test_sm" gsimTreePath="test_gsim" unit="USD">\n    <lossCurve assetRef="a1">\n      <gml:Point>\n        <gml:pos>15.48 38.09</gml:pos>\n      </gml:Point>\n      <poEs>1.0 0.875 0.75 0.625 0.5 0.375 0.25 0.125 0.0</poEs>\n      <losses>34.3158848505 85.399369608 117.245425501 149.043885673 172.193230479 195.342575285 218.49192009 241.641264896 264.790609701</losses>\n      <lossRatios>0.0114386282835 0.028466456536 0.0390818085005 0.0496812952244 0.057397743493 0.0651141917615 0.07283064003 0.0805470882986 0.0882635365671</lossRatios>\n    </lossCurve>\n    <lossCurve assetRef="a2">\n      <gml:Point>\n        <gml:pos>15.56 38.17</gml:pos>\n      </gml:Point>\n      <poEs>1.0 0.875 0.75 0.625 0.5 0.375 0.25 0.125 0.0</poEs>\n      <losses>8.76706068558 30.247789599 30.8360122116 31.1984360596 31.2782554554 31.3580748512 31.437894247 31.5177136426 31.5975330384</losses>\n      <lossRatios>0.00438353034279 0.0151238947995 0.0154180061058 0.0155992180298 0.0156391277277 0.0156790374256 0.0157189471235 0.0157588568213 0.0157987665192</lossRatios>\n    </lossCurve>\n    <lossCurve assetRef="a3">\n      <gml:Point>\n        <gml:pos>15.48 38.25</gml:pos>\n      </gml:Point>\n      <poEs>1.0 0.875 0.75 0.625 0.5 0.375 0.25 0.125 0.0</poEs>\n      <losses>11.5144783375 34.5514270991 39.0887926791 43.4302192561 44.7861535336 46.1420878111 47.4980220885 48.853956366 50.2098906434</losses>\n      <lossRatios>0.0115144783375 0.0345514270991 0.0390887926791 0.0434302192561 0.0447861535336 0.0461420878111 0.0474980220885 0.048853956366 0.0502098906434</lossRatios>\n    </lossCurve>\n  </lossCurves>\n</nrml>\n"""

    EXPECTED_AGG_LOSS_CURVE_XML = """<?xml version='1.0' encoding='UTF-8'?>
    <nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">\n  <aggregateLossCurve investigationTime="50.0" sourceModelTreePath="test_sm" gsimTreePath="test_gsim" unit="USD">\n    <poEs>1.0 0.875 0.75 0.625 0.5 0.375 0.25 0.125 0.0</poEs>\n    <losses>55.0411 157.5215 191.8946 222.8025 244.5078 266.2131 287.9184 309.6237 331.3290</losses>\n  </aggregateLossCurve>\n</nrml>\n"""

    @noseattr('qa', 'risk', 'event_based')
    def test(self):
        self._run_test()

    def hazard_id(self):
        job = helpers.get_hazard_job(
            helpers.demo_file("event_based_hazard/job.ini"))

        job.hazard_calculation = models.HazardCalculation.objects.create(
            owner=job.hazard_calculation.owner,
            truncation_level=job.hazard_calculation.truncation_level,
            maximum_distance=job.hazard_calculation.maximum_distance,
            intensity_measure_types_and_levels=(
                job.hazard_calculation.intensity_measure_types_and_levels),
            calculation_mode="event_based",
            investigation_time=50,
            ses_per_logic_tree_path=1)
        job.save()
        hc = job.hazard_calculation

        gmf_set = models.GmfSet.objects.create(
            gmf_collection=models.GmfCollection.objects.create(
                output=models.Output.objects.create_output(
                    job, "Test Hazard output", "gmf"),
                lt_realization=models.LtRealization.objects.create(
                    hazard_calculation=job.hazard_calculation,
                    ordinal=1, seed=1, weight=None,
                    sm_lt_path="test_sm", gsim_lt_path="test_gsim",
                    is_complete=False, total_items=1, completed_items=1),
                complete_logic_tree_gmf=False),
            investigation_time=hc.investigation_time,
            ses_ordinal=1,
            complete_logic_tree_gmf=False)

        with open(os.path.join(
                os.path.dirname(__file__), 'gmf.csv'), 'rb') as csvfile:
            gmfreader = csv.reader(csvfile, delimiter=',')
            locations = gmfreader.next()

            for i, gmvs in enumerate(
                    numpy.array([[float(x) for x in row]
                                 for row in gmfreader]).transpose()):
                models.Gmf.objects.create(
                    gmf_set=gmf_set,
                    imt="PGA", gmvs=gmvs,
                    result_grp_ordinal=1,
                    location="POINT(%s)" % locations[i])

        return gmf_set.gmf_collection.output.id

    def actual_data(self, job):
        return ([curve.poes
                for curve in models.LossCurveData.objects.filter(
                    loss_curve__output__oq_job=job,
                    loss_curve__aggregate=False,
                    loss_curve__insured=False).order_by('asset_ref')] +
                [curve.losses
                for curve in models.LossCurveData.objects.filter(
                    loss_curve__output__oq_job=job,
                    loss_curve__aggregate=False,
                    loss_curve__insured=False).order_by('asset_ref')] +
                [curve.losses
                for curve in models.AggregateLossCurveData.objects.filter(
                    loss_curve__output__oq_job=job,
                    loss_curve__aggregate=True,
                    loss_curve__insured=False)])

    def expected_data(self):

        poes = [1., 0.875, 0.75, 0.625, 0.5, 0.375, 0.25, 0.125, 0.]

        losses_1 = [34.3158848505, 85.3993696079, 117.245425502,
                    149.043885673, 172.193230479, 195.342575284, 218.49192009,
                    241.641264896, 264.790609701]

        losses_2 = [8.76706068558, 30.247789599, 30.8360122117,
                    31.1984360597, 31.2782554554, 31.3580748512, 31.4378942469,
                    31.5177136427, 31.5975330384]

        losses_3 = [11.5144783375, 34.5514270991, 39.0887926791,
                    43.4302192561, 44.7861535336, 46.1420878111,
                    47.4980220885, 48.853956366, 50.2098906434]

        expected_aggregate_losses = [55.0410853535, 157.521455945,
                                     191.894562028, 222.802501033,
                                     244.507808584, 266.213116134,
                                     287.918423685, 309.623731235,
                                     331.329038786]

        return [poes, poes, poes, losses_1, losses_2, losses_3,
                expected_aggregate_losses]

    def expected_outputs(self):
        return [self.EXPECTED_LOSS_CURVE_XML, self.EXPECTED_AGG_LOSS_CURVE_XML]
<commit_msg>formatting fix<commit_after># Copyright (c) 2010-2012, GEM Foundation.
#
# OpenQuake is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OpenQuake is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with OpenQuake.  If not, see <http://www.gnu.org/licenses/>.

import os
import csv
import numpy

from nose.plugins.attrib import attr as noseattr

from qa_tests import risk
from tests.utils import helpers

from openquake.engine.db import models

# FIXME(lp). This is a regression testing. Data has not been validated
# by an alternative reliable implemantation

class EventBasedRiskCase2TestCase(risk.BaseRiskQATestCase):
    cfg = os.path.join(os.path.dirname(__file__), 'job.ini')

    EXPECTED_LOSS_CURVE_XML = """<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <lossCurves investigationTime="50.0" sourceModelTreePath="test_sm" gsimTreePath="test_gsim" unit="USD">
    <lossCurve assetRef="a1">
      <gml:Point>
        <gml:pos>15.48 38.09</gml:pos>
      </gml:Point>
      <poEs>1.0 0.875 0.75 0.625 0.5 0.375 0.25 0.125 0.0</poEs>
      <losses>34.3158848505 85.399369608 117.245425501 149.043885673 172.193230479 195.342575285 218.49192009 241.641264896 264.790609701</losses>
      <lossRatios>0.0114386282835 0.028466456536 0.0390818085005 0.0496812952244 0.057397743493 0.0651141917615 0.07283064003 0.0805470882986 0.0882635365671</lossRatios>
    </lossCurve>
    <lossCurve assetRef="a2">
      <gml:Point>
        <gml:pos>15.56 38.17</gml:pos>
      </gml:Point>
      <poEs>1.0 0.875 0.75 0.625 0.5 0.375 0.25 0.125 0.0</poEs>
      <losses>8.76706068558 30.247789599 30.8360122116 31.1984360596 31.2782554554 31.3580748512 31.437894247 31.5177136426 31.5975330384</losses>
      <lossRatios>0.00438353034279 0.0151238947995 0.0154180061058 0.0155992180298 0.0156391277277 0.0156790374256 0.0157189471235 0.0157588568213 0.0157987665192</lossRatios>
    </lossCurve>
    <lossCurve assetRef="a3">
      <gml:Point>
        <gml:pos>15.48 38.25</gml:pos>
      </gml:Point>
      <poEs>1.0 0.875 0.75 0.625 0.5 0.375 0.25 0.125 0.0</poEs>
      <losses>11.5144783375 34.5514270991 39.0887926791 43.4302192561 44.7861535336 46.1420878111 47.4980220885 48.853956366 50.2098906434</losses>
      <lossRatios>0.0115144783375 0.0345514270991 0.0390887926791 0.0434302192561 0.0447861535336 0.0461420878111 0.0474980220885 0.048853956366 0.0502098906434</lossRatios>
    </lossCurve>
  </lossCurves>
</nrml>
"""

    EXPECTED_AGG_LOSS_CURVE_XML = """<?xml version='1.0' encoding='UTF-8'?>
    <nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <aggregateLossCurve investigationTime="50.0" sourceModelTreePath="test_sm" gsimTreePath="test_gsim" unit="USD">
    <poEs>1.0 0.875 0.75 0.625 0.5 0.375 0.25 0.125 0.0</poEs>
    <losses>55.0411 157.5215 191.8946 222.8025 244.5078 266.2131 287.9184 309.6237 331.3290</losses>
  </aggregateLossCurve>
</nrml>
"""

    @noseattr('qa', 'risk', 'event_based')
    def test(self):
        self._run_test()

    def hazard_id(self):
        job = helpers.get_hazard_job(
            helpers.demo_file("event_based_hazard/job.ini"))

        job.hazard_calculation = models.HazardCalculation.objects.create(
            owner=job.hazard_calculation.owner,
            truncation_level=job.hazard_calculation.truncation_level,
            maximum_distance=job.hazard_calculation.maximum_distance,
            intensity_measure_types_and_levels=(
                job.hazard_calculation.intensity_measure_types_and_levels),
            calculation_mode="event_based",
            investigation_time=50,
            ses_per_logic_tree_path=1)
        job.save()
        hc = job.hazard_calculation

        gmf_set = models.GmfSet.objects.create(
            gmf_collection=models.GmfCollection.objects.create(
                output=models.Output.objects.create_output(
                    job, "Test Hazard output", "gmf"),
                lt_realization=models.LtRealization.objects.create(
                    hazard_calculation=job.hazard_calculation,
                    ordinal=1, seed=1, weight=None,
                    sm_lt_path="test_sm", gsim_lt_path="test_gsim",
                    is_complete=False, total_items=1, completed_items=1),
                complete_logic_tree_gmf=False),
            investigation_time=hc.investigation_time,
            ses_ordinal=1,
            complete_logic_tree_gmf=False)

        with open(os.path.join(
                os.path.dirname(__file__), 'gmf.csv'), 'rb') as csvfile:
            gmfreader = csv.reader(csvfile, delimiter=',')
            locations = gmfreader.next()

            for i, gmvs in enumerate(
                    numpy.array([[float(x) for x in row]
                                 for row in gmfreader]).transpose()):
                models.Gmf.objects.create(
                    gmf_set=gmf_set,
                    imt="PGA", gmvs=gmvs,
                    result_grp_ordinal=1,
                    location="POINT(%s)" % locations[i])

        return gmf_set.gmf_collection.output.id

    def actual_data(self, job):
        return ([curve.poes
                for curve in models.LossCurveData.objects.filter(
                    loss_curve__output__oq_job=job,
                    loss_curve__aggregate=False,
                    loss_curve__insured=False).order_by('asset_ref')] +
                [curve.losses
                for curve in models.LossCurveData.objects.filter(
                    loss_curve__output__oq_job=job,
                    loss_curve__aggregate=False,
                    loss_curve__insured=False).order_by('asset_ref')] +
                [curve.losses
                for curve in models.AggregateLossCurveData.objects.filter(
                    loss_curve__output__oq_job=job,
                    loss_curve__aggregate=True,
                    loss_curve__insured=False)])

    def expected_data(self):

        poes = [1., 0.875, 0.75, 0.625, 0.5, 0.375, 0.25, 0.125, 0.]

        losses_1 = [34.3158848505, 85.3993696079, 117.245425502,
                    149.043885673, 172.193230479, 195.342575284, 218.49192009,
                    241.641264896, 264.790609701]

        losses_2 = [8.76706068558, 30.247789599, 30.8360122117,
                    31.1984360597, 31.2782554554, 31.3580748512, 31.4378942469,
                    31.5177136427, 31.5975330384]

        losses_3 = [11.5144783375, 34.5514270991, 39.0887926791,
                    43.4302192561, 44.7861535336, 46.1420878111,
                    47.4980220885, 48.853956366, 50.2098906434]

        expected_aggregate_losses = [55.0410853535, 157.521455945,
                                     191.894562028, 222.802501033,
                                     244.507808584, 266.213116134,
                                     287.918423685, 309.623731235,
                                     331.329038786]

        return [poes, poes, poes, losses_1, losses_2, losses_3,
                expected_aggregate_losses]

    def expected_outputs(self):
        return [self.EXPECTED_LOSS_CURVE_XML, self.EXPECTED_AGG_LOSS_CURVE_XML]
<|endoftext|>


<commit_before>import subprocess
import shlex
import re
import os
import time
import urllib2
import platform
import json
import sys
import base64

from socketIO_client import SocketIO, LoggingNamespace

print "test1"

if len(sys.argv) >= 4:
    print "using dev port 8122"
    port = 8122
else:
    print "using prod port 8022"
    port = 8022

print "initializing socket io"
socketIO = SocketIO('runmyrobot.com', port, LoggingNamespace)
print "finished initializing socket io"

#ffmpeg -f qtkit -i 0 -f mpeg1video -b 400k -r 30 -s 320x240 http://52.8.81.124:8082/hello/320/240/

def onHandleCameraCommand(*args):
    #thread.start_new_thread(handle_command, args)
    print args

socketIO.on('command_to_camera', onHandleCameraCommand)

def getVideoPort():

    url = 'http://runmyrobot.com/get_video_port/%s' % cameraIDAnswer

    for retryNumber in range(2000):
        try:
            print "GET", url
            response = urllib2.urlopen(url).read()
            break
        except:
            print "could not open url ", url
            time.sleep(2)

    return json.loads(response)['mpeg_stream_port']

def runFfmpeg(commandLine):

    print commandLine
    ffmpegProcess = subprocess.Popen(shlex.split(commandLine))
    print "command started"

    return ffmpegProcess

def handleDarwin(deviceNumber, videoPort):

    p = subprocess.Popen(["ffmpeg", "-list_devices", "true", "-f", "qtkit", "-i", "dummy"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    out, err = p.communicate()

    print err

    deviceAnswer = raw_input("Enter the number of the camera device for your robot from the list above: ")
    commandLine = 'ffmpeg -f qtkit -i %s -f mpeg1video -b 400k -r 30 -s 320x240 http://runmyrobot.com:%s/hello/320/240/' % (deviceAnswer, videoPort)

    process = runFfmpeg(commandLine)

    return {'process': process, 'device_answer': deviceAnswer}

def handleLinux(deviceNumber, videoPort):

    #p = subprocess.Popen(["ffmpeg", "-list_devices", "true", "-f", "qtkit", "-i", "dummy"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    #out, err = p.communicate()
    #print err

    os.system("v4l2-ctl -c brightness=150 -c contrast=100 -c saturation=100")

    if deviceNumber is None:
        deviceAnswer = raw_input("Enter the number of the camera device for your robot: ")
    else:
        deviceAnswer = str(deviceNumber)

    commandLine = '/usr/local/bin/ffmpeg -s 320x240 -f video4linux2 -i /dev/video%s -f mpeg1video -b 1k -r 20 http://runmyrobot.com:%s/hello/320/240/' % (deviceAnswer, videoPort)

    process = runFfmpeg(commandLine)

    return {'process': process, 'device_answer': deviceAnswer}

def handleWindows(deviceNumber, videoPort):

    p = subprocess.Popen(["ffmpeg", "-list_devices", "true", "-f", "dshow", "-i", "dummy"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    out, err = p.communicate()

    lines = err.split('\n')

    count = 0

    devices = []

    for line in lines:

        #if "]  \"" in line:
        #    print "line:", line

        m = re.search('.*\\"(.*)\\"', line)
        if m != None:
            #print line
            if m.group(1)[0:1] != '@':
                print count, m.group(1)
                devices.append(m.group(1))
                count += 1

    if deviceNumber is None:
        deviceAnswer = raw_input("Enter the number of the camera device for your robot from the list above: ")
    else:
        deviceAnswer = str(deviceNumber)

    device = devices[int(deviceAnswer)]
    commandLine = 'ffmpeg -s 320x240 -f dshow -i video="%s" -f mpeg1video -b 200k -r 20 http://runmyrobot.com:%s/hello/320/240/' % (device, videoPort)

    process = runFfmpeg(commandLine)

    return {'process': process, 'device_answer': device}

def snapShot(operatingSystem, inputDeviceID, filename="snapshot.jpg"):

    try:
        os.remove('snapshot.jpg')
    except:
        print "did not remove file"

    commandLineDict = {
        'Darwin': 'ffmpeg -y -f qtkit -i %s -vframes 1 %s' % (inputDeviceID, filename),
        'Linux': '/usr/local/bin/ffmpeg -y -f video4linux2 -i /dev/video%s -vframes 1 -q:v 1000 -vf scale=320:240 %s' % (inputDeviceID, filename),
        'Windows': 'ffmpeg -y -s 320x240 -f dshow -i video="%s" -vframes 1 %s' % (inputDeviceID, filename)}

    print commandLineDict[operatingSystem]
    os.system(commandLineDict[operatingSystem])

def startVideoCapture():

    videoPort = getVideoPort()
    print "video port:", videoPort

    if len(sys.argv) >= 3:
        deviceNumber = sys.argv[2]
    else:
        deviceNumber = None

    result = None
    if platform.system() == 'Darwin':
        result = handleDarwin(deviceNumber, videoPort)
    elif platform.system() == 'Linux':
        result = handleLinux(deviceNumber, videoPort)
    elif platform.system() == 'Windows':
        result = handleWindows(deviceNumber, videoPort)
    else:
        print "unknown platform", platform.system()

    return result

def timeInMilliseconds():
    return int(round(time.time() * 1000))

def main():

    print "main"

    streamProcessDict = None

    twitterSnapCount = 0

    while True:

        if streamProcessDict is not None:
            print "stopping previously running ffmpeg (needs to happen if this is not the first iteration)"
            streamProcessDict['process'].kill()

        print "starting process just to get device result" # this should be a separate function so you don't have to do this
        streamProcessDict = startVideoCapture()
        inputDeviceID = streamProcessDict['device_answer']
        print "stopping video capture"
        streamProcessDict['process'].kill()

        #print "sleeping"
        #time.sleep(3)
        #frameCount = int(round(time.time() * 1000))

        videoWithSnapshots = False
        while videoWithSnapshots:

            frameCount = timeInMilliseconds()

            print "taking single frame image"
            snapShot(platform.system(), inputDeviceID, filename="single_frame_image.jpg")

            with open ("single_frame_image.jpg", 'rb') as f:

                # every so many frames, post a snapshot to twitter
                #if frameCount % 450 == 0:
                if frameCount % 6000 == 0:
                        data = f.read()
                        print "emit"
                        socketIO.emit('snapshot', {'frame_count':frameCount, 'image':base64.b64encode(data)})
                data = f.read()

            print "emit"
            socketIO.emit('single_frame_image', {'frame_count':frameCount, 'image':base64.b64encode(data)})
            time.sleep(0)

            #frameCount += 1

        print "taking snapshot"
        snapShot(platform.system(), inputDeviceID)
        with open ("snapshot.jpg", 'rb') as f:
            data = f.read()
        print "emit"

        # skip sending the first image because it's mostly black, maybe completely black
        #todo: should find out why this black image happens
        if twitterSnapCount > 0:
            socketIO.emit('snapshot', {'image':base64.b64encode(data)})

        print "starting video capture"
        streamProcessDict = startVideoCapture()

        # This loop counts out a delay that occurs between twitter snapshots.
        # Every 50 seconds, it kills and restarts ffmpeg.
        # Every 40 seconds, it sends a signal to the server indicating that ffmpeg is alive.
        period = 2*60*60 # period in seconds between snaps
        for count in range(period):
            time.sleep(1)

            if count % 50 == 30:
                print "stopping video capture just in case it has reached a state where it's looping forever, not sending video, and not dying as a process, which can happen"
                streamProcessDict['process'].kill()

            if count % 40 == 0:
                print "checking to see if ffmpeg is running"
                if streamProcessDict['process'].poll() is None:
                    socketIO.emit('video_stream_process_exists', {})

            # if the video stream process dies, restart it
            if streamProcessDict['process'].poll() is not None:
                streamProcessDict = startVideoCapture()

        #print "taking snapshot"
        #snapShot(platform.system(), inputDeviceID)
        #with open ("snapshot.jpg", 'rb') as f:
        #    data = f.read()
        #print "emit"
        #socketIO.emit('snapshot', {'image':base64.b64encode(data)})

        twitterSnapCount += 1

print "test2"

if __name__ == "__main__":

    if len(sys.argv) > 1:
        cameraIDAnswer = sys.argv[1]
    else:
        cameraIDAnswer = raw_input("Enter the Camera ID for your robot, you can get it from the runmyrobot.com website: ")

    main()

<commit_msg>commit<commit_after>import subprocess
import shlex
import re
import os
import time
import urllib2
import platform
import json
import sys
import base64

from socketIO_client import SocketIO, LoggingNamespace

print "test1"

if len(sys.argv) >= 4:
    print "using dev port 8122"
    port = 8122
else:
    print "using prod port 8022"
    port = 8022

print "initializing socket io"
socketIO = SocketIO('runmyrobot.com', port, LoggingNamespace)
print "finished initializing socket io"

#ffmpeg -f qtkit -i 0 -f mpeg1video -b 400k -r 30 -s 320x240 http://52.8.81.124:8082/hello/320/240/

def onHandleCameraCommand(*args):
    #thread.start_new_thread(handle_command, args)
    print args

socketIO.on('command_to_camera', onHandleCameraCommand)

def getVideoPort():

    url = 'http://runmyrobot.com/get_video_port/%s' % cameraIDAnswer

    for retryNumber in range(2000):
        try:
            print "GET", url
            response = urllib2.urlopen(url).read()
            break
        except:
            print "could not open url ", url
            time.sleep(2)

    return json.loads(response)['mpeg_stream_port']

def runFfmpeg(commandLine):

    print commandLine
    ffmpegProcess = subprocess.Popen(shlex.split(commandLine))
    print "command started"

    return ffmpegProcess

def handleDarwin(deviceNumber, videoPort):

    p = subprocess.Popen(["ffmpeg", "-list_devices", "true", "-f", "qtkit", "-i", "dummy"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    out, err = p.communicate()

    print err

    deviceAnswer = raw_input("Enter the number of the camera device for your robot from the list above: ")
    commandLine = 'ffmpeg -f qtkit -i %s -f mpeg1video -b 400k -r 30 -s 320x240 http://runmyrobot.com:%s/hello/320/240/' % (deviceAnswer, videoPort)

    process = runFfmpeg(commandLine)

    return {'process': process, 'device_answer': deviceAnswer}

def handleLinux(deviceNumber, videoPort):

    #p = subprocess.Popen(["ffmpeg", "-list_devices", "true", "-f", "qtkit", "-i", "dummy"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    #out, err = p.communicate()
    #print err

    os.system("v4l2-ctl -c brightness=150 -c contrast=100 -c saturation=100")

    if deviceNumber is None:
        deviceAnswer = raw_input("Enter the number of the camera device for your robot: ")
    else:
        deviceAnswer = str(deviceNumber)

    commandLine = '/usr/local/bin/ffmpeg -s 320x240 -f video4linux2 -i /dev/video%s -f mpeg1video -b 1k -r 20 http://runmyrobot.com:%s/hello/320/240/' % (deviceAnswer, videoPort)

    process = runFfmpeg(commandLine)

    return {'process': process, 'device_answer': deviceAnswer}

def handleWindows(deviceNumber, videoPort):

    p = subprocess.Popen(["ffmpeg", "-list_devices", "true", "-f", "dshow", "-i", "dummy"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    out, err = p.communicate()

    lines = err.split('\n')

    count = 0

    devices = []

    for line in lines:

        #if "]  \"" in line:
        #    print "line:", line

        m = re.search('.*\\"(.*)\\"', line)
        if m != None:
            #print line
            if m.group(1)[0:1] != '@':
                print count, m.group(1)
                devices.append(m.group(1))
                count += 1

    if deviceNumber is None:
        deviceAnswer = raw_input("Enter the number of the camera device for your robot from the list above: ")
    else:
        deviceAnswer = str(deviceNumber)

    device = devices[int(deviceAnswer)]
    commandLine = 'ffmpeg -s 320x240 -f dshow -i video="%s" -f mpeg1video -b 200k -r 20 http://runmyrobot.com:%s/hello/320/240/' % (device, videoPort)

    process = runFfmpeg(commandLine)

    return {'process': process, 'device_answer': device}

def snapShot(operatingSystem, inputDeviceID, filename="snapshot.jpg"):

    try:
        os.remove('snapshot.jpg')
    except:
        print "did not remove file"

    commandLineDict = {
        'Darwin': 'ffmpeg -y -f qtkit -i %s -vframes 1 %s' % (inputDeviceID, filename),
        'Linux': '/usr/local/bin/ffmpeg -y -f video4linux2 -i /dev/video%s -vframes 1 -q:v 1000 -vf scale=320:240 %s' % (inputDeviceID, filename),
        'Windows': 'ffmpeg -y -s 320x240 -f dshow -i video="%s" -vframes 1 %s' % (inputDeviceID, filename)}

    print commandLineDict[operatingSystem]
    os.system(commandLineDict[operatingSystem])

def startVideoCapture():

    videoPort = getVideoPort()
    print "video port:", videoPort

    if len(sys.argv) >= 3:
        deviceNumber = sys.argv[2]
    else:
        deviceNumber = None

    result = None
    if platform.system() == 'Darwin':
        result = handleDarwin(deviceNumber, videoPort)
    elif platform.system() == 'Linux':
        result = handleLinux(deviceNumber, videoPort)
    elif platform.system() == 'Windows':
        result = handleWindows(deviceNumber, videoPort)
    else:
        print "unknown platform", platform.system()

    return result

def timeInMilliseconds():
    return int(round(time.time() * 1000))

def main():

    print "main"

    streamProcessDict = None

    twitterSnapCount = 0

    while True:

        if streamProcessDict is not None:
            print "stopping previously running ffmpeg (needs to happen if this is not the first iteration)"
            streamProcessDict['process'].kill()

        print "starting process just to get device result" # this should be a separate function so you don't have to do this
        streamProcessDict = startVideoCapture()
        inputDeviceID = streamProcessDict['device_answer']
        print "stopping video capture"
        streamProcessDict['process'].kill()

        #print "sleeping"
        #time.sleep(3)
        #frameCount = int(round(time.time() * 1000))

        videoWithSnapshots = False
        while videoWithSnapshots:

            frameCount = timeInMilliseconds()

            print "taking single frame image"
            snapShot(platform.system(), inputDeviceID, filename="single_frame_image.jpg")

            with open ("single_frame_image.jpg", 'rb') as f:

                # every so many frames, post a snapshot to twitter
                #if frameCount % 450 == 0:
                if frameCount % 6000 == 0:
                        data = f.read()
                        print "emit"
                        socketIO.emit('snapshot', {'frame_count':frameCount, 'image':base64.b64encode(data)})
                data = f.read()

            print "emit"
            socketIO.emit('single_frame_image', {'frame_count':frameCount, 'image':base64.b64encode(data)})
            time.sleep(0)

            #frameCount += 1

        print "taking snapshot"
        snapShot(platform.system(), inputDeviceID)
        with open ("snapshot.jpg", 'rb') as f:
            data = f.read()
        print "emit"

        # skip sending the first image because it's mostly black, maybe completely black
        #todo: should find out why this black image happens
        if twitterSnapCount > 0:
            socketIO.emit('snapshot', {'image':base64.b64encode(data)})

        print "starting video capture"
        streamProcessDict = startVideoCapture()

        # This loop counts out a delay that occurs between twitter snapshots.
        # Every 50 seconds, it kills and restarts ffmpeg.
        # Every 40 seconds, it sends a signal to the server indicating that ffmpeg is alive.
        period = 2*60*60 # period in seconds between snaps
        for count in range(period):
            time.sleep(1)

            if count % 50 == 30:
                print "stopping video capture just in case it has reached a state where it's looping forever, not sending video, and not dying as a process, which can happen"
                streamProcessDict['process'].kill()

            if count % 40 == 0:
                print "checking to see if ffmpeg is running"
                if streamProcessDict['process'].poll() is None:
                    socketIO.emit('video_stream_process_exists', {"camera_id":cameraIDAnswer})

            # if the video stream process dies, restart it
            if streamProcessDict['process'].poll() is not None:
                streamProcessDict = startVideoCapture()

        #print "taking snapshot"
        #snapShot(platform.system(), inputDeviceID)
        #with open ("snapshot.jpg", 'rb') as f:
        #    data = f.read()
        #print "emit"
        #socketIO.emit('snapshot', {'image':base64.b64encode(data)})

        twitterSnapCount += 1

print "test2"

if __name__ == "__main__":

    if len(sys.argv) > 1:
        cameraIDAnswer = sys.argv[1]
    else:
        cameraIDAnswer = raw_input("Enter the Camera ID for your robot, you can get it from the runmyrobot.com website: ")

    main()

<|endoftext|>


<commit_before>import subprocess
import shlex
import re
import os
import time
import urllib2
import platform
import json
import sys
import base64
import random

from socketIO_client import SocketIO, LoggingNamespace

print "test1"

if len(sys.argv) >= 4:
    print "using dev port 8122"
    port = 8122
else:
    print "using prod port 8022"
    port = 8022

print "initializing socket io"
socketIO = SocketIO('runmyrobot.com', port, LoggingNamespace)
print "finished initializing socket io"

#ffmpeg -f qtkit -i 0 -f mpeg1video -b 400k -r 30 -s 320x240 http://52.8.81.124:8082/hello/320/240/

def onHandleCameraCommand(*args):
    #thread.start_new_thread(handle_command, args)
    print args

socketIO.on('command_to_camera', onHandleCameraCommand)

def randomSleep():
    """A short wait is good for quick recovery, but sometimes a longer delay is needed or it will just keep trying and failing short intervals, like because the system thinks the port is still in use and every retry makes the system think it's still in use. So, this has a high likelihood of picking a short interval, but will pick a long one sometimes."""

    timeToWait = random.choice((0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 5))
    print "sleeping", timeToWait
    time.sleep(timeToWait)

def getVideoPort():

    url = 'http://runmyrobot.com/get_video_port/%s' % cameraIDAnswer

    for retryNumber in range(2000):
        try:
            print "GET", url
            response = urllib2.urlopen(url).read()
            break
        except:
            print "could not open url ", url
            time.sleep(2)

    return json.loads(response)['mpeg_stream_port']

def runFfmpeg(commandLine):

    print commandLine
    ffmpegProcess = subprocess.Popen(shlex.split(commandLine))
    print "command started"

    return ffmpegProcess

def handleDarwin(deviceNumber, videoPort):

    p = subprocess.Popen(["ffmpeg", "-list_devices", "true", "-f", "qtkit", "-i", "dummy"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    out, err = p.communicate()

    print err

    deviceAnswer = raw_input("Enter the number of the camera device for your robot from the list above: ")
    commandLine = 'ffmpeg -f qtkit -i %s -f mpeg1video -b 400k -r 30 -s 320x240 http://runmyrobot.com:%s/hello/320/240/' % (deviceAnswer, videoPort)

    process = runFfmpeg(commandLine)

    return {'process': process, 'device_answer': deviceAnswer}

def handleLinux(deviceNumber, videoPort):

    print "sleeping to give the camera time to start working"
    randomSleep()
    print "finished sleeping"

    #p = subprocess.Popen(["ffmpeg", "-list_devices", "true", "-f", "qtkit", "-i", "dummy"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    #out, err = p.communicate()
    #print err

    #os.system("v4l2-ctl -c brightness=10 -c contrast=25 -c saturation=40")
    os.system("v4l2-ctl -c brightness=180 -c contrast=40 -c saturation=60") # Skippy
    #os.system("v4l2-ctl -c brightness=50 -c contrast=50 -c saturation=80")
    #os.system("v4l2-ctl -c brightness=200 -c contrast=100 -c saturation=100")

    if deviceNumber is None:
        deviceAnswer = raw_input("Enter the number of the camera device for your robot: ")
    else:
        deviceAnswer = str(deviceNumber)

    #commandLine = '/usr/local/bin/ffmpeg -s 320x240 -f video4linux2 -i /dev/video%s -f mpeg1video -b 1k -r 20 http://runmyrobot.com:%s/hello/320/240/' % (deviceAnswer, videoPort)
    commandLine = '/usr/local/bin/ffmpeg -s 640x480 -f video4linux2 -i /dev/video%s -f mpeg1video -b 1k -r 20 http://runmyrobot.com:%s/hello/640/480/' % (deviceAnswer, videoPort)
    #commandLine = '/usr/local/bin/ffmpeg -s 1280x720 -f video4linux2 -i /dev/video%s -f mpeg1video -b 1k -r 20 http://runmyrobot.com:%s/hello/1280/720/' % (deviceAnswer, videoPort)

    process = runFfmpeg(commandLine)

    return {'process': process, 'device_answer': deviceAnswer}

def handleWindows(deviceNumber, videoPort):

    p = subprocess.Popen(["ffmpeg", "-list_devices", "true", "-f", "dshow", "-i", "dummy"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    out, err = p.communicate()

    lines = err.split('\n')

    count = 0

    devices = []

    for line in lines:

        #if "]  \"" in line:
        #    print "line:", line

        m = re.search('.*\\"(.*)\\"', line)
        if m != None:
            #print line
            if m.group(1)[0:1] != '@':
                print count, m.group(1)
                devices.append(m.group(1))
                count += 1

    if deviceNumber is None:
        deviceAnswer = raw_input("Enter the number of the camera device for your robot from the list above: ")
    else:
        deviceAnswer = str(deviceNumber)

    device = devices[int(deviceAnswer)]
    commandLine = 'ffmpeg -s 320x240 -f dshow -i video="%s" -f mpeg1video -b 200k -r 20 http://runmyrobot.com:%s/hello/320/240/' % (device, videoPort)

    process = runFfmpeg(commandLine)

    return {'process': process, 'device_answer': device}

def snapShot(operatingSystem, inputDeviceID, filename="snapshot.jpg"):

    try:
        os.remove('snapshot.jpg')
    except:
        print "did not remove file"

    commandLineDict = {
        'Darwin': 'ffmpeg -y -f qtkit -i %s -vframes 1 %s' % (inputDeviceID, filename),
        'Linux': '/usr/local/bin/ffmpeg -y -f video4linux2 -i /dev/video%s -vframes 1 -q:v 1000 -vf scale=320:240 %s' % (inputDeviceID, filename),
        'Windows': 'ffmpeg -y -s 320x240 -f dshow -i video="%s" -vframes 1 %s' % (inputDeviceID, filename)}

    print commandLineDict[operatingSystem]
    os.system(commandLineDict[operatingSystem])

def startVideoCapture():

    videoPort = getVideoPort()
    print "video port:", videoPort

    if len(sys.argv) >= 3:
        deviceNumber = sys.argv[2]
    else:
        deviceNumber = None

    result = None
    if platform.system() == 'Darwin':
        result = handleDarwin(deviceNumber, videoPort)
    elif platform.system() == 'Linux':
        result = handleLinux(deviceNumber, videoPort)
    elif platform.system() == 'Windows':
        result = handleWindows(deviceNumber, videoPort)
    else:
        print "unknown platform", platform.system()

    return result

def timeInMilliseconds():
    return int(round(time.time() * 1000))

def main():

    print "main"

    streamProcessDict = None

    twitterSnapCount = 0

    while True:

        socketIO.emit('send_video_status', {'send_video_process_exists': True,
                                            'camera_id':cameraIDAnswer})

        if streamProcessDict is not None:
            print "stopping previously running ffmpeg (needs to happen if this is not the first iteration)"
            streamProcessDict['process'].kill()

        print "starting process just to get device result" # this should be a separate function so you don't have to do this
        streamProcessDict = startVideoCapture()
        inputDeviceID = streamProcessDict['device_answer']
        print "stopping video capture"
        streamProcessDict['process'].kill()

        #print "sleeping"
        #time.sleep(3)
        #frameCount = int(round(time.time() * 1000))

        videoWithSnapshots = False
        while videoWithSnapshots:

            frameCount = timeInMilliseconds()

            print "taking single frame image"
            snapShot(platform.system(), inputDeviceID, filename="single_frame_image.jpg")

            with open ("single_frame_image.jpg", 'rb') as f:

                # every so many frames, post a snapshot to twitter
                #if frameCount % 450 == 0:
                if frameCount % 6000 == 0:
                        data = f.read()
                        print "emit"
                        socketIO.emit('snapshot', {'frame_count':frameCount, 'image':base64.b64encode(data)})
                data = f.read()

            print "emit"
            socketIO.emit('single_frame_image', {'frame_count':frameCount, 'image':base64.b64encode(data)})
            time.sleep(0)

            #frameCount += 1

        if 1:
            print "taking snapshot"
            snapShot(platform.system(), inputDeviceID)
            with open ("snapshot.jpg", 'rb') as f:
                data = f.read()
            print "emit"

            # skip sending the first image because it's mostly black, maybe completely black
            #todo: should find out why this black image happens
            if twitterSnapCount > 0:
                socketIO.emit('snapshot', {'image':base64.b64encode(data)})

        print "starting video capture"
        streamProcessDict = startVideoCapture()

        # This loop counts out a delay that occurs between twitter snapshots.
        # Every 50 seconds, it kills and restarts ffmpeg.
        # Every 40 seconds, it sends a signal to the server indicating status of processes.
        period = 2*60*60 # period in seconds between snaps
        for count in range(period):
            time.sleep(1)

            if count % 20 == 0:
                socketIO.emit('send_video_status', {'send_video_process_exists': True,
                                                    'camera_id':cameraIDAnswer})

            if count % 161 == 30:
                print "stopping video capture just in case it has reached a state where it's looping forever, not sending video, and not dying as a process, which can happen"
                streamProcessDict['process'].kill()

            if count % 80 == 75:
                print "send status about this process and its child process ffmpeg"
                ffmpegProcessExists = streamProcessDict['process'].poll() is None
                socketIO.emit('send_video_status', {'send_video_process_exists': True,
                                                    'ffmpeg_process_exists': ffmpegProcessExists,
                                                    'camera_id':cameraIDAnswer})

            #if count % 190 == 180:
            #    print "reboot system in case the webcam is not working"
            #    os.system("sudo reboot")

            # if the video stream process dies, restart it
            if streamProcessDict['process'].poll() is not None:
                # wait before trying to start ffmpeg
                print "ffmpeg process is dead, waiting before trying to restart"
                randomSleep()
                streamProcessDict = startVideoCapture()

        #print "taking snapshot"
        #snapShot(platform.system(), inputDeviceID)
        #with open ("snapshot.jpg", 'rb') as f:
        #    data = f.read()
        #print "emit"
        #socketIO.emit('snapshot', {'image':base64.b64encode(data)})

        twitterSnapCount += 1

print "test2"

if __name__ == "__main__":

    if len(sys.argv) > 1:
        cameraIDAnswer = sys.argv[1]
    else:
        cameraIDAnswer = raw_input("Enter the Camera ID for your robot, you can get it from the runmyrobot.com website: ")

    main()

<commit_msg>commit<commit_after>import subprocess
import shlex
import re
import os
import time
import urllib2
import platform
import json
import sys
import base64
import random

from socketIO_client import SocketIO, LoggingNamespace

print "test1"

if len(sys.argv) >= 4:
    print "using dev port 8122"
    port = 8122
else:
    print "using prod port 8022"
    port = 8022

print "initializing socket io"
socketIO = SocketIO('runmyrobot.com', port, LoggingNamespace)
print "finished initializing socket io"

#ffmpeg -f qtkit -i 0 -f mpeg1video -b 400k -r 30 -s 320x240 http://52.8.81.124:8082/hello/320/240/

def onHandleCameraCommand(*args):
    #thread.start_new_thread(handle_command, args)
    print args

socketIO.on('command_to_camera', onHandleCameraCommand)

def randomSleep():
    """A short wait is good for quick recovery, but sometimes a longer delay is needed or it will just keep trying and failing short intervals, like because the system thinks the port is still in use and every retry makes the system think it's still in use. So, this has a high likelihood of picking a short interval, but will pick a long one sometimes."""

    timeToWait = random.choice((0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 5))
    print "sleeping", timeToWait
    time.sleep(timeToWait)

def getVideoPort():

    url = 'http://runmyrobot.com/get_video_port/%s' % cameraIDAnswer

    for retryNumber in range(2000):
        try:
            print "GET", url
            response = urllib2.urlopen(url).read()
            break
        except:
            print "could not open url ", url
            time.sleep(2)

    return json.loads(response)['mpeg_stream_port']

def runFfmpeg(commandLine):

    print commandLine
    ffmpegProcess = subprocess.Popen(shlex.split(commandLine))
    print "command started"

    return ffmpegProcess

def handleDarwin(deviceNumber, videoPort):

    p = subprocess.Popen(["ffmpeg", "-list_devices", "true", "-f", "qtkit", "-i", "dummy"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    out, err = p.communicate()

    print err

    deviceAnswer = raw_input("Enter the number of the camera device for your robot from the list above: ")
    commandLine = 'ffmpeg -f qtkit -i %s -f mpeg1video -b 400k -r 30 -s 320x240 http://runmyrobot.com:%s/hello/320/240/' % (deviceAnswer, videoPort)

    process = runFfmpeg(commandLine)

    return {'process': process, 'device_answer': deviceAnswer}

def handleLinux(deviceNumber, videoPort):

    print "sleeping to give the camera time to start working"
    randomSleep()
    print "finished sleeping"

    #p = subprocess.Popen(["ffmpeg", "-list_devices", "true", "-f", "qtkit", "-i", "dummy"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    #out, err = p.communicate()
    #print err

    #os.system("v4l2-ctl -c brightness=10 -c contrast=25 -c saturation=40")
    os.system("v4l2-ctl -c brightness=240 -c contrast=75 -c saturation=60") # Skippy
    #os.system("v4l2-ctl -c brightness=50 -c contrast=50 -c saturation=80")
    #os.system("v4l2-ctl -c brightness=200 -c contrast=100 -c saturation=100")

    if deviceNumber is None:
        deviceAnswer = raw_input("Enter the number of the camera device for your robot: ")
    else:
        deviceAnswer = str(deviceNumber)

    #commandLine = '/usr/local/bin/ffmpeg -s 320x240 -f video4linux2 -i /dev/video%s -f mpeg1video -b 1k -r 20 http://runmyrobot.com:%s/hello/320/240/' % (deviceAnswer, videoPort)
    commandLine = '/usr/local/bin/ffmpeg -s 640x480 -f video4linux2 -i /dev/video%s -f mpeg1video -b 1k -r 20 http://runmyrobot.com:%s/hello/640/480/' % (deviceAnswer, videoPort)
    #commandLine = '/usr/local/bin/ffmpeg -s 1280x720 -f video4linux2 -i /dev/video%s -f mpeg1video -b 1k -r 20 http://runmyrobot.com:%s/hello/1280/720/' % (deviceAnswer, videoPort)

    process = runFfmpeg(commandLine)

    return {'process': process, 'device_answer': deviceAnswer}

def handleWindows(deviceNumber, videoPort):

    p = subprocess.Popen(["ffmpeg", "-list_devices", "true", "-f", "dshow", "-i", "dummy"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    out, err = p.communicate()

    lines = err.split('\n')

    count = 0

    devices = []

    for line in lines:

        #if "]  \"" in line:
        #    print "line:", line

        m = re.search('.*\\"(.*)\\"', line)
        if m != None:
            #print line
            if m.group(1)[0:1] != '@':
                print count, m.group(1)
                devices.append(m.group(1))
                count += 1

    if deviceNumber is None:
        deviceAnswer = raw_input("Enter the number of the camera device for your robot from the list above: ")
    else:
        deviceAnswer = str(deviceNumber)

    device = devices[int(deviceAnswer)]
    commandLine = 'ffmpeg -s 320x240 -f dshow -i video="%s" -f mpeg1video -b 200k -r 20 http://runmyrobot.com:%s/hello/320/240/' % (device, videoPort)

    process = runFfmpeg(commandLine)

    return {'process': process, 'device_answer': device}

def snapShot(operatingSystem, inputDeviceID, filename="snapshot.jpg"):

    try:
        os.remove('snapshot.jpg')
    except:
        print "did not remove file"

    commandLineDict = {
        'Darwin': 'ffmpeg -y -f qtkit -i %s -vframes 1 %s' % (inputDeviceID, filename),
        'Linux': '/usr/local/bin/ffmpeg -y -f video4linux2 -i /dev/video%s -vframes 1 -q:v 1000 -vf scale=320:240 %s' % (inputDeviceID, filename),
        'Windows': 'ffmpeg -y -s 320x240 -f dshow -i video="%s" -vframes 1 %s' % (inputDeviceID, filename)}

    print commandLineDict[operatingSystem]
    os.system(commandLineDict[operatingSystem])

def startVideoCapture():

    videoPort = getVideoPort()
    print "video port:", videoPort

    if len(sys.argv) >= 3:
        deviceNumber = sys.argv[2]
    else:
        deviceNumber = None

    result = None
    if platform.system() == 'Darwin':
        result = handleDarwin(deviceNumber, videoPort)
    elif platform.system() == 'Linux':
        result = handleLinux(deviceNumber, videoPort)
    elif platform.system() == 'Windows':
        result = handleWindows(deviceNumber, videoPort)
    else:
        print "unknown platform", platform.system()

    return result

def timeInMilliseconds():
    return int(round(time.time() * 1000))

def main():

    print "main"

    streamProcessDict = None

    twitterSnapCount = 0

    while True:

        socketIO.emit('send_video_status', {'send_video_process_exists': True,
                                            'camera_id':cameraIDAnswer})

        if streamProcessDict is not None:
            print "stopping previously running ffmpeg (needs to happen if this is not the first iteration)"
            streamProcessDict['process'].kill()

        print "starting process just to get device result" # this should be a separate function so you don't have to do this
        streamProcessDict = startVideoCapture()
        inputDeviceID = streamProcessDict['device_answer']
        print "stopping video capture"
        streamProcessDict['process'].kill()

        #print "sleeping"
        #time.sleep(3)
        #frameCount = int(round(time.time() * 1000))

        videoWithSnapshots = False
        while videoWithSnapshots:

            frameCount = timeInMilliseconds()

            print "taking single frame image"
            snapShot(platform.system(), inputDeviceID, filename="single_frame_image.jpg")

            with open ("single_frame_image.jpg", 'rb') as f:

                # every so many frames, post a snapshot to twitter
                #if frameCount % 450 == 0:
                if frameCount % 6000 == 0:
                        data = f.read()
                        print "emit"
                        socketIO.emit('snapshot', {'frame_count':frameCount, 'image':base64.b64encode(data)})
                data = f.read()

            print "emit"
            socketIO.emit('single_frame_image', {'frame_count':frameCount, 'image':base64.b64encode(data)})
            time.sleep(0)

            #frameCount += 1

        if 1:
            print "taking snapshot"
            snapShot(platform.system(), inputDeviceID)
            with open ("snapshot.jpg", 'rb') as f:
                data = f.read()
            print "emit"

            # skip sending the first image because it's mostly black, maybe completely black
            #todo: should find out why this black image happens
            if twitterSnapCount > 0:
                socketIO.emit('snapshot', {'image':base64.b64encode(data)})

        print "starting video capture"
        streamProcessDict = startVideoCapture()

        # This loop counts out a delay that occurs between twitter snapshots.
        # Every 50 seconds, it kills and restarts ffmpeg.
        # Every 40 seconds, it sends a signal to the server indicating status of processes.
        period = 2*60*60 # period in seconds between snaps
        for count in range(period):
            time.sleep(1)

            if count % 20 == 0:
                socketIO.emit('send_video_status', {'send_video_process_exists': True,
                                                    'camera_id':cameraIDAnswer})

            if count % 161 == 30:
                print "stopping video capture just in case it has reached a state where it's looping forever, not sending video, and not dying as a process, which can happen"
                streamProcessDict['process'].kill()

            if count % 80 == 75:
                print "send status about this process and its child process ffmpeg"
                ffmpegProcessExists = streamProcessDict['process'].poll() is None
                socketIO.emit('send_video_status', {'send_video_process_exists': True,
                                                    'ffmpeg_process_exists': ffmpegProcessExists,
                                                    'camera_id':cameraIDAnswer})

            #if count % 190 == 180:
            #    print "reboot system in case the webcam is not working"
            #    os.system("sudo reboot")

            # if the video stream process dies, restart it
            if streamProcessDict['process'].poll() is not None:
                # wait before trying to start ffmpeg
                print "ffmpeg process is dead, waiting before trying to restart"
                randomSleep()
                streamProcessDict = startVideoCapture()

        #print "taking snapshot"
        #snapShot(platform.system(), inputDeviceID)
        #with open ("snapshot.jpg", 'rb') as f:
        #    data = f.read()
        #print "emit"
        #socketIO.emit('snapshot', {'image':base64.b64encode(data)})

        twitterSnapCount += 1

print "test2"

if __name__ == "__main__":

    if len(sys.argv) > 1:
        cameraIDAnswer = sys.argv[1]
    else:
        cameraIDAnswer = raw_input("Enter the Camera ID for your robot, you can get it from the runmyrobot.com website: ")

    main()

<|endoftext|>


<commit_before>import time
import grovepi
import atexit
import json
import sys

import argparse

from datetime import datetime

# get import working from a directory
from sensorvalue import SensorValue
from config import settings

def main():

    # two collections that really matter
    #one is the list of sensors that are supported by this code.
    supportedsensors = ['loudness', 'airquality', 'gas', 'tempandhumidity']

    #two is the list of sensors configured in the settings.yaml file
    configuredsensors = settings["core"]["configuredsensors"]

    if not isconfigvalid(supportedsensors):
        print 'Configuration is invalid'
        return

    sensordatafile = getsensorfilename()

    parser = argparse.ArgumentParser(description='Use this to poll sensor data from raspberry pi and the grove pi')
    parser.add_argument(
        '-p', '--poll', help='Poll all sensors in the config file at the configured interval', action='store_true')

    parser.add_argument('-s', '--sensortest', help='Name of the sensor to test. Supported sensors: ' + str(supportedsensors), default='nosensor')

    parser.add_argument('-m', '--mock', help='Do a mock sensor test', action='store_true')
    args = vars(parser.parse_args())

    parser.print_usage()

    mockingmode = False
    if args['mock']:
        mockingmode = True

    if args['poll']:
            startautopolling(supportedsensors, mockingmode)

    if args['sensortest'] in supportedsensors:
        sensordata = getsensordata(args['sensortest'], mockingmode)

        if sensordata != None:
            for data in sensordata:
                if data == None:
                    print 'No data was returned by the sensor'
                else:
                    print data.yaml()

    if args['sensortest'] in ['nosensor'] and args['sensortest'] not in supportedsensors:
        print 'the sensor ' +  args['sensortest'] + ' is not supported'
        print 'currently using a dictionary called supportedsensors at the top of this file to manage this list'
        print 'supported sensors: ' + str(supportedsensors)

    if args['sensortest'] == "all":
        print "Looping through the sensors " + str(supportedsensors)
        for item in supportedsensors:
            data = getsensordata(item, mockingmode)
            for item in data:
                print item.yaml()

def isconfigvalid(supportedsensors):
    configuredsensors = settings["core"]["configuredsensors"]

    for sensor in configuredsensors:
        if sensor not in supportedsensors:
            print "The " + sensor + "sensor is not supported"
            return False

    return True

def getsensorconfig(sensorname):
    if sensorname in settings.keys():
        print sensorname + " is configured for port " + str(settings[sensorname]["port"])

        return settings[sensorname]["port"]
    else:
        print "missing config value for " + sensorname
        return

def getsensordata(sensorname, enablemocking):

    if enablemocking:
        return

    if sensorname == "loudness":
        return getloudnessinfo(sensorname)
    if sensorname == "airquality":
        return getairqualitysensorvalue(sensorname)
    if sensorname == "gas":
        return getgassensorvalue(sensorname)
    if sensorname == "tempandhumidity":
        return gettempandhumidity(sensorname)
    if sensorname == "dust":
        return readdustsensor(sensorname)

def startautopolling(supportedsenors, enablemocking):

    print "Starting to Autopoll the sensors"
    pollinginterval = settings["core"]["pollinginterval"]

    #start the polling loop
    while True:
        if pollinginterval != settings["core"]["pollinginterval"]:
            print "polling interval changed from " + str(pollinginterval) + " to " + settings["core"]["pollinginterval"]
            pollinginterval = settings["core"]["pollinginterval"]

        #check for a new polling interval
        values = []

        for item in supportedsenors:
            values.append(getsensordata(item, enablemocking))

        writetofile(values)

        time.sleep(pollinginterval)

        # getairqualitysensorvalue()
        # getgassensorvalue(location, gas_sensor_pin)
        # https://github.com/DexterInd/GrovePi/blob/master/Software/Python/grove_gas_sensor.py

def writetofile(data):
    with open(sensorfilename, 'ab') as f:
        for item in data:
            f.write(item.writecsv() + '\n')

def getsensorfilename():
    return "sensorlog" + datetime.now().strftime('%Y%m%d%H%M%S') + ".log"

def readdustsensor(sensorname):

    sensordata = []

    pin = getsensorconfig(sensorname)

    atexit.register(grovepi.dust_sensor_dis)

    print("Reading from the dust sensor")
    grovepi.dust_sensor_en()

    try:
                [new_val,lowpulseoccupancy] = grovepi.dustSensorRead()

                print "new_val: " + str(new_val)
                print "lowpulseoccupancy: " + str(lowpulseoccupancy)

                if new_val:
                    print(lowpulseoccupancy)
                    sensordata.append(SensorValue(lowpulseoccupancy, 'none', 'Dust', 'location'))

    except IOError:
        print ("Error")

    return sensordata

def gettempandhumidity(sensorname):

    pin = getsensorconfig(sensorname)
    dht_sensor_type = 0             # change this depending on your sensor type - see header comment

    try:
        [temp,humidity] = grovepi.dht(pin,dht_sensor_type)
        print "temp =", temp, " humidity =", humidity

        sensordata = []
        sensordata.append(SensorValue(temp, 'none', 'temp', 'location'))
        sensordata.append(SensorValue(humidity, 'none', 'humidity', 'location'))

    except IOError:
        print "Error"
    return sensordata

def getloudnessinfo(sensorname):

    sensordata = []
    pin = getsensorconfig(sensorname)
    try:
        # Read the sound level
        sensor_value = grovepi.analogRead(pin)
        sensordata.append(SensorValue(sensor_value, 'none', 'Loudness', 'location'))

    except IOError:
        print ("Error")

    return sensordata

def getgassensorvalue(sensorname):

    sensordata = []
    pin = getsensorconfig(sensorname)

    grovepi.pinMode(pin,"INPUT")
    try:
        # Get sensor value
        sensor_value = grovepi.analogRead(pin)

        # Calculate gas density - large value means more dense gas
        density = (float)(sensor_value / 1024)
        sensordata.append(SensorValue(sensor_value, 'none', 'Gas', 'location'))

    except IOError:
        print ("Error")

    return sensordata

def getairqualitysensorvalue(sensorname):

    sensordata = []
    pin = getsensorconfig(sensorname)

    grovepi.pinMode(pin,"INPUT")

    try:
        # Get sensor value
        sensor_value = grovepi.analogRead(pin)

        if sensor_value > 700:
            print ("High pollution")
        elif sensor_value > 300:
            print ("Low pollution")
        else:
            print ("Air fresh")
        sensordata.append(SensorValue(sensor_value, 'none', 'airquality', 'location'))
    except IOError:
        print ("Error")

    return sensordata

if __name__ == '__main__':
    main()

<commit_msg>fix bug<commit_after>import time
import grovepi
import atexit
import json
import sys

import argparse

from datetime import datetime

# get import working from a directory
from sensorvalue import SensorValue
from config import settings

def main():

    # two collections that really matter
    #one is the list of sensors that are supported by this code.
    supportedsensors = ['loudness', 'airquality', 'gas', 'tempandhumidity']

    #two is the list of sensors configured in the settings.yaml file
    configuredsensors = settings["core"]["configuredsensors"]

    if not isconfigvalid(supportedsensors):
        print 'Configuration is invalid'
        return

    sensordatafile = getsensorfilename()

    parser = argparse.ArgumentParser(description='Use this to poll sensor data from raspberry pi and the grove pi')
    parser.add_argument(
        '-p', '--poll', help='Poll all sensors in the config file at the configured interval', action='store_true')

    parser.add_argument('-s', '--sensortest', help='Name of the sensor to test. Supported sensors: ' + str(supportedsensors), default='nosensor')

    parser.add_argument('-m', '--mock', help='Do a mock sensor test', action='store_true')
    args = vars(parser.parse_args())

    parser.print_usage()

    mockingmode = False
    if args['mock']:
        mockingmode = True

    if args['poll']:
            startautopolling(supportedsensors, mockingmode)

    if args['sensortest'] in supportedsensors:
        sensordata = getsensordata(args['sensortest'], mockingmode)

        if sensordata != None:
            for data in sensordata:
                if data == None:
                    print 'No data was returned by the sensor'
                else:
                    print data.yaml()

    if args['sensortest'] in ['nosensor'] and args['sensortest'] not in supportedsensors:
        print 'the sensor ' +  args['sensortest'] + ' is not supported'
        print 'currently using a dictionary called supportedsensors at the top of this file to manage this list'
        print 'supported sensors: ' + str(supportedsensors)

    if args['sensortest'] == "all":
        print "Looping through the sensors " + str(supportedsensors)
        for item in supportedsensors:
            data = getsensordata(item, mockingmode)
            for item in data:
                print item.yaml()

def isconfigvalid(supportedsensors):
    configuredsensors = settings["core"]["configuredsensors"]

    for sensor in configuredsensors:
        if sensor not in supportedsensors:
            print "The " + sensor + "sensor is not supported"
            return False

    return True

def getsensorconfig(sensorname):
    if sensorname in settings.keys():
        print sensorname + " is configured for port " + str(settings[sensorname]["port"])

        return settings[sensorname]["port"]
    else:
        print "missing config value for " + sensorname
        return

def getsensordata(sensorname, enablemocking):

    if enablemocking:
        return

    if sensorname == "loudness":
        return getloudnessinfo(sensorname)
    if sensorname == "airquality":
        return getairqualitysensorvalue(sensorname)
    if sensorname == "gas":
        return getgassensorvalue(sensorname)
    if sensorname == "tempandhumidity":
        return gettempandhumidity(sensorname)
    if sensorname == "dust":
        return readdustsensor(sensorname)

def startautopolling(configuredsensors, enablemocking):

    print "Starting to Autopoll the sensors"
    pollinginterval = settings["core"]["pollinginterval"]

    #start the polling loop
    while True:
        if pollinginterval != settings["core"]["pollinginterval"]:
            print "polling interval changed from " + str(pollinginterval) + " to " + settings["core"]["pollinginterval"]
            pollinginterval = settings["core"]["pollinginterval"]

        #check for a new polling interval
        values = []

        for item in configuredsensors:
            values.append(getsensordata(item, enablemocking))

        writetofile(values)

        time.sleep(pollinginterval)

        # getairqualitysensorvalue()
        # getgassensorvalue(location, gas_sensor_pin)
        # https://github.com/DexterInd/GrovePi/blob/master/Software/Python/grove_gas_sensor.py

def writetofile(data):
    with open(sensorfilename, 'ab') as f:
        for item in data:
            f.write(item.writecsv() + '\n')

def getsensorfilename():
    return "sensorlog" + datetime.now().strftime('%Y%m%d%H%M%S') + ".log"

def readdustsensor(sensorname):

    sensordata = []

    pin = getsensorconfig(sensorname)

    atexit.register(grovepi.dust_sensor_dis)

    print("Reading from the dust sensor")
    grovepi.dust_sensor_en()

    try:
                [new_val,lowpulseoccupancy] = grovepi.dustSensorRead()

                print "new_val: " + str(new_val)
                print "lowpulseoccupancy: " + str(lowpulseoccupancy)

                if new_val:
                    print(lowpulseoccupancy)
                    sensordata.append(SensorValue(lowpulseoccupancy, 'none', 'Dust', 'location'))

    except IOError:
        print ("Error")

    return sensordata

def gettempandhumidity(sensorname):

    pin = getsensorconfig(sensorname)
    dht_sensor_type = 0             # change this depending on your sensor type - see header comment

    try:
        [temp,humidity] = grovepi.dht(pin,dht_sensor_type)
        print "temp =", temp, " humidity =", humidity

        sensordata = []
        sensordata.append(SensorValue(temp, 'none', 'temp', 'location'))
        sensordata.append(SensorValue(humidity, 'none', 'humidity', 'location'))

    except IOError:
        print "Error"
    return sensordata

def getloudnessinfo(sensorname):

    sensordata = []
    pin = getsensorconfig(sensorname)
    try:
        # Read the sound level
        sensor_value = grovepi.analogRead(pin)
        sensordata.append(SensorValue(sensor_value, 'none', 'Loudness', 'location'))

    except IOError:
        print ("Error")

    return sensordata

def getgassensorvalue(sensorname):

    sensordata = []
    pin = getsensorconfig(sensorname)

    grovepi.pinMode(pin,"INPUT")
    try:
        # Get sensor value
        sensor_value = grovepi.analogRead(pin)

        # Calculate gas density - large value means more dense gas
        density = (float)(sensor_value / 1024)
        sensordata.append(SensorValue(sensor_value, 'none', 'Gas', 'location'))

    except IOError:
        print ("Error")

    return sensordata

def getairqualitysensorvalue(sensorname):

    sensordata = []
    pin = getsensorconfig(sensorname)

    grovepi.pinMode(pin,"INPUT")

    try:
        # Get sensor value
        sensor_value = grovepi.analogRead(pin)

        if sensor_value > 700:
            print ("High pollution")
        elif sensor_value > 300:
            print ("Low pollution")
        else:
            print ("Air fresh")
        sensordata.append(SensorValue(sensor_value, 'none', 'airquality', 'location'))
    except IOError:
        print ("Error")

    return sensordata

if __name__ == '__main__':
    main()

<|endoftext|>


<commit_before># (c) EZER'ARCH - Python script
# This creates a GUI-less mod manager. Main runtime.
# Released under MIT License
# TO-DO:
# 1) Config files must be txt
# 2) Create config by "current status" in mod directory.
# 3) Code duplication. Make it simple.
# 4) Documentation
# 3) Get rid of EasyGUI.
# 4) Modularity.

# Simple
print ("\nThis is a Python application.")
print ("Created by: Ezer'Arch (www.ezerarch.com)")
print ("Creation date: 2013/10/23")

#### LIBS

import os
import glob
import subprocess
import easygui as eg

#### FUNCTIONS

# DEF: list and select the config

def cfgMan( path ):
    print( "\n-- Configs available --" )
    dir_cfg = os.path.join( path, '*.cfg' )
    lst_cfgs = glob.glob( dir_cfg )    # Display a list of CFGs
    if lst_cfgs:
        n = 1
        for path_cfg in lst_cfgs:
            print( '[', n, ']', os.path.split( path_cfg )[1][:-4] ) # namefile without extension
            n+= 1
        print( '[ ENTER ] for none and exit' )
        # CFG
        n = input ( 'Choose a config by typing a number: ' )
        if n is not '':
            cfg_n = int( n )
            path_cfg = lst_cfgs[ cfg_n - 1 ] # pick one from the list of cfgs
            file_cfg = os.path.split( path_cfg )[1][:-4] # file name of cfg
            print( 'You chose [', cfg_n, ']:', file_cfg, '.' ) # prompt user
            return path_cfg
        else:
            print( 'You chose no config. Nothing done.' )
            return None
    else:
        print( 'There is no config in the directory!' )
        return None

# DEF: list files in a directory (path)

def listFiles( path, tup_ext ):
    lst_files = os.listdir( path ) # list all content, includes folders
    lst_files_ext = [] # empty list
    for file in lst_files:
        if file.endswith( tup_ext ): # has the extension (.xxx) found in the tuple
            lst_files_ext.append( file ) # add the item in the list
    return lst_files_ext

# DEF: get the files to filter out

def readCfg( path ):
    if path is not None:
        lst_files_filter = [line.strip() for line in open( path )]
        return lst_files_filter
    else:
        pass

# DEF: rename all files not found in lst_files to -str_suffix

def renFiles( path, lst_files, lst_files_filter, str_suffix ):
    """
path: the path where the function will take place
lst_files: list of files
lst_files_filter: list of files to be filtered out
str_suffix: file extension sufix
"""
    # Prompt
    print( "\n-- Renaming files --" )
    print( "Files from the config: " )
    for file in lst_files_filter:
        if file in lst_files:
            print( '*', file )
        else:
            print( '*', file, '(* not found! *)' )
    # Rename
    lst_files_rename = []
    for file in lst_files:
        if not file in lst_files_filter:
            path_file = os.path.join( path, file )
            path_file_new = path_file + str_suffix
            os.rename( path_file, path_file_new )
            lst_files_rename.append( file ) # add the item in the list
    print( "\nFiles renamed: " )
    for file in lst_files_rename:
        print( '*', file )

# DEF: rename back

def renFiles_back( path, lst_files, tup_ext ):
    print( "\n-- Renaming files back --" )
    print( 'Files found to be renamed back:', len( lst_files ) )
    for file in lst_files:
        if file.endswith( tup_ext ):
            path_file = os.path.join( path, file )
            path_file_new = path_file[:-1] # remove the last character
            os.rename( path_file, path_file_new )
    print( "Files renamed back!" )

# DEF: create CFG

def createCfg( pathdir_mod , file_exts ):
    files = listFiles( pathdir_mod, file_exts )
    cfg_files = eg.multchoicebox(msg='Pick as many items as you like.', title='Choose files', choices=files)
    if cfg_files:
        cfg_filename = input( "Type a name for the CFG: " ) + '.cfg'
        pathfile_cfg = os.path.join( pathdir_mod, 'MiniModManager', cfg_filename )
        f = open( pathfile_cfg,'w')
        for line in cfg_files:
            f.write( line + '\n' )
        f.close()
        print( "CFG created/edited." )

        key = input( 'Do you want to use the just-created config? y/<n>: ' ).lower()
        if key == 'y':
            files_filter = readCfg( pathfile_cfg ) # open cfg
            renFiles( pathdir_mod, files, files_filter, 'X' ) # rename files with 'X' in the end
    else:
        print( "No CFG was created/edited." )

#### VARIABLES

# Paths
path_app = os.getenv('APPDATA') # get appdata path
path_sav = os.path.join( path_app, '.minecraft\\saves' ) # make saves path
path_mod = os.path.join( path_app, '.minecraft\\mods' ) # make mod path
path_man = os.path.join( path_mod, 'MiniModManager' ) # make MiniModManager path

# File extensions
file_exts = ('.jar', '.zip') # files to be renamed
file_exts_x = ('.jarX', '.zipX') # files to be renamed back

#### EXECUTION

# HELLO WORLD!
print( '\nInitializing...')

# Check for folders
if os.path.exists( path_mod ): # mod directory
    print( 'Minecraft Mod directory found...' )
else:
    print( 'Minecraft Mod directory NOT found!' )

if os.path.exists( path_man ): # manager directory
    print( 'MiniModManager directory found...' )
else:
    print( 'MiniModManager directory was not found! Creating one in Minecraft\Mods.' )
    os.makedirs( path_man ) # create manager directory

# Message
print( "\n*** MiniModManager is ready ***\nThis is a GUI-less mod manager that blocks files that shouldn't be loaded." )
input( "\nHit ENTER to proceed: " )

# Options
option = input( '''\nSelect an option:
1 = Use a config
2 = Unblock all files
3 = Create a config
4 = Open MiniModManager directory in the file manager
5 = Open Mods directory in the file manager
6 = Open Saves directory in the file manager
7 = Block all files
ENTER = Exit
Enter the number for the option: ''' )

if option == '1':
    # check for *X-files
    files = listFiles( path_mod, file_exts_x ) # files to rename back
    if files:
        print( 'Your mod folder contains remaned files! Renaming them back... ' )
        renFiles_back( path_mod, files, file_exts_x )

    # Init CFG manager
    path_cfg = cfgMan( path_man )

    if path_cfg:
        # List the files
        lst_files = listFiles( path_mod, file_exts ) # list all files in the directory
        # Rename files
        lst_files_filter = readCfg( path_cfg ) # open cfg
        renFiles( path_mod, lst_files, lst_files_filter, 'X' ) # rename files with 'X' in the end

elif option == '2':
    #Rename files back
    lst_files = listFiles( path_mod, file_exts_x ) # files to rename back
    renFiles_back( path_mod, lst_files, file_exts_x )

elif option == '3':
    # check for _X-files
    files = listFiles( path_mod, file_exts_x ) # files to rename back
    if files:
        print( 'Your mod folder contains remaned files! Renaming them back... ' )
        renFiles_back( path_mod, files, file_exts_x )

    #Creates a new config
    createCfg( path_mod, file_exts )

elif option == '4':
    # open MiniModManager in the file manager
    print( 'Opening MiniModManager in your file manager...' )
    print( '>>>' )
    subprocess.Popen('explorer ' + path_man)
    print( 'Done!' )

elif option == '5':
    # open Mods directory in the file manager
    print( 'Opening Mods directory in your file manager...' )
    print( '>>>' )
    subprocess.Popen('explorer ' + path_mod)
    print( 'Done!' )

elif option == '6':
    # open Saves directory in the file manager
    print( 'Opening Saves directory in your file manager...' )
    print( '>>>' )
    subprocess.Popen('explorer ' + path_sav)
    print( 'Done!' )

elif option == '7':
    # block all files
    print( 'Blocking all files...' )
    # List the files
    lst_files = listFiles( path_mod, file_exts ) # list all files in the directory
    renFiles( path_mod, lst_files, lst_files, 'X' ) # rename files with 'X' in the end
    print( 'Done!' )

else:
    print( 'Nothing was done.' )

# Exit
input("\nHit ENTER to exit.")

#EOF
<commit_msg>Generic error<commit_after># (c) EZER'ARCH - Python script
# This creates a GUI-less mod manager. Main runtime.
# Released under MIT License
# TO-DO:
# 1) Config files must be txt
# 2) Create config by "current status" in mod directory.
# 3) Code duplication. Make it simple.
# 4) Documentation
# 3) Get rid of EasyGUI.
# 4) Modularity.

# Simple
print ("\nThis is a Python application.")
print ("Created by: Ezer'Arch (www.ezerarch.com)")
print ("Creation date: 2013/10/23")

#### LIBS

import os
import glob
import subprocess
import easygui as eg

#### FUNCTIONS

# DEF: generic error

def err():
    """
Generic error to be displayed to the user.
"""
    print( "***Error: Check if Minecraft is running with mods, or if there's duplicate files!***")
    pass

# DEF: list and select the config

def cfgMan( path ):
    print( "\n-- Configs available --" )
    dir_cfg = os.path.join( path, '*.cfg' )
    lst_cfgs = glob.glob( dir_cfg )    # Display a list of CFGs
    if lst_cfgs:
        n = 1
        for path_cfg in lst_cfgs:
            print( '[', n, ']', os.path.split( path_cfg )[1][:-4] ) # namefile without extension
            n+= 1
        print( '[ ENTER ] for none and exit' )
        # CFG
        n = input ( 'Choose a config by typing a number: ' )
        if n is not '':
            cfg_n = int( n )
            path_cfg = lst_cfgs[ cfg_n - 1 ] # pick one from the list of cfgs
            file_cfg = os.path.split( path_cfg )[1][:-4] # file name of cfg
            print( 'You chose [', cfg_n, ']:', file_cfg, '.' ) # prompt user
            return path_cfg
        else:
            print( 'You chose no config. Nothing done.' )
            return None
    else:
        print( 'There is no config in the directory!' )
        return None

# DEF: list files in a directory (path)

def listFiles( path, tup_ext ):
    lst_files = os.listdir( path ) # list all content, includes folders
    lst_files_ext = [] # empty list
    for file in lst_files:
        if file.endswith( tup_ext ): # has the extension (.xxx) found in the tuple
            lst_files_ext.append( file ) # add the item in the list
    return lst_files_ext

# DEF: get the files to filter out

def readCfg( path ):
    if path is not None:
        lst_files_filter = [line.strip() for line in open( path )]
        return lst_files_filter
    else:
        pass

# DEF: rename all files not found in lst_files to -str_suffix

def renFiles( path, lst_files, lst_files_filter, str_suffix ):
    """
path: the path where the function will take place
lst_files: list of files
lst_files_filter: list of files to be filtered out
str_suffix: file extension sufix
"""
    # Prompt
    print( "\n-- Renaming files --" )
    print( "Files from the config: " )
    for file in lst_files_filter:
        if file in lst_files:
            print( '*', file )
        else:
            print( '*', file, '(* not found! *)' )
    # Rename
    lst_files_rename = []
    for file in lst_files:
        if not file in lst_files_filter:
            path_file = os.path.join( path, file )
            path_file_new = path_file + str_suffix
            os.rename( path_file, path_file_new )
            lst_files_rename.append( file ) # add the item in the list
    print( "\nFiles renamed: " )
    for file in lst_files_rename:
        print( '*', file )

# DEF: rename back

def renFiles_back( path, lst_files, tup_ext ):
    print( "\n-- Renaming files back --" )
    print( 'Files found to be renamed back:', len( lst_files ) )
    for file in lst_files:
        if file.endswith( tup_ext ):
            path_file = os.path.join( path, file )
            path_file_new = path_file[:-1] # remove the last character
            os.rename( path_file, path_file_new )
    print( "Files renamed back!" )

# DEF: create CFG

def createCfg( pathdir_mod , file_exts ):
    files = listFiles( pathdir_mod, file_exts )
    cfg_files = eg.multchoicebox(msg='Pick as many items as you like.', title='Choose files', choices=files)
    if cfg_files:
        cfg_filename = input( "Type a name for the CFG: " ) + '.cfg'
        pathfile_cfg = os.path.join( pathdir_mod, 'MiniModManager', cfg_filename )
        f = open( pathfile_cfg,'w')
        for line in cfg_files:
            f.write( line + '\n' )
        f.close()
        print( "CFG created/edited." )

        key = input( 'Do you want to use the just-created config? y/<n>: ' ).lower()
        if key == 'y':
            files_filter = readCfg( pathfile_cfg ) # open cfg
            renFiles( pathdir_mod, files, files_filter, 'X' ) # rename files with 'X' in the end
    else:
        print( "No CFG was created/edited." )

#### VARIABLES

# Paths
path_app = os.getenv('APPDATA') # get appdata path
path_sav = os.path.join( path_app, '.minecraft\\saves' ) # make saves path
path_mod = os.path.join( path_app, '.minecraft\\mods' ) # make mod path
path_man = os.path.join( path_mod, 'MiniModManager' ) # make MiniModManager path

# File extensions
file_exts = ('.jar', '.zip') # files to be renamed
file_exts_x = ('.jarX', '.zipX') # files to be renamed back

#### EXECUTION

# HELLO WORLD!
print( '\nInitializing...')

# Check for folders
if os.path.exists( path_mod ): # mod directory
    print( 'Minecraft Mod directory found...' )
else:
    print( 'Minecraft Mod directory NOT found!' )

if os.path.exists( path_man ): # manager directory
    print( 'MiniModManager directory found...' )
else:
    print( 'MiniModManager directory was not found! Creating one in Minecraft\Mods.' )
    os.makedirs( path_man ) # create manager directory

# Message
print( "\n*** MiniModManager is ready ***\nThis is a GUI-less mod manager that blocks files that shouldn't be loaded." )
input( "\nHit ENTER to proceed: " )

# Options
option = input( '''\nSelect an option:
1 = Use a config
2 = Unblock all files
3 = Create a config
4 = Open MiniModManager directory in the file manager
5 = Open Mods directory in the file manager
6 = Open Saves directory in the file manager
7 = Block all files
ENTER = Exit
Enter the number for the option: ''' )

if option == '1':
    # check for *X-files
    files = listFiles( path_mod, file_exts_x ) # files to rename back
    if files:
        print( 'Your mod folder contains remaned files! Renaming them back... ' )
        renFiles_back( path_mod, files, file_exts_x )

    # Init CFG manager
    path_cfg = cfgMan( path_man )

    if path_cfg:
        # List the files
        lst_files = listFiles( path_mod, file_exts ) # list all files in the directory
        # Rename files
        lst_files_filter = readCfg( path_cfg ) # open cfg
        renFiles( path_mod, lst_files, lst_files_filter, 'X' ) # rename files with 'X' in the end

elif option == '2':
    #Rename files back
    lst_files = listFiles( path_mod, file_exts_x ) # files to rename back
    renFiles_back( path_mod, lst_files, file_exts_x )

elif option == '3':
    # check for _X-files
    files = listFiles( path_mod, file_exts_x ) # files to rename back
    if files:
        print( 'Your mod folder contains remaned files! Renaming them back... ' )
        renFiles_back( path_mod, files, file_exts_x )

    #Creates a new config
    createCfg( path_mod, file_exts )

elif option == '4':
    # open MiniModManager in the file manager
    print( 'Opening MiniModManager in your file manager...' )
    print( '>>>' )
    subprocess.Popen('explorer ' + path_man)
    print( 'Done!' )

elif option == '5':
    # open Mods directory in the file manager
    print( 'Opening Mods directory in your file manager...' )
    print( '>>>' )
    subprocess.Popen('explorer ' + path_mod)
    print( 'Done!' )

elif option == '6':
    # open Saves directory in the file manager
    print( 'Opening Saves directory in your file manager...' )
    print( '>>>' )
    subprocess.Popen('explorer ' + path_sav)
    print( 'Done!' )

elif option == '7':
    # block all files
    print( 'Blocking all files...' )
    # List the files
    lst_files = listFiles( path_mod, file_exts ) # list all files in the directory
    renFiles( path_mod, lst_files, lst_files, 'X' ) # rename files with 'X' in the end
    print( 'Done!' )

else:
    print( 'Nothing was done.' )

# Exit
input("\nHit ENTER to exit.")

#EOF
<|endoftext|>