def database(self, db_name): if self.uri: self._db = CouchDatabase(db_name, create=True, uri=self.uri) else: self._db = CouchDatabase(db_name, create=True) if self.record_type != None: self._refresh_treeview()#first time treeview is reset
def test_dont_delete_selected_rows(self): #create some records db = CouchDatabase(self.dbname, create=True) ids = [] for i in xrange(0, 10): ids.append( db.put_record( Record({ "key1_1": "val1_%s" % str(i), "iter_count": i, "record_type": self.record_type }))) #build the CouchGrid cw = CouchGrid(self.dbname, record_type=self.record_type) cw.selected_record_ids = [ids[0], ids[5], ids[9]] cw.remove_selected_rows(delete=False) cw.selected_record_ids = [ids[1], ids[4], ids[8]] cw.remove_selected_rows() self.assertEqual(self.db.get_record(ids[0]) is not None, True) self.assertEqual(self.db.get_record(ids[5]) is not None, True) self.assertEqual(self.db.get_record(ids[9]) is not None, True) self.assertEqual(self.db.get_record(ids[1]) is not None, True) self.assertEqual(self.db.get_record(ids[2]) is not None, True) self.assertEqual(self.db.get_record(ids[3]) is not None, True) self.assertEqual(self.db.get_record(ids[4]) is not None, True) self.assertEqual(self.db.get_record(ids[6]) is not None, True) self.assertEqual(self.db.get_record(ids[7]) is not None, True) self.assertEqual(self.db.get_record(ids[8]) is not None, True)
def test_selected_id_property(self): #create some records db = CouchDatabase(self.dbname, create=True) id1 = db.put_record( Record({ "key1_1": "val1_1", "key1_2": "val1_2", "key1_3": "val1_3", "record_type": self.record_type })) id2 = db.put_record( Record({ "key1_1": "val2_1", "key1_2": "val2_2", "key1_3": "val2_3", "record_type": self.record_type })) #build the CouchGrid cw = CouchGrid(self.dbname) cw.record_type = self.record_type #make sure the record ids are selected properly cw.selected_record_ids = [id1] self.assertEqual(cw.selected_record_ids[0], id1) cw.selected_record_ids = [id2] self.assertEqual(cw.selected_record_ids[0], id2)
def finish_initializing(self, builder): """finish_initalizing should be called after parsing the ui definition and creating a AboutXratersDialog object with it in order to finish initializing the start of the new AboutXratersDialog instance. """ #get a reference to the builder and set up the signals self.builder = builder self.builder.connect_signals(self) #set up couchdb and the preference info self.__db_name = "xraters" self.__database = CouchDatabase(self.__db_name, create=True) self.__preferences = None self.__key = None #set the record type and then initalize the preferences self.__record_type = "http://wiki.ubuntu.com/Quickly/RecordTypes/Xraters/Preferences" self.__preferences = self.get_preferences() #TODO:code for other initialization actions should be added here self.__entryWiiAddress = self.builder.get_object("entry_WiiAddress") self.__entryWiiAddress.set_text(self.__preferences['wiiAddress']) self.__filechooserOutput = self.builder.get_object("filechooser_output") self.__filechooserOutput.set_filename(self.__preferences['outputDir'])
def finish_initializing(self, builder): """finish_initalizing should be called after parsing the ui definition and creating a AboutWiitrackerDialog object with it in order to finish initializing the start of the new AboutWiitrackerDialog instance. """ #get a reference to the builder and set up the signals self.builder = builder self.builder.connect_signals(self) #set up couchdb and the preference info self.__db_name = "wiitracker" self.__database = CouchDatabase(self.__db_name, create=True) self.__preferences = None self.__key = None #set the record type and then initalize the preferences self.__record_type = "http://wiki.ubuntu.com/Quickly/RecordTypes/Wiitracker/Preferences" self.__preferences = self.get_preferences() #TODO:code for other initialization actions should be added here self.addressEntry = self.builder.get_object("AddressEntry") self.addressEntry.set_text(self.__preferences['wiiAddress']) self.dampingScale = self.builder.get_object("dampingScale") self.dampingScale.set_range(1, 100) self.dampingScale.set_value(self.__preferences['filterSize'])
def finish_initializing(self, builder): """Called while initializing this instance in __new__ finish_initalizing should be called after parsing the ui definition and creating a PreferencesGsqlDinkyDialog object with it in order to finish initializing the start of the new PerferencesGsqlDinkyDialog instance. Put your initialization code in here and leave __init__ undefined. """ # Get a reference to the builder and set up the signals. self.builder = builder self.builder.connect_signals(self) # Set up couchdb and the preference info. self._db_name = "gsql-dinky" self._database = CouchDatabase(self._db_name, create=True) self._preferences = None self._key = None # Set the record type and then initalize the preferences. self._record_type = ( "http://wiki.ubuntu.com/Quickly/RecordTypes/GsqlDinky/" "Preferences") self._preferences = self.get_preferences()
def finish_initializing(self, builder): """finish_initalizing should be called after parsing the ui definition and creating a AboutPhotobombDialog object with it in order to finish initializing the start of the new AboutPhotobombDialog instance. """ #get a reference to the builder and set up the signals self.builder = builder self.builder.connect_signals(self) #set up couchdb and the preference info self.__db_name = "photobomb" self.__database = CouchDatabase(self.__db_name, create=True) self.__preferences = None self.__key = None #set the record type and then initalize the preferences self.__record_type = "http://wiki.ubuntu.com/Quickly/RecordTypes/Photobomb/Preferences" self.__preferences = self.get_preferences()
def __init__(self): if CouchDatabase: self.db = sqlite3.connect(SQLITE_DB_FILENAME) sqlite_accounts = json.loads(self.List()) try: accounts = CouchDatabase(COUCH_DB_ACCOUNTS, create=False) records = accounts.get_records() except NoSuchDatabase: log.logger.info("Nothing to migrate from desktopcouch") return migrate = {} log.logger.info( "Looking for accounts to migrate from desktopcouch to sqlite") for record in records: id = str(record["value"]["protocol"] + "-" + record["value"]["username"]) migrate[id] = True if len(sqlite_accounts) > 0: for sqlite_account in sqlite_accounts: if record["value"]["protocol"] == sqlite_account[ "service"] and record["value"][ "username"] == sqlite_account["username"]: migrate[id] = False if migrate[id]: new_account = {} new_account["service"] = record["value"]["protocol"] new_account["id"] = record["value"]["_id"] for param in SERVICES[record["value"] ["protocol"]]["config"]: key = param.replace('private:', '') new_account[key] = record["value"][key] log.logger.info( "Found account %s - %s that needs to be migrated", new_account["service"], new_account["username"]) self.Create(json.dumps(new_account))
def test_all_from_database(self): #create some records db = CouchDatabase(self.dbname, create=True) db.put_record( Record({ "key1_1": "val1_1", "key1_2": "val1_2", "key1_3": "val1_3", "record_type": self.record_type })) db.put_record( Record({ "key1_1": "val2_1", "key1_2": "val2_2", "key1_3": "val2_3", "record_type": self.record_type })) #build the CouchGrid cw = CouchGrid(self.dbname) cw.record_type = self.record_type #make sure there are three columns and two rows self.assertEqual(cw.get_model().get_n_columns(), 4) self.assertEqual(len(cw.get_model()), 2)
def __init__( self, database_name, record_type=None, dictionaries=None, editable=False, keys=None, type_hints=None, uri=None): """Create a new Couchwidget arguments: database_name - specify the name of the database in the desktop couchdb to use. If the specified database does not exist, it will be created. optional arguments: record_type - a string to specify the record_type to use in retrieving and creating records. Note that if no records exist in the CouchDB then the keys argument must also be used or a RuntimeError will result. dictionaries - a list of dictionaries to initialize in the grid. If these haven't been added to desktopcouch, the will be automatically persisted and updated using the recored_type specified. Any previously saved data of the same record_type will also be displayed. keys - a list of strings specifying keys to use in the columns of the CouchGrid. The keys will also be used for the column titles and keys in desktop couch. If a record does not contain a value for a specified key the CouchGrid will simply display an empty cell of the appropriate type. If the widget is set to editable, the user will be able to add values to the database. The types for the columns will be inferred by the key based on some conventions. the key "id" is assumed to be an integer, as is any key ending in " count". A key ending in "?" is assumed to be a Boolean displayed with a checkbox. The key "price" is assumed to be currency, as is any key ending in "count". There may be others. Defaults can be overridden using type-hints. All other keys will be assumed to be strings. type-hints - a dictionary containing keys specificed for the TreeView and GridColumns. Used to override types inferred by convention, or for changing the type of a column from the default of a string to something else. uri - A uri for the DesktopCouch. This is only used to choose a Couch database running remotely. The default is to use the local desktopcouch database. """ if type(database_name) is not type(str()): raise TypeError("database_name is required and must be a string") #set up the database before trying to use it self.uri = uri self._record_type = None self._db = None if record_type is not None: self._record_type = record_type if dictionaries is not None and keys is None: DictionaryGrid.__init__(self, None, editable, keys, type_hints) else: DictionaryGrid.__init__(self, None, editable, keys, type_hints) if self.uri: self._db = CouchDatabase(database_name, create=True, uri=self.uri) else: self._db = CouchDatabase(database_name, create=True) if dictionaries is not None: for d in dictionaries: self._persist_dict_to_couch(d) self._refresh_treeview()
#!/usr/bin/env python # # [SNIPPET_NAME: Delete Records] # [SNIPPET_CATEGORIES: DesktopCouch] # [SNIPPET_DESCRIPTION: Delete records from the desktop couch database] # [SNIPPET_AUTHOR: Andy Breiner <*****@*****.**>] # [SNIPPET_DOCS: http://www.freedesktop.org/wiki/Specifications/desktopcouch/Documentation/SimpleGuide] # [SNIPPET_LICENSE: GPL] from desktopcouch.records.server import CouchDatabase from desktopcouch.records.record import Record #Similar to the fetchrecord.py by Huntly Cameron #See fetchrecord.py for additional information db = CouchDatabase("deleterecordsexample", create=True) record_type = "http://example.com/delete-record-type.html" record = Record({"first" : "Andy", "last" : "Breiner"}, record_type) db.put_record(record) record = Record({"first" : "Jono", "last" : "Bacon"}, record_type) db.put_record(record) results = db.get_records(record_type = record_type, create_view = True) for records in results: record = records.value print "First: %s" % record["first"] print "Last : %s" % record["last"]
def db_connect(self): '''connect to couchdb create if necessary''' # logging.basicConfig will be called now self._database = CouchDatabase(self._db_name, create=True)
def db_connect(self): # logging.basicConfig will be called now self._database = CouchDatabase(self._db_name, create=True)
# [SNIPPET_NAME: Fetch Records] # [SNIPPET_CATEGORIES: DesktopCouch] # [SNIPPET_DESCRIPTION: Fetch records from the desktop couch database] # [SNIPPET_AUTHOR: Huntly Cameron <*****@*****.**>] # [SNIPPET_DOCS: http://www.freedesktop.org/wiki/Specifications/desktopcouch/Documentation/SimpleGuide] # [SNIPPET_LICENSE: GPL] from desktopcouch.records.server import CouchDatabase from desktopcouch.records.record import Record #First, we want to put a record into a database and then get it out #create the database db = CouchDatabase("fetchrecordsexample", create=True) #Create some records record_type = "http://example.com/fetch-record-type.html" new_record = Record({ "afield": "a value", "anotherfield": "another value" }, record_type) another_record = Record( { "afield": "some value", "anotherfield": "some other value" }, record_type) #put our new records into the datbase db.put_record(new_record) db.put_record(another_record) #Fetch all the records in the database and display them
def setUp(self): TestCase.setUp(self) self.dbname = self._testMethodName self.db = CouchDatabase(self.dbname, create=True) self.record_type = "test_record_type"
# [SNIPPET_NAME: Create a Database] # [SNIPPET_CATEGORIES: DesktopCouch] # [SNIPPET_DESCRIPTION: Creating a new database in desktopcouch] # [SNIPPET_AUTHOR: Huntly Cameron <*****@*****.**>] # [SNIPPET_DOCS: http://www.freedesktop.org/wiki/Specifications/desktopcouch/Documentation/SimpleGuide] # [SNIPPET_LICENSE: GPL] from desktopcouch.records.server import CouchDatabase #Create a database object. Your database needs to exist. #If it doesn't, you can create it by passing create=True. db = CouchDatabase('createdatabaseexample', create=True) #run xdg-open /home/$USER/.local/share/desktop-couch/couchdb.html #from the terminal to view the desktopcouch
# [SNIPPET_NAME: Listen to changes in CouchDatabase] # [SNIPPET_CATEGORIES: DesktopCouch] # [SNIPPET_DESCRIPTION: Register a callback that will listen to changes in the CouchDatabase] # [SNIPPET_AUTHOR: Manuel de la Pena <*****@*****.**>] # [SNIPPET_DOCS: http://www.themacaque.com/wiki/doku.php?id=desktopcouch:listen_to_changes] # [SNIPPET_LICENSE: GPL] import time from desktopcouch.records.server import CouchDatabase # we are going to be listening to the changes in this callback def changes_cb(seq=None, id=None, changes=None): print seq print id print changes db = CouchDatabase("listenchangesexample", create=True) # is better, use glib main loop or twisted task! while True: db.report_changes(changes_cb) time.sleep(30) # got to /home/$USER/.local/share/desktop-couch/couchdb.html and make # changes to the listenchangesexample to see the callback output
# [SNIPPET_NAME: Add record] # [SNIPPET_CATEGORIES: DesktopCouch] # [SNIPPET_DESCRIPTION: Add a new record into the database] # [SNIPPET_AUTHOR: Huntly Cameron <*****@*****.**>] # [SNIPPET_DOCS: http://www.freedesktop.org/wiki/Specifications/desktopcouch/Documentation/SimpleGuide] # [SNIPPET_LICENSE: GPL] from desktopcouch.records.server import CouchDatabase from desktopcouch.records.record import Record #create the database db = CouchDatabase("addrecordexample", create=True) #Records work like python dictionaries, and *should* have an #online description of how the record should look like. record_type = "http://example.com/somerecordtype.html" new_record = Record({ "a field": "a value", "another field": "another value" }, record_type) #put our new record into the datbase db.put_record(new_record) #run xdg-open /home/$USER/.local/share/desktop-couch/couchdb.html #from the terminal to view the desktopcouch
# [SNIPPET_NAME: Add an attachment to a Desktopcouch Record] # [SNIPPET_CATEGORIES: DesktopCouch] # [SNIPPET_DESCRIPTION: Shows how to add an attachment to a Desktopcouch Record that will be stored and replicated by CouchDB] # [SNIPPET_AUTHOR: Manuel de la Pena <*****@*****.**>] # [SNIPPET_DOCS: http://www.themacaque.com/wiki/doku.php?id=desktopcouch:create_attachments] # [SNIPPET_LICENSE: GPL] import sys from desktopcouch.records.record import Record from desktopcouch.records.server import CouchDatabase # get the jpg to add from the command line if len(sys.argv) > 1: path = sys.argv[0] db = CouchDatabase("addattachment", create=True) record = Record(record_type="url") record.attach(path, "blob", "image/jpg") db.put_record(record) else: print "Please pass the path of the jpg to add." # got to /home/$USER/.local/share/desktop-couch/couchdb.html to see the # attached file