forked from skarim/vobject
-
Notifications
You must be signed in to change notification settings - Fork 14
allow multiple values for some properties if language differs #17
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
da4089
wants to merge
1
commit into
py-vobject:master
Choose a base branch
from
haansn08:prop-l10n
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 all 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| from . import base | ||
|
|
||
| properties_allow_localization = ['NAME','SUMMARY','DESCRIPTION','LOCATION'] | ||
|
|
||
| #------------------------ Abstract class for behavior -------------------------- | ||
| class Behavior(object): | ||
|
|
@@ -81,22 +82,52 @@ def validate(cls, obj, raiseException=False, complainUnrecognized=False): | |
| return cls.lineValidate(obj, raiseException, complainUnrecognized) | ||
| elif isinstance(obj, base.Component): | ||
| count = {} | ||
| language_count = {} #example: {'SUMMARY': {'en': 1, 'ja': 1} , 'DESCRIPTION': {None: 1}} | ||
| for child in obj.getChildren(): | ||
| if not child.validate(raiseException, complainUnrecognized): | ||
| return False | ||
| name = child.name.upper() | ||
| count[name] = count.get(name, 0) + 1 | ||
|
|
||
| if isinstance(child, base.ContentLine) and name in properties_allow_localization: | ||
| # for some properties, we count separately with respect to language | ||
| lang = child.params.get('LANGUAGE', []) | ||
| if len(lang) == 0: | ||
| lang = None | ||
| elif len(lang) == 1: | ||
| lang = lang[0].lower() | ||
| elif len(lang) > 1: | ||
| if raiseException: | ||
| m = "Multiple language parameters specified for property {1}" | ||
| raise base.ValidateError(m.format(name)) | ||
| return False | ||
| if name in language_count: | ||
| language_count[name][lang] = language_count[name].get(lang, 0) + 1 | ||
| else: | ||
| language_count[name] = {lang: 1} | ||
|
Comment on lines
+104
to
+107
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. Either only line 105 and no if-statement at all, or change line 105 to since you ensured that it exists in line 104 |
||
|
|
||
| for key, val in cls.knownChildren.items(): | ||
| if count.get(key, 0) < val[0]: | ||
| minimum_count = val[0] | ||
| if count.get(key, 0) < minimum_count: | ||
| if raiseException: | ||
| m = "{0} components must contain at least {1} {2}" | ||
| raise base.ValidateError(m .format(cls.name, val[0], key)) | ||
| raise base.ValidateError(m .format(cls.name, minimum_count, key)) | ||
| return False | ||
| if val[1] and count.get(key, 0) > val[1]: | ||
| if raiseException: | ||
| maximum_count = val[1] | ||
| if maximum_count and count.get(key, 0) > maximum_count: | ||
| if key in properties_allow_localization: | ||
| # check if the maximum is exceeded for any language | ||
| for lang, count_for_lang in language_count.get(key, {}).items(): | ||
| if count_for_lang > maximum_count: | ||
| if raiseException: | ||
| m = "{0} components cannot contain more than {1} {2} with the same language" | ||
| raise base.ValidateError(m.format(cls.name, maximum_count, key)) | ||
| return False | ||
| elif raiseException: | ||
| m = "{0} components cannot contain more than {1} {2}" | ||
| raise base.ValidateError(m.format(cls.name, val[1], key)) | ||
| return False | ||
| raise base.ValidateError(m.format(cls.name, maximum_count, key)) | ||
| else: | ||
| return False | ||
| return True | ||
| else: | ||
| err = "{0} is not a Component or Contentline".format(obj) | ||
|
|
||
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.