class IssuesLog(): def __init__(self): self._connect() # it is not incremental so we first drop the table self._drop_db() self._create_db() def _connect(self): opts = Config() self.database = create_database('mysql://' + opts.db_user_out + ':' + opts.db_password_out + '@' + opts.db_hostname_out + ':' + opts.db_port_out + '/' + opts.db_database_out) self.store = Store(self.database) def _create_db(self): self.store.execute(self._get_sql_create()) def _drop_db(self): self.store.execute(self._get_sql_drop()) def _get_people_id(self, email): """ Gets the id of an user """ try: p = self.store.find(DBPeople, DBPeople.email == email).one() return p.id except (AttributeError, NotOneError): p = self.store.find(DBPeople, DBPeople.user_id == email).one() try: return p.id except AttributeError: # no person was found in People with the email above, so # we include it printdbg("Person not found. Inserted with email %s " % (email)) dp = DBPeople(email) self.store.add(dp) self.store.commit() return dp.id def _get_sql_drop(self): """ Abstract method for inserting extra data related to a change """ raise NotImplementedError def _get_sql_create(self): """ Abstract method for inserting extra data related to a change """ raise NotImplementedError def _get_tracker_id(self, issue_id): """ Returns tracker id from issues """ result = self.store.find(DBIssue.tracker_id, DBIssue.id == issue_id).one() return result def _copy_issue_ext(self, aux, db_ilog): """ Abstract method for inserting extra data related to a change """ raise NotImplementedError # TODO: reuse _copy_standard_values def _copy_issue(self, db_ilog): """ This method returns a copy of the DB*Log object """ aux = self._get_dbissues_object(db_ilog.issue, db_ilog.tracker_id) aux.issue_id = db_ilog.issue_id aux.change_id = db_ilog.change_id aux.changed_by = db_ilog.changed_by aux.type = db_ilog.type aux.summary = db_ilog.summary aux.description = db_ilog.description aux.status = db_ilog.status aux.resolution = db_ilog.resolution aux.priority = db_ilog.priority aux.submitted_by = db_ilog.submitted_by aux.date = db_ilog.date aux.assigned_to = db_ilog.assigned_to aux = self._copy_issue_ext(aux, db_ilog) return aux def _assign_values(self, db_ilog, field, value): """ Abstract method for inserting extra data related to a change """ raise NotImplementedError def _build_initial_state(self, db_ilog): """ This method gets the first changes of every field in order to get the initial state of the bug """ fields = self.store.execute("SELECT DISTINCT(field) FROM changes " + "WHERE issue_id=%s" % (db_ilog.issue_id)) for f in fields: values = self.store.execute( "SELECT old_value FROM changes WHERE issue_id=%s AND \ field=\"%s\" ORDER BY changed_on LIMIT 1" % (db_ilog.issue_id, f[0])) for v in values: db_ilog = self._assign_values(db_ilog, f[0], v[0]) return db_ilog def _get_dbissues_object(self, issue_name, tracker_id): """ Abstract method for inserting extra data related to a change """ raise NotImplementedError def _copy_standard_values(self, issue, issue_log): """ Copy the standard values from the issue object to the issue_log object """ issue_log.issue_id = issue.id issue_log.type = issue.type issue_log.summary = issue.summary issue_log.description = issue.description issue_log.status = issue.status issue_log.resolution = issue.resolution issue_log.priority = issue.priority issue_log.submitted_by = issue.submitted_by issue_log.date = issue.submitted_on issue_log.assigned_to = issue.assigned_to return issue_log def _print_final_msg(self): """ Abstract method for inserting extra data related to a change """ raise NotImplementedError def _get_changes(self, issue_id): aux = self.store.execute("SELECT id, field, new_value, changed_by, \ changed_on FROM changes where issue_id=%s" % (issue_id)) return aux def _post_history(self, issue_id): """ Abstract method for inserting extra data usign full issue history """ pass def run(self): ndone = 0 issues = self.store.find(DBIssue) total = str(issues.count()) print ("[IssuesLog] Total issues to analyze: " + str(issues.count())) for i in issues: if (ndone % 1000 == 0): print ("[IssuesLog] Analyzed " + str(ndone) + "/" + str(total)) db_ilog = self._get_dbissues_object(i.issue, i.tracker_id) db_ilog = self._copy_standard_values(i, db_ilog) final_status = db_ilog.status db_ilog = self._build_initial_state(db_ilog) self.store.add(db_ilog) self.store.flush() # the code below gets all the changes and insert a row per change changes = self._get_changes(db_ilog.issue_id) for ch in changes: change_id = ch[0] field = ch[1] new_value = ch[2] changed_by = ch[3] date = ch[4] # we need a new object to be inserted in the database db_ilog = self._copy_issue(db_ilog) db_ilog.date = date db_ilog.change_id = change_id db_ilog.changed_by = changed_by db_ilog = self._assign_values(db_ilog, field, new_value) try: self.store.add(db_ilog) self.store.flush() except: # self.store.rollback() # is this useful in this context? traceback.print_exc() self._post_history(db_ilog, final_status) self.store.commit() ndone += 1 self._print_final_msg()
for description, price, date in [ ('Cup of coffee', 2.04, today - datetime.timedelta(1)), ('Chocolate bar', 1.85, today - datetime.timedelta(40)), ('Candy', 0.99, today - datetime.timedelta(30)), ('Grape Juice', 3.38, today - datetime.timedelta(23)), ('Ice tea', 1.25, today - datetime.timedelta(10)), ('Cookies', 0.85, today - datetime.timedelta(5)), ('Noogies', 1.45, today - datetime.timedelta(2)), ('Chocolate bar', 1.85, today)]: s = Sale() s.description=unicode(description) s.price=price s.date=date store.add(s) store.flush() class PurchaseViewer(gtk.Window): def __init__(self): gtk.Window.__init__(self) self.set_title('Purchases') self.search = SearchContainer(self.get_columns()) self.search.set_summary_label('price') self.add(self.search) self._setup_searching() self._create_filters() def _setup_searching(self): self.query = StormQueryExecuter(store) self.search.set_query_executer(self.query)
class StormStorageBackend(StorageBackend): """Storage back-end based on the Storm ORM framework.""" def __init__(self): self.store = None def set_config(self, **kwargs): """Set the configuration of this back-end.""" uri = kwargs['uri'] database = create_database(uri) self.store = Store(database) self.logger = logging.getLogger('StormStorageBackend') handler = logging.StreamHandler() formatter = logging.Formatter(kwargs['log_format']) handler.setFormatter(formatter) self.logger.addHandler(handler) self.logger.setLevel( logging.__getattribute__(kwargs['log_level'])) def create_node(self, node, jid, node_config): """Create a PubSub node with the given configuration. Creates the Node, NodeConfig, Affiliation and Subscription model for the given node. """ self.logger.debug('Creating node %s for jid %s with config %s' % (node, jid, node_config)) new_node = Node(node) self.store.add(new_node) config = copy.deepcopy(DEFAULT_CONFIG) config.update(node_config) for key, value in config.items(): new_node_config = NodeConfig(node, key, value) new_node_config.updated = datetime.utcnow() self.store.add(new_node_config) affiliation = Affiliation(node, jid, u'owner', datetime.utcnow()) self.store.add(affiliation) subscription = Subscription(node, jid, jid, u'subscribed', datetime.utcnow()) self.store.add(subscription) def create_channel(self, jid): """Create a channel for the given JID. Creates all the required PubSub nodes that constitute a channel, with the appropriate permissions. """ self.logger.debug('Creating channel for %s' % jid) creation_date = unicode(datetime.utcnow().isoformat()) self.create_node(u'/user/%s/posts' % jid, jid, {u'channelType': u'personal', u'creationDate': creation_date, u'defaultAffiliation': u'publisher', u'description': u'buddycloud channel for %s' % jid, u'title': jid}) self.create_node(u'/user/%s/geo/current' % jid, jid, {u'creationDate': creation_date, u'description': u'Where %s is at now' % jid, u'title': u'%s Current Location' % jid}) self.create_node(u'/user/%s/geo/next' % jid, jid, {u'creationDate': creation_date, u'description': u'Where %s intends to go' % jid, u'title': u'%s Next Location' % jid}) self.create_node(u'/user/%s/geo/previous' % jid, jid, {u'creationDate': creation_date, u'description': u'Where %s has been before' % jid, u'title': u'%s Previous Location' % jid}) self.create_node(u'/user/%s/status' % jid, jid, {u'creationDate': creation_date, u'description': u'M000D', u'title': u'%s status updates' % jid}) self.create_node(u'/user/%s/subscriptions' % jid, jid, {u'creationDate': creation_date, u'description': u'Browse my interests', u'title': u'%s subscriptions' % jid}) self.store.commit() def get_node(self, node): """Get the requested PubSub node.""" self.logger.debug('Getting node %s' % node) the_node = self.store.get(Node, node) self.logger.debug('Returning node %s' % the_node) return the_node def get_nodes(self): """Get a list of all the available PubSub nodes.""" self.logger.debug('Getting list of available nodes.') node_list = self.store.find(Node) self.logger.debug('Returning list of available node %s' % node_list) return node_list def add_item(self, node, item_id, item): """Add an item to the requested PubSub node.""" new_item = Item(node, unicode(item_id), datetime.utcnow(), item) self.store.add(new_item) self.store.commit() def shutdown(self): """Shut down this storage module - flush, commit and close the store.""" self.store.flush() self.store.commit() self.store.close()
class IssuesLog(): def __init__(self): self._connect() # it is not incremental so we first drop the table self._drop_db() self._create_db() def _connect(self): opts = Config() self.database = create_database('mysql://' + opts.db_user_out + ':' + opts.db_password_out + '@' + opts.db_hostname_out + ':' + opts.db_port_out + '/' + opts.db_database_out) self.store = Store(self.database) def _create_db(self): self.store.execute(self._get_sql_create()) def _drop_db(self): self.store.execute(self._get_sql_drop()) def _get_people_id(self, email): """ Gets the id of an user """ try: p = self.store.find(DBPeople, DBPeople.email == email).one() return p.id except (AttributeError, NotOneError): p = self.store.find(DBPeople, DBPeople.user_id == email).one() try: return p.id except AttributeError: # no person was found in People with the email above, so # we include it printdbg("Person not found. Inserted with email %s " % (email)) dp = DBPeople(email) self.store.add(dp) self.store.commit() return dp.id def _get_sql_drop(self): """ Abstract method for inserting extra data related to a change """ raise NotImplementedError def _get_sql_create(self): """ Abstract method for inserting extra data related to a change """ raise NotImplementedError def _get_tracker_id(self, issue_id): """ Returns tracker id from issues """ result = self.store.find(DBIssue.tracker_id, DBIssue.id == issue_id).one() return result def _copy_issue_ext(self, aux, db_ilog): """ Abstract method for inserting extra data related to a change """ raise NotImplementedError # TODO: reuse _copy_standard_values def _copy_issue(self, db_ilog): """ This method returns a copy of the DB*Log object """ aux = self._get_dbissues_object(db_ilog.issue, db_ilog.tracker_id) aux.issue_id = db_ilog.issue_id aux.change_id = db_ilog.change_id aux.type = db_ilog.type aux.summary = db_ilog.summary aux.description = db_ilog.description aux.status = db_ilog.status aux.resolution = db_ilog.resolution aux.priority = db_ilog.priority aux.submitted_by = db_ilog.submitted_by aux.date = db_ilog.date aux.assigned_to = db_ilog.assigned_to aux = self._copy_issue_ext(aux, db_ilog) return aux def _assign_values(self, db_ilog, field, value): """ Abstract method for inserting extra data related to a change """ raise NotImplementedError def _build_initial_state(self, db_ilog): """ This method gets the first changes of every field in order to get the initial state of the bug """ fields = self.store.execute("SELECT DISTINCT(field) FROM changes " + "WHERE issue_id=%s" % (db_ilog.issue_id)) for f in fields: values = self.store.execute( "SELECT old_value FROM changes WHERE issue_id=%s AND \ field=\"%s\" ORDER BY changed_on LIMIT 1" % (db_ilog.issue_id, f[0])) for v in values: db_ilog = self._assign_values(db_ilog, f[0], v[0]) # Initial status does not have a real change db_ilog.change_id = 0 return db_ilog def _get_dbissues_object(self, issue_name, tracker_id): """ Abstract method for inserting extra data related to a change """ raise NotImplementedError def _copy_standard_values(self, issue, issue_log): """ Copy the standard values from the issue object to the issue_log object """ issue_log.issue_id = issue.id issue_log.type = issue.type issue_log.summary = issue.summary issue_log.description = issue.description issue_log.status = issue.status issue_log.resolution = issue.resolution issue_log.priority = issue.priority issue_log.submitted_by = issue.submitted_by issue_log.date = issue.submitted_on issue_log.assigned_to = issue.assigned_to return issue_log def _print_final_msg(self): """ Abstract method for inserting extra data related to a change """ raise NotImplementedError def _get_changes(self, issue_id): aux = self.store.execute("SELECT id, field, new_value, changed_by, \ changed_on FROM changes where issue_id=%s" % (issue_id)) return aux def _post_history(self, issue_id): """ Abstract method for inserting extra data usign full issue history """ pass def run(self): ndone = 0 issues = self.store.find(DBIssue) total = str(issues.count()) print("[IssuesLog] Total issues to analyze: " + str(issues.count())) for i in issues: if (ndone % 1000 == 0): print("[IssuesLog] Analyzed " + str(ndone) + "/" + str(total)) db_ilog = self._get_dbissues_object(i.issue, i.tracker_id) db_ilog = self._copy_standard_values(i, db_ilog) final_status = db_ilog.status db_ilog = self._build_initial_state(db_ilog) self.store.add(db_ilog) self.store.flush() # the code below gets all the changes and insert a row per change changes = self._get_changes(db_ilog.issue_id) for ch in changes: change_id = ch[0] field = ch[1] new_value = ch[2] changed_by = ch[3] date = ch[4] # we need a new object to be inserted in the database db_ilog = self._copy_issue(db_ilog) db_ilog.date = date db_ilog.change_id = change_id db_ilog.submitted_by = changed_by db_ilog = self._assign_values(db_ilog, field, new_value) try: self.store.add(db_ilog) self.store.flush() except: # self.store.rollback() # is this useful in this context? traceback.print_exc() ##self._post_history(db_ilog, final_status) self.store.commit() ndone += 1 self._print_final_msg()
"CREATE TABLE address (id INTEGER PRIMARY KEY, address VARCHAR, person_id INTEGER, " "FOREIGN KEY(person_id) REFERENCES person(id))") person = Person() person.name = u'person' print(person) print("%r, %r" % (person.id, person.name)) # None, u'person' # Notice that person.id is None since the Person instance is not attached to a valid database store yet. store.add(person) print("%r, %r" % (person.id, person.name)) # None, u'person' # Since the store hasn't flushed the Person instance into the sqlite database yet, person.id is still None. store.flush() print("%r, %r" % (person.id, person.name)) # 1, u'person' # Now the store has flushed the Person instance, we got an id value for person. address = Address() address.person = person address.address = 'address' print("%r, %r, %r" % (address.id, address.person, address.address)) # None,, 'address' print(address.person == person) # True store.add(address)
from storm.locals import Store, Int, Unicode, create_database conn = Store(create_database("postgres://*****:*****@127.0.0.1/test")) class Person(object): __storm_table__ = 'person' id = Int(primary=True) name = Unicode() person = Person() person.name = 'admin' conn.add(person) conn.flush() conn.find(Person, Person.name == 'admin').one()