Skip to content

Commit

Permalink
Init all events and most from_api_doc functions fleshed out. One test.
Browse files Browse the repository at this point in the history
  • Loading branch information
halfak committed Aug 2, 2014
0 parents commit 02e06d2
Show file tree
Hide file tree
Showing 51 changed files with 2,193 additions and 0 deletions.
56 changes: 56 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Temp files
*~

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]

# C extensions
*.so

# Distribution / packaging
.Python
env/
bin/
build/
develop-eggs/
dist/
eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.cache
nosetests.xml
coverage.xml

# Translations
*.mo

# Mr Developer
.mr.developer.cfg
.project
.pydevproject

# Rope
.ropeproject

# Django stuff:
*.log
*.pot

# Sphinx documentation
doc/_build/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2014 Aaron Halfaker

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Empty file added MANIFEST.in
Empty file.
Empty file added README.rst
Empty file.
Empty file added mw_events/__init__.py
Empty file.
1 change: 1 addition & 0 deletions mw_events/configuration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DEFAULTS = {}
5 changes: 5 additions & 0 deletions mw_events/defaults.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@



PARAMS_TIME_FORMAT = "expires %H:%M, %d %B %Y (UTC)"
PARAMS_INDEFINITE = "indefinite"
Empty file added mw_events/events/__init__.py
Empty file.
96 changes: 96 additions & 0 deletions mw_events/events/event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
from collections import defaultdict

from mw import Timestamp

from jsonable import JSONable

from ..types import User


class Event(JSONable):
__slots__ = ('timestamp', 'user', 'comment')
MATCHES = NotImplemented
EVENTS = defaultdict(lambda: [])
PRIORITY = 99

def initialize(self, timestamp, user, comment):
self.timestamp = Timestamp(timestamp)
self.user = User(user)
self.comment = str(comment)

@classmethod
def register(cls, EventClass):
for match in EventClass.MATCHES:
cls.EVENTS[match].append(EventClass)
cls.EVENTS[match].sort(key=lambda e:e.PRIORITY)

@classmethod
def matches(cls, match):
return cls.EVENTS[match]

@classmethod
def from_api_doc(cls, api_doc):
match = Match.from_api_doc(api_doc)

for EventClass in cls.matches(match):
yield EventClass.from_api_doc(api_doc)

@classmethod
def from_db_row(cls, db_row):
match = Match.from_db_row(db_row)

for EventClass in cls.matches(match):
yield EventClass.from_db_row(api_doc)

class Match:

RC_TYPES = {
0: "edit",
1: "new",
2: "move",
3: "log",
4: "move_over_redirect",
5: "external"
}

def __init__(self, type, action, has_rev_id, rc_type):
self.type = str(type)
self.action = str(action)
self.has_rev_id = bool(has_rev_id)
self.rc_type = str(rc_type)

def __eq__(self, other):
try:
return (
self.type == other.type and
self.action == other.action and
self.has_rev_id == other.has_rev_id and
self.rc_type == other.rc_type
)
except AttributeError:
return False

def __hash__(self):
return hash((self.type, self.action, self.has_rev_id, self.rc_type))

@classmethod
def from_api_doc(cls, api_doc):

return cls(
doc.get('logtype'),
doc.get('logaction'),
doc.get('revid', 0) > 0,
doc['type']
)


@classmethod
def from_db_row(cls, db_row):
return cls(
db_row.get('log_type'),
db_row.get('log_action'),
'rev_id' in db_row and \
db_row['rev_id'] is not None and
db_row['rev_id'] > 0,
cls.RC_TYPES[db_row['rc_type']]
)
Loading

0 comments on commit 02e06d2

Please sign in to comment.