Skip to content
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

Add new method "Session.get_default_name()" #70

Merged
merged 1 commit into from
Jul 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ To be released.
:class:`~libearth.feed.Text` type.
The :func:`~libearth.sanitizer.sanitize_html()` function became to
additionally require ``base_uri`` parameter.
- Added :meth:`~libearth.session.Session.get_default_name()` for default
session name.


Version 0.3.3
Expand Down
12 changes: 11 additions & 1 deletion libearth/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"""
import collections
import datetime
import platform
import re
import uuid
import xml.sax
Expand Down Expand Up @@ -72,7 +73,7 @@ def __new__(cls, identifier=None):
if not (identifier is None or isinstance(identifier, string_type)):
raise TypeError('identifier must be a string, not ' +
repr(identifier))
identifier = str(identifier) if identifier else str(uuid.getnode())
identifier = str(identifier) if identifier else cls.get_default_name()
if not cls.IDENTIFIER_PATTERN.match(identifier):
raise ValueError('invalid identifier format: ' + repr(identifier))
try:
Expand All @@ -92,6 +93,15 @@ def __ne__(self, other):
def __hash__(self):
return hash(self.identifier)

@staticmethod
def get_default_name():
"""Create default session name.

:return: Default session name formatted as "Hostname-UUID".
:rtype: :class:`str`
"""
return '{0}-{1}'.format(platform.node(), uuid.getnode())

def revise(self, document):
"""Mark the given ``document`` as the latest revision of the current
session.
Expand Down