-
Notifications
You must be signed in to change notification settings - Fork 20
improve xml_dump processing. #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
groceryheist
wants to merge
4
commits into
mediawiki-utilities:master
Choose a base branch
from
groceryheist:groceryheist
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,7 +73,7 @@ class Iterator(serializable.Type): | |
'__pages') | ||
|
||
def __init__(self, site_name=None, dbname=None, base=None, generator=None, | ||
case=None, namespaces=None, pages=None): | ||
case=None, namespaces=None, pages=None, lang=None): | ||
|
||
self.site_name = none_or(site_name, str) | ||
""" | ||
|
@@ -109,6 +109,11 @@ def __init__(self, site_name=None, dbname=None, base=None, generator=None, | |
# Should be a lazy generator of page info | ||
self.__pages = pages | ||
|
||
self.lang = none_or(lang, str) | ||
""" | ||
A 2 character language code. | ||
""" | ||
|
||
def __iter__(self): | ||
return self.__pages | ||
|
||
|
@@ -140,6 +145,9 @@ def load_site_info(cls, element): | |
namespaces = {} | ||
|
||
for sub_element in element: | ||
|
||
if sub_element.tag == 'siteinfo': | ||
return(cls.load_site_info(sub_element)) | ||
if sub_element.tag == 'sitename': | ||
site_name = sub_element.text | ||
if sub_element.tag == 'dbname': | ||
|
@@ -152,24 +160,21 @@ def load_site_info(cls, element): | |
case = sub_element.text | ||
elif sub_element.tag == 'namespaces': | ||
namespaces = cls.load_namespaces(sub_element) | ||
|
||
return site_name, dbname, base, generator, case, namespaces | ||
|
||
@classmethod | ||
def load_pages(cls, element): | ||
|
||
for sub_element in element: | ||
tag = sub_element.tag | ||
|
||
if tag == "page": | ||
yield Page.from_element(sub_element) | ||
else: | ||
assert MalformedXML("Expected to see 'page'. " + | ||
"Instead saw '{0}'".format(tag)) | ||
|
||
@classmethod | ||
def from_element(cls, element): | ||
|
||
def from_element(cls, element, lang=None): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not extract the lang inside of this method? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea. |
||
site_name = None | ||
base = None | ||
generator = None | ||
|
@@ -187,20 +192,22 @@ def from_element(cls, element): | |
# Consume all <page> | ||
pages = cls.load_pages(element) | ||
|
||
return cls(site_name, dbname, base, generator, case, namespaces, pages) | ||
return cls(site_name, dbname, base, generator, case, namespaces, pages, lang) | ||
|
||
@classmethod | ||
def from_file(cls, f): | ||
element = ElementIterator.from_file(f) | ||
assert element.tag == "mediawiki" | ||
return cls.from_element(element) | ||
lang = element.attr("lang") | ||
return cls.from_element(element, lang=lang) | ||
|
||
@classmethod | ||
def from_string(cls, string): | ||
f = io.StringIO(string) | ||
element = ElementIterator.from_file(f) | ||
assert element.tag == "mediawiki" | ||
return cls.from_element(element) | ||
lang = element.attr("xml:lang") | ||
return cls.from_element(element, lang=lang) | ||
|
||
@classmethod | ||
def from_page_xml(cls, page_xml): | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return
isn't a functionThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, what's going on here? Is there a inside of a tag?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean is language data inside a tag?
Unfortunately not. The language is in the xml header, not a tag.