def push_to_github(self): """ Push changes to the artist-whitelist to CHIRP Github git-dir and work-tree must be specified because we are operating outside of the repo directory. TODO: remove abs paths """ git_dir = '/home/musiclib/chirpradio-machine/.git' work_tree = '/home/musiclib/chirpradio-machine' # commit changes commit_command = 'git --git-dir=%s --work-tree=%s commit %s -m "Adding new artists"' % ( git_dir, work_tree, artists._WHITELIST_FILE, ) commit_output = subprocess.check_output(commit_command, shell=True, stderr=subprocess.STDOUT) Messages.add_message(commit_output, 'success') # push changes push_command = 'git --git-dir=%s --work-tree=%s push' % (git_dir, work_tree) push_output = subprocess.check_output(push_command, shell=True, stderr=subprocess.STDOUT) Messages.add_message(push_output, 'success')
def delete(self, key): if auth.user_is_admin(): Group.get(key).delete() else: Messages.add('Only an administrator may delete groups. This ' + 'incident has been logged.') return self.redirect('/groups')
def delete(self, key): if auth.user_is_admin(): Project.get(key).delete() else: Messages.add('Only and administrator may delete projects. This ' + 'incident has been logged.') return self.redirect('/projects')
def signup(self): user = auth.User(auth.current_user()) if user.in_group(): Messages.add('You are already in a group') return self.redirect('/groups') else: return self.render('groups_signup')
class SwapPoint(object): def __init__(self, config, markets): self.swap_point_rate = config.config_data['swap_point_rate'] self.markets = markets self.messages = Messages(config.config_data['message_language']) def cal_net_swap_point(self, futures_type, position_yen): maturity_datetime = datetime.strptime(self.markets.futures[futures_type]['maturity_date'] + ' 00:00:00', '%Y/%m/%d %H:%M:%S') now_datetime = datetime.strptime(datetime.now().strftime('%Y/%m/%d') + ' 00:00:00', '%Y/%m/%d %H:%M:%S') delta = (maturity_datetime - now_datetime).days logger.debug(self.messages.show('CALCULATE_SWAP_POINT')) logger.debug('product_code : ' + self.markets.futures[futures_type]['product_code']) logger.debug('maturity date' + maturity_datetime.strftime('%Y/%m/%d %H:%M:%S')) logger.debug('now' + now_datetime.strftime('%Y/%m/%d %H:%M:%S')) logger.debug(self.messages.show('NUM_OF_SWAP_POINT_OCCURRENCES')) net_swap_point = 0 for i in range(delta): buf_swap_point = position_yen * self.swap_point_rate net_swap_point += buf_swap_point position_yen -= buf_swap_point logger.debug('[' + str(i+1) + '/' + str(delta) + ']') logger.debug(self.messages.show('NET_SWAP_POINT'), str(net_swap_point)) logger.debug(self.messages.show('ONE_SWAP_POINT'), str(buf_swap_point)) logger.debug(self.messages.show('POSITION_YEN') + str(position_yen)) return net_swap_point
def delete(self, key): if auth.user_is_admin(): Idea.get(key).delete() else: Messages.add('Only and administrator may delete submitted ideas. ' + 'This incident has been logged.') return self.redirect('/ideas')
def run(self): # First message needs to be the handshake data = self.connection.recv(BUFFER_SIZE) print(data) data = data.decode("utf-8") print(data) self.connection.send(b"ok") info_hash = Messages.getInfoHash(data) print(info_hash) path = FileTable.getByInfoHash(info_hash) print(path) file = open(path, "r") while True: data = self.connection.recv(BUFFER_SIZE) data = data.decode("utf-8") mt = int(Messages.getMessageType(data)) if (mt == 1): pieceNumber = int(Messages.getNPiece(data)) piece = ReadFile.getNPiece(file, pieceNumber, 65536) print(piece) self.connection.send(piece.encode()) if (mt == 2): break file.close() self.connection.send(b"finished") self.connection.close()
def create(self): """Handles the POST data for creating a group. Form Variables: name: the name of the new group public: true if the group should be joinable by the public """ if auth.logged_in(): name = self.request.get('name') public = self.request.get('public') == 'public' owner = auth.current_user() if Group.exists(name): Messages.add('A group with that name already exists') return self.redirect('/groups/signup') Group(name=name, public=public, owner=owner, members=[owner]).put() return self.redirect('/groups') else: Messages.add('You must be logged in to create a group') return self.redirect('/groups/signup')
def __init__(self, listen_count=None, serv_host=None, serv_port=None): if listen_count is None: listen_count = 5 if serv_host is None: serv_host = 'localhost' if serv_port is None: serv_port = 3490 # set server messages worker self.MsgWorker = Messages(host=serv_host, port=serv_port, backlog=listen_count) # set data base worker self.DbWorker = DbHandler() # set command worker self.CmdWorker = Commands() self.uid_link = {} self.user_list = [] self.server_password = '******' # initialize server socket self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.server.bind((serv_host, serv_port)) self.server.listen(listen_count) print(self.MsgWorker.welcome_string()) # set signal handler signal.signal(signal.SIGINT, self.sighandler)
def water_make_maxmin(self, var, start, stop, index, type): lim_dict = dict(wat_dist=(start, stop, 0, self.ny1max), wat_time=(0, self.ny1max, None, None)) if self.change_limits.isChecked(): ''' Get manually typed values ''' if (len(self.box_minw.text()) > 0 and len(self.box_maxw.text()) > 0): funcs = dict(wat_time=(self.box_minw, self.box_maxw), wat_dist=(self.box_minw, self.box_maxw)) min = float(funcs[type][0].text()) max = float(funcs[type][1].text()) else: Messages.no_limits('sediment or water') lims = lim_dict[type] min = varmin(self, var, lims) max = varmax(self, var, lims) else: lims = lim_dict[type] min = varmin(self, var, lims) max = varmax(self, var, lims) maxmin = check_minmax(self, min, max, index) return maxmin
def setUp(self): self.mutation_matrix = np.array( [[0.96, 0.02, 0., 0., 0., 0., 0., 0.02], [0.02, 0.96, 0.02, 0., 0., 0., 0., 0.], [0., 0.02, 0.96, 0.02, 0., 0., 0., 0.], [0., 0., 0.02, 0.96, 0.02, 0., 0., 0.], [0., 0., 0., 0.02, 0.96, 0.02, 0., 0.], [0., 0., 0., 0., 0.02, 0.96, 0.02, 0.], [0., 0., 0., 0., 0., 0.02, 0.96, 0.02], [0.02, 0., 0., 0., 0., 0., 0.02, 0.96]]) pchem_vals = [ 0.05, 0.15, 0.24, 0.41, 0.44, 0.51, 0.54, 0.79, 0.83, 0.92 ] site_ids = range(10) aa_ids = range(10) self.site_types = Ring_Site_Types(.25, zip(aa_ids, pchem_vals), zip(site_ids, pchem_vals), [1] * 10) self.code_matrix = np.ones((8, 10)) / 10 self.fitness_contributions = None self.message = Messages(self.code_matrix, self.site_types.fitness_matrix, self.mutation_matrix)
class Messages_Test(unittest.TestCase): def setUp(self): self.mutation_matrix = np.array( [[0.96, 0.02, 0., 0., 0., 0., 0., 0.02], [0.02, 0.96, 0.02, 0., 0., 0., 0., 0.], [0., 0.02, 0.96, 0.02, 0., 0., 0., 0.], [0., 0., 0.02, 0.96, 0.02, 0., 0., 0.], [0., 0., 0., 0.02, 0.96, 0.02, 0., 0.], [0., 0., 0., 0., 0.02, 0.96, 0.02, 0.], [0., 0., 0., 0., 0., 0.02, 0.96, 0.02], [0.02, 0., 0., 0., 0., 0., 0.02, 0.96]]) pchem_vals = [ 0.05, 0.15, 0.24, 0.41, 0.44, 0.51, 0.54, 0.79, 0.83, 0.92 ] site_ids = range(10) aa_ids = range(10) self.site_types = Ring_Site_Types(.25, zip(aa_ids, pchem_vals), zip(site_ids, pchem_vals), [1] * 10) self.code_matrix = np.ones((8, 10)) / 10 self.fitness_contributions = None self.message = Messages(self.code_matrix, self.site_types.fitness_matrix, self.mutation_matrix) def test_mutation_selection_matrix(self): mutation_selection_matrices = [ self.message._mutation_selection_matrix(i) for i in xrange(10) ] for i in xrange(10): self.assertTrue( np.allclose(mutation_selection_matrices[i], correct_mutation_selection_matrices[i]), msg="Mutation Selection matrix did not match for site index {}." .format(i)) def test_equilibrium_codon_usage(self): self.assertTrue( np.allclose( self.message. codon_usage, # Should default to calculating codon usage correct_equilibrium_codon_usage)) # at equilibrium def test_equilibrium_fitness_contributions(self): self.assertTrue( np.allclose( self.message. fitness_contributions, # Should also default to at equilibrium correct_equilibrium_fitness_contributions)) def test_nonequilibrium_fitness_contributions(self): self.message.calculate_at_codon_usage(nonequilibrium_codon_usage) fitness_contributions = self.message.fitness_contributions self.assertAlmostEqual(correct_nonequilibrium_fitness, np.prod(fitness_contributions))
def delete(self, key): """Deletes a project idea.""" if auth.user_is_admin(): Idea.get(key).delete() else: Messages.add('Only and administrator may delete submitted ' + 'ideas. This incident has been logged.') return self.redirect('/ideas')
def __init__(self, clientId, accessCode=None): self.__channelNames = ChannelNames(clientId) self.__messages = Messages(clientId, accessCode) self.__listeners = [] self.__connected = False self.__connection = None self.__channel = None self.__clientId = clientId
def delete(self, key): """Deletes a group.""" if auth.user_is_admin(): Group.get(key).delete() else: Messages.add('Only an administrator may delete groups. This ' + 'incident has been logged.') return self.redirect('/groups')
def signup(self): """Displays the group sign up form.""" user = auth.User(auth.current_user()) if user.in_group(): Messages.add('You are already in a group') return self.redirect('/groups') else: return self.render('groups_signup')
def get(self): generate_nml_out = subprocess.check_output('do_generate_collection_nml', shell=True, stderr=subprocess.STDOUT) Messages.add_message(generate_nml_out, 'success') install = 'install -m 0775 -g traktor output.nml /mnt/disk_array/traktor/TraktorProRootDirectory/new-collection.nml' subprocess.call(install, shell=True, stderr=subprocess.STDOUT) current_route.CURRENT_ROUTE = 'push'
def delete(self, key): """Deletes a project.""" if auth.user_is_admin(): Project.get(key).delete() else: Messages.add('Only and administrator may delete projects. This ' + 'incident has been logged.') return self.redirect('/projects')
class EventHandler(object): def __init__(self): self.dict = OrderedDict() self.dict['a'] = False self.dict['b'] = False self.dict['c'] = False self.dict['d'] = False self.dict['e'] = False self.dict['f'] = False self.messages = Messages() # 向服务器发送item def send(self): for key in self.dict: value = self.dict[key] if not value: self.item = key break if self.dict[key]: return host = self.messages.getValue("socket1", "host") port = int(self.messages.getValue("socket1", "port")) params = (("deliverItem", self.item), self.sendCallBack) # 检查当前item在服务器的状态,7次 Thread(target=TCPClient(host, port).send, args=params).start() self.check(self.item) def sendCallBack(self, params): if params[1]: print("Send", params[0], "成功") elif len(params) == 3: print("Send", params[0], "出现异常", params[2]) # 轮询检查服务器端item的还行状况,7次 def check(self, item): totalTimes = 7 times = 0 while times < totalTimes: time.sleep(1) if self.dict[item]: return host = self.messages.getValue("socket2", "host") port = int(self.messages.getValue("socket2", "port")) params = (("checkItemStatus", item), self.checkCallBack) Thread(target=TCPClient(host, port).send, args=params).start() times += 1 else: print("已经对", item, "发起", totalTimes, "次校验,没有结果. 任务终止.") def checkCallBack(self, params): if params[1]: self.dict[params[0]] = params[1] print("Check", params[0], "成功。当前队列状态为:", self.dict.items()) self.send() elif len(params) == 3: print("Check", params[0], "出现异常:", params[2]) else: print("Check", params[0], "未得到结果")
def do_push_artists(self): # patch credentials if not request.headers.get('Authorization'): abort(401) else: auth = request.headers['Authorization'].lstrip('Basic ') username, password = base64.b64decode(auth).split(':') if username and password: conf.CHIRPRADIO_AUTH = '%s %s' % (username, password) chirpradio.connect() else: abort(401) dry_run = False # reload artists from file artists._init() # Find all of the library artists all_library_artists = set(artists.all()) # Find all of the artists in the cloud. all_chirpradio_artists = set() mapped = 0 t1 = time.time() for art in models.Artist.fetch_all(): if art.revoked: continue std_name = artists.standardize(art.name) if std_name != art.name: #print "Mapping %d: %s => %s" % (mapped, art.name, std_name) mapped += 1 art.name = std_name idx = search.Indexer() idx._transaction = art.parent_key() idx.add_artist(art) if not dry_run: idx.save() all_chirpradio_artists.add(art.name) to_push = list(all_library_artists.difference(all_chirpradio_artists)) Messages.add_message("Pushing %d artists" % len(to_push), 'warning') while to_push: # Push the artists in batches of 50 this_push = to_push[:50] to_push = to_push[50:] idx = search.Indexer() for name in this_push: #print name art = models.Artist.create(parent=idx.transaction, name=name) idx.add_artist(art) if not dry_run: idx.save() #print "+++++ Indexer saved" Messages.add_message("Artist push complete. OK!", 'success')
def __init__(self, config, queue, markets, api): self.config = config self.markets = markets self.api = api self.best_ask_bid = BestAskBidAdapter(queue, 'spot_trading','weekly','biweekly','3month') self.trade_pairs = TradePairs() self.trade = Trade(self.api, config, markets) self.scheduler = Scheduler(self.config, self.markets) self.messages = Messages(self.config.config_data['message_language'])
def get(self): generate_nml_out = subprocess.check_output( 'do_generate_collection_nml', shell=True, stderr=subprocess.STDOUT) Messages.add_message(generate_nml_out, 'success') install = 'install -m 0775 -g traktor output.nml /mnt/disk_array/traktor/TraktorProRootDirectory/new-collection.nml' subprocess.call(install, shell=True, stderr=subprocess.STDOUT) current_route.CURRENT_ROUTE = 'push'
def update_group(self, key): if not auth.logged_in(): return self.redirect('/groups') user = auth.current_user() group = Group.get(key) if group.owner.user_id() != user.user_id() and not auth.user_is_admin(): Messages.add('Only the owner of the group owner may modify it') return self.redirect('/groups') name = self.request.get('name') public = self.request.get('public') == 'public' abandon = self.request.get('abandon-project') sub_text = self.request.get('submission-text') sub_url = self.request.get('submission-url') remove_submission = self.request.get_all('remove-submission') remove = self.request.get_all('remove') owner = self.request.get('owner') delete = self.request.get('delete') if delete: group.delete() return self.redirect('/groups') group.name = name group.public = public if abandon: group.project = None if sub_text and sub_url: Submission(text=sub_text, url=sub_url, group=group).put() for sub in Submission.get(remove_submission): sub.delete() pending = list(group.pending_users) for user in pending: approve = self.request.get("approve-%s" % user) if approve == "approve": group.members.append(user) group.pending_users.remove(user) elif approve == "refuse": group.pending_users.remove(user) group.owner = auth.user_from_email(owner) for user in remove: if auth.user_from_email(user) == group.owner: Messages.add('Cannot remove the group owner') return self.redirect('/groups/%s/edit' % key) else: group.members.remove(auth.user_from_email(user)) group.put() return self.redirect('/groups/%s' % key)
def __init__(self, newclass=False, ismessages=False, comments=False, configure=None,\ construct=None): self.configure = None if configure != None: self.configure = self._load_configure(configure) else: self.newclass = newclass self.comments = comments self.messages = Messages(ismessages) self.construct = construct
def __init__(self, page): self.page = page self.conf = conf.getInstance() messageLikes_addHeaders(page) messageComments_addHeaders(page) self.messages = Messages() self.scroll_pos = 0 self.num_messages = None self.url_preview_html = unicode(open('url_preview.html').read())
def __init__(self, host, port, vk, sk): threading.Thread.__init__(self) self.messages = Messages() self.mailbox = Mailbox(host, port, timeout=None) self.vk = vk self.sk = sk self.session = None self.number = None self.number_of_players = None self.players = {}
def leave(self, key): group = Group.get(key) user = auth.User(auth.current_user()) if user.group != group: Messages.add('You cannot leave a group you are not in') return self.redirect('/groups/%s' % key) group.members.remove(user.gae_user) group.put() return self.redirect('/groups')
def edit(self, key): if not auth.logged_in(): return self.redirect('/groups') user = auth.current_user() group = Group.get(key) if group.owner.user_id() == user.user_id() or auth.user_is_admin(): return self.render('groups_edit', { 'group': group }) else: Messages.add('Only the owner of this group may edit it') return self.redirect('/groups/%s' % key)
def edit(self, key): """Displays the group moderation form.""" if not auth.logged_in(): return self.redirect('/groups') user = auth.current_user() group = Group.get(key) if group.owner.user_id() == user.user_id() or auth.user_is_admin(): return self.render('groups_edit', {'group': group}) else: Messages.add('Only the owner of this group may edit it') return self.redirect('/groups/%s' % key)
def leave(self, key): """Removes the current user from the group's roster.""" group = Group.get(key) user = auth.User(auth.current_user()) if user.group != group: Messages.add('You cannot leave a group you are not in') return self.redirect('/groups/%s' % key) group.members.remove(user.gae_user) group.put() return self.redirect('/groups')
def claim(self, key): user = auth.User(auth.current_user()) group = user.group if group.owner.user_id() == auth.current_user().user_id(): project = Project.get(key) group.project = project group.put() return self.redirect('/groups/%s' % group.key()) else: Messages.add('You are not the owner of your group. Only the ' + 'owner of the group may select a project.') return self.redirect('/projects')
def approve(self, key): if auth.user_is_admin(): idea = Idea.get(key) Project(name=idea.name, description=idea.description, author=idea.author, post_time=idea.post_time).put() idea.delete() return self.redirect('/projects') else: Messages.add('Only and administrator may approve submitted ' + 'ideas. This incident has been logged.') return self.redirect('/ideas')
def claim(self, key): """Claims a project for a group.""" user = auth.User(auth.current_user()) group = user.group if group.owner.user_id() == auth.current_user().user_id(): project = Project.get(key) group.project = project group.put() return self.redirect('/groups/%s' % group.key()) else: Messages.add('You are not the owner of your group. Only the ' + 'owner of the group may select a project.') return self.redirect('/projects')
def approve(self, key): """Promotes a project idea to an accepted project.""" if auth.user_is_admin(): idea = Idea.get(key) Project(name=idea.name, description=idea.description, author=idea.author, post_time=idea.post_time).put() idea.delete() return self.redirect('/projects') else: Messages.add('Only and administrator may approve submitted ' + 'ideas. This incident has been logged.') return self.redirect('/ideas')
def _code_fitness(self, code, codon_usage=None): """ Gets the overall fitness of a code at the given codon usage, if no codon usage is given the codon usage of the currently fixed code at equilibrium is used. """ fitness_contributions = None if codon_usage is None: if self._last_messages is None: if code == self._current_code: msgs = Messages(self._current_code.effective_code_matrix, self._fitness_matrix, self._message_mutation_matrix) msgs.calculate_at_equilibrium() self._last_messages = msgs fitness_contributions = msgs.fitness_contributions else: self._code_fitness(self._current_code) if fitness_contributions is None: msgs = Messages(code.effective_code_matrix, self._fitness_matrix, self._message_mutation_matrix) msgs.calculate_at_codon_usage(self._last_messages.codon_usage) fitness_contributions = msgs.fitness_contributions overall_fitness = self._overall_fitness(fitness_contributions) if codon_usage is None and code == self._current_code: self._current_code_fitness = overall_fitness return overall_fitness
def getMessages(): messages = Messages() if request.method == 'POST': if request.data: # Content-Type: application/json # ie. - { "foo": "bar", "baz": "moe" } data = json.loads(request.data) else: # Content-Type: x-www-form-urlencoded # ie. - foo=bar&baz=moe data = dict((k, request.values[k]) for k in request.values.keys()) return jsonify(messages.add(data)) else: return jsonify(messages.getMessages())
def __init__(self, access_token=''): self.Account = Account(access_token=access_token) self.Apps = Apps(access_token=access_token) self.Audio = Audio(access_token=access_token) self.Auth = Auth(access_token=access_token) self.Board = Board(access_token=access_token) self.Database = Database(access_token=access_token) self.Docs = Docs(access_token=access_token) self.Other = Other(access_token=access_token) self.Fave = Fave(access_token=access_token) self.Friends = Friends(access_token=access_token) self.Gifts = Gifts(access_token=access_token) self.Groups = Groups(access_token=access_token) self.Likes = Likes(access_token=access_token) self.Market = Market(access_token=access_token) self.Messages = Messages(access_token=access_token) self.Newsfeed = Newsfeed(access_token=access_token) self.Notes = Notes(access_token=access_token) self.Notifications = Notifications(access_token=access_token) self.Pages = Pages(access_token=access_token) self.Photos = Photos(access_token=access_token) self.Places = Places(access_token=access_token) self.Polls = Polls(access_token=access_token) self.Search = Search(access_token=access_token) self.Stats = Stats(access_token=access_token) self.Status = Status(access_token=access_token) self.Storage = Storage(access_token=access_token) self.Users = Users(access_token=access_token) self.Utils = Utils(access_token=access_token) self.Video = Video(access_token=access_token) self.Wall = Wall(access_token=access_token) self.Widgets = Widgets(access_token=access_token)
def render(self, template_name, data={}): """Renders the template in the site wide manner. Retrieves the template data needed for the base template (login URL and text, user information, etc.) and merges it with the data passed to the method. Templates are retrieved from the template directory specified in the settings and appended with the suffix ".html" Arguments: template_name: the name of the template. this is the file name of the template without the .html extension. data: a dictionary containing data to be passed to the template. """ (login_text, login_url) = auth.login_logout(self.request) if auth.logged_in(): data['user'] = auth.User(auth.current_user()) data['admin'] = auth.user_is_admin() data['login_url'] = login_url data['login_text'] = login_text data['messages'] = Messages.get() path = os.path.join(settings.BASE_DIR, settings.TEMPLATE_DIR, "%s.html" % template_name) return self.response.out.write(template.render(path, data))
def download_song(self, metadata): dl_url = metadata['dl_url'] timestamp = int(time()) fn = [ 'bctcache', str(timestamp), metadata['artist_id'], metadata['track_id'] ] filename = '_'.join(fn) + '.mp3' path = os.path.join(c['cache_dir'], filename) # If exists, load, if not dl rel_path = c['cache_dir'].split('/')[-1] local_path = rel_path + '/' + filename if os.path.isfile(path) is True: MPDQueue.add_song(local_path) else: try: r = requests.get(dl_url, stream=dl_url, headers=HEADERS) except requests.exceptions.RequestException as e: print(e) quit() Messages().now_loading(metadata['artist'], metadata['track']) with open(path, 'wb') as t: total_length = int(r.headers.get('content-length', 0)) for chunk in progress.bar(r.iter_content(chunk_size=1024), expected_size=(total_length / 1024) + 1): if chunk: t.write(chunk) t.flush() self.write_ID3_tags(filename, metadata) MPDQueue().add_song(local_path)
def restart(): # this really could be health lock = None try: lock = LockFile('json/health.json', 'r') lock.acquire() with open('json/health.json', 'r') as json_file: data = json.load(json_file, encoding='utf-8') if request.method == 'POST': status = request.args.get('status', type=str) if status is None: print 'no status given, defaults to true' status = 'true' data['restart'] = status with open('json/health.json', 'w') as json_file: json_file.write(json.dumps(data)) lock.release() return 'restart set to %s' % status if request.method == 'GET': lock.release() return data['restart'] except IOError: if lock is not None: lock.release() return Messages.inventoryNotFound()
def __init__(self, url, channel, queue=None, config=None): self.url = url self.channel = channel self.queue = queue self.config = config self.messages = Messages(self.config.config_data['message_language']) self.ws = websocket_rev.WebSocketApp(self.url, header=None, on_open=self.on_open, on_message=self.on_message, on_error=self.on_error, on_close=self.on_close) #define websocket websocket_rev.enableTrace(True) self.is_running = True
def dump_dropbox(self): drop = chirp.library.dropbox.Dropbox() result = [] for path in sorted(drop._dirs): try: chirp_albums = chirp.library.album.from_directory(path, fast=True) except (IOError, chirp.library.album.AlbumError), e: Messages.add_message('There was an error at %s.' % path, 'error') # propagate error to ui so the album may be removed result.append({'path': path, 'title': 'There was an error at %s' % path, 'error': True}) continue # build albums for album in chirp_albums: json = album_to_json(album, path) result.append(json)
def get_startstop(self): start = self.numday_box.value() stop = self.numday_stop_box.value() if stop <= start: stop = len(self.time) start = 0 Messages.StartStop() return start, stop
def __init__(self): self.dict = OrderedDict() self.dict['a'] = False self.dict['b'] = False self.dict['c'] = False self.dict['d'] = False self.dict['e'] = False self.dict['f'] = False self.messages = Messages()
def get_user_or_auto_register(user_dict): try: username = user_dict.get("username", None) phone = user_dict.get("phone", None) if not username: return Users.get_none() user = Users.get_user(username) if not user: user = Users.add(user_dict) if not user: return Users.get_none() else: data_list = [phone] Messages.send_notification(phone, data_list) return user return user except Exception as ex: Logs.print_current_function_name_and_line_number(ex) return Users.get_none()
class Messages_Test(unittest.TestCase): def setUp(self): self.mutation_matrix = np.array([[ 0.96, 0.02, 0. , 0. , 0. , 0. , 0. , 0.02], [ 0.02, 0.96, 0.02, 0. , 0. , 0. , 0. , 0. ], [ 0. , 0.02, 0.96, 0.02, 0. , 0. , 0. , 0. ], [ 0. , 0. , 0.02, 0.96, 0.02, 0. , 0. , 0. ], [ 0. , 0. , 0. , 0.02, 0.96, 0.02, 0. , 0. ], [ 0. , 0. , 0. , 0. , 0.02, 0.96, 0.02, 0. ], [ 0. , 0. , 0. , 0. , 0. , 0.02, 0.96, 0.02], [ 0.02, 0. , 0. , 0. , 0. , 0. , 0.02, 0.96]]) pchem_vals = [0.05,0.15,0.24,0.41,0.44,0.51,0.54,0.79,0.83,0.92] site_ids = range(10) aa_ids = range(10) self.site_types = Ring_Site_Types(.25, zip(aa_ids, pchem_vals), zip(site_ids, pchem_vals), [1] * 10) self.code_matrix = np.ones((8,10)) / 10 self.fitness_contributions = None self.message = Messages(self.code_matrix, self.site_types.fitness_matrix, self.mutation_matrix) def test_mutation_selection_matrix(self): mutation_selection_matrices = [self.message._mutation_selection_matrix(i) for i in xrange(10)] for i in xrange(10): self.assertTrue(np.allclose(mutation_selection_matrices[i], correct_mutation_selection_matrices[i]), msg="Mutation Selection matrix did not match for site index {}.".format(i)) def test_equilibrium_codon_usage(self): self.assertTrue(np.allclose(self.message.codon_usage, # Should default to calculating codon usage correct_equilibrium_codon_usage)) # at equilibrium def test_equilibrium_fitness_contributions(self): self.assertTrue(np.allclose(self.message.fitness_contributions, # Should also default to at equilibrium correct_equilibrium_fitness_contributions)) def test_nonequilibrium_fitness_contributions(self): self.message.calculate_at_codon_usage(nonequilibrium_codon_usage) fitness_contributions = self.message.fitness_contributions self.assertAlmostEqual(correct_nonequilibrium_fitness, np.prod(fitness_contributions))
def _code_fitness(self, code, codon_usage = None): """ Gets the overall fitness of a code at the given codon usage, if no codon usage is given the codon usage of the currently fixed code at equilibrium is used. """ fitness_contributions = None if codon_usage is None: if self._last_messages is None: if code == self._current_code: msgs = Messages(self._current_code.effective_code_matrix, self._fitness_matrix, self._message_mutation_matrix) msgs.calculate_at_equilibrium() self._last_messages = msgs fitness_contributions = msgs.fitness_contributions else: self._code_fitness(self._current_code) if fitness_contributions is None: msgs = Messages(code.effective_code_matrix, self._fitness_matrix, self._message_mutation_matrix) msgs.calculate_at_codon_usage(self._last_messages.codon_usage) fitness_contributions = msgs.fitness_contributions overall_fitness = self._overall_fitness(fitness_contributions) if codon_usage is None and code == self._current_code: self._current_code_fitness = overall_fitness return overall_fitness
def add_artists(self): error = False drop = dropbox.Dropbox() new_artists = set() for au_file in drop.tracks(): try: tpe1 = au_file.mutagen_id3["TPE1"].text[0] except: Messages.add_messaage('** file: %r' % au_file.path, 'error') error = True # TODO propagate error to client raise if artists.standardize(tpe1) is None: new_artists.add(tpe1) # do not write if errors if not error and new_artists: to_print = list(new_artists) to_print.extend(artists.all()) to_print.sort(key=artists.sort_key) output = codecs.open(artists._WHITELIST_FILE, "w", "utf-8") for tpe1 in to_print: output.write(tpe1) output.write("\n") output.close() # reload whitelist from file artists._init() message = "Artist whitelist updated.<br>New artists added:<br>" message += "<br>".join(list(new_artists)) Messages.add_message(message, 'success') # push to github self.push_to_github()
def load_program(): # Load the top program from the server. # TODO: Check again later if no program is loaded try: reqUrl = SERVER + "/programs?q=" + urllib2.quote('{"executed":false}') print reqUrl program = urllib2.urlopen(reqUrl) program = program.read() print "program retrieved" print program program = json.loads(program) print "json parsed" print program program = program[0] print "program from index" print program say("Program loaded") # Load the user's Twitter bio if possible. if program.has_key("twitterHandle"): handle = program["twitterHandle"].replace("@", "") bio = get_twitter_bio(program["twitterHandle"]) if bio: say(Messages.greeting(program["twitterHandle"], bio)) else: say(Messages.greeting(program["twitterHandle"])) else: say("say", "Oops, could not find twitter handle.") return program except Exception as e: print "No program to load" print e return False
def join(self, key): group = Group.get(key) user = auth.User(auth.current_user()) if user.in_group(): Messages.add('You are already in a group') return self.redirect('/groups/%s' % key) if user.pending_join(): Messages.add('You have already applied to join a group') return self.redirect('/groups/%s' % key) if group.public: group.members.append(auth.current_user()) Messages.add('You have joined the group') else: group.pending_users.append(auth.current_user()) Messages.add('You have requested to join the group') group.put() return self.redirect('/groups/%s' % key)
def get_inventory(): """ TODO loop through inventory to make sure uuid of every item is unique only send invenotory if all unique """ lock = None try: lock = LockFile('json/inventory.json') lock.acquire() with open('json/inventory.json', 'r') as json_file: data = json.load(json_file) # this will throw correct errors lock.release() return json.dumps(data) except IOError: if lock is not None: lock.release() return Messages.inventoryNotFound()
def setUp(self): self.mutation_matrix = np.array([[ 0.96, 0.02, 0. , 0. , 0. , 0. , 0. , 0.02], [ 0.02, 0.96, 0.02, 0. , 0. , 0. , 0. , 0. ], [ 0. , 0.02, 0.96, 0.02, 0. , 0. , 0. , 0. ], [ 0. , 0. , 0.02, 0.96, 0.02, 0. , 0. , 0. ], [ 0. , 0. , 0. , 0.02, 0.96, 0.02, 0. , 0. ], [ 0. , 0. , 0. , 0. , 0.02, 0.96, 0.02, 0. ], [ 0. , 0. , 0. , 0. , 0. , 0.02, 0.96, 0.02], [ 0.02, 0. , 0. , 0. , 0. , 0. , 0.02, 0.96]]) pchem_vals = [0.05,0.15,0.24,0.41,0.44,0.51,0.54,0.79,0.83,0.92] site_ids = range(10) aa_ids = range(10) self.site_types = Ring_Site_Types(.25, zip(aa_ids, pchem_vals), zip(site_ids, pchem_vals), [1] * 10) self.code_matrix = np.ones((8,10)) / 10 self.fitness_contributions = None self.message = Messages(self.code_matrix, self.site_types.fitness_matrix, self.mutation_matrix)
def health(part): lock = None try: lock = LockFile('json/health.json') lock.acquire() # TODO need to check if scanner is actually on, file is not enough with open('json/health.json', 'r') as json_file: data = json.load(json_file, encoding='utf-8') #status = request.args.get('status', type=str) if request.method == 'POST': status = request.args.get('status', type=str) data[part] = status # print status with open('json/health.json', 'w') as json_file: json_file.write(json.dumps(data)) lock.release() return '' if request.method == 'GET': if part == 'scanner': # check if process is running running = RestUtils.find_process('barcode_scanner', False) if not running: data[part] = 'critical' with open('json/health.json', 'w') as json_file: json_file.write(json.dumps(data)) lock.release() return data[part] except IOError: if lock is not None: lock.release() return Messages.inventoryNotFound()
def update_group(self, key): """Updates a group with information from the moderation form. Form Variables: name: the name of the group public: true if the group should be joinable by the public abandon-project: true if the group moderator wants to abandon the current project submission-text: the text to be displayed for the new submission submission-url: the URL of the new submission remove-submission: a list of submissions to be removed remove: a list of users to be removed from the group owner: the owner of the group delete: true if the group moderator wants to disband the group """ if not auth.logged_in(): return self.redirect('/groups') user = auth.current_user() group = Group.get(key) if (group.owner.user_id() != user.user_id() and not auth.user_is_admin()): Messages.add('Only the owner of the group owner may modify it') return self.redirect('/groups') name = self.request.get('name') public = self.request.get('public') == 'public' abandon = self.request.get('abandon-project') sub_text = self.request.get('submission-text') sub_url = self.request.get('submission-url') remove_submission = self.request.get_all('remove-submission') remove = self.request.get_all('remove') owner = self.request.get('owner') delete = self.request.get('delete') if delete: group.delete() return self.redirect('/groups') group.name = name group.public = public if abandon: group.project = None if sub_text and sub_url: Submission(text=sub_text, url=sub_url, group=group).put() for sub in Submission.get(remove_submission): sub.delete() pending = list(group.pending_users) for user in pending: approve = self.request.get("approve-%s" % user) if approve == "approve": group.members.append(user) group.pending_users.remove(user) elif approve == "refuse": group.pending_users.remove(user) group.owner = auth.user_from_email(owner) for user in remove: if auth.user_from_email(user) == group.owner: Messages.add('Cannot remove the group owner') return self.redirect('/groups/%s/edit' % key) else: group.members.remove(auth.user_from_email(user)) group.put() return self.redirect('/groups/%s' % key)
def import_albums(self, inbox): prescan_timestamp = timestamp.now() # timestamp to be referenced by push step ImportTimeStamp.import_time_stamp = timestamp.now() Messages.add_message('Import time stamp set: %s' % ImportTimeStamp.import_time_stamp, 'warning') error_count = 0 album_count = 0 seen_fp = {} albums = [] transaction = [] db = database.Database(LIBRARY_DB) dirs = inbox._dirs for path in sorted(dirs): try: albs = album.from_directory(path) except analyzer.InvalidFileError, ex: album_message = "<br>***** INVALID FILE ERROR<br>" album_message += "<br>%s" % str(ex) Messages.add_message(album_message, 'error') error_count += 1 albums.append({'path': path, 'title': 'There was an error at %s' % path, 'error': True}) continue for alb in albs: # generate response album_path = path album_response = album_to_json(alb, album_path) # initialize error state # import process will halt if an error is seen album_error = False alb.drop_payloads() album_count += 1 # start album_message album_message = (u'"%s"<br>' % alb.title()).encode("utf-8") if alb.tags(): album_message += "(%s)" % ", ".join(alb.tags()) duration_ms = sum(au.duration_ms for au in alb.all_au_files) if alb.is_compilation(): album_message += "Compilation<br>" for i, au in enumerate(alb.all_au_files): album_message += " %02d:" % (i+1,) try: album_message += unicode(au.mutagen_id3["TPE1"]).encode("utf-8") except UnicodeDecodeError, e: album_message += "<br>***** Encoding ERROR<br>" album_message += "<br>%s" % str(ex) error_count += 1 album_error = True else: album_message += alb.artist_name().encode("utf-8") album_message += "<br>%d tracks / %d minutes<br>" % ( len(alb.all_au_files), int(duration_ms / 60000)) album_message += "ID=%015x<br>" % alb.album_id # Check that the album isn't already in library. collision = False for au in alb.all_au_files: if au.fingerprint in seen_fp: album_message += "<br>***** ERROR: DUPLICATE TRACK WITHIN IMPORT<br>" collision = True break fp_au_file = db.get_by_fingerprint(au.fingerprint) if fp_au_file is not None: album_message += "<br>***** ERROR: TRACK ALREADY IN LIBRARY" collision = True break seen_fp[au.fingerprint] = au if collision: album_error = True error_count += 1 # Attach a dummy volume # and timestamp alb.set_volume_and_import_timestamp(0xff, prescan_timestamp) try: alb.standardize() except (import_file.ImportFileError, album.AlbumError), ex: album_message += "<br>***** IMPORT ERROR<br>" album_message += "<br>%s" % str(ex) error_count += 1 album_error = True