def secret_key(key=None, filename='invenio.cfg', silent=True): """Generate and append SECRET_KEY to invenio.cfg. Useful for the installation process. """ print(">>> Going to generate random SECRET_KEY...") try: d = get_instance_config_object(filename) except Exception as e: print("ERROR: ", str(e), file=sys.stderr) sys.exit(1) if len(d.__dict__.get('SECRET_KEY', '')) > 0: print("ERROR: SECRET_KEY is already filled.") sys.exit(1) from invenio.base.config import SECRET_KEY if current_app.config.get('SECRET_KEY') != SECRET_KEY: print("WARNING: custom config package already contains SECRET_KEY.", file=sys.stderr) print(">>> No need to generate secret key.") else: if key is None: key = generate_secret_key() with current_app.open_instance_resource(filename, 'a') as config_file: print('SECRET_KEY =', pformat(key), file=config_file) print(">>> SECRET_KEY appended to `%s`." % (config_file.name, ))
def update(filename='invenio.cfg', silent=True): """Update new config.py from conf options. The previous config.py is kept in a backup copy. """ d = get_instance_config_object(filename, silent) new_config = StringIO() keys = set(d.__dict__.keys()) | set(default_keys()) keys = list(keys) keys.sort() for key in keys: if key != key.upper(): continue value = d.__dict__.get(key, current_app.config[key]) type_ = type(value) prmt = key + ' (' + type_.__name__ + ') [' + pformat(value) + ']: ' new_value = raw_input(prmt) try: new_value = ast.literal_eval(new_value) except (SyntaxError, ValueError): pass print('>>>', key, '=', pformat(new_value)) print(key, '=', pformat(new_value), file=new_config) with current_app.open_instance_resource(filename, 'w') as config_file: config_file.write(new_config.getvalue())
def secret_key(key=None, filename='invenio.cfg', silent=True): """Generate and append SECRET_KEY to invenio.cfg. Useful for the installation process. """ print(">>> Going to generate random SECRET_KEY...") try: d = get_instance_config_object(filename) except Exception as e: print("ERROR: ", str(e), file=sys.stderr) sys.exit(1) if len(d.__dict__.get('SECRET_KEY', '')) > 0: print("ERROR: SECRET_KEY is already filled.") sys.exit(1) from ..config import SECRET_KEY if current_app.config.get('SECRET_KEY') != SECRET_KEY: print("WARNING: custom config package already contains SECRET_KEY.", file=sys.stderr) print(">>> No need to generate secret key.") else: if key is None: key = generate_secret_key() with current_app.open_instance_resource(filename, 'a') as config_file: print('SECRET_KEY =', pformat(key), file=config_file) print(">>> SECRET_KEY appended to `%s`." % (config_file.name, ))
def locate(filename='invenio.cfg'): """Print the location of the configuration file.""" try: with current_app.open_instance_resource(filename, 'r') as config_file: print(os.path.abspath(config_file.name)) except IOError: print("ERROR: configuration file does not exist.", file=sys.stderr) return 1
def init_db(fn): if 'epg' in fn: db = get_db(epg=True) else: db = get_db() if 'secret' in fn: with current_app.open_instance_resource(fn) as f: db.executescript(f.read().decode('utf8')) else: with current_app.open_resource(fn) as f: db.executescript(f.read().decode('utf8'))
def read_db_config(filename="dbconfig.json"): """Read database configuration data from a json file.""" config_data = None with current_app.open_instance_resource(filename, mode="r") as file: config_data = load(file) return config_data
def get_instance_config_object(filename='invenio.cfg', silent=True): """Get the configuration object from the given filename.""" d = imp.new_module('config') d.__file__ = filename try: with current_app.open_instance_resource(filename) as config_file: exec(compile(config_file.read(), filename, 'exec'), d.__dict__) except IOError as e: if not (silent and e.errno in (errno.ENOENT, errno.EISDIR)): e.strerror = 'Unable to load configuration file (%s)' % e.strerror raise return d
def torrent_file(): info_hash = request.args.get('info_hash') torrent_file_path = os.path.join(app.instance_path, info_hash+'.torrent') if info_hash is None or not redis.sismember('torrents', info_hash) \ or not os.path.isfile(torrent_file_path): abort(404) name = redis.hget(_get_torrent_key(info_hash), 'name') or info_hash filename = name + '.torrent' return send_file(app.open_instance_resource(info_hash+'.torrent'), as_attachment=True, attachment_filename=filename)
def build_flow(state=None): """Build Flow Object""" with current_app.open_instance_resource( current_app.config["YOUTUBE_API_CLIENT_SECRET_FILE"], "r" ) as json_file: client_config = json.load(json_file) flow = Flow.from_client_config( client_config, scopes=current_app.config["YOUTUBE_READ_WRITE_SSL_SCOPE"], state=state, ) flow.redirect_uri = url_for("user.setting_youtube_oauth_callback", _external=True) return flow
def torrent_file(): info_hash = request.args.get('info_hash') torrent_file_path = os.path.join(app.instance_path, info_hash + '.torrent') if info_hash is None or not redis.sismember('torrents', info_hash) \ or not os.path.isfile(torrent_file_path): abort(404) name = redis.hget(_get_torrent_key(info_hash), 'name') or info_hash filename = name + '.torrent' return send_file(app.open_instance_resource(info_hash + '.torrent'), as_attachment=True, attachment_filename=filename)
def build_index(): # reperisci dati di tutti i film movies = db.get_all_movies() # costruisci indice (spazio vettoriale e vettori dei film) raise Exception("INSERIRE ISTRUZIONI MANCANTI QUI") # vect = ... # dtm = ... # salva indice su file with current_app.open_instance_resource(INDEX_FILENAME, "wb") as f: pickle.dump((movies.id, vect, dtm), f) # mostra messaggio all'utente flash("Built matrix of movies names with {} terms".format( len(vect.get_feature_names())))
def search_movie(query_str): # carica indice da file with current_app.open_instance_resource(INDEX_FILENAME, "rb") as f: mids, vectorizer, dtm = pickle.load(f) # converti stringa cercata in vettore query_bow = vectorizer.transform([query_str]) # calcola similarità tra stringa cercata e titoli di tutti i film similarities = pd.Series(dtm.dot(query_bow.T).toarray().ravel(), index=mids).sort_values(ascending=False) # seleziona i 50 film con similarità maggiore similarities = similarities[similarities > 0].head(50) # reperisci informazioni sui film movies = db.get_movies([int(x) for x in similarities.index]) return movies
def get_wks(sheet_tab_name=None): scope = ['https://spreadsheets.google.com/feeds'] with current_app.open_instance_resource( 'service-credentials.json') as f: credentials = ServiceAccountCredentials.from_json_keyfile_dict( json.load(f), scope) gc = gspread.authorize(credentials) try: return (gc.open(current_app.config["SPREADSHEET_NAME"]).worksheet( sheet_tab_name or current_app.config["SHEET_TAB_NAME"])) except: return
def authorize(): try: auth_server.validate_consent_request() except OAuth2Error: return auth_server.create_authorization_response() federate = request.args.get('federate') if federate: return federate_login(federate) else: try: with current_app.open_instance_resource('federate.html','r') as f: return render_template_string(f.read(), qp=request.args) except IOError: return render_template('federate.html', qp=request.args)
def webhook(): """ Run a custom python script when requested. User can pull git repositories, log something to a file, or do anything else in there. :return: always 204 NO CONTENT """ try: with current_app.open_instance_resource('webhook.py', 'r') as script_file: exec(script_file.read( )) # if there is the 'webhook.py' script, we execute it's content except FileNotFoundError: pass return '', 204
def get_fedex_rates(self, origin_address=default_origin, weight=1.0): """ Get FedEx Rates using webservices :param origin_address: 'ShippingAddress' :param weight: in ounces :return: rate dict """ fedex_client = zeep.Client( current_app.open_instance_resource("RateService_v24.wsdl")) requested_shipment = { "Shipper": { "Address": { "PostalCode": origin_address.postal_code, "CountryCode": origin_address.country, "StateOrProvinceCode": origin_address.state, } }, "Recipient": { "Address": { "PostalCode": self.postal_code or 32703, "CountryCode": self.country or "US", } }, "RequestedPackageLineItems": { "Weight": { "Units": "LB", "Value": weight }, "GroupPackageCount": 1, }, "PackageCount": 1, } try: rate_request = fedex_client.service.getRates( WebAuthenticationDetail=WebAuthenticationDetail, ClientDetail=ClientDetail, Version=Version, RequestedShipment=requested_shipment, ) ground_rate = [ reply for reply in rate_request["RateReplyDetails"] if reply["ServiceType"] == "FEDEX_GROUND" ] ground_rate_value = ground_rate[0]["RatedShipmentDetails"][0][ "ShipmentRateDetail"]["TotalNetFedExCharge"]["Amount"] except (IndexError, zeep.exceptions.ValidationError): ground_rate_value = None return {"FEDEX_GROUND": ground_rate_value}
def status(sha): """Show the status of a commit. **deprecated** static files aren't used anymore. To be removed at some point. :param sha: identifier of a commit. """ try: with current_app.open_instance_resource( "status_{sha}.txt".format(sha=sha), "r") as f: status = f.read() except IOError: raise NotFound("{sha} was not found.".format(sha=sha)) status = status if len(status) > 0 else sha + ": Everything OK" return render_template("status.html", status=status)
def upload_from_url(id): ''' expects json. expects url,filename,resource_type ''' modelrun = ModelRun.query.get(id) if modelrun: if modelrun.progress_state == PROGRESS_STATES['NOT_STARTED']: try: data = json.loads(request.get_data()) except ValueError: return jsonify({'message': 'Please specify valid json'}), 400 if not ('url' in data and 'resource_type' in data and 'filename' in data): return jsonify({'message': 'Invalid Input Provided'}), 400 try: filedata = requests.get(data['url']) tmp_loc = os.path.join('/tmp/', data['filename']) with app.open_instance_resource(tmp_loc, 'wb') as f: f.write(filedata.content) resource_file = storage.upload(tmp_loc) resource_type = data['resource_type'] m = { 'modelrun_id': id, 'resource_type': resource_type, 'resource_name': resource_file.name, #'resource_url': url_for('modelresource.download_resource_by_name', name=resource_file.name, _external=True), 'resource_size': resource_file.size } resource = ModelResource.create(**m) return jsonify({ 'message': "Resource create for model run " + str(id), 'resource': modelresource_serializer(resource) }), 201 except Exception, e: return jsonify({'message': 'Couldn\'t get file from url.'}), 400 else: return jsonify({ 'message': 'Uploading resources to new modelrun is permitted only' }), 400
def set_(name, value, filename='invenio.cfg'): """Set instance config variable with `value`.""" name = name.upper() try: d = get_instance_config_object(filename) except Exception as e: print("ERROR: ", str(e), file=sys.stderr) sys.exit(1) if name in d.__dict__: print("ERROR: %s is already filled." % (name, )) sys.exit(1) try: value = ast.literal_eval(value) except (SyntaxError, ValueError): pass with current_app.open_instance_resource(filename, 'a') as config_file: print(name, '=', pformat(value), file=config_file)
def on_pull_request(self, data): errors = [] state = "failure" url = data["pull_request"]["commits_url"] errors = check_messages(url) state = "error" if len(errors) > 0 else "success" commit_sha = data["pull_request"]["head"]["sha"] with current_app.open_instance_resource("status_{sha}.txt".format(sha=commit_sha), "w+") as f: f.write("\n".join(errors)) body = dict( state=state, target_url=url_for("status", commit_sha=commit_sha, _external=True), description="\n".join(errors)[:130], ) requests.post( data["pull_request"]["statuses_url"], data=json.dumps(body), headers=current_app.config["HEADERS"] ) return body
def download_from_url(): url = request.form['banner-file-url'] filename = urlparse(url).path.split('/')[-1] # check file for illegal filetypes if not allowed_file(filename): flash('File not allowed.', 'error') return None filename = secure_filename(filename) # make sure filename is safe path = os.path.join(current_app.config['UPLOAD_FOLDER'], filename) with current_app.open_instance_resource(path, 'wb') as f: response = requests.get(url, stream=True) if not response.ok: flash("Couldn't download specified file.", 'error') return None # Write to file for block in response.iter_content(1024): if not block: break f.write(block) return filename
def upload_from_url(id): ''' expects json. expects url,filename,resource_type ''' modelrun = ModelRun.query.get(id) if modelrun: if modelrun.progress_state == PROGRESS_STATES['NOT_STARTED']: try: data = json.loads(request.get_data()) except ValueError: return jsonify({'message': 'Please specify valid json'}), 400 if not ('url' in data and 'resource_type' in data and 'filename' in data): return jsonify({'message': 'Invalid Input Provided'}), 400 try: filedata = requests.get(data['url']) tmp_loc = os.path.join('/tmp/', data['filename']) with app.open_instance_resource(tmp_loc, 'wb') as f: f.write(filedata.content) resource_file = storage.upload(tmp_loc) resource_type = data['resource_type'] m = { 'modelrun_id': id, 'resource_type': resource_type, 'resource_name': resource_file.name, #'resource_url': url_for('modelresource.download_resource_by_name', name=resource_file.name, _external=True), 'resource_size': resource_file.size } resource = ModelResource.create(**m) return jsonify({'message': "Resource create for model run " + str(id), 'resource': modelresource_serializer(resource)}), 201 except Exception, e: return jsonify({'message': 'Couldn\'t get file from url.'}), 400 else: return jsonify({'message': 'Uploading resources to new modelrun is permitted only'}), 400
def initial_value(taskname): _task = Task.query.filter_by(name=taskname).first() user = User.query.filter_by(id=_task.user_id).first() dest = os.path.join(current_app.config.get('UPLOADS_DEFAULT_DEST'), taskname) bladed = Bladed(os.path.join(dest, _task.bladed_filename)) only_in_bladed = ['P_DMGT'] with current_app.open_instance_resource('name_mapping.json') as f: symbols_name = OrderedDict(json.load(f)) symbols_value = {'params': [], 'filters': [], 'schedules': []} symbols_db_path = os.path.join(current_app.instance_path, 'symbols.db') pattern = re.compile(r'\d+$') symbols = SymbolDB() symbols.load_db(symbols_db_path) symbols.connect() p_name = [ p for p in symbols_name.keys() if 'P_' in p and p not in only_in_bladed ] f_name = [p for p in symbols_name.keys() if 'F_' in p] t_name = [p for p in symbols_name.keys() if 'T_' in p] p_queried = symbols.multi_query(p_name) f_queried = symbols.multi_query(f_name) t_queried = symbols.multi_query(t_name) symbols.close() t_queried = {pattern.sub("", k, 1): v for k, v in t_queried.items()} f_queried = {pattern.sub("", k, 1): v for k, v in f_queried.items()} xml_path = os.path.join(dest, _task.xml_filename) xml = XML() try: xml.open(xml_path) except FileNotFoundError: return jsonify(symbols_value) xml_values = {} for name in symbols_name.keys(): if name in only_in_bladed: xml_values[name] = "" continue xml_values.update(xml.find(name)) if task is not None: for name, value in xml_values.items(): if 'P_' in name: symbols_value['params'].append({ 'name': f'<span class="name text-theme" data-toggle="tooltip" data-placement="right" title="' f'{p_queried[name].at["Description_en_GB"] if name not in only_in_bladed and name in p_queried.keys() else symbols_name[name]["description_zh"]}' f'">{name}</span>', 'bladed_value': '-' if not symbols_name[name]['bladed'] else f'<input type="text" class="table-value-bladed text-primary" id="{name}-bladed" disabled value="{bladed.query(symbols_name[name]["bladed"])[1]}"' f'style="background-color:transparent;border:0;text-align:center;width:100px;">', 'symbol_value': '-' if name in only_in_bladed else f'<input type="text" class="table-value text-primary" id="{name}-symbol" disabled value="{value}"' f'style="background-color:transparent;border:0;text-align:center;width:100px;" onchange="checkChanged()">', 'description': symbols_name[name]["description_zh"] }) if 'T_' in name: for index in value.index: symbols_value['schedules'].append({ 'Name': f'<span class="name text-theme" data-toggle="tooltip" data-placement="right" title="' f'{t_queried[name].at["Description_en_GB"]}">{name}</span>', 'Enabled': f'<input type="text" class="table-value enable-col text-danger" id="{name}{index}-Enabled" disabled value="{value.at[0, "Enabled"]}"' f'style="background-color:transparent;border:0;text-align:center;width:50px;" onchange="checkChanged()">', 'Display_Name': '-', '0': '-' if '_0' not in value.columns else f'<input type="text" class="table-value text-primary" id="{name}{index}-0" disabled value="{value.at[index, "_0"]}"' f'style="background-color:transparent;border:0;text-align:center;width:100px;" onchange="checkChanged()">', '1': '-' if '_1' not in value.columns else f'<input type="text" class="table-value text-primary" id="{name}{index}-1" disabled value="{value.at[index, "_1"]}"' f'style="background-color:transparent;border:0;text-align:center;width:100px;" onchange="checkChanged()">', '2': '-' if '_2' not in value.columns else f'<input type="text" class="table-value text-primary" id="{name}{index}-2" disabled value="{value.at[index, "_2"]}"' f'style="background-color:transparent;border:0;text-align:center;width:100px;" onchange="checkChanged()">', '3': '-' if '_3' not in value.columns else f'<input type="text" class="table-value text-primary" id="{name}{index}-3" disabled value="{value.at[index, "_3"]}"' f'style="background-color:transparent;border:0;text-align:center;width:100px;" onchange="checkChanged()">', '4': '-' if '_4' not in value.columns else f'<input type="text" class="table-value text-primary" id="{name}{index}-4" disabled value="{value.at[index, "_4"]}"' f'style="background-color:transparent;border:0;text-align:center;width:100px;" onchange="checkChanged()">', '5': '-' if '_5' not in value.columns else f'<input type="text" class="table-value text-primary" id="{name}{index}-5" disabled value="{value.at[index, "_5"]}"' f'style="background-color:transparent;border:0;text-align:center;width:100px;" onchange="checkChanged()">', '6': '-' if '_6' not in value.columns else f'<input type="text" class="table-value text-primary" id="{name}{index}-6" disabled value="{value.at[index, "_6"]}"' f'style="background-color:transparent;border:0;text-align:center;width:100px;" onchange="checkChanged()">', '7': '-' if '_7' not in value.columns else f'<input type="text" class="table-value text-primary" id="{name}{index}-7" disabled value="{value.at[index, "_7"]}"' f'style="background-color:transparent;border:0;text-align:center;width:100px;" onchange="checkChanged()">', '8': '-' if '_8' not in value.columns else f'<input type="text" class="table-value text-primary" id="{name}{index}-8" disabled value="{value.at[index, "_8"]}"' f'style="background-color:transparent;border:0;text-align:center;width:100px;" onchange="checkChanged()">', '9': '-' if '_9' not in value.columns else f'<input type="text" class="table-value text-primary" id="{name}{index}-9" disabled value="{value.at[index, "_9"]}"' f'style="background-color:transparent;border:0;text-align:center;width:100px;" onchange="checkChanged()">' }) if 'F_' in name: for index in value.index: symbols_value['filters'].append({ 'Name': f'<span class="name text-theme" data-toggle="tooltip" data-placement="right" title="' f'{f_queried[name].at["Description_en_GB"]}">{name}</span>', 'Enabled': f'<input type="text" class="table-value enable-col text-danger" id="{name}{index}-Enabled" disabled value="{value.at[index, "Enabled"]}"' f'style="background-color:transparent;border:0;text-align:center;width:50px;" onchange="checkChanged()">', 'Display_Name': f'{index+1}', 'Numerator_Type': value.at[index, "Numerator_Type"], 'Denominator_Type': value.at[index, "Denominator_Type"], 'Numerator_TC': value.at[index, "Numerator_TC"], # f'<input type="text" class="table-value" disabled value="{value.at[index, "Numerator_TC"]}"' # f'style="background-color:transparent;border:0;text-align:center;width:100px;">', 'Denominator_TC': value.at[index, "Denominator_TC"], # f'<input type="text" class="table-value" disabled value="{value.at[index, "Denominator_TC"]}"' # f'style="background-color:transparent;border:0;text-align:center;width:100px;">', 'Numerator_Frequency': f'<input type="text" class="table-value num-frequency-col text-primary" id="{name}{index}-Numerator_Frequency" disabled value="{value.at[index, "Numerator_Frequency"]}"' f'style="background-color:transparent;border:0;text-align:center;width:100px;" onkeyup="equals(\'{name}{index}-Numerator_Frequency\', \'{name}{index}-Denominator_Frequency\')" onchange="checkChanged()">', 'Numerator_Damping_Ratio': f'<input type="text" class="table-value text-primary" id="{name}{index}-Numerator_Damping_Ratio" disabled value="{value.at[index, "Numerator_Damping_Ratio"]}"' f'style="background-color:transparent;border:0;text-align:center;width:100px;" onchange="checkChanged()">', 'Denominator_Frequency': f'<input type="text" class="table-value den-frequency-col text-primary" id="{name}{index}-Denominator_Frequency" disabled value="{value.at[index, "Denominator_Frequency"]}"' f'style="background-color:transparent;border:0;text-align:center;width:100px;" onkeyup="equals(\'{name}{index}-Denominator_Frequency\', \'{name}{index}-Numerator_Frequency\')" onchange="checkChanged()">', 'Denominator_Damping_Ratio': f'<input type="text" class="table-value text-primary" id="{name}{index}-Denominator_Damping_Ratio" disabled value="{value.at[index, "Denominator_Damping_Ratio"]}"' f'style="background-color:transparent;border:0;text-align:center;width:100px;" onchange="checkChanged()">', 'W0': f'<input type="text" class="table-value text-primary" id="{name}{index}-W0" disabled value="{value.at[index, "W0"]}"' f'style="background-color:transparent;border:0;text-align:center;width:100px;" onchange="checkChanged()">', 'Prewarping_Wc': f'<input type="text" class="table-value text-primary" id="{name}{index}-Prewarping_Wc" disabled value="{value.at[index, "Prewarping_Wc"]}"' f'style="background-color:transparent;border:0;text-align:center;width:100px;" onchange="checkChanged()">', }) return jsonify(symbols_value)
def index(): return send_file(current_app.open_instance_resource('trees/index.html'))
def init_db(): db = get_db() with current_app.open_instance_resource('schema.sql') as f: db.executescript(f.read().decode('utf-8'))
def smmze(): data = request.get_json() print(data, file=sys.stderr) print(request.data, file=sys.stderr) sm_type = data.get('type') senNum = 5 try: snum = int(data.get('num')) print(snum, file=sys.stderr) if snum > 0 and snum < 50: senNum = snum except: senNum = 5 if sm_type == 'text': text = data.get('data') if text != '': # send the highlighted text as well subject = ConRanker.getSubject(text) processed = ConRanker.summary(text, senNum) #summarize(text, senNum) summarized = [] for sentence in processed: summarized.append(str(sentence)) return jsonify(og=text, summary=summarized, subject=subject) #highlight='blahblah' return jsonify(og='', summary='', subject='') #highlight='' elif sm_type == 'url': #############################################################################3 link = data.get('data') errors = 'nothing here for now but will be changed in due time. when me president, they see' if link == '' or not validators.url(link): return 'Bad URL' r = requests.get(link, allow_redirects=True) print(r.status_code) if r.status_code == 200: conttype = r.headers['content-type'].split(';', 1)[0] print("ur content type is: ", conttype) filename = link.rsplit('/', 1)[1] print('downloading: ' + filename) if r and allowed_mime(conttype): ext = MIME_TO_EXT.get( conttype) #filename.rsplit('.', 1)[1].lower() filename = str(uuid.uuid4()) + '.' + ext basedir = os.path.abspath(os.path.dirname(__file__)) with app.open_instance_resource( os.path.join(basedir, app.config['UPLOAD_FOLDER'], filename), 'wb') as f: f.write(r.content) unprocessed = '' if not (ext == 'jpg' or ext == 'jpeg' or ext == 'png' or ext == 'gif'): unprocessed = textract.process( url_for('summary.uploaded_file', filename=filename)) else: img = Image.open( url_for('summary.uploaded_file', filename=filename)) unprocessed = pytesseract.image_to_string(img, lang='eng') try: unprocessed = unprocessed.decode('utf-8') except AttributeError: pass subject = ConRanker.getSubject(unprocessed) processed = ConRanker.summary( unprocessed, senNum) #summarize(unprocessed, sentenceNum) os.remove( os.path.join(basedir, app.config['UPLOAD_FOLDER'], filename)) summarized = [] for sentence in processed: summarized.append(str(sentence)) return jsonify(og=unprocessed, summary=summarized, subject=subject) return jsonify(error=errors) #############################################################################3 return 'test'
def classify_reviews(reviews): with current_app.open_instance_resource(CLASSIFIER_FILENAME, "rb") as f: model = pickle.load(f) proba = model.predict_proba(reviews) return pd.DataFrame(proba, columns=model.classes_)
def classify_review(review): with current_app.open_instance_resource(CLASSIFIER_FILENAME, "rb") as f: model = pickle.load(f) return pd.Series(model.predict_proba([review]).ravel(), index=model.classes_)
def train_review_classifier(): reviews = db.get_all_reviews() raise Exception("INSERIRE ISTRUZIONI MANCANTI QUI") with current_app.open_instance_resource(CLASSIFIER_FILENAME, "wb") as f: pickle.dump(model, f) flash("Trained review classification model")
def set_value(taskname): _task = Task.query.filter_by(name=taskname).first() # user = User.query.filter_by(id=_task.user_id).first() # dest = os.path.join(current_app.config.get( # 'UPLOADS_DEFAULT_DEST'), user.username, taskname) dest = os.path.join(current_app.config.get('UPLOADS_DEFAULT_DEST'), taskname) _task.status = "Working" data = request.json['data'] isgitted = request.json['isgitted'] description = request.json['description'] if request.json[ 'description'] else "Updated" cfg = current_app.config # git_path = os.path.join(cfg.get('UPLOADS_DEFAULT_DEST'), user.username) git_path = os.path.join(cfg.get('UPLOADS_DEFAULT_DEST')) bladed_data = { k.split('-')[0]: v['new'].strip() for k, v in data.items() if '-bladed' in k } symbol_data = { k: v['new'].strip() for k, v in data.items() if '-bladed' not in k } if bladed_data: with current_app.open_instance_resource('name_mapping.json') as f: symbols_name = OrderedDict(json.load(f)) bladed = Bladed(os.path.join(dest, _task.bladed_filename)) bladed_args = { symbols_name[k]['bladed']: v for k, v in bladed_data.items() } bladed.set(**bladed_args) new_name = request.json['newname'] if os.path.splitext(request.json['newname'])[-1] in ['.xml'] else \ request.json['newname'] + '.xml' new_name_path = os.path.join(dest, new_name) xml_path = os.path.join(dest, _task.xml_filename) if xml_path != new_name_path: shutil.copy(xml_path, new_name_path) _task.xml_filename = new_name xml = XML() try: xml.open(new_name_path) except FileNotFoundError: return None fine_data = {} pattern = re.compile(r'^(\S+?)(\d*)-(\S+)$') for name, data in symbol_data.items(): m = pattern.search(name) true_name, row, col = m.groups() col = f'_{col}' if col.isdigit() else col.replace('_', '') if true_name not in fine_data.keys(): fine_data[true_name] = {row: {col: data}} else: if row not in fine_data[true_name].keys(): fine_data[true_name][row] = {col: data} else: fine_data[true_name][row].update({col: data}) p_list = [p for p in symbol_data if 'P_' in p] t_list = [p for p in symbol_data if 'P_' not in p] xml.update(p_list, t_list, **fine_data) readme_text = os.path.join(dest, 'README.txt') with open(readme_text, 'a', encoding='utf-8') as f: f.write(description) _task.status = "Dirty" if _task.isgitted else "Saved" if isgitted: # 提交至Git并push git_commit_push(git_path, description) _task.status = "Clean" return initial_value(taskname)
def fd(self): if self._fd is None: self._fd = current_app.open_instance_resource( self.name + ".lock", "w") return self._fd