def file(self): file_path = configuration.get("DownloadSite", "client_path") if not file_path or not os.path.exists(file_path): msg = "I can't serve the file {} because it doesn't exist.".format( file_path) mainlog.error(msg) raise cherrypy.HTTPError(404, message=msg) # Won't create an exception # I don't inject at update time because if one copies # a delivery_slips in, then that delivery_slips must be injected as well. public_ip = configuration.get("DEFAULT", "public_ip") if not public_ip: public_ip = guess_server_public_ip() mainlog.warn( "Server configuration is borken : missing DEFAULT/public_ip. I'll default to what I guessed instead : {}" .format(public_ip)) inject_public_ip_in_client(public_ip) return cherrypy.lib.static.serve_download( file_path, name=configuration.get("Globals", "codename") + '.zip')
def upload_template_document3(self, encoding_safe_filename, doc_id, uploaded_file): """ Create or replace a new template document. :param encoding_safe_filename: :param uploaded_file: :param doc_id: :return: """ mainlog.warn("upload_template_document3 : DEPRECATED") doc_id = int(doc_id) if doc_id == 0: mainlog.debug("upload_template_document3 : upload") file_id = documents_service.save_template(uploaded_file.file, encoding_safe_filename) return str(file_id) elif doc_id > 0: mainlog.debug( "upload_template_document3 : replace doc_id={}".format( doc_id)) documents_service.replace_template(doc_id, uploaded_file.file, encoding_safe_filename) return
def load_network_param(self): """ Load the network parameters. The priority is the config file first, then, if empty or not properly filled in, we guess it for net.cfg. This is for the delivery_slips side. :return: """ ip_address = None try: f = open(os.path.join(resource_dir, "net.cfg")) ip_address = f.read().strip() f.close() except Exception as ex: mainlog.warn("net.cfg file not found, using current configuration") return # if string_represents_ip_address(ip_address): mainlog.debug( "net.cfg file was read. Server is there : {}".format(ip_address)) if ":" in ip_address: host, port = ip_address.split(":") port = int(port) mainlog.debug("Address has port : {}:{}".format(host, port)) self.set_server_network_address(host, port, overwrite=False) else: self.set_server_network_address(ip_address, overwrite=False)
def typed_values_to_jsonable(metadata: SQLAMetaData, type_, recursive_set, value): mainlog.debug("typed_values_to_jsonable : {}, value is {}".format( type_, value)) # Check fundamental types first to optimize conversion speed if value is None: return None elif type_ in (int, float, str, bool, sqltypes.Integer, sqltypes.Float, sqltypes.String, sqltypes.Boolean): return value elif type_ == Decimal: return Decimal(value) elif type_ in (date, sqltypes.Date): return value.strftime("%Y-%m-%d") elif type_ == datetime: return value.strftime("%Y-%m-%dT%H:%M:%S.%f") elif isinstance(type_, Enum): return value elif type_ == KeyedTuplesSequence: return keyed_tuple_sequence_to_jsonable(metada, recursive_set, type_, value) elif type_ == sqlalchemy.util._collections.KeyedTuple: # We assume each keyed tuple only contains only jsonable values. # FIXME That's rather bold ! r = value._asdict() # mainlog.debug(r) return r elif type_ == bytes: # Python 3 return base64.b64encode(value).decode('ascii') elif isinstance(type_, Sequence): # In particular, this works for Sequence[KeyedTuples] return [ typed_values_to_jsonable(metadata, type_.target, recursive_set, v) for v in value ] elif isinstance(type_, Tuple): mainlog.debug("typed_values_to_jsonable : Tuple") assert len(type_.targets) == len( value ), "Value (of type tuple) length doesn't match prototype's length" return [ typed_values_to_jsonable(metadata, type_.targets[i], recursive_set, value[i]) for i in range(len(type_.targets)) ] elif metadata.is_class_mapped(type_): return sqla_to_dict(metadata, type_, value, recursive_set) elif hasattr(type_, 'to_jsonable'): return type_.to_jsonable(value) elif isinstance(value, object): raise Exception( "Unable to transform object to jsonable : {} - {} (was it mapped ? {})" .format(type_, value, metadata.is_class_mapped(type_))) # return value.__dict__ else: mainlog.warn("Unable to serialize type {}".format(type_)) return value
def upload_file(self, file_id, description, uploaded_file): mainlog.warn("upload_file : DEPRECATED") # !!! Deprecated, use upload_file2 which is unicode safe # mainlog.debug("upload_file {} {} {}".format(file_id,description,uploaded_file)) file_id = documents_service.save(int(file_id), uploaded_file.file, uploaded_file.filename, description) return str(file_id)
def upload_file2(self, file_id, description, encoding_safe_filename, uploaded_file): mainlog.warn("upload_file2 : DEPRECATED") # DEPRECATED # mainlog.debug(u"upload_file2 {} {} {}".format(file_id,description,uploaded_file)) file_id = documents_service.save(int(file_id), uploaded_file.file, encoding_safe_filename, description) return str(file_id)
def _register_service_for_in_process(self, service): registered = 0 for m_name, m in inspect.getmembers(service): if hasattr(m, '_json_callable'): self._json_caller.register_in_process_call(m) registered += 1 if registered > 0: mainlog.info("Registered {} methods on {}".format( registered, service)) else: mainlog.warn("Registered NO method for {}".format(service))
def load(self, config_path, config_spec): self._config_file = config_path self._config_spec = config_spec config_path = os.path.normpath(os.path.join(os.getcwd(), config_path)) mainlog.info("Reading configuration file -> {}".format(config_path)) mainlog.debug( "Reading configuration spec file -> {}".format(config_spec)) if not os.path.exists(config_path): mainlog.error( "Configuration file not found at {}".format(config_path)) raise Exception( "Configuration file not found at {}".format(config_path)) try: self.base_configuration = configobj.ConfigObj( infile=config_path, configspec=config_spec, encoding='utf-8') except UnicodeDecodeError: mainlog.warn( "The encoding of the config file is not UTF-8. I'll try {}". format(locale.getpreferredencoding())) self.base_configuration = configobj.ConfigObj( infile=config_path, configspec=config_spec, encoding=locale.getpreferredencoding()) self.base_configuration.validate(validate.Validator()) if 'Programs' not in self.base_configuration or 'pdf_viewer' not in self.base_configuration[ 'Programs'] or not self.base_configuration['Programs'][ 'pdf_viewer'] or not os.path.exists( self.base_configuration['Programs']['pdf_viewer']): if platform.system() == 'Linux': self.base_configuration['Programs']['pdf_viewer'] = 'xpdf' else: self.base_configuration['Programs'][ 'pdf_viewer'] = os.path.join(resource_dir, 'SumatraPDF.exe')
exit(0) else: exit(-1) p = path_to_config("server.cfg") if os.path.exists(p): load_configuration_server(p, "server_config_check.cfg") else: mainlog.error( "Configuration file not found (looked here : {}). You should use --make-config." .format(p)) exit(-1) if args.demo_database: mainlog.warn( "Creating a demonstration database with {} orders ! This will destroy the current database." .format(args.demo_database)) create_demo_database(args.demo_database) exit(0) if args.reset_database: try: create_blank_database(configuration.get("Database", "admin_url"), configuration.get("Database", "url")) exit(0) except Exception as ex: mainlog.exception(ex) exit(-1) elif args.restore_backup: try: