def db_to_specifier(db_string): """ Return the database specifier for a database string. This accepts a database name or URL, and returns a database specifier in the format accepted by ``specifier_to_db``. It is recommended that you consult the documentation for that function for an explanation of the format. """ local_match = PLAIN_RE.match(db_string) remote_match = URL_RE.match(db_string) # If this looks like a local specifier: if local_match: return 'local:' + local_match.groupdict()['database'] # If this looks like a remote specifier: elif remote_match: # Just a fancy way of getting 3 variables in 2 lines... hostname, portnum, database = map(remote_match.groupdict().get, ('hostname', 'portnum', 'database')) local_url = settings._('COUCHDB_SERVER', 'http://127.0.0.1:5984/') localhost, localport = urlparse.urlparse(local_url)[1].split(':') # If it's the local server, then return a local specifier. if (localhost == hostname) and (localport == portnum): return 'local:' + database # Otherwise, prepare and return the remote specifier. return 'remote:%s:%s:%s' % (hostname, portnum, database) # Throw a wobbly. raise ValueError('Invalid database string: %r' % (db_string,))
def main(host=settings._('VIEW_SERVER_HOST')): host, port = host.split(':') # Should be in format 127.0.0.1:5936 port = int(port) server = ViewServer((host, port), ViewServerRequestHandler) server_thread = threading.Thread(target=server.serve_forever) server_thread.start() print 'View server running at %s on port %s' % (host, port)
def render(self, context): # We pre-stored these arguments in __init__, remember? value = settings._(*self.setting_args) # If a variable name was provided, use it. if self.var_name: context[self.var_name] = value return '' # Otherwise, render the setting as a string in the template. else: return str(value)
def get_server_from_db(db_string): """Return a CouchDB server instance from a database string.""" local_match = PLAIN_RE.match(db_string) remote_match = URL_RE.match(db_string) # If this looks like a local specifier: if local_match: return shortcuts.get_server() elif remote_match: hostname, portnum, database = map(remote_match.groupdict().get, ('hostname', 'portnum', 'database')) local_url = settings._('COUCHDB_SERVER', 'http://127.0.0.1:5984/') localhost, localport = urlparse.urlparse(local_url)[1].split(':') # If it's the local server, then return a local specifier. if (localhost == hostname) and (localport == portnum): return shortcuts.get_server() return shortcuts.get_server( server_url=('http://%s:%s' % (hostname, portnum))) raise ValueError('Invalid database string: %r' % (db_string,))
def specifier_to_db(db_spec): """ Return the database string for a database specifier. The database specifier takes a custom format for specifying local and remote databases. A local database is specified by the following format: local:<db_name> For example, a database called 'sessions' would be specified by the string ``'local:sessions'``. Remote databases are specified like this: remote:<host>:<port_num>:<db_name> For example, a database called 'log' on the server 'dev.example.com' at port number 5984 would be specified by ``'remote:dev.example.com:5984:log'``. These specifiers are translated into strings acceptable to CouchDB; local specs are turned into the database name alone, and remote specs are turned into ``'http://host:port/db_name'`` URLs. """ local_match = LOCAL_RE.match(db_spec) remote_match = REMOTE_RE.match(db_spec) plain_match = PLAIN_RE.match(db_spec) # If this looks like a local specifier: if local_match: return local_match.groupdict()['database'] # If this looks like a remote specifier: elif remote_match: # A fancy 'unpacking'... hostname, portnum, database = map(remote_match.groupdict().get, ('hostname', 'portnum', 'database')) local_url = settings._('COUCHDB_SERVER', 'http://127.0.0.1:5984/') localhost, localport = urlparse.urlparse(local_url)[1].split(':') # If it's local, return a local DB string. if (localhost == hostname) and (localport == portnum): return database # Otherwise, get a remote URL. return 'http://%s:%s/%s' % (hostname, portnum, database) # If this looks like a plain database name, return it. elif plain_match: return plain_match.groupdict()['database'] # Throw a wobbly. raise ValueError('Invalid database spec: %r' % (db_spec,))