<commit_before>#!/usr/bin/env python
#
# Appcelerator Titanium Module Packager
#
#
import os, subprocess, sys, glob, string
import zipfile
from datetime import date

cwd = os.path.abspath(os.path.dirname(sys._getframe(0).f_code.co_filename))
os.chdir(cwd)
required_module_keys = ['name','version','moduleid','description','copyright','license','copyright','platform','minsdk']
module_defaults = {
	'description':'My module',
	'author': 'Your Name',
	'license' : 'Specify your license',
	'copyright' : 'Copyright (c) %s by Your Company' % str(date.today().year),
}
module_license_default = "TODO: place your license here and we'll include it in the module distribution"

def find_sdk(config):
	sdk = config['TITANIUM_SDK']
	return os.path.expandvars(os.path.expanduser(sdk))

def replace_vars(config,token):
	idx = token.find('$(')
	while idx != -1:
		idx2 = token.find(')',idx+2)
		if idx2 == -1: break
		key = token[idx+2:idx2]
		if not config.has_key(key): break
		token = token.replace('$(%s)' % key, config[key])
		idx = token.find('$(')
	return token

def read_ti_xcconfig():
	contents = open(os.path.join(cwd,'titanium.xcconfig')).read()
	config = {}
	for line in contents.splitlines(False):
		line = line.strip()
		if line[0:2]=='//': continue
		idx = line.find('=')
		if idx > 0:
			key = line[0:idx].strip()
			value = line[idx+1:].strip()
			config[key] = replace_vars(config,value)
	return config

def generate_doc(config):
	docdir = os.path.join(cwd,'documentation')
	if not os.path.exists(docdir):
		print "Couldn't find documentation file at: %s" % docdir
		return None

	try:
		import markdown2 as markdown
	except ImportError:
		import markdown
	documentation = []
	for file in os.listdir(docdir):
		if file in ignoreFiles or os.path.isdir(os.path.join(docdir, file)):
			continue
		md = open(os.path.join(docdir,file)).read()
		html = markdown.markdown(md)
		documentation.append({file:html});
	return documentation

def compile_js(manifest,config):
	js_file = os.path.join(cwd,'assets','__MODULE_ID__.js')
	if not os.path.exists(js_file): return

	from compiler import Compiler
	try:
		import json
	except:
		import simplejson as json

	compiler = Compiler(cwd, manifest['moduleid'], manifest['name'], 'commonjs')
	root_asset, module_assets = compiler.compile_module()

	root_asset_content = """
%s

	return filterDataInRange([NSData dataWithBytesNoCopy:data length:sizeof(data) freeWhenDone:NO], ranges[0]);
""" % root_asset

	module_asset_content = """
%s

	NSNumber *index = [map objectForKey:path];
	if (index == nil) {
		return nil;
	}
	return filterDataInRange([NSData dataWithBytesNoCopy:data length:sizeof(data) freeWhenDone:NO], ranges[index.integerValue]);
""" % module_assets

	from tools import splice_code

	assets_router = os.path.join(cwd,'Classes','___PROJECTNAMEASIDENTIFIER___ModuleAssets.m')
	splice_code(assets_router, 'asset', root_asset_content)
	splice_code(assets_router, 'resolve_asset', module_asset_content)

	# Generate the exports after crawling all of the available JS source
	exports = open('metadata.json','w')
	json.dump({'exports':compiler.exports }, exports)
	exports.close()

def die(msg):
	print msg
	sys.exit(1)

def warn(msg):
	print "[WARN] %s" % msg

def validate_license():
	c = open(os.path.join(cwd,'LICENSE')).read()
	if c.find(module_license_default)!=-1:
		warn('please update the LICENSE file with your license text before distributing')

def validate_manifest():
	path = os.path.join(cwd,'manifest')
	f = open(path)
	if not os.path.exists(path): die("missing %s" % path)
	manifest = {}
	for line in f.readlines():
		line = line.strip()
		if line[0:1]=='#': continue
		if line.find(':') < 0: continue
		key,value = line.split(':')
		manifest[key.strip()]=value.strip()
	for key in required_module_keys:
		if not manifest.has_key(key): die("missing required manifest key '%s'" % key)
		if module_defaults.has_key(key):
			defvalue = module_defaults[key]
			curvalue = manifest[key]
			if curvalue==defvalue: warn("please update the manifest key: '%s' to a non-default value" % key)
	return manifest,path

ignoreFiles = ['.DS_Store','.gitignore','libTitanium.a','titanium.jar','README']
ignoreDirs = ['.DS_Store','.svn','.git','CVSROOT']

def zip_dir(zf,dir,basepath,ignoreExt=[]):
	if not os.path.exists(dir): return
	for root, dirs, files in os.walk(dir):
		for name in ignoreDirs:
			if name in dirs:
				dirs.remove(name)	# don't visit ignored directories
		for file in files:
			if file in ignoreFiles: continue
			e = os.path.splitext(file)
			if len(e) == 2 and e[1] in ignoreExt: continue
			from_ = os.path.join(root, file)
			to_ = from_.replace(dir, '%s/%s'%(basepath,dir), 1)
			zf.write(from_, to_)

def glob_libfiles():
	files = []
	for libfile in glob.glob('build/**/*.a'):
		if libfile.find('Release-')!=-1:
			files.append(libfile)
	return files

def build_module(manifest,config):
	from tools import ensure_dev_path
	ensure_dev_path()

	rc = os.system("xcodebuild -sdk iphoneos -configuration Release")
	if rc != 0:
		die("xcodebuild failed")
	rc = os.system("xcodebuild -sdk iphonesimulator -configuration Release")
	if rc != 0:
		die("xcodebuild failed")
    # build the merged library using lipo
	moduleid = manifest['moduleid']
	libpaths = ''
	for libfile in glob_libfiles():
		libpaths+='%s ' % libfile

	os.system("lipo %s -create -output build/lib%s.a" %(libpaths,moduleid))

def package_module(manifest,mf,config):
	name = manifest['name'].lower()
	moduleid = manifest['moduleid'].lower()
	version = manifest['version']
	modulezip = '%s-iphone-%s.zip' % (moduleid,version)
	if os.path.exists(modulezip): os.remove(modulezip)
	zf = zipfile.ZipFile(modulezip, 'w', zipfile.ZIP_DEFLATED)
	modulepath = 'modules/iphone/%s/%s' % (moduleid,version)
	zf.write(mf,'%s/manifest' % modulepath)
	libname = 'lib%s.a' % moduleid
	zf.write('build/%s' % libname, '%s/%s' % (modulepath,libname))
	docs = generate_doc(config)
	if docs!=None:
		for doc in docs:
			for file, html in doc.iteritems():
				filename = string.replace(file,'.md','.html')
				zf.writestr('%s/documentation/%s'%(modulepath,filename),html)
	zip_dir(zf,'assets',modulepath,['.pyc','.js'])
	zip_dir(zf,'example',modulepath,['.pyc'])
	zip_dir(zf,'platform',modulepath,['.pyc','.js'])
	zf.write('LICENSE','%s/LICENSE' % modulepath)
	zf.write('module.xcconfig','%s/module.xcconfig' % modulepath)
	exports_file = 'metadata.json'
	if os.path.exists(exports_file):
		zf.write(exports_file, '%s/%s' % (modulepath, exports_file))
	zf.close()

if __name__ == '__main__':
	manifest,mf = validate_manifest()
	validate_license()
	config = read_ti_xcconfig()

	sdk = find_sdk(config)
	sys.path.insert(0,os.path.join(sdk,'iphone'))
	sys.path.append(os.path.join(sdk, "common"))

	compile_js(manifest,config)
	build_module(manifest,config)
	package_module(manifest,mf,config)
	sys.exit(0)

<commit_msg>[TIMOB-10688] Module build.py template: adding YML apidoc generation and option to skip it<commit_after>#!/usr/bin/env python
#
# Appcelerator Titanium Module Packager
#
#
import os, subprocess, sys, glob, string, optparse, subprocess
import zipfile
from datetime import date

cwd = os.path.abspath(os.path.dirname(sys._getframe(0).f_code.co_filename))
os.chdir(cwd)
required_module_keys = ['name','version','moduleid','description','copyright','license','copyright','platform','minsdk']
module_defaults = {
	'description':'My module',
	'author': 'Your Name',
	'license' : 'Specify your license',
	'copyright' : 'Copyright (c) %s by Your Company' % str(date.today().year),
}
module_license_default = "TODO: place your license here and we'll include it in the module distribution"

def find_sdk(config):
	sdk = config['TITANIUM_SDK']
	return os.path.expandvars(os.path.expanduser(sdk))

def replace_vars(config,token):
	idx = token.find('$(')
	while idx != -1:
		idx2 = token.find(')',idx+2)
		if idx2 == -1: break
		key = token[idx+2:idx2]
		if not config.has_key(key): break
		token = token.replace('$(%s)' % key, config[key])
		idx = token.find('$(')
	return token

def read_ti_xcconfig():
	contents = open(os.path.join(cwd,'titanium.xcconfig')).read()
	config = {}
	for line in contents.splitlines(False):
		line = line.strip()
		if line[0:2]=='//': continue
		idx = line.find('=')
		if idx > 0:
			key = line[0:idx].strip()
			value = line[idx+1:].strip()
			config[key] = replace_vars(config,value)
	return config

def generate_doc(config):
	docdir = os.path.join(cwd,'documentation')
	if not os.path.exists(docdir):
		warn("Couldn't find documentation file at: %s" % docdir)
		return None

	try:
		import markdown2 as markdown
	except ImportError:
		import markdown
	documentation = []
	for file in os.listdir(docdir):
		if file in ignoreFiles or os.path.isdir(os.path.join(docdir, file)):
			continue
		md = open(os.path.join(docdir,file)).read()
		html = markdown.markdown(md)
		documentation.append({file:html});
	return documentation

def compile_js(manifest,config):
	js_file = os.path.join(cwd,'assets','__MODULE_ID__.js')
	if not os.path.exists(js_file): return

	from compiler import Compiler
	try:
		import json
	except:
		import simplejson as json

	compiler = Compiler(cwd, manifest['moduleid'], manifest['name'], 'commonjs')
	root_asset, module_assets = compiler.compile_module()

	root_asset_content = """
%s

	return filterDataInRange([NSData dataWithBytesNoCopy:data length:sizeof(data) freeWhenDone:NO], ranges[0]);
""" % root_asset

	module_asset_content = """
%s

	NSNumber *index = [map objectForKey:path];
	if (index == nil) {
		return nil;
	}
	return filterDataInRange([NSData dataWithBytesNoCopy:data length:sizeof(data) freeWhenDone:NO], ranges[index.integerValue]);
""" % module_assets

	from tools import splice_code

	assets_router = os.path.join(cwd,'Classes','___PROJECTNAMEASIDENTIFIER___ModuleAssets.m')
	splice_code(assets_router, 'asset', root_asset_content)
	splice_code(assets_router, 'resolve_asset', module_asset_content)

	# Generate the exports after crawling all of the available JS source
	exports = open('metadata.json','w')
	json.dump({'exports':compiler.exports }, exports)
	exports.close()

def die(msg):
	print msg
	sys.exit(1)

def info(msg):
	print "[INFO] %s" % msg

def warn(msg):
	print "[WARN] %s" % msg

def validate_license():
	c = open(os.path.join(cwd,'LICENSE')).read()
	if c.find(module_license_default)!=-1:
		warn('please update the LICENSE file with your license text before distributing')

def validate_manifest():
	path = os.path.join(cwd,'manifest')
	f = open(path)
	if not os.path.exists(path): die("missing %s" % path)
	manifest = {}
	for line in f.readlines():
		line = line.strip()
		if line[0:1]=='#': continue
		if line.find(':') < 0: continue
		key,value = line.split(':')
		manifest[key.strip()]=value.strip()
	for key in required_module_keys:
		if not manifest.has_key(key): die("missing required manifest key '%s'" % key)
		if module_defaults.has_key(key):
			defvalue = module_defaults[key]
			curvalue = manifest[key]
			if curvalue==defvalue: warn("please update the manifest key: '%s' to a non-default value" % key)
	return manifest,path

ignoreFiles = ['.DS_Store','.gitignore','libTitanium.a','titanium.jar','README']
ignoreDirs = ['.DS_Store','.svn','.git','CVSROOT']

def zip_dir(zf,dir,basepath,ignoreExt=[]):
	if not os.path.exists(dir): return
	for root, dirs, files in os.walk(dir):
		for name in ignoreDirs:
			if name in dirs:
				dirs.remove(name)	# don't visit ignored directories
		for file in files:
			if file in ignoreFiles: continue
			e = os.path.splitext(file)
			if len(e) == 2 and e[1] in ignoreExt: continue
			from_ = os.path.join(root, file)
			to_ = from_.replace(dir, '%s/%s'%(basepath,dir), 1)
			zf.write(from_, to_)

def glob_libfiles():
	files = []
	for libfile in glob.glob('build/**/*.a'):
		if libfile.find('Release-')!=-1:
			files.append(libfile)
	return files

def build_module(manifest,config):
	from tools import ensure_dev_path
	ensure_dev_path()

	rc = os.system("xcodebuild -sdk iphoneos -configuration Release")
	if rc != 0:
		die("xcodebuild failed")
	rc = os.system("xcodebuild -sdk iphonesimulator -configuration Release")
	if rc != 0:
		die("xcodebuild failed")
    # build the merged library using lipo
	moduleid = manifest['moduleid']
	libpaths = ''
	for libfile in glob_libfiles():
		libpaths+='%s ' % libfile

	os.system("lipo %s -create -output build/lib%s.a" %(libpaths,moduleid))

def generate_apidoc(apidoc_build_path):
	global options

	if options.skip_docs:
		info("Skipping documentation generation.")
		return False
	else:
		info("Module apidoc generation can be skipped using --skip-docs")
	apidoc_path = os.path.join(cwd, "apidoc")
	if not os.path.exists(apidoc_path):
		warn("Skipping apidoc generation. No apidoc folder found at: %s" % apidoc_path)
		return False

	if not os.path.exists(apidoc_build_path):
	    os.makedirs(apidoc_build_path)
	ti_root = string.strip(subprocess.check_output(["echo $TI_ROOT"], shell=True))
	if not len(ti_root) > 0:
		warn("Not generating documentation from the apidoc folder. The titanium_mobile repo could not be found.")
		warn("Alias the folder containing the titanium_mobile repo to $TI_ROOT")
		return False
	docgen = os.path.join(ti_root, "titanium_mobile", "apidoc", "docgen.py")
	if not os.path.exists(docgen):
		warn("Not generating documentation from the apidoc folder. Couldn't find docgen.py at: %s" % docgen)
		return False

	info("Generating documentation from the apidoc folder.")
	rc = os.system("\"%s\" --format=jsca,modulehtml --css=styles.css -o \"%s\" -e \"%s\"" % (docgen, apidoc_build_path, apidoc_path))
	if rc != 0:
		die("docgen failed")
	return True

def package_module(manifest,mf,config):
	name = manifest['name'].lower()
	moduleid = manifest['moduleid'].lower()
	version = manifest['version']
	modulezip = '%s-iphone-%s.zip' % (moduleid,version)
	if os.path.exists(modulezip): os.remove(modulezip)
	zf = zipfile.ZipFile(modulezip, 'w', zipfile.ZIP_DEFLATED)
	modulepath = 'modules/iphone/%s/%s' % (moduleid,version)
	zf.write(mf,'%s/manifest' % modulepath)
	libname = 'lib%s.a' % moduleid
	zf.write('build/%s' % libname, '%s/%s' % (modulepath,libname))
	docs = generate_doc(config)
	if docs!=None:
		for doc in docs:
			for file, html in doc.iteritems():
				filename = string.replace(file,'.md','.html')
				zf.writestr('%s/documentation/%s'%(modulepath,filename),html)

	apidoc_build_path = os.path.join(cwd, "build", "apidoc")
	if generate_apidoc(apidoc_build_path):
		for file in os.listdir(apidoc_build_path):
			if file in ignoreFiles or os.path.isdir(os.path.join(apidoc_build_path, file)):
				continue
			zf.write(os.path.join(apidoc_build_path, file), '%s/documentation/apidoc/%s' % (modulepath, file))

	zip_dir(zf,'assets',modulepath,['.pyc','.js'])
	zip_dir(zf,'example',modulepath,['.pyc'])
	zip_dir(zf,'platform',modulepath,['.pyc','.js'])
	zf.write('LICENSE','%s/LICENSE' % modulepath)
	zf.write('module.xcconfig','%s/module.xcconfig' % modulepath)
	exports_file = 'metadata.json'
	if os.path.exists(exports_file):
		zf.write(exports_file, '%s/%s' % (modulepath, exports_file))
	zf.close()

if __name__ == '__main__':
	global options

	parser = optparse.OptionParser()
	parser.add_option("-s", "--skip-docs",
			dest="skip_docs",
			action="store_true",
			help="Will skip building documentation in apidoc folder",
			default=False)
	(options, args) = parser.parse_args()

	manifest,mf = validate_manifest()
	validate_license()
	config = read_ti_xcconfig()

	sdk = find_sdk(config)
	sys.path.insert(0,os.path.join(sdk,'iphone'))
	sys.path.append(os.path.join(sdk, "common"))

	compile_js(manifest,config)
	build_module(manifest,config)
	package_module(manifest,mf,config)
	sys.exit(0)

<|endoftext|>


<commit_before>from django.core.management.base import NoArgsCommand
import sys
from cfda.models import *
from metrics.models import *
from settings import *
from decimal import Decimal
from faads_scorecard.completeness import MetricTester
import pickle
import sys
import csv

def load_pickle_file(filename):
    f = open(filename, 'r')
    r = pickle.load(f)
    f.close()
    return r

def main():

    # clear out old summary objects
    ProgramCompleteness.objects.all().delete()
    ProgramCompletenessDetail.objects.all().delete()

    for y in FISCAL_YEARS:
        print 'processing fiscal year %d' % y
        data_pct = load_pickle_file('completeness/output/%d.pickle' % y)
        data_bad_dollars = load_pickle_file('completeness/output/%d-dollars_misreported.pickle' % y)
        data_total_dollars = load_pickle_file('completeness/output/%d-dollars_total.pickle' % y)

        for program_number in data_pct:
            print '   processing %s' % program_number
            program = None
            try:
                program = Program.objects.get(program_number=program_number)
            except Program.DoesNotExist, e:
                print "   no entry found for program with CFDA number %s" % program_number
                continue

            # save detailed program-level test information
            PCD = ProgramCompletenessDetail()
            PCD.fiscal_year = y
            PCD.program = program
            PCD.agency = PCD.program.agency

            total_successful_tests = 0
            total_tests = 0

            for metric_name in data_pct[program_number]:

                if metric_name[0:2]=='__':
                    continue

                metric_model_name = metric_name.split('.')[-1]
                setattr(PCD, metric_model_name, Decimal(str(data_pct[program_number][metric_name].dollars_of_failed_tests)))

            PCD.save()

            # save program-level summary
            PC = ProgramCompleteness()
            PC.fiscal_year = y
            PC.program = program
            PC.agency = PC.program.agency
            PC.completeness_failed_dollars = data_bad_dollars.get(program.program_number, None)
            PC.completeness_total_dollars = data_total_dollars.get(program.program_number, None)
            PC.save()

class Command(NoArgsCommand):
    help = "Import pickle files produced by completeness testing"

    def handle_noargs(self, **options):
        main()

<commit_msg>fixed completeness duplication issue<commit_after>from django.core.management.base import NoArgsCommand
import sys
from cfda.models import *
from metrics.models import *
from settings import *
from decimal import Decimal
from faads_scorecard.completeness import MetricTester
import pickle
import sys
import csv

def load_pickle_file(filename):
    f = open(filename, 'r')
    r = pickle.load(f)
    f.close()
    return r

def main():

    # clear out old summary objects
    ProgramCompleteness.objects.all().delete()
    ProgramCompletenessDetail.objects.all().delete()

    for y in FISCAL_YEARS:
        print 'processing fiscal year %d' % y
        data_pct = load_pickle_file('completeness/output/%d.pickle' % y)
        data_bad_dollars = load_pickle_file('completeness/output/%d-dollars_misreported.pickle' % y)
        data_total_dollars = load_pickle_file('completeness/output/%d-dollars_total.pickle' % y)

        for program_number in data_pct:
            print '   processing %s' % program_number
            program = None
            try:
                program = Program.objects.get(program_number=program_number)
            except Program.DoesNotExist, e:
                print "   no entry found for program with CFDA number %s" % program_number
                continue

            # save detailed program-level test information
            try:
                PCD = ProgramCompletenessDetail.objects.get(program=program, fiscal_year=y)
            except ProgramCompletenessDetail.DoesNotExist:
                PCD = ProgramCompletenessDetail()
            PCD.fiscal_year = y
            PCD.program = program
            PCD.agency = PCD.program.agency

            total_successful_tests = 0
            total_tests = 0

            for metric_name in data_pct[program_number]:

                if metric_name[0:2]=='__':
                    continue

                metric_model_name = metric_name.split('.')[-1]
                if hasattr(PCD, metric_model_name) and getattr(PCD, metric_model_name):
                    to_add = getattr(PCD, metric_model_name) +  Decimal(str(data_pct[program_number][metric_name].dollars_of_failed_tests))
                else:
                    to_add = Decimal(str(data_pct[program_number][metric_name].dollars_of_failed_tests))

                setattr(PCD, metric_model_name, to_add)

            PCD.save()

            # save program-level summary
            try:
                PC = ProgramCompleteness.objects.get(program=program, fiscal_year=y)
            except ProgramCompleteness.DoesNotExist:
                PC = ProgramCompleteness()
            PC.fiscal_year = y
            PC.program = program
            PC.agency = PC.program.agency
            PC.completeness_failed_dollars = data_bad_dollars.get(program.program_number, None)
            PC.completeness_total_dollars = data_total_dollars.get(program.program_number, None)
            PC.save()

class Command(NoArgsCommand):
    help = "Import pickle files produced by completeness testing"

    def handle_noargs(self, **options):
        main()

<|endoftext|>


<commit_before># -*- encoding: utf-8 -*-
from supriya.tools.ugentools.Filter import Filter

class DetectSilence(Filter):
    r'''Evaluates `done_action` when input falls below `threshold`.

    ::

        >>> source = ugentools.WhiteNoise.ar()
        >>> source *= ugentools.Line.kr(start=1, stop=0)
        >>> detect_silence = ugentools.DetectSilence.kr(
        ...     done_action=DoneAction.FREE_SYNTH,
        ...     source=source,
        ...     threshold=0.0001,
        ...     time=1.0,
        ...     )
        >>> detect_silence
        DetectSilence.kr()

    '''

    ### CLASS VARIABLES ###

    __documentation_section__ = 'Envelope Utility UGens'

    __slots__ = ()

    _ordered_input_names = (
        'source',
        'threshold',
        'time',
        'done_action',
        )

    _valid_calculation_rates = None

    ### INITIALIZER ###

    def __init__(
        self,
        calculation_rate=None,
        done_action=0,
        source=0,
        threshold=0.0001,
        time=0.1,
        ):
        Filter.__init__(
            self,
            calculation_rate=calculation_rate,
            threshold=threshold,
            done_action=done_action,
            source=source,
            time=time,
            )

    ### PRIVATE METHODS ###

    def _get_outputs(self):
        return []

    ### PUBLIC METHODS ###

    @classmethod
    def ar(
        cls,
        threshold=0.0001,
        done_action=0,
        source=0,
        time=0.1,
        ):
        r'''Constructs an audio-rate DetectSilence.

        ::

            >>> source = ugentools.WhiteNoise.ar()
            >>> source *= ugentools.Line.kr(start=1, stop=0)
            >>> detect_silence = ugentools.DetectSilence.ar(
            ...     done_action=DoneAction.FREE_SYNTH,
            ...     source=source,
            ...     threshold=0.0001,
            ...     time=1.0,
            ...     )
            >>> detect_silence
            DetectSilence.ar()

        Returns ugen graph.
        '''
        from supriya.tools import synthdeftools
        calculation_rate = synthdeftools.CalculationRate.AUDIO
        ugen = cls._new_expanded(
            calculation_rate=calculation_rate,
            threshold=threshold,
            done_action=done_action,
            source=source,
            time=time,
            )
        return ugen

    @classmethod
    def kr(
        cls,
        threshold=0.0001,
        done_action=0,
        source=0,
        time=0.1,
        ):
        r'''Constructs a control-rate DetectSilence.

        ::

            >>> source = ugentools.WhiteNoise.ar()
            >>> source *= ugentools.Line.kr(start=1, stop=0)
            >>> detect_silence = ugentools.DetectSilence.kr(
            ...     done_action=DoneAction.FREE_SYNTH,
            ...     source=source,
            ...     threshold=0.0001,
            ...     time=1.0,
            ...     )
            >>> detect_silence
            DetectSilence.kr()

        Returns ugen graph.
        '''
        from supriya.tools import synthdeftools
        calculation_rate = synthdeftools.CalculationRate.CONTROL
        ugen = cls._new_expanded(
            calculation_rate=calculation_rate,
            threshold=threshold,
            done_action=done_action,
            source=source,
            time=time,
            )
        return ugen

    ### PUBLIC PROPERTIES ###

    @property
    def done_action(self):
        r'''Gets `done_action` input of DetectSilence.

        ::

            >>> source = ugentools.WhiteNoise.ar()
            >>> source *= ugentools.Line.kr(start=1, stop=0)
            >>> detect_silence = ugentools.DetectSilence.kr(
            ...     done_action=DoneAction.FREE_SYNTH,
            ...     source=source,
            ...     threshold=0.0001,
            ...     time=1.0,
            ...     )
            >>> detect_silence.done_action
            2.0

        Returns ugen input.
        '''
        index = self._ordered_input_names.index('done_action')
        return self._inputs[index]

    @property
    def source(self):
        r'''Gets `source` input of DetectSilence.

        ::

            >>> source = ugentools.WhiteNoise.ar()
            >>> source *= ugentools.Line.kr(start=1, stop=0)
            >>> detect_silence = ugentools.DetectSilence.kr(
            ...     done_action=DoneAction.FREE_SYNTH,
            ...     source=source,
            ...     threshold=0.0001,
            ...     time=1.0,
            ...     )
            >>> detect_silence.source
            OutputProxy(
                source=BinaryOpUGen(
                    left=OutputProxy(
                        source=WhiteNoise(
                            calculation_rate=<CalculationRate.AUDIO: 2>
                            ),
                        output_index=0
                        ),
                    right=OutputProxy(
                        source=Line(
                            calculation_rate=<CalculationRate.CONTROL: 1>,
                            done_action=0.0,
                            duration=1.0,
                            start=1.0,
                            stop=0.0
                            ),
                        output_index=0
                        ),
                    calculation_rate=<CalculationRate.AUDIO: 2>,
                    special_index=2
                    ),
                output_index=0
                )

        Returns ugen input.
        '''
        index = self._ordered_input_names.index('source')
        return self._inputs[index]

    @property
    def threshold(self):
        r'''Gets `threshold` input of DetectSilence.

        ::

            >>> source = ugentools.WhiteNoise.ar()
            >>> source *= ugentools.Line.kr(start=1, stop=0)
            >>> detect_silence = ugentools.DetectSilence.kr(
            ...     done_action=DoneAction.FREE_SYNTH,
            ...     source=source,
            ...     threshold=0.0001,
            ...     time=1.0,
            ...     )
            >>> detect_silence.threshold
            0.0001

        Returns ugen input.
        '''
        index = self._ordered_input_names.index('threshold')
        return self._inputs[index]

    @property
    def time(self):
        r'''Gets `time` input of DetectSilence.

        ::

            >>> source = ugentools.WhiteNoise.ar()
            >>> source *= ugentools.Line.kr(start=1, stop=0)
            >>> detect_silence = ugentools.DetectSilence.kr(
            ...     done_action=DoneAction.FREE_SYNTH,
            ...     source=source,
            ...     threshold=0.0001,
            ...     time=1.0,
            ...     )
            >>> detect_silence.time
            1.0

        Returns ugen input.
        '''
        index = self._ordered_input_names.index('time')
        return self._inputs[index]<commit_msg>Cleaned up DetectSilence.<commit_after># -*- encoding: utf-8 -*-
from supriya.tools.ugentools.Filter import Filter

class DetectSilence(Filter):
    r'''Evaluates `done_action` when input falls below `threshold`.

    ::

        >>> source = ugentools.WhiteNoise.ar()
        >>> source *= ugentools.Line.kr(start=1, stop=0)
        >>> detect_silence = ugentools.DetectSilence.kr(
        ...     done_action=DoneAction.FREE_SYNTH,
        ...     source=source,
        ...     threshold=0.0001,
        ...     time=1.0,
        ...     )
        >>> detect_silence
        DetectSilence.kr()

    '''

    ### CLASS VARIABLES ###

    __documentation_section__ = 'Envelope Utility UGens'

    __slots__ = ()

    _ordered_input_names = (
        'source',
        'threshold',
        'time',
        'done_action',
        )

    _valid_calculation_rates = None

    ### INITIALIZER ###

    def __init__(
        self,
        calculation_rate=None,
        done_action=0,
        source=0,
        threshold=0.0001,
        time=0.1,
        ):
        Filter.__init__(
            self,
            calculation_rate=calculation_rate,
            threshold=threshold,
            done_action=done_action,
            source=source,
            time=time,
            )

    ### PUBLIC METHODS ###

    @classmethod
    def ar(
        cls,
        threshold=0.0001,
        done_action=0,
        source=0,
        time=0.1,
        ):
        r'''Constructs an audio-rate DetectSilence.

        ::

            >>> source = ugentools.WhiteNoise.ar()
            >>> source *= ugentools.Line.kr(start=1, stop=0)
            >>> detect_silence = ugentools.DetectSilence.ar(
            ...     done_action=DoneAction.FREE_SYNTH,
            ...     source=source,
            ...     threshold=0.0001,
            ...     time=1.0,
            ...     )
            >>> detect_silence
            DetectSilence.ar()

        Returns ugen graph.
        '''
        from supriya.tools import synthdeftools
        calculation_rate = synthdeftools.CalculationRate.AUDIO
        ugen = cls._new_expanded(
            calculation_rate=calculation_rate,
            threshold=threshold,
            done_action=done_action,
            source=source,
            time=time,
            )
        return ugen

    @classmethod
    def kr(
        cls,
        threshold=0.0001,
        done_action=0,
        source=0,
        time=0.1,
        ):
        r'''Constructs a control-rate DetectSilence.

        ::

            >>> source = ugentools.WhiteNoise.ar()
            >>> source *= ugentools.Line.kr(start=1, stop=0)
            >>> detect_silence = ugentools.DetectSilence.kr(
            ...     done_action=DoneAction.FREE_SYNTH,
            ...     source=source,
            ...     threshold=0.0001,
            ...     time=1.0,
            ...     )
            >>> detect_silence
            DetectSilence.kr()

        Returns ugen graph.
        '''
        from supriya.tools import synthdeftools
        calculation_rate = synthdeftools.CalculationRate.CONTROL
        ugen = cls._new_expanded(
            calculation_rate=calculation_rate,
            threshold=threshold,
            done_action=done_action,
            source=source,
            time=time,
            )
        return ugen

    ### PUBLIC PROPERTIES ###

    @property
    def done_action(self):
        r'''Gets `done_action` input of DetectSilence.

        ::

            >>> source = ugentools.WhiteNoise.ar()
            >>> source *= ugentools.Line.kr(start=1, stop=0)
            >>> detect_silence = ugentools.DetectSilence.kr(
            ...     done_action=DoneAction.FREE_SYNTH,
            ...     source=source,
            ...     threshold=0.0001,
            ...     time=1.0,
            ...     )
            >>> detect_silence.done_action
            2.0

        Returns ugen input.
        '''
        index = self._ordered_input_names.index('done_action')
        return self._inputs[index]

    @property
    def source(self):
        r'''Gets `source` input of DetectSilence.

        ::

            >>> source = ugentools.WhiteNoise.ar()
            >>> source *= ugentools.Line.kr(start=1, stop=0)
            >>> detect_silence = ugentools.DetectSilence.kr(
            ...     done_action=DoneAction.FREE_SYNTH,
            ...     source=source,
            ...     threshold=0.0001,
            ...     time=1.0,
            ...     )
            >>> detect_silence.source
            OutputProxy(
                source=BinaryOpUGen(
                    left=OutputProxy(
                        source=WhiteNoise(
                            calculation_rate=<CalculationRate.AUDIO: 2>
                            ),
                        output_index=0
                        ),
                    right=OutputProxy(
                        source=Line(
                            calculation_rate=<CalculationRate.CONTROL: 1>,
                            done_action=0.0,
                            duration=1.0,
                            start=1.0,
                            stop=0.0
                            ),
                        output_index=0
                        ),
                    calculation_rate=<CalculationRate.AUDIO: 2>,
                    special_index=2
                    ),
                output_index=0
                )

        Returns ugen input.
        '''
        index = self._ordered_input_names.index('source')
        return self._inputs[index]

    @property
    def threshold(self):
        r'''Gets `threshold` input of DetectSilence.

        ::

            >>> source = ugentools.WhiteNoise.ar()
            >>> source *= ugentools.Line.kr(start=1, stop=0)
            >>> detect_silence = ugentools.DetectSilence.kr(
            ...     done_action=DoneAction.FREE_SYNTH,
            ...     source=source,
            ...     threshold=0.0001,
            ...     time=1.0,
            ...     )
            >>> detect_silence.threshold
            0.0001

        Returns ugen input.
        '''
        index = self._ordered_input_names.index('threshold')
        return self._inputs[index]

    @property
    def time(self):
        r'''Gets `time` input of DetectSilence.

        ::

            >>> source = ugentools.WhiteNoise.ar()
            >>> source *= ugentools.Line.kr(start=1, stop=0)
            >>> detect_silence = ugentools.DetectSilence.kr(
            ...     done_action=DoneAction.FREE_SYNTH,
            ...     source=source,
            ...     threshold=0.0001,
            ...     time=1.0,
            ...     )
            >>> detect_silence.time
            1.0

        Returns ugen input.
        '''
        index = self._ordered_input_names.index('time')
        return self._inputs[index]<|endoftext|>


<commit_before>"""
github3.legacy
==============

This module contains legacy objects for use with the Search_ section of the
API.

.. _Search: http://developer.github.com/v3/search/

"""

from .models import GitHubCore
from re import match

class LegacyIssue(GitHubCore):
    """The :class:`LegacyIssue <LegacyIssue>` object. This object is only
    every used in conjuction with the :func:`search_issues
    <github3.github.GitHub.search_issues>`. Unfortunately, GitHub hasn't
    updated the search functionality to use the objects as they exist now.
    """
    def __init__(self, issue, session=None):
        super(LegacyIssue, self).__init__(issue, session)
        self._gravid = issue.get('gravatar_id', '')
        self._pos = issue.get('position', 0)
        self._num = issue.get('number', 0)
        self._votes = issue.get('votes', 0)
        self._created = None
        if issue.get('created_at'):
            created = issue.get('created_at')[:-6] + 'Z'
            self._created = self._strptime(created)
        self._updated = None
        if issue.get('updated_at'):
            updated = issue.get('updated_at')[:-6] + 'Z'
            self._updated = self._strptime(updated)
        self._comments = issue.get('comments', 0)
        self._body = issue.get('body', '')
        self._title = issue.get('title', '')
        self._html = issue.get('html_url', '')
        self._user = issue.get('user', '')
        self._labels = issue.get('labels', [])
        self._state = issue.get('state', '')

    def __repr__(self):
        return '<Legacy Issue [{0}:{1}]>'.format(self._user, self._num)

    @property
    def body(self):
        """Body of the issue"""
        return self._body

    @property
    def comments(self):
        """Number of comments on the issue"""
        return self._comments

    @property
    def created_at(self):
        """datetime object representing the creation of the issue"""
        return self._created

    @property
    def gravatar_id(self):
        """id of the gravatar account"""
        return self._gravid

    @property
    def html_url(self):
        """URL of the issue"""
        return self._html

    @property
    def labels(self):
        """list of labels applied to this issue"""
        return self._labels

    @property
    def number(self):
        """Issue number"""
        return self._num

    @property
    def position(self):
        """Position"""
        return self._pos

    @property
    def state(self):
        """State of the issue, i.e., open or closed"""
        return self._state

    @property
    def title(self):
        """Title of the issue"""
        return self._title

    @property
    def updated_at(self):
        """datetime object representing the last time the issue was updated"""
        return self._updated

    @property
    def user(self):
        """User's login"""
        return self._user

    @property
    def votes(self):
        """Number of votes on this issue. Probably effectively deprecated"""
        return self._votes

class LegacyRepo(GitHubCore):
    """The :class:`LegacyRepo <LegacyRepo>` object. This wraps data returned
    using the :func:`search_repos <github3.github.GitHub.search_repos>`"""
    def __init__(self, repo, session=None):
        super(LegacyRepo, self).__init__(repo, session)
        self._created = None
        if repo.get('created'):
            created = repo.get('created')[:-6] + 'Z'
            self._created = self._strptime(created)
        self._desc = repo.get('description', '')
        self._followers = repo.get('followers', 0)
        self._fork = repo.get('fork', False)
        self._forks = repo.get('forks', 0)
        self._has_dl = repo.get('has_downloads', False)
        self._has_is = repo.get('has_issues', False)
        self._has_w = repo.get('has_wiki', False)
        self._home = repo.get('homepage', '')
        self._lang = repo.get('language', '')
        self._name = repo.get('name', '')
        self._open = repo.get('open_issues', 0)
        self._owner = repo.get('owner', '')
        self._priv = repo.get('private', False)
        self._pushed = None
        if repo.get('pushed_at'):
            pushed = repo.get('pushed_at')[:-6] + 'Z'
            self._pushed = self._strptime(pushed)
        self._score = repo.get('score', 0.0)
        self._sz = repo.get('size', 0)
        self._type = repo.get('type', 'repo')
        self._user = repo.get('username', '')
        self._url = repo.get('url', '')
        self._watchers = repo.get('watchers', 0)

    def __repr__(self):
        return '<Legacy Repo [{0}/{1}]>'.format(self._owner, self._name)

    @property
    def created(self):
        """datetime object representing the date of creation of this repo"""
        return self._created

    @property
    def created_at(self):
        """datetime object representing the date of creation of this repo"""
        return self._created

    @property
    def description(self):
        """description of this repository"""
        return self._desc

    @property
    def followers(self):
        """Number of followers"""
        return self._followers

    @property
    def forks(self):
        """Number of forks of this repository"""
        return self._forks

    def has_downloads(self):
        """Checks if this repository has downloads"""
        return self._has_dl

    def has_issues(self):
        """Checks if this repository has issues"""
        return self._has_is

    def has_wiki(self):
        """Checks if this repository has a wiki"""
        return self._has_w

    @property
    def homepage(self):
        """URL of the website for this repository"""
        return self._home

    def is_fork(self):
        """Checks if this repository is a fork"""
        return self._fork

    @property
    def language(self):
        """Language used in this repository"""
        return self._lang

    @property
    def name(self):
        """Name of this repository"""
        return self._name

    @property
    def open_issues(self):
        """Number of open issues"""
        return self._open

    @property
    def owner(self):
        """Owner of this repository"""
        return self._owner

    def is_private(self):
        """Checks if this repository is private"""
        return self._priv

    @property
    def pushed(self):
        """datetime object representing the last time the repo was pushed to"""
        return self._pushed

    @property
    def pushed_at(self):
        """datetime object representing the last time the repo was pushed to"""
        return self._pushed

    @property
    def score(self):
        """Score"""
        return self._score

    @property
    def size(self):
        """Size of the repo"""
        return self._sz

    @property
    def type(self):
        """Type of object"""
        return self._type

    @property
    def user(self):
        """User"""
        return self._user

    @property
    def url(self):
        """URL of the project on GitHub"""
        return self._url

    @property
    def watchers(self):
        """Number of people watching this project"""
        return self._watchers

class LegacyUser(GitHubCore):
    """The :class:`LegacyUser <LegacyUser>` object. This handles information
    returned by :func:`search_users <github3.github.GitHub.search_users>`.
    """
    def __init__(self, user, session=None):
        super(LegacyUser, self).__init__(user, session)
        self._created = None
        if user.get('created'):
            created = user.get('created')
            if not match(r'\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$', created):
                created = created[:-6] + 'Z'
            self._created = self._strptime(created)
        self._followers = user.get('followers', 0)
        # same as followers_count
        self._fullname = user.get('fullname', '')
        self._gravid = user.get('gravatar_id', '')
        self._id = user.get('id', '')
        self._lang = user.get('language', '')
        self._loc = user.get('location', '')
        self._login = user.get('login', '')
        # name: same as fullname
        self._pubrepo = user.get('public_repo_count', 0)
        self._pushed = None
        if user.get('pushed'):
            pushed = user.get('pushed')[:-5] + 'Z'
            self._pushed = self._strptime(pushed)
        self._rec = user.get('record', '')
        self._repos = user.get('repos', 0)
        self._score = user.get('score', 0.0)
        self._type = user.get('type', 'user')
        # username: same as login

    def __repr__(self):
        return '<Legacy User [{0}]>'.format(self._login)

    @property
    def created(self):
        """datetime object representing when the account was created"""
        return self._created

    @property
    def created_at(self):
        """datetime object representing when the account was created"""
        return self._created

    @property
    def followers(self):
        """Number of followers"""
        return self._followers

    @property
    def followers_count(self):
        """Number of followers"""
        return self._followers

    @property
    def fullname(self):
        """Full name of this user"""
        return self._fullname

    @property
    def gravatar_id(self):
        """Gravatar id for this user"""
        return self._gravid

    @property
    def id(self):
        """Unique id of this user"""
        return self._id

    @property
    def language(self):
        """Language"""
        return self._lang

    @property
    def location(self):
        """Location of this user"""
        return self._loc

    @property
    def login(self):
        """username for the user"""
        return self._login

    @property
    def name(self):
        """Full name of the user"""
        return self._fullname

    @property
    def public_repo_count(self):
        """Number of public repos owned by this user"""
        return self._pubrepo

    @property
    def pushed(self):
        """datetime representing the last time this user pushed"""
        return self._pushed

    @property
    def record(self):
        """User's record"""
        return self._rec

    @property
    def repos(self):
        """Number of repos owned by the user"""
        return self._repos

    @property
    def score(self):
        """Score"""
        return self._score

    @property
    def type(self):
        """Type of user"""
        return self._type
<commit_msg>Reworked legacy module.<commit_after>"""
github3.legacy
==============

This module contains legacy objects for use with the Search_ section of the
API.

.. _Search: http://developer.github.com/v3/search/

"""

from .models import GitHubCore
from re import match

class LegacyIssue(GitHubCore):
    """The :class:`LegacyIssue <LegacyIssue>` object. This object is only
    every used in conjuction with the :func:`search_issues
    <github3.github.GitHub.search_issues>`. Unfortunately, GitHub hasn't
    updated the search functionality to use the objects as they exist now.
    """
    def __init__(self, issue, session=None):
        super(LegacyIssue, self).__init__(issue, session)
        #: id of the gravatar account
        self.gravatar_id = issue.get('gravatar_id', '')
        #: Position
        self.position = issue.get('position', 0)
        #: Issue number
        self.number = issue.get('number', 0)
        #: Number of votes on this issue. Probably effectively deprecated
        self.votes = issue.get('votes', 0)
        #: datetime object representing the creation of the issue
        self.created_at = None
        if issue.get('created_at'):
            created = issue.get('created_at')[:-6] + 'Z'
            self.created_at = self._strptime(created)

        #: datetime object representing the last time the issue was updated
        self.updated_at = None
        if issue.get('updated_at'):
            updated = issue.get('updated_at')[:-6] + 'Z'
            self.updated_at = self._strptime(updated)
        #: Number of comments on the issue
        self.comments = issue.get('comments', 0)
        #: Body of the issue
        self.body = issue.get('body', '')
        #: Title of the issue
        self.title = issue.get('title', '')
        #: URL of the issue
        self.html_url = issue.get('html_url', '')
        #: User's login
        self.user = issue.get('user', '')
        #: list of labels applied to this issue
        self.labels = issue.get('labels', [])
        #: State of the issue, i.e., open or closed
        self.state = issue.get('state', '')

    def __repr__(self):
        return '<Legacy Issue [{0}:{1}]>'.format(self.user, self.number)

class LegacyRepo(GitHubCore):
    """The :class:`LegacyRepo <LegacyRepo>` object. This wraps data returned
    using the :func:`search_repos <github3.github.GitHub.search_repos>`"""
    def __init__(self, repo, session=None):
        super(LegacyRepo, self).__init__(repo, session)
        #: datetime object representing the date of creation of this repo
        self.created_at = None
        if repo.get('created'):
            created = repo.get('created')[:-6] + 'Z'
            self.created_at = self._strptime(created)
        #: datetime object representing the date of creation of this repo
        self.created
        #: description of this repository
        self.description = repo.get('description', '')
        #: Number of followers
        self.followers = repo.get('followers', 0)
        self._fork = repo.get('fork', False)
        #: Number of forks of this repository
        self.forks = repo.get('forks', 0)
        self._has_dl = repo.get('has_downloads', False)
        self._has_is = repo.get('has_issues', False)
        self._has_w = repo.get('has_wiki', False)
        #: URL of the website for this repository
        self.homepage = repo.get('homepage', '')
        #: Language used in this repository
        self.language = repo.get('language', '')
        #: Name of this repository
        self.name = repo.get('name', '')
        #: Number of open issues
        self.open_issues = repo.get('open_issues', 0)
        #: Owner of this repository
        self.owner = repo.get('owner', '')
        self._priv = repo.get('private', False)
        #: datetime object representing the last time the repo was pushed to
        self.pushed = None
        if repo.get('pushed_at'):
            pushed = repo.get('pushed_at')[:-6] + 'Z'
            self.pushed = self._strptime(pushed)
        #: datetime object representing the last time the repo was pushed to
        self.pushed_at
        #: Score
        self.score = repo.get('score', 0.0)
        #: Size of the repo
        self.size = repo.get('size', 0)
        #: Type of object
        self.type = repo.get('type', 'repo')
        #: User's login
        self.user = repo.get('username', '')
        #: URL of the project on GitHub
        self.url = repo.get('url', '')
        #: Number of people watching this project
        self.watchers = repo.get('watchers', 0)

    def __repr__(self):
        return '<Legacy Repo [{0}/{1}]>'.format(self.owner, self.name)

    def has_downloads(self):
        """Checks if this repository has downloads"""
        return self._has_dl

    def has_issues(self):
        """Checks if this repository has issues"""
        return self._has_is

    def has_wiki(self):
        """Checks if this repository has a wiki"""
        return self._has_w

    def is_private(self):
        """Checks if this repository is private"""
        return self._priv

class LegacyUser(GitHubCore):
    """The :class:`LegacyUser <LegacyUser>` object. This handles information
    returned by :func:`search_users <github3.github.GitHub.search_users>`.
    """
    def __init__(self, user, session=None):
        super(LegacyUser, self).__init__(user, session)
        #: datetime object representing when the account was created
        self.created = None
        if user.get('created'):
            created = user.get('created')
            if not match(r'\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$', created):
                created = created[:-6] + 'Z'
            self.created = self._strptime(created)
        #: datetime object representing when the account was created
        self.created_at = self.created

        # same as followers_count
        #: Number of followers
        self.followers = user.get('followers', 0)
        #: Number of followers
        self.followers_count = self.followers
        #: Full name of this user
        self.fullname = user.get('fullname', '')
        #: Gravatar id for this user
        self.gravatar_id = user.get('gravatar_id', '')
        #: Unique id of this user
        self.id = user.get('id', '')
        #: Language
        self.language = user.get('language', '')
        #: Location of this user
        self.location = user.get('location', '')
        #: username for the user
        self.login = user.get('login', '')
        #: Full name of this user
        self.name = user.get('fullname', '')
        #: Number of public repos owned by this user
        self.public_repo_count = user.get('public_repo_count', 0)
        #: datetime representing the last time this user pushed
        self.pushed = None
        if user.get('pushed'):
            pushed = user.get('pushed')[:-5] + 'Z'
            self.pushed = self._strptime(pushed)
        #: datetime representing the last time this user pushed
        self.pushed_at = self.pushed
        #: User's record
        self.record = user.get('record', '')
        #: Number of repos owned by the user
        self.repos = user.get('repos', 0)
        #: Score
        self.score = user.get('score', 0.0)
        #: Type of user
        self.type = user.get('type', 'user')
        # username: same as login

    def __repr__(self):
        return '<Legacy User [{0}]>'.format(self.login)
<|endoftext|>


<commit_before>from xml.sax.saxutils import escape
import codecs
import json
import sys

class JsonOutput():
    def __init__(self, groups, board, inject_xml_file_name):
        all_json=dict()
        all_json['version']=1
        all_json['uid']=1  #COMPONENT_INFORMATION.comp_metadata_type
        all_json['scope']="Firmware"
        all_params=[]
        all_json['parameters']=all_params

        #xml_parameters = ET.Element("parameters")
        #xml_version = ET.SubElement(xml_parameters, "version")
        #xml_version.text = "3"
        #xml_version = ET.SubElement(xml_parameters, "parameter_version_major")
        #xml_version.text = "1"
        #xml_version = ET.SubElement(xml_parameters, "parameter_version_minor")
        #xml_version.text = "15"

        schema_map = {
                        "short_desc": "shortDesc",
			"long_desc": "longDesc",
			"unit": "units",
			}
        schema_map_typed = {
			"min": "min",
			"max": "max",
			"increment": "increment",
			}
        schema_map_fix_type = {
			"reboot_required": ("rebootRequired", bool),
			"decimal": ("decimalPlaces", int),
			}
        allowed_types = { "Uint8", "Int8", "Uint16", "Int16", "Uint32", "Int32", "Float"}

        last_param_name = ""
        board_specific_param_set = False
        for group in groups:
            group_name=group.GetName()

            def get_typed_value(value: str, type_name: str):
                if type_name == 'Float': return float(value)
                if type_name == 'Int32': return int(value)
                return value

            for param in group.GetParams():
                if (last_param_name == param.GetName() and not board_specific_param_set) or last_param_name != param.GetName():
                    curr_param=dict()
                    curr_param['name'] = param.GetName()
                    type_name = param.GetType().capitalize()
                    curr_param['type'] = type_name
                    if not type_name in allowed_types:
                        print("Error: %s type not supported: curr_param['type']" % (curr_param['name'],curr_param['type']) )
                        sys.Exit(1)
                    curr_param['default'] = get_typed_value(param.GetDefault(), type_name)

                    curr_param['group'] = group_name
                    if param.GetCategory():
                        curr_param['category'] = param.GetCategory()

                    if param.GetVolatile():
                        curr_param['volatile'] = True

                    last_param_name = param.GetName()
                    for code in param.GetFieldCodes():
                        value = param.GetFieldValue(code)
                        if code == "board":
                            if value == board:
                                board_specific_param_set = True
                                # JSON schema has no field for board_specific schema. Ignore.
                            else:
                                #xml_group.remove(xml_param)
                                continue
                        else:
                            #map PX4 param field names to schema names
                            if code in schema_map:
                                curr_param[schema_map[code]] = value
                            elif code in schema_map_typed:
                                curr_param[schema_map_typed[code]] = get_typed_value(value, type_name)
                            elif code in schema_map_fix_type:
                                curr_param[schema_map_fix_type[code][0]] = schema_map_fix_type[code][1](value)
                            else:
                                print('ERROR: Field not in json schema: %s' % code)
                                sys.exit(1)

                if last_param_name != param.GetName():
                    board_specific_param_set = False

                enum_codes=param.GetEnumCodes() or '' # Gets numerical values for parameter.
                if enum_codes:
                    enum_codes=sorted(enum_codes,key=float)
                    codes_list=list()
                    for item in enum_codes:
                        code_dict=dict()
                        code_dict['value']=get_typed_value(item, type_name)
                        code_dict['description']=param.GetEnumValue(item)
                        codes_list.append(code_dict)
                    curr_param['values'] = codes_list

                if len(param.GetBitmaskList()) > 0:
                    bitmasks_list=list()
                    for index in param.GetBitmaskList():
                        bitmask_dict=dict()
                        bitmask_dict['index']=int(index)
                        bitmask_dict['description']=param.GetBitmaskBit(index)
                        bitmasks_list.append(bitmask_dict)
                    curr_param['bitmask'] = bitmasks_list

                all_params.append(curr_param)

        #Json string output.
        self.output = json.dumps(all_json,indent=2)

    def Save(self, filename):
        with codecs.open(filename, 'w', 'utf-8') as f:
            f.write(self.output)

<commit_msg>parameters: remove uid from json output<commit_after>from xml.sax.saxutils import escape
import codecs
import json
import sys

class JsonOutput():
    def __init__(self, groups, board, inject_xml_file_name):
        all_json=dict()
        all_json['version']=1
        all_json['scope']="Firmware"
        all_params=[]
        all_json['parameters']=all_params

        schema_map = {
                        "short_desc": "shortDesc",
			"long_desc": "longDesc",
			"unit": "units",
			}
        schema_map_typed = {
			"min": "min",
			"max": "max",
			"increment": "increment",
			}
        schema_map_fix_type = {
			"reboot_required": ("rebootRequired", bool),
			"decimal": ("decimalPlaces", int),
			}
        allowed_types = { "Uint8", "Int8", "Uint16", "Int16", "Uint32", "Int32", "Float"}

        last_param_name = ""
        board_specific_param_set = False
        for group in groups:
            group_name=group.GetName()

            def get_typed_value(value: str, type_name: str):
                if type_name == 'Float': return float(value)
                if type_name == 'Int32': return int(value)
                return value

            for param in group.GetParams():
                if (last_param_name == param.GetName() and not board_specific_param_set) or last_param_name != param.GetName():
                    curr_param=dict()
                    curr_param['name'] = param.GetName()
                    type_name = param.GetType().capitalize()
                    curr_param['type'] = type_name
                    if not type_name in allowed_types:
                        print("Error: %s type not supported: curr_param['type']" % (curr_param['name'],curr_param['type']) )
                        sys.Exit(1)
                    curr_param['default'] = get_typed_value(param.GetDefault(), type_name)

                    curr_param['group'] = group_name
                    if param.GetCategory():
                        curr_param['category'] = param.GetCategory()

                    if param.GetVolatile():
                        curr_param['volatile'] = True

                    last_param_name = param.GetName()
                    for code in param.GetFieldCodes():
                        value = param.GetFieldValue(code)
                        if code == "board":
                            if value == board:
                                board_specific_param_set = True
                                # JSON schema has no field for board_specific schema. Ignore.
                            else:
                                #xml_group.remove(xml_param)
                                continue
                        else:
                            #map PX4 param field names to schema names
                            if code in schema_map:
                                curr_param[schema_map[code]] = value
                            elif code in schema_map_typed:
                                curr_param[schema_map_typed[code]] = get_typed_value(value, type_name)
                            elif code in schema_map_fix_type:
                                curr_param[schema_map_fix_type[code][0]] = schema_map_fix_type[code][1](value)
                            else:
                                print('ERROR: Field not in json schema: %s' % code)
                                sys.exit(1)

                if last_param_name != param.GetName():
                    board_specific_param_set = False

                enum_codes=param.GetEnumCodes() or '' # Gets numerical values for parameter.
                if enum_codes:
                    enum_codes=sorted(enum_codes,key=float)
                    codes_list=list()
                    for item in enum_codes:
                        code_dict=dict()
                        code_dict['value']=get_typed_value(item, type_name)
                        code_dict['description']=param.GetEnumValue(item)
                        codes_list.append(code_dict)
                    curr_param['values'] = codes_list

                if len(param.GetBitmaskList()) > 0:
                    bitmasks_list=list()
                    for index in param.GetBitmaskList():
                        bitmask_dict=dict()
                        bitmask_dict['index']=int(index)
                        bitmask_dict['description']=param.GetBitmaskBit(index)
                        bitmasks_list.append(bitmask_dict)
                    curr_param['bitmask'] = bitmasks_list

                all_params.append(curr_param)

        #Json string output.
        self.output = json.dumps(all_json,indent=2)

    def Save(self, filename):
        with codecs.open(filename, 'w', 'utf-8') as f:
            f.write(self.output)

<|endoftext|>


<commit_before>#!/usr/bin/env python
"""
Copyright (c) 2015 Andrew Azarov
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
from datetime import datetime
from subprocess import Popen, PIPE
import yaml
import json
import signal

def assrt(check, error=None):
    if check:
        return 1
    else:
        if error:
            raise AssertionError(error)
        raise AssertionError

def do(line):
    return Popen(line, shell=True, bufsize=4096, stdout=PIPE)

class gnt_ext_backup(object):

    """gnt_ext_backup object

    Keyword arguments:

    unique_id           -- Unique identifier for backups (default set to current date and hour)
    retention_period    -- Backup retention period (default set to 1 day)
    backup_user_server  -- login@server.hostname credentials for SSH (default not set). Key auth for SSH should be setup beforehand,
                            I suggest chroot on target (or even jail) with only lz4, sh and find commands
    lv_backup_extension -- Extension for LV name of backup snapshot, without dot (default is bak)
    backup_folder       -- Remote server folder to place backups (default is ./upload/)
    backup_extension    -- Extension for resulting backup files (default is raw)
    compression         -- Dictionary of ingress and egress (default lz4 commands, do not remove the pipes!)
    debug               -- Do not perform actions if set, just print them

    """

    def __init__(self, **kwargs):
        # Set instance defaults
        # For simplicity set to timestamp
        self.unique_id = datetime.now().strftime("%Y-%m-%d-%H")
        self.retention_period = 7
        self.backup_user_server = None
        self.lv_backup_extension = 'bak'
        self.backup_folder = './upload/'
        self.backup_extension = 'raw'
        self.compression = {'egress': '| lz4 -1c |', 'ingress': 'lz4 -dc |'}
        self.debug = 0
        self.instances_names = None
        self.dd_buffer = '128M'
        self.lv_size = '1G'
        self.instances_complete = 0
        for i in ['unique_id', 'retention_period', 'backup_user_server',
                  'lv_backup_extension', 'backup_extension', 'backup_folder',
                  'compression', 'debug', 'instances_names', 'dd_buffer', 'lv_size']:
            if i in kwargs and kwargs[i]:
                setattr(self, i, kwargs[i])
            if i != 'instances_names':  # It can be None
                assrt(self.__dict__[i] is not None, "%s is not set" % i)

        if not self.instances_names:
            instances_raw_info = do('gnt-instance info --all')
        else:
            instances_raw_info = do(
                'gnt-instance info ' + ' '.join(self.instances_names))
        self.instances = yaml.load(instances_raw_info.stdout.read())

        self.ssh_cmd = 'ssh -oStrictHostKeyChecking=no ' + \
            self.backup_user_server
        assrt(isinstance(self.retention_period, int), "%r is not int" %
              self.retention_period)
        assrt([isinstance(ids, str) for ids in self.compression.values()],
              "%r is not correct" % self.compression)
        assrt(isinstance(self.lv_backup_extension, str), "%r is not str" %
              self.lv_backup_extension)
        assrt(isinstance(self.backup_extension, str), "%r is not str" %
              self.backup_extension)
        assrt(isinstance(self.backup_folder, str), "%r is not str" %
              self.backup_folder)
        assrt(isinstance(self.backup_user_server, str), "%r is not str" %
              self.backup_user_server)
        assrt(len(self.backup_user_server.split('@')) == 2, "%r is incorrect" %
              self.backup_user_server)
        signal.signal(signal.SIGHUP, self.wall)

    def wall(self, signum, frame):
        do('echo "{}% backup done" | wall'.format(self.instances_complete/len(self.instances))

    def perform_backup(self):
        for instance in self.instances:
            name = instance['Instance name']
            primary_node = [i['primary']
                            for i in instance['Nodes'] if 'primary' in i][0]
            disks = [(i['logical_id'], i['on primary'].split()[0])
                     for i in instance['Disks']]
            command = "gnt-cluster command -n " + primary_node
            for disk in disks:
                drive = {}
                drive['vg'], drive['lv'] = disk[0].split('/')

                print(
                    '{}: Backing up {} {}'.format(self.unique_id, name, disk[0]))
                cmd_list = [
                    [
                        command,
                        "\"lvcreate -L" + self.lv_size + " -s -n",
                        '.'.join(
                            [drive['lv'], self.unique_id, self.lv_backup_extension]),
                        disk[1],
                        "\""
                    ],
                    [
                        command,
                        "\"dd if=" +
                        '.'.join(
                            [disk[1], self.unique_id, self.lv_backup_extension]),
                        "bs=" + self.dd_buffer,
                        self.compression['egress'],
                        self.ssh_cmd,
                        "'" + self.compression['ingress'],
                        "dd bs=" + self.dd_buffer,
                        "of=" + self.backup_folder +
                        '.'.join(
                            [self.unique_id, drive['lv'], name, primary_node, self.backup_extension]),
                        "'\""
                    ],
                    [
                        command,
                        "\"lvremove -f",
                        '.'.join(
                            [disk[0], self.unique_id, self.lv_backup_extension]),
                        "\""
                    ]
                ]
                for cmd in cmd_list:
                    if self.debug:
                        print(' '.join(cmd))
                    else:
                        do(' '.join(cmd)).wait()
                print('{}: Done {} {}'.format(self.unique_id, name, disk[0]))
                print('-' * 100)
                self.instances_complete += 1
        cmd = [
            self.ssh_cmd,
            "\"",
            'find',
            self.backup_folder,
            '-name \'' + '.'.join(
                ['*', '*', '*', '*', self.backup_extension]) + '\'',
            '-ctime +' + str(self.retention_period),
            '-delete',
            "\""
        ]
        if self.debug:
            print(' '.join(cmd))
        else:
            do(' '.join(cmd)).wait()
        print('Done cleaning old backups')
        print('-' * 100)

if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument("-i",
                        "--id",
                        dest='unique_id',
                        type=str,
                        default=None,
                        help="Unique id to identify backups, default is date with hour",
                        required=False)
    parser.add_argument("-n",
                        "--instances_names",
                        dest='instances_names',
                        type=str,
                        nargs='+',
                        help="Space separated list of instances to backup",
                        required=False)
    parser.add_argument("-r",
                        "--retention_period",
                        dest='retention_period',
                        type=int,
                        default=None,
                        help="Backup retention period in days, default is 1",
                        required=False)
    parser.add_argument("-b",
                        "--backup_user_server",
                        dest='backup_user_server',
                        type=str,
                        default=None,
                        help="Backup login and server ssh style: login@backup.server",
                        required=True)
    parser.add_argument("-l",
                        "--lv_backup_extension",
                        type=str,
                        dest='lv_backup_extension',
                        default=None,
                        help="LV backup snapshot extension to identify",
                        required=False)
    parser.add_argument("-e",
                        "--backup_extension",
                        type=str,
                        dest='backup_extension',
                        default=None,
                        help="Backup extension for files on the backup target",
                        required=False)
    parser.add_argument("-f",
                        "--backup_folder",
                        type=str,
                        dest='backup_folder',
                        default=None,
                        help="Backup folder on remote server",
                        required=False)
    parser.add_argument("-c",
                        "--compression",
                        type=json.loads,
                        dest='compression',
                        default=None,
                        help="JSON array like {'egress': '| lz4 -1c |', 'ingress': 'lz4 -dc >'}",
                        required=False)
    parser.add_argument("-d",
                        "--debug",
                        type=int,
                        dest='debug',
                        default=None,
                        help="If debug is 1 - disable performing actions and just print them out",
                        required=False)
    parser.add_argument("--lv_size",
                        type=str,
                        dest='lv_size',
                        default=None,
                        help="LV snapshot size - default 1G, check man lvcreate for snapshot sizes",
                        required=False)
    parser.add_argument("--dd_buffer",
                        type=str,
                        dest='dd_buffer',
                        default=None,
                        help="dd buffer size, default 128MB for fast gbit transfers, adjust to your memory and network size",
                        required=False)
    a = parser.parse_args()
    arguments = {}
    for i in ['unique_id', 'retention_period', 'backup_user_server',
              'lv_backup_extension', 'backup_extension', 'backup_folder',
              'compression', 'debug', 'instances_names', 'lv_size', 'dd_buffer']:
        if hasattr(a, i) and getattr(a, i):
            arguments[i] = getattr(a, i)
    backup_job = gnt_ext_backup(**arguments)
    backup_job.perform_backup()
<commit_msg>Minor fixes<commit_after>#!/usr/bin/env python
"""
Copyright (c) 2015 Andrew Azarov
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
from datetime import datetime
from subprocess import Popen, PIPE
import yaml
import json
import signal

def assrt(check, error=None):
    if check:
        return 1
    else:
        if error:
            raise AssertionError(error)
        raise AssertionError

def do(line):
    return Popen(line, shell=True, bufsize=4096, stdout=PIPE)

class gnt_ext_backup(object):

    """gnt_ext_backup object

    Keyword arguments:

    unique_id           -- Unique identifier for backups (default set to current date and hour)
    retention_period    -- Backup retention period (default set to 1 day)
    backup_user_server  -- login@server.hostname credentials for SSH (default not set). Key auth for SSH should be setup beforehand,
                            I suggest chroot on target (or even jail) with only lz4, sh and find commands
    lv_backup_extension -- Extension for LV name of backup snapshot, without dot (default is bak)
    backup_folder       -- Remote server folder to place backups (default is ./upload/)
    backup_extension    -- Extension for resulting backup files (default is raw)
    compression         -- Dictionary of ingress and egress (default lz4 commands, do not remove the pipes!)
    debug               -- Do not perform actions if set, just print them

    """

    def __init__(self, **kwargs):
        # Set instance defaults
        # For simplicity set to timestamp
        self.unique_id = datetime.now().strftime("%Y-%m-%d-%H")
        self.retention_period = 7
        self.backup_user_server = None
        self.lv_backup_extension = 'bak'
        self.backup_folder = './upload/'
        self.backup_extension = 'raw'
        self.compression = {'egress': '| lz4 -1c |', 'ingress': 'lz4 -dc |'}
        self.debug = 0
        self.instances_names = None
        self.dd_buffer = '128M'
        self.lv_size = '1G'
        self.instances_complete = 0
        for i in ['unique_id', 'retention_period', 'backup_user_server',
                  'lv_backup_extension', 'backup_extension', 'backup_folder',
                  'compression', 'debug', 'instances_names', 'dd_buffer', 'lv_size']:
            if i in kwargs and kwargs[i]:
                setattr(self, i, kwargs[i])
            if i != 'instances_names':  # It can be None
                assrt(self.__dict__[i] is not None, "%s is not set" % i)

        if not self.instances_names:
            instances_raw_info = do('gnt-instance info --all')
        else:
            instances_raw_info = do(
                'gnt-instance info ' + ' '.join(self.instances_names))
        self.instances = yaml.load(instances_raw_info.stdout.read())

        self.ssh_cmd = 'ssh -oStrictHostKeyChecking=no ' + \
            self.backup_user_server
        assrt(isinstance(self.retention_period, int), "%r is not int" %
              self.retention_period)
        assrt([isinstance(ids, str) for ids in self.compression.values()],
              "%r is not correct" % self.compression)
        assrt(isinstance(self.lv_backup_extension, str), "%r is not str" %
              self.lv_backup_extension)
        assrt(isinstance(self.backup_extension, str), "%r is not str" %
              self.backup_extension)
        assrt(isinstance(self.backup_folder, str), "%r is not str" %
              self.backup_folder)
        assrt(isinstance(self.backup_user_server, str), "%r is not str" %
              self.backup_user_server)
        assrt(len(self.backup_user_server.split('@')) == 2, "%r is incorrect" %
              self.backup_user_server)
        signal.signal(signal.SIGHUP, self.wall)

    def wall(self, signum, frame):
        do('echo "{}% backup done" | wall'.format(self.instances_complete / len(self.instances)))

    def perform_backup(self):
        for instance in self.instances:
            name = instance['Instance name']
            primary_node = [i['primary']
                            for i in instance['Nodes'] if 'primary' in i][0]
            disks = [(i['logical_id'], i['on primary'].split()[0])
                     for i in instance['Disks']]
            command = "gnt-cluster command -n " + primary_node
            for disk in disks:
                drive = {}
                drive['vg'], drive['lv'] = disk[0].split('/')

                print(
                    '{}: Backing up {} {}'.format(self.unique_id, name, disk[0]))
                cmd_list = [
                    [
                        command,
                        "\"lvcreate -L" + self.lv_size + " -s -n",
                        '.'.join(
                            [drive['lv'], self.unique_id, self.lv_backup_extension]),
                        disk[1],
                        "\""
                    ],
                    [
                        command,
                        "\"dd if=" +
                        '.'.join(
                            [disk[1], self.unique_id, self.lv_backup_extension]),
                        "bs=" + self.dd_buffer,
                        self.compression['egress'],
                        self.ssh_cmd,
                        "'" + self.compression['ingress'],
                        "dd bs=" + self.dd_buffer,
                        "of=" + self.backup_folder +
                        '.'.join(
                            [self.unique_id, drive['lv'], name, primary_node, self.backup_extension]),
                        "'\""
                    ],
                    [
                        command,
                        "\"lvremove -f",
                        '.'.join(
                            [disk[0], self.unique_id, self.lv_backup_extension]),
                        "\""
                    ]
                ]
                for cmd in cmd_list:
                    if self.debug:
                        print(' '.join(cmd))
                    else:
                        do(' '.join(cmd)).wait()
                print('{}: Done {} {}'.format(self.unique_id, name, disk[0]))
                print('-' * 100)
                self.instances_complete += 1
        cmd = [
            self.ssh_cmd,
            "\"",
            'find',
            self.backup_folder,
            '-name \'' + '.'.join(
                ['*', '*', '*', '*', self.backup_extension]) + '\'',
            '-ctime +' + str(self.retention_period),
            '-delete',
            "\""
        ]
        if self.debug:
            print(' '.join(cmd))
        else:
            do(' '.join(cmd)).wait()
        print('Done cleaning old backups')
        print('-' * 100)

if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument("-i",
                        "--id",
                        dest='unique_id',
                        type=str,
                        default=None,
                        help="Unique id to identify backups, default is date with hour",
                        required=False)
    parser.add_argument("-n",
                        "--instances_names",
                        dest='instances_names',
                        type=str,
                        nargs='+',
                        help="Space separated list of instances to backup",
                        required=False)
    parser.add_argument("-r",
                        "--retention_period",
                        dest='retention_period',
                        type=int,
                        default=None,
                        help="Backup retention period in days, default is 1",
                        required=False)
    parser.add_argument("-b",
                        "--backup_user_server",
                        dest='backup_user_server',
                        type=str,
                        default=None,
                        help="Backup login and server ssh style: login@backup.server",
                        required=True)
    parser.add_argument("-l",
                        "--lv_backup_extension",
                        type=str,
                        dest='lv_backup_extension',
                        default=None,
                        help="LV backup snapshot extension to identify",
                        required=False)
    parser.add_argument("-e",
                        "--backup_extension",
                        type=str,
                        dest='backup_extension',
                        default=None,
                        help="Backup extension for files on the backup target",
                        required=False)
    parser.add_argument("-f",
                        "--backup_folder",
                        type=str,
                        dest='backup_folder',
                        default=None,
                        help="Backup folder on remote server",
                        required=False)
    parser.add_argument("-c",
                        "--compression",
                        type=json.loads,
                        dest='compression',
                        default=None,
                        help="JSON array like {'egress': '| lz4 -1c |', 'ingress': 'lz4 -dc >'}",
                        required=False)
    parser.add_argument("-d",
                        "--debug",
                        type=int,
                        dest='debug',
                        default=None,
                        help="If debug is 1 - disable performing actions and just print them out",
                        required=False)
    parser.add_argument("--lv_size",
                        type=str,
                        dest='lv_size',
                        default=None,
                        help="LV snapshot size - default 1G, check man lvcreate for snapshot sizes",
                        required=False)
    parser.add_argument("--dd_buffer",
                        type=str,
                        dest='dd_buffer',
                        default=None,
                        help="dd buffer size, default 128MB for fast gbit transfers, adjust to your memory and network size",
                        required=False)
    a = parser.parse_args()
    arguments = {}
    for i in ['unique_id', 'retention_period', 'backup_user_server',
              'lv_backup_extension', 'backup_extension', 'backup_folder',
              'compression', 'debug', 'instances_names', 'lv_size', 'dd_buffer']:
        if hasattr(a, i) and getattr(a, i):
            arguments[i] = getattr(a, i)
    backup_job = gnt_ext_backup(**arguments)
    backup_job.perform_backup()
<|endoftext|>


<commit_before>from gosubl import gs
import sublime
import sys

if gs.PY3K:
	from something_borrowed.diff_match_patch.python3.diff_match_patch import diff_match_patch
else:
	from something_borrowed.diff_match_patch.python2.diff_match_patch import diff_match_patch

class MergeException(Exception):
	pass

def _merge(view, size, text, edit):
	def ss(start, end):
		return view.substr(sublime.Region(start, end))
	dmp = diff_match_patch()
	diffs = dmp.diff_main(ss(0, size), text)
	dmp.diff_cleanupEfficiency(diffs)
	i = 0
	dirty = False
	for d in diffs:
		k, s = d
		l = len(s)
		if k == 0:
			# match
			l = len(s)
			if ss(i, i+l) != s:
				raise MergeException('mismatch', dirty)
			i += l
		else:
			dirty = True
			if k > 0:
				# insert
				view.insert(edit, i, s)
				i += l
			else:
				# delete
				if ss(i, i+l) != s:
					raise MergeException('mismatch', dirty)
				view.erase(edit, sublime.Region(i, i+l))
	return dirty

def merge(view, size, text):
	edit = view.begin_edit()
	vs = view.settings()
	ttts = vs.get("translate_tabs_to_spaces")
	vs.set("translate_tabs_to_spaces", False)
	origin_src = view.substr(sublime.Region(0, view.size()))
	if not origin_src.strip():
		return (False, '')

	try:
		dirty = False
		err = ''
		if size < 0:
			size = view.size()
		dirty = _merge(view, size, text, edit)
	except MergeException as ex:
		dirty = True
		err = "Could not merge changes into the buffer, edit aborted: %s" % ex[0]
		view.replace(edit, sublime.Region(0, view.size()), origin_src)
	except Exception as ex:
		err = "where ma bees at?: %s" % ex
	finally:
		view.end_edit(edit)
		vs.set("translate_tabs_to_spaces", ttts)
		return (dirty, err)

def gs_init():
	pass

<commit_msg>* remove call to begin/end_edit<commit_after>from gosubl import gs
import sublime
import sys

if gs.PY3K:
	from something_borrowed.diff_match_patch.python3.diff_match_patch import diff_match_patch
else:
	from something_borrowed.diff_match_patch.python2.diff_match_patch import diff_match_patch

class MergeException(Exception):
	pass

def _merge(view, size, text, edit):
	def ss(start, end):
		return view.substr(sublime.Region(start, end))
	dmp = diff_match_patch()
	diffs = dmp.diff_main(ss(0, size), text)
	dmp.diff_cleanupEfficiency(diffs)
	i = 0
	dirty = False
	for d in diffs:
		k, s = d
		l = len(s)
		if k == 0:
			# match
			l = len(s)
			if ss(i, i+l) != s:
				raise MergeException('mismatch', dirty)
			i += l
		else:
			dirty = True
			if k > 0:
				# insert
				view.insert(edit, i, s)
				i += l
			else:
				# delete
				if ss(i, i+l) != s:
					raise MergeException('mismatch', dirty)
				view.erase(edit, sublime.Region(i, i+l))
	return dirty

def merge(view, size, text, edit):
	vs = view.settings()
	ttts = vs.get("translate_tabs_to_spaces")
	vs.set("translate_tabs_to_spaces", False)
	origin_src = view.substr(sublime.Region(0, view.size()))
	if not origin_src.strip():
		return (False, '')

	try:
		dirty = False
		err = ''
		if size < 0:
			size = view.size()
		dirty = _merge(view, size, text, edit)
	except MergeException as ex:
		dirty = True
		err = "Could not merge changes into the buffer, edit aborted: %s" % ex[0]
		view.replace(edit, sublime.Region(0, view.size()), origin_src)
	except Exception as ex:
		err = "where ma bees at?: %s" % ex
	finally:
		vs.set("translate_tabs_to_spaces", ttts)
		return (dirty, err)

def gs_init():
	pass

<|endoftext|>