signature
stringlengths
8
3.44k
body
stringlengths
0
1.41M
docstring
stringlengths
1
122k
id
stringlengths
5
17
@verify_type(hello_message=bytes, invert_hello=bool)<EOL><INDENT>@verify_value(hello_message=lambda x: len(x.decode('<STR_LIT:ascii>')) >= <NUM_LIT:0>)<EOL>@verify_value(hello_message=lambda x: WBeaconGouverneurMessenger.__message_splitter__ not in x)<EOL>def __init__(self, hello_message, invert_hello=False):<DEDENT>
WBeaconMessengerBase.__init__(self)<EOL>self.__gouverneur_message = hello_message<EOL>self.__invert_hello = invert_hello<EOL>
Construct new messenger :param hello_message: Message header :param invert_hello: this flag defines whether response will have the original header \ ('hello_message' value) or it will have reversed value. For example, when this flag is set to True \ and 'hello_message' is b'sample', then response will have b'elpmas' header.
f9862:c2:m0
def invert_hello(self):
return self.__invert_hello<EOL>
Return whether this messenger was constructed with 'invert_hello' or not. :return: bool
f9862:c2:m1
@verify_type(invert_hello=bool)<EOL><INDENT>def hello_message(self, invert_hello=False):<DEDENT>
if invert_hello is False:<EOL><INDENT>return self.__gouverneur_message<EOL><DEDENT>hello_message = []<EOL>for i in range(len(self.__gouverneur_message) - <NUM_LIT:1>, -<NUM_LIT:1>, -<NUM_LIT:1>):<EOL><INDENT>hello_message.append(self.__gouverneur_message[i])<EOL><DEDENT>return bytes(hello_message)<EOL>
Return message header. :param invert_hello: whether to return the original header (in case of False value) or reversed \ one (in case of True value). :return: bytes
f9862:c2:m2
@verify_type('<STR_LIT>', beacon_config=WConfig, invert_hello=bool)<EOL><INDENT>def _message(self, beacon_config, invert_hello=False):<DEDENT>
message = self.hello_message(invert_hello=invert_hello) + self._message_address_generate(beacon_config)<EOL>return message<EOL>
Generate request/response message. :param beacon_config: server or client configuration. Client configuration is used for request and \ server configuration for response :param invert_hello: return message with reverse header when this argument is set to True. :return: bytes
f9862:c2:m3
@verify_type(beacon_config=WConfig)<EOL><INDENT>def _message_address_generate(self, beacon_config):<DEDENT>
address = None<EOL>if beacon_config['<STR_LIT>']['<STR_LIT>'] != '<STR_LIT>':<EOL><INDENT>address = str(WIPV4SocketInfo.parse_address(<EOL>beacon_config['<STR_LIT>']['<STR_LIT>']<EOL>)).encode('<STR_LIT:ascii>')<EOL><DEDENT>if address is not None:<EOL><INDENT>address = WBeaconGouverneurMessenger.__message_splitter__ + address<EOL>if beacon_config['<STR_LIT>']['<STR_LIT>'] != '<STR_LIT>':<EOL><INDENT>port = beacon_config.getint('<STR_LIT>', '<STR_LIT>')<EOL>address += WBeaconGouverneurMessenger.__message_splitter__ + str(port).encode('<STR_LIT:ascii>')<EOL><DEDENT><DEDENT>return address if address is not None else b'<STR_LIT>'<EOL>
Generate address for request/response message. :param beacon_config: server or client configuration. Client configuration is used for request and \ server configuration for response :return: bytes
f9862:c2:m4
@verify_type(message=bytes, invert_hello=bool)<EOL><INDENT>def _message_address_parse(self, message, invert_hello=False):<DEDENT>
message_header = self.hello_message(invert_hello=invert_hello)<EOL>if message[:len(message_header)] != message_header:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>message = message[len(message_header):]<EOL>message_parts = message.split(WBeaconGouverneurMessenger.__message_splitter__)<EOL>address = None<EOL>port = None<EOL>if len(message_parts) > <NUM_LIT:3>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>elif len(message_parts) == <NUM_LIT:3>:<EOL><INDENT>address = WIPV4SocketInfo.parse_address(message_parts[<NUM_LIT:1>].decode('<STR_LIT:ascii>'))<EOL>port = WIPPort(int(message_parts[<NUM_LIT:2>]))<EOL><DEDENT>elif len(message_parts) == <NUM_LIT:2> and len(message_parts[<NUM_LIT:1>]) > <NUM_LIT:0>:<EOL><INDENT>address = WIPV4SocketInfo.parse_address(message_parts[<NUM_LIT:1>].decode('<STR_LIT:ascii>'))<EOL><DEDENT>return WIPV4SocketInfo(address, port)<EOL>
Read address from beacon message. If no address is specified then "nullable" WIPV4SocketInfo returns :param message: message to parse :param invert_hello: defines whether message header is the original one or reversed. :return: WIPV4SocketInfo
f9862:c2:m5
@verify_type('<STR_LIT>', beacon_config=WConfig)<EOL><INDENT>def request(self, beacon_config):<DEDENT>
return self._message(beacon_config)<EOL>
:meth:`.WBeaconMessengerBase.request` method implementation. see :class:`.WBeaconGouverneurMessenger`
f9862:c2:m6
@verify_type('<STR_LIT>', beacon_config=WConfig, request=bytes, client_address=WIPV4SocketInfo)<EOL><INDENT>def has_response(self, beacon_config, request, client_address):<DEDENT>
try:<EOL><INDENT>self._message_address_parse(request, invert_hello=self.__invert_hello)<EOL>return True<EOL><DEDENT>except ValueError:<EOL><INDENT>pass<EOL><DEDENT>return False<EOL>
:meth:`.WBeaconMessengerBase.has_response` method implementation. This method compares request header with internal one.
f9862:c2:m7
@verify_type('<STR_LIT>', beacon_config=WConfig, request=bytes, client_address=WIPV4SocketInfo)<EOL><INDENT>def response(self, beacon_config, request, client_address):<DEDENT>
return self._message(beacon_config, invert_hello=self.__invert_hello)<EOL>
:meth:`.WBeaconMessengerBase.request` method implementation. see :class:`.WBeaconGouverneurMessenger`
f9862:c2:m8
@verify_type('<STR_LIT>', beacon_config=WConfig, request=bytes)<EOL><INDENT>@verify_type(client_address=WIPV4SocketInfo)<EOL>def response_address(self, beacon_config, request, client_address):<DEDENT>
si = self._message_address_parse(request, invert_hello=self.__invert_hello)<EOL>address = si.address()<EOL>port = si.port()<EOL>return WIPV4SocketInfo(<EOL>address if address is not None else client_address.address(),<EOL>port if port is not None else client_address.port()<EOL>)<EOL>
:meth:`.WBeaconMessengerBase.request` method implementation. see :class:`.WBeaconGouverneurMessenger`
f9862:c2:m9
@verify_type('<STR_LIT>', beacon_config=WConfig, response=bytes, server_address=WIPV4SocketInfo)<EOL><INDENT>def valid_response(self, beacon_config, response, server_address):<DEDENT>
try:<EOL><INDENT>self._message_address_parse(response, invert_hello=self.__invert_hello)<EOL>return True<EOL><DEDENT>except ValueError:<EOL><INDENT>pass<EOL><DEDENT>return False<EOL>
:meth:`.WBeaconMessengerBase.valid_response` method implementation. Response when it has correct header. Response header must be reversed if this messenger was constructed with 'invert_hello' flag.
f9862:c2:m10
@verify_type('<STR_LIT>', hello_message=bytes, invert_hello=bool)<EOL><INDENT>@verify_value('<STR_LIT>', hello_message=lambda x: len(x.decode('<STR_LIT:ascii>')) >= <NUM_LIT:0>)<EOL>@verify_value('<STR_LIT>', hello_message=lambda x: WHostgroupBeaconMessenger.__message_groups_splitter__ not in x)<EOL>@verify_type(hostgroup_names=str)<EOL>@verify_value(hostgroup_names=lambda x: WHostgroupBeaconMessenger.re_hostgroup_name.match(x) is not None)<EOL>def __init__(self, hello_message, *hostgroup_names, invert_hello=False):<DEDENT>
WBeaconGouverneurMessenger.__init__(self, hello_message, invert_hello=invert_hello)<EOL>self.__hostgroups = []<EOL>self.__hostgroups.extend([x.encode() for x in hostgroup_names])<EOL>
Create new messenger :param hello_message: same as hello_message in :meth:`.WBeaconGouverneurMessenger.__init__` :param hostgroup_names: list of host group names
f9862:c3:m0
def hostgroups(self):
return [x.decode() for x in self.__hostgroups]<EOL>
Return list of host group names :return: list of str
f9862:c3:m1
@verify_type('<STR_LIT>', beacon_config=WConfig, invert_hello=bool)<EOL><INDENT>def _message(self, beacon_config, invert_hello=False):<DEDENT>
m = WBeaconGouverneurMessenger._message(self, beacon_config, invert_hello=invert_hello)<EOL>hostgroups = self._message_hostgroup_generate()<EOL>if len(hostgroups) > <NUM_LIT:0>:<EOL><INDENT>m += (WHostgroupBeaconMessenger.__message_groups_splitter__ + hostgroups)<EOL><DEDENT>return m<EOL>
Overridden :meth:`.WBeaconGouverneurMessenger._message` method. Appends encoded host group names to requests and responses. :param beacon_config: beacon configuration :return: bytes
f9862:c3:m2
def _message_hostgroup_generate(self):
return b'<STR_LIT:U+002C>'.join(self.__hostgroups)<EOL>
Encode messenger host group names :return: bytes
f9862:c3:m3
@verify_type(message=bytes)<EOL><INDENT>def _message_hostgroup_parse(self, message):<DEDENT>
splitter_count = message.count(WHostgroupBeaconMessenger.__message_groups_splitter__)<EOL>if splitter_count == <NUM_LIT:0>:<EOL><INDENT>return [], WBeaconGouverneurMessenger._message_address_parse(self, message)<EOL><DEDENT>elif splitter_count == <NUM_LIT:1>:<EOL><INDENT>splitter_pos = message.find(WHostgroupBeaconMessenger.__message_groups_splitter__)<EOL>groups = []<EOL>group_splitter = WHostgroupBeaconMessenger.__group_splitter__<EOL>for group_name in message[(splitter_pos + <NUM_LIT:1>):].split(group_splitter):<EOL><INDENT>groups.append(group_name.strip())<EOL><DEDENT>address = WBeaconGouverneurMessenger._message_address_parse(self, message[:splitter_pos])<EOL>return groups, address<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>
Parse given message and return list of group names and socket information. Socket information is parsed in :meth:`.WBeaconGouverneurMessenger._message_address_parse` method :param message: bytes :return: tuple of list of group names and WIPV4SocketInfo
f9862:c3:m4
@verify_type('<STR_LIT>', beacon_config=WConfig, request=bytes, client_address=WIPV4SocketInfo)<EOL><INDENT>def has_response(self, beacon_config, request, client_address):<DEDENT>
try:<EOL><INDENT>groups, address = self._message_hostgroup_parse(request)<EOL>if len(self.__hostgroups) == <NUM_LIT:0> or len(groups) == <NUM_LIT:0>:<EOL><INDENT>return True<EOL><DEDENT>for group_name in groups:<EOL><INDENT>if group_name in self.__hostgroups:<EOL><INDENT>return True<EOL><DEDENT><DEDENT>return False<EOL><DEDENT>except ValueError:<EOL><INDENT>pass<EOL><DEDENT>return False<EOL>
:meth:`.WBeaconMessengerBase.has_response` method implementation. This method compares request headers as :meth:`.WBeaconGouverneurMessenger.has_response` do and compares specified group names with internal names.
f9862:c3:m5
@verify_type('<STR_LIT>', beacon_config=WConfig, request=bytes)<EOL><INDENT>@verify_type(client_address=WIPV4SocketInfo)<EOL>def response_address(self, beacon_config, request, client_address):<DEDENT>
si = self._message_hostgroup_parse(request)[<NUM_LIT:1>]<EOL>address = si.address()<EOL>port = si.port()<EOL>return WIPV4SocketInfo(<EOL>address if address is not None else client_address.address(),<EOL>port if port is not None else client_address.port()<EOL>)<EOL>
:meth:`.WBeaconMessengerBase.response_address` method implementation. It just removes host group names part and return :meth:`.WBeaconMessengerBase.response_address` result
f9862:c3:m6
def __init__(self):
WBroadcastNetworkTransport.__init__(self, client_configuration, server_configuration)<EOL>
Create new broadcast transport
f9863:c0:m0
def __init__(self):
WMulticastNetworkTransport.__init__(self, client_configuration, server_configuration)<EOL>
Create new multicast transport
f9863:c1:m0
@verify_type(uri=WURI, http_protocol_scheme=str)<EOL><INDENT>@verify_value(http_protocol_scheme=lambda x: x.lower() in ('<STR_LIT:http>', '<STR_LIT>'))<EOL>def __init__(self, uri, http_protocol_scheme):<DEDENT>
WVirtualDirectoryClient.__init__(self, uri)<EOL>webdav_uri = '<STR_LIT>' % (http_protocol_scheme, uri.hostname())<EOL>if uri.port() is not None:<EOL><INDENT>webdav_uri += ('<STR_LIT>' % uri.port())<EOL><DEDENT>if uri.path() is not None:<EOL><INDENT>webdav_uri += ('<STR_LIT>' % uri.path())<EOL><DEDENT>webdav_options = {<EOL>'<STR_LIT>': webdav_uri<EOL>}<EOL>if uri.username() is not None:<EOL><INDENT>webdav_options['<STR_LIT>'] = uri.username()<EOL>if uri.password() is not None:<EOL><INDENT>webdav_options['<STR_LIT>'] = uri.password()<EOL><DEDENT><DEDENT>query = uri.query()<EOL>if query is not None:<EOL><INDENT>parsed_query = WURIQuery.parse(query)<EOL>self.session_path(parsed_query['<STR_LIT>'][<NUM_LIT:0>])<EOL><DEDENT>self.__dav_client = webdav3.client.Client(webdav_options)<EOL>
Create new WebDAV client :param uri: URI for a client connection :param http_protocol_scheme: transport protocol name that must be used in order to connect to a server
f9864:c0:m0
def dav_client(self):
return self.__dav_client<EOL>
Return DAV-client for accessing a server (internal usage only) :return: webdav3.client.Client
f9864:c0:m1
def connect(self):
try:<EOL><INDENT>self.list_directory()<EOL><DEDENT>except WClientCapabilityError as e:<EOL><INDENT>raise WClientConnectionError('<STR_LIT>') from e<EOL><DEDENT>
:meth:`.WNetworkClientProto.connect` method implementation
f9864:c0:m2
def disconnect(self):
self.session_path(self.directory_sep())<EOL>
:meth:`.WNetworkClientProto.disconnect` method implementation
f9864:c0:m3
@classmethod<EOL><INDENT>@abstractmethod<EOL>def scheme_name(cls):<DEDENT>
raise NotImplementedError('<STR_LIT>')<EOL>
Derived classes must return scheme name from configuration URI :return: str
f9864:c0:m4
@classmethod<EOL><INDENT>def scheme_specification(cls):<DEDENT>
return WSchemeSpecification(<EOL>cls.scheme_name(),<EOL>WURIComponentVerifier(WURI.Component.username, WURIComponentVerifier.Requirement.optional),<EOL>WURIComponentVerifier(WURI.Component.password, WURIComponentVerifier.Requirement.optional),<EOL>WURIComponentVerifier(WURI.Component.hostname, WURIComponentVerifier.Requirement.required),<EOL>WURIComponentVerifier(WURI.Component.port, WURIComponentVerifier.Requirement.optional),<EOL>WURIComponentVerifier(WURI.Component.path, WURIComponentVerifier.Requirement.optional),<EOL>WURIQueryVerifier(<EOL>WURIComponentVerifier.Requirement.optional,<EOL>WStrictURIQuery.ParameterSpecification(<EOL>'<STR_LIT>', nullable=False, multiple=False, optional=False<EOL>),<EOL>extra_parameters=False<EOL>)<EOL>)<EOL>
:meth:`.WSchemeHandler.scheme_specification` method implementation
f9864:c0:m5
@WNetworkClientCapabilities.capability(WNetworkClientCapabilities.current_dir)<EOL><INDENT>def current_directory(self, *args, **kwargs):<DEDENT>
return self.session_path()<EOL>
:meth:`.WNetworkClientProto.current_directory` method implementation
f9864:c0:m6
@WNetworkClientCapabilities.capability(WNetworkClientCapabilities.change_dir, WebDavException, ValueError)<EOL><INDENT>@verify_type(path=str)<EOL>@verify_value(path=lambda x: len(x) > <NUM_LIT:0>)<EOL>def change_directory(self, path, *args, **kwargs):<DEDENT>
client = self.dav_client()<EOL>previous_path = self.session_path()<EOL>try:<EOL><INDENT>if client.is_dir(self.session_path(path)) is False:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT><DEDENT>except Exception:<EOL><INDENT>self.session_path(previous_path)<EOL>raise<EOL><DEDENT>
:meth:`.WNetworkClientProto.change_directory` method implementation
f9864:c0:m7
@WNetworkClientCapabilities.capability(WNetworkClientCapabilities.list_dir, WebDavException)<EOL><INDENT>def list_directory(self, *args, **kwargs):<DEDENT>
return tuple(self.dav_client().list(self.session_path()))<EOL>
:meth:`.WNetworkClientProto.list_directory` method implementation
f9864:c0:m8
@WNetworkClientCapabilities.capability(WNetworkClientCapabilities.make_dir, WebDavException)<EOL><INDENT>@verify_type(directory_name=str)<EOL>@verify_value(directory_name=lambda x: len(x) > <NUM_LIT:0>)<EOL>def make_directory(self, directory_name, *args, **kwargs):<DEDENT>
self.dav_client().mkdir(self.join_path(self.session_path(), directory_name))<EOL>
:meth:`.WNetworkClientProto.make_directory` method implementation
f9864:c0:m9
@WNetworkClientCapabilities.capability(WNetworkClientCapabilities.remove_dir, WebDavException, ValueError)<EOL><INDENT>@verify_type(directory_name=str)<EOL>@verify_value(directory_name=lambda x: len(x) > <NUM_LIT:0>)<EOL>def remove_directory(self, directory_name, *args, **kwargs):<DEDENT>
client = self.dav_client()<EOL>remote_path = self.join_path(self.session_path(), directory_name)<EOL>if client.is_dir(remote_path) is False:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>client.clean(remote_path)<EOL>
:meth:`.WNetworkClientProto.remove_directory` method implementation
f9864:c0:m10
@WNetworkClientCapabilities.capability(WNetworkClientCapabilities.upload_file, WebDavException)<EOL><INDENT>@verify_type(file_name=str)<EOL>@verify_value(file_name=lambda x: len(x) > <NUM_LIT:0>)<EOL>def upload_file(self, file_name, file_obj, *args, **kwargs):<DEDENT>
self.dav_client().upload_to(file_obj, self.join_path(self.session_path(), file_name))<EOL>
:meth:`.WNetworkClientProto.upload_file` method implementation
f9864:c0:m11
@WNetworkClientCapabilities.capability(WNetworkClientCapabilities.remove_file, WebDavException, ValueError)<EOL><INDENT>@verify_type(file_name=str)<EOL>@verify_value(file_name=lambda x: len(x) > <NUM_LIT:0>)<EOL>def remove_file(self, file_name, *args, **kwargs):<DEDENT>
client = self.dav_client()<EOL>remote_path = self.join_path(self.session_path(), file_name)<EOL>if client.is_dir(remote_path) is True:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>client.clean(remote_path)<EOL>
:meth:`.WNetworkClientProto.remove_file` method implementation
f9864:c0:m12
@verify_type('<STR_LIT>', uri=WURI)<EOL><INDENT>def __init__(self, uri):<DEDENT>
WWebDavClientBase.__init__(self, uri, '<STR_LIT:http>')<EOL>
Create new DAV-client :param uri: URI for a client connection
f9864:c1:m0
@classmethod<EOL><INDENT>def scheme_name(cls):<DEDENT>
return '<STR_LIT>'<EOL>
:meth:`.WWebDavClientBase.scheme_name` method implementation
f9864:c1:m1
@verify_type('<STR_LIT>', uri=WURI)<EOL><INDENT>def __init__(self, uri):<DEDENT>
WWebDavClientBase.__init__(self, uri, '<STR_LIT>')<EOL>
Create new DAVS-client :param uri: URI for a client connection
f9864:c2:m0
@classmethod<EOL><INDENT>def scheme_name(cls):<DEDENT>
return '<STR_LIT>'<EOL>
:meth:`.WWebDavClientBase.scheme_name` method implementation
f9864:c2:m1
@verify_type(uri=WURI)<EOL><INDENT>def __init__(self, uri):<DEDENT>
WVirtualDirectoryClient.__init__(self, uri, start_path=uri.path())<EOL>
Create new client that interacts with local filesystem :param uri: URI for a client connection
f9866:c0:m0
def directory_sep(self):
return os.path.sep<EOL>
:meth:`.WNetworkClientProto.directory_sep` implementation
f9866:c0:m1
def connect(self):
try:<EOL><INDENT>self.list_directory()<EOL><DEDENT>except WClientCapabilityError as e:<EOL><INDENT>raise WClientConnectionError(<EOL>'<STR_LIT>'<EOL>) from e<EOL><DEDENT>
:meth:`.WNetworkClientProto.connect` method implementation
f9866:c0:m2
def disconnect(self):
self.session_path(self.directory_sep())<EOL>
:meth:`.WNetworkClientProto.disconnect` method implementation
f9866:c0:m3
@classmethod<EOL><INDENT>def scheme_specification(cls):<DEDENT>
return WSchemeSpecification(<EOL>'<STR_LIT:file>',<EOL>WURIComponentVerifier(WURI.Component.path, WURIComponentVerifier.Requirement.optional)<EOL>)<EOL>
:meth:`.WSchemeHandler.scheme_specification` method implementation
f9866:c0:m4
@WNetworkClientCapabilities.capability(WNetworkClientCapabilities.current_dir, *__basic_file_exceptions__)<EOL><INDENT>def current_directory(self, *args, **kwargs):<DEDENT>
return self.session_path()<EOL>
:meth:`.WNetworkClientProto.current_directory` method implementation
f9866:c0:m5
@WNetworkClientCapabilities.capability(<EOL>WNetworkClientCapabilities.change_dir, ValueError, *__basic_file_exceptions__<EOL>)<EOL><INDENT>@verify_type(path=str)<EOL>@verify_value(path=lambda x: len(x) > <NUM_LIT:0>)<EOL>def change_directory(self, path, *args, **kwargs):<DEDENT>
previous_path = self.session_path()<EOL>self.session_path(path)<EOL>if os.path.isdir(self.full_path()) is False:<EOL><INDENT>self.session_path(previous_path)<EOL>raise ValueError('<STR_LIT>')<EOL><DEDENT>
:meth:`.WNetworkClientProto.change_directory` method implementation
f9866:c0:m6
@WNetworkClientCapabilities.capability(WNetworkClientCapabilities.list_dir, *__basic_file_exceptions__)<EOL><INDENT>def list_directory(self, *args, **kwargs):<DEDENT>
return tuple(os.listdir(self.full_path()))<EOL>
:meth:`.WNetworkClientProto.list_directory` method implementation
f9866:c0:m7
@WNetworkClientCapabilities.capability(WNetworkClientCapabilities.make_dir, *__basic_file_exceptions__)<EOL><INDENT>@verify_type(directory_name=str)<EOL>@verify_value(directory_name=lambda x: len(x) > <NUM_LIT:0>)<EOL>def make_directory(self, directory_name, *args, **kwargs):<DEDENT>
previous_path = self.session_path()<EOL>try:<EOL><INDENT>self.session_path(directory_name)<EOL>os.mkdir(self.full_path())<EOL><DEDENT>finally:<EOL><INDENT>self.session_path(previous_path)<EOL><DEDENT>
:meth:`.WNetworkClientProto.make_directory` method implementation
f9866:c0:m8
@WNetworkClientCapabilities.capability(WNetworkClientCapabilities.remove_dir, *__basic_file_exceptions__)<EOL><INDENT>@verify_type(directory_name=str)<EOL>@verify_value(directory_name=lambda x: len(x) > <NUM_LIT:0>)<EOL>def remove_directory(self, directory_name, *args, **kwargs):<DEDENT>
previous_path = self.session_path()<EOL>try:<EOL><INDENT>self.session_path(directory_name)<EOL>os.rmdir(self.full_path())<EOL><DEDENT>finally:<EOL><INDENT>self.session_path(previous_path)<EOL><DEDENT>
:meth:`.WNetworkClientProto.remove_directory` method implementation
f9866:c0:m9
@WNetworkClientCapabilities.capability(WNetworkClientCapabilities.upload_file, *__basic_file_exceptions__)<EOL><INDENT>@verify_type(file_name=str)<EOL>@verify_value(file_name=lambda x: len(x) > <NUM_LIT:0>)<EOL>def upload_file(self, file_name, file_obj, *args, **kwargs):<DEDENT>
previous_path = self.session_path()<EOL>try:<EOL><INDENT>self.session_path(file_name)<EOL>with open(self.full_path(), mode='<STR_LIT:wb>') as f:<EOL><INDENT>chunk = file_obj.read(io.DEFAULT_BUFFER_SIZE)<EOL>while len(chunk) > <NUM_LIT:0>:<EOL><INDENT>f.write(chunk)<EOL>chunk = file_obj.read(io.DEFAULT_BUFFER_SIZE)<EOL><DEDENT><DEDENT><DEDENT>finally:<EOL><INDENT>self.session_path(previous_path)<EOL><DEDENT>
:meth:`.WNetworkClientProto.upload_file` method implementation
f9866:c0:m10
@WNetworkClientCapabilities.capability(WNetworkClientCapabilities.remove_file, *__basic_file_exceptions__)<EOL><INDENT>@verify_type(file_name=str)<EOL>@verify_value(file_name=lambda x: len(x) > <NUM_LIT:0>)<EOL>def remove_file(self, file_name, *args, **kwargs):<DEDENT>
previous_path = self.session_path()<EOL>try:<EOL><INDENT>self.session_path(file_name)<EOL>os.unlink(self.full_path())<EOL><DEDENT>finally:<EOL><INDENT>self.session_path(previous_path)<EOL><DEDENT>
:meth:`.WNetworkClientProto.remove_file` method implementation
f9866:c0:m11
@verify_type('<STR_LIT>', uri=WURI)<EOL><INDENT>def __init__(self, uri):<DEDENT>
WNetworkClientProto.__init__(self, uri)<EOL>self.__ftp_client = ftplib.FTP()<EOL>self.__ftp_connect_args = {'<STR_LIT:host>': uri.hostname()}<EOL>self.__ftp_auth_args = {}<EOL>if uri.username() is not None:<EOL><INDENT>self.__ftp_auth_args['<STR_LIT:user>'] = uri.username()<EOL><DEDENT>if uri.password():<EOL><INDENT>self.__ftp_auth_args['<STR_LIT>'] = uri.password()<EOL><DEDENT>
Create new FTP-client :param uri: URI for a client connection
f9867:c0:m0
def ftp_client(self):
return self.__ftp_client<EOL>
Return FTP-client for accessing a server (internal usage only) :return: ftplib.FTP
f9867:c0:m1
def connect(self):
exceptions = list(__basic_ftp_exceptions__)<EOL>exceptions.append(OSError) <EOL>exceptions.append(ConnectionRefusedError) <EOL>try:<EOL><INDENT>self.ftp_client().connect(**self.__ftp_connect_args)<EOL>self.ftp_client().login(**self.__ftp_auth_args)<EOL><DEDENT>except tuple(exceptions) as e:<EOL><INDENT>raise WClientConnectionError('<STR_LIT>') from e<EOL><DEDENT>try:<EOL><INDENT>path = self.uri().path()<EOL>if path is None:<EOL><INDENT>path = self.directory_sep()<EOL><DEDENT>self.change_directory(path)<EOL><DEDENT>except WClientCapabilityError as e:<EOL><INDENT>raise WClientConnectionError(<EOL>'<STR_LIT>'<EOL>) from e<EOL><DEDENT>
:meth:`.WNetworkClientProto.connect` method implementation
f9867:c0:m2
def disconnect(self):
self.ftp_client().close()<EOL>
:meth:`.WNetworkClientProto.disconnect` method implementation
f9867:c0:m3
@classmethod<EOL><INDENT>def scheme_specification(cls):<DEDENT>
return WSchemeSpecification(<EOL>'<STR_LIT>',<EOL>WURIComponentVerifier(WURI.Component.username, WURIComponentVerifier.Requirement.optional),<EOL>WURIComponentVerifier(WURI.Component.password, WURIComponentVerifier.Requirement.optional),<EOL>WURIComponentVerifier(WURI.Component.hostname, WURIComponentVerifier.Requirement.required),<EOL>WURIComponentVerifier(WURI.Component.path, WURIComponentVerifier.Requirement.optional)<EOL>)<EOL>
:meth:`.WSchemeHandler.scheme_specification` method implementation
f9867:c0:m4
@WNetworkClientCapabilities.capability(WNetworkClientCapabilities.current_dir, *__basic_ftp_exceptions__)<EOL><INDENT>def current_directory(self, *args, **kwargs):<DEDENT>
return self.ftp_client().pwd()<EOL>
:meth:`.WNetworkClientProto.current_directory` method implementation
f9867:c0:m5
@WNetworkClientCapabilities.capability(WNetworkClientCapabilities.change_dir, *__basic_ftp_exceptions__)<EOL><INDENT>@verify_type(path=str)<EOL>@verify_value(path=lambda x: len(x) > <NUM_LIT:0>)<EOL>def change_directory(self, path, *args, **kwargs):<DEDENT>
self.ftp_client().cwd(path)<EOL>
:meth:`.WNetworkClientProto.change_directory` method implementation
f9867:c0:m6
@WNetworkClientCapabilities.capability(WNetworkClientCapabilities.list_dir, *__basic_ftp_exceptions__)<EOL><INDENT>def list_directory(self, *args, **kwargs):<DEDENT>
return tuple(self.ftp_client().nlst())<EOL>
:meth:`.WNetworkClientProto.list_directory` method implementation
f9867:c0:m7
@WNetworkClientCapabilities.capability(WNetworkClientCapabilities.make_dir, *__basic_ftp_exceptions__)<EOL><INDENT>@verify_type(directory_name=str)<EOL>@verify_value(directory_name=lambda x: len(x) > <NUM_LIT:0>)<EOL>def make_directory(self, directory_name, *args, **kwargs):<DEDENT>
self.ftp_client().mkd(directory_name)<EOL>
:meth:`.WNetworkClientProto.make_directory` method implementation
f9867:c0:m8
@WNetworkClientCapabilities.capability(WNetworkClientCapabilities.remove_dir, *__basic_ftp_exceptions__)<EOL><INDENT>@verify_type(directory_name=str)<EOL>@verify_value(directory_name=lambda x: len(x) > <NUM_LIT:0>)<EOL>def remove_directory(self, directory_name, *args, **kwargs):<DEDENT>
self.ftp_client().rmd(directory_name)<EOL>
:meth:`.WNetworkClientProto.remove_directory` method implementation
f9867:c0:m9
@WNetworkClientCapabilities.capability(WNetworkClientCapabilities.upload_file, *__basic_ftp_exceptions__)<EOL><INDENT>@verify_type(file_name=str)<EOL>@verify_value(file_name=lambda x: len(x) > <NUM_LIT:0>)<EOL>def upload_file(self, file_name, file_obj, *args, **kwargs):<DEDENT>
self.ftp_client().storbinary('<STR_LIT>' + file_name, file_obj)<EOL>
:meth:`.WNetworkClientProto.upload_file` method implementation
f9867:c0:m10
@WNetworkClientCapabilities.capability(WNetworkClientCapabilities.remove_file, *__basic_ftp_exceptions__)<EOL><INDENT>@verify_type(file_name=str)<EOL>@verify_value(file_name=lambda x: len(x) > <NUM_LIT:0>)<EOL>def remove_file(self, file_name, *args, **kwargs):<DEDENT>
self.ftp_client().delete(file_name)<EOL>
:meth:`.WNetworkClientProto.remove_file` method implementation
f9867:c0:m11
@verify_type(start_path=(str, None))<EOL><INDENT>@verify_type('<STR_LIT>', uri=WURI)<EOL>@verify_value(start_path=lambda x: x is None or len(x) > <NUM_LIT:0>)<EOL>def __init__(self, uri, start_path=None):<DEDENT>
WNetworkClientProto.__init__(self, uri)<EOL>self.__session_path = self.directory_sep()<EOL>self.__normalize_re = re.compile('<STR_LIT>' % (self.directory_sep(), self.directory_sep()))<EOL>self.__start_path = self.normalize_path(start_path) if start_path is not None else self.directory_sep()<EOL>
Create new client :param uri: same as uri in :meth:`.WNetworkClientProto.__init__` :param start_path: this is a start point that prepends before a session directory for getting a \ full path. By default, start point is a same as a directory separator)
f9868:c0:m0
def start_path(self):
return self.__start_path<EOL>
Return a start path for this client :return: str
f9868:c0:m1
@verify_type(path=str)<EOL><INDENT>@verify_value(path=lambda x: len(x) > <NUM_LIT:0>)<EOL>def normalize_path(self, path):<DEDENT>
return self.__normalize_re.sub(self.directory_sep(), path)<EOL>
Normalize the given path, like removing redundant directory separators :param path: path to normalize :return: str
f9868:c0:m2
@verify_type(path=str)<EOL><INDENT>@verify_value(path=lambda x: len(x) > <NUM_LIT:0>)<EOL>def join_path(self, *path):<DEDENT>
path = self.directory_sep().join(path)<EOL>return self.normalize_path(path)<EOL>
Unite entries to generate a single path :param path: path items to unite :return: str
f9868:c0:m3
@verify_type(path=(str, None))<EOL><INDENT>@verify_value(path=lambda x: x is None or len(x) > <NUM_LIT:0>)<EOL>def session_path(self, path=None):<DEDENT>
if path is not None:<EOL><INDENT>if path.startswith(self.directory_sep()) is True:<EOL><INDENT>self.__session_path = self.normalize_path(path)<EOL><DEDENT>else:<EOL><INDENT>self.__session_path = self.join_path(self.__session_path, path)<EOL><DEDENT><DEDENT>return self.__session_path<EOL>
Set and/or get current session path :param path: if defined, then set this value as a current session directory. If this value starts \ with a directory separator, then it is treated as an absolute path. In this case this value replaces a current one, otherwise this value is appended to a current one. :return: str
f9868:c0:m4
def full_path(self):
return self.normalize_path(self.directory_sep().join((self.start_path(), self.session_path())))<EOL>
Return a full path to a current session directory. A result is made by joining a start path with current session directory :return: str
f9868:c0:m5
@staticmethod<EOL><INDENT>@verify_subclass(wrap_exceptions=Exception)<EOL>def capability(cap, *wrap_exceptions):<DEDENT>
if isinstance(cap, WNetworkClientCapabilities) is True:<EOL><INDENT>cap = cap.value<EOL><DEDENT>elif isinstance(cap, str) is False:<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>def first_level_decorator(decorated_function):<EOL><INDENT>def second_level_decorator(original_function, *args, **kwargs):<EOL><INDENT>if len(wrap_exceptions) == <NUM_LIT:0>:<EOL><INDENT>return original_function(*args, **kwargs)<EOL><DEDENT>try:<EOL><INDENT>return original_function(*args, **kwargs)<EOL><DEDENT>except wrap_exceptions as e:<EOL><INDENT>raise WClientCapabilityError(<EOL>'<STR_LIT>' % cap<EOL>) from e<EOL><DEDENT><DEDENT>result_fn = decorator(second_level_decorator)(decorated_function)<EOL>result_fn.__capability_name__ = cap<EOL>return result_fn<EOL><DEDENT>return first_level_decorator<EOL>
Return a decorator, that registers function as capability. Also, all specified exceptions are caught and instead of them the :class:`.WClientCapabilityError` exception is raised :param cap: target function capability (may be a str or :class:`.WNetworkClientCapabilities` class ) :param wrap_exceptions: exceptions to caught :return: decorator
f9869:c2:m0
@verify_type(uri=WURI)<EOL><INDENT>def __init__(self, uri):<DEDENT>
WSchemeHandler.__init__(self)<EOL>WCapabilitiesHolder.__init__(self)<EOL>self.__uri = uri<EOL>
Create new network client :param uri: URI, that describes client connection
f9869:c3:m0
@abstractmethod<EOL><INDENT>def connect(self):<DEDENT>
raise NotImplementedError('<STR_LIT>')<EOL>
Connect to a source specified in URI :return: None
f9869:c3:m1
@abstractmethod<EOL><INDENT>def disconnect(self):<DEDENT>
raise NotImplementedError('<STR_LIT>')<EOL>
Disconnect from a source :return: None
f9869:c3:m2
def uri(self):
return self.__uri<EOL>
Return an URI, that was specified in a constructor :return: WURI
f9869:c3:m3
@verify_type(cap_name=(str, WNetworkClientCapabilities))<EOL><INDENT>def capability(self, cap_name):<DEDENT>
if isinstance(cap_name, WNetworkClientCapabilities) is True:<EOL><INDENT>cap_name = cap_name.value<EOL><DEDENT>return WCapabilitiesHolder.capability(self, cap_name)<EOL>
Overrides original :meth:`.WCapabilitiesHolder.capability` method to support :class:`.WNetworkClientCapabilities` as a capability value
f9869:c3:m4
@verify_type(caps=(str, WNetworkClientCapabilities))<EOL><INDENT>def has_capabilities(self, *caps):<DEDENT>
for cap in caps:<EOL><INDENT>if isinstance(cap, WNetworkClientCapabilities) is True:<EOL><INDENT>cap = cap.value<EOL><DEDENT>if WCapabilitiesHolder.has_capabilities(self, cap) is False:<EOL><INDENT>return False<EOL><DEDENT><DEDENT>return True<EOL>
Overrides original :meth:`.WCapabilitiesHolder.has_capabilities` method to support :class:`.WNetworkClientCapabilities` as a capability value
f9869:c3:m5
@verify_type(cap_name=(str, WNetworkClientCapabilities))<EOL><INDENT>def __call__(self, cap_name, *args, **kwargs):<DEDENT>
if isinstance(cap_name, WNetworkClientCapabilities) is True:<EOL><INDENT>cap_name = cap_name.value<EOL><DEDENT>return WCapabilitiesHolder.__call__(self, cap_name, *args, **kwargs)<EOL>
Overrides original :meth:`.WCapabilitiesHolder.__call__` method to support :class:`.WNetworkClientCapabilities` as a capability value
f9869:c3:m6
@classmethod<EOL><INDENT>@verify_type(uri=WURI)<EOL>def create_handler(cls, uri, **kwargs):<DEDENT>
return cls(uri)<EOL>
:meth:`.WSchemeHandler.create_handler` method implementation
f9869:c3:m7
def directory_sep(self):
return '<STR_LIT:/>'<EOL>
Return symbol that is used by this client as a directory separator. If a path starts with that symbol then it treats as an absolute path by default :return: str
f9869:c3:m8
def current_directory(self, *args, **kwargs):
raise NotImplementedError('<STR_LIT>')<EOL>
Return current session directory :param args: extra positional arguments that may be used by a capability :param kwargs: extra keyword-based arguments that may be used by a capability :return: None
f9869:c3:m9
@verify_type(path=str)<EOL><INDENT>@verify_value(path=lambda x: len(x) > <NUM_LIT:0>)<EOL>def change_directory(self, path, *args, **kwargs):<DEDENT>
raise NotImplementedError('<STR_LIT>')<EOL>
Change current session directory to the specified one. If the path begins with directory separator then it may be treated as an absolute path :param path: target directory :param args: extra positional arguments that may be used by a capability :param kwargs: extra keyword-based arguments that may be used by a capability :return: None
f9869:c3:m10
def list_directory(self, *args, **kwargs):
raise NotImplementedError('<STR_LIT>')<EOL>
List current session directory :param args: extra positional arguments that may be used by a capability :param kwargs: extra keyword-based arguments that may be used by a capability :return: tuple of str
f9869:c3:m11
@verify_type(directory_name=str)<EOL><INDENT>@verify_value(directory_name=lambda x: len(x) > <NUM_LIT:0>)<EOL>def make_directory(self, directory_name, *args, **kwargs):<DEDENT>
raise NotImplementedError('<STR_LIT>')<EOL>
Create directory. A directory is created in a current session directory. And a name must not contain a directory separator :param directory_name: directory name to create :param args: extra positional arguments that may be used by a capability :param kwargs: extra keyword-based arguments that may be used by a capability :return: None
f9869:c3:m12
@verify_type(directory_name=str)<EOL><INDENT>@verify_value(directory_name=lambda x: len(x) > <NUM_LIT:0>)<EOL>def remove_directory(self, directory_name, *args, **kwargs):<DEDENT>
raise NotImplementedError('<STR_LIT>')<EOL>
Remove directory. A directory is removed from a current session directory. And a name must not contain a directory separator. :param directory_name: directory name to remove :param args: extra positional arguments that may be used by a capability :param kwargs: extra keyword-based arguments that may be used by a capability :return: None
f9869:c3:m13
@verify_type(file_name=str)<EOL><INDENT>@verify_value(file_name=lambda x: len(x) > <NUM_LIT:0>)<EOL>def upload_file(self, file_name, file_obj, *args, **kwargs):<DEDENT>
raise NotImplementedError('<STR_LIT>')<EOL>
Upload file. File will be uploaded to a current session directory. A name must not contain a directory separator :param file_name: target file name :param file_obj: source object to upload :param args: extra positional arguments that may be used by a capability :param kwargs: extra keyword-based arguments that may be used by a capability :return: None
f9869:c3:m14
@verify_type(file_name=str)<EOL><INDENT>@verify_value(file_name=lambda x: len(x) > <NUM_LIT:0>)<EOL>def remove_file(self, file_name, *args, **kwargs):<DEDENT>
raise NotImplementedError('<STR_LIT>')<EOL>
Remove file. File will be removed from a current session directory. A name must not contain a directory separator :param file_name: file to remove :param args: extra positional arguments that may be used by a capability :param kwargs: extra keyword-based arguments that may be used by a capability :return: None
f9869:c3:m15
@abstractmethod<EOL><INDENT>@verify_type(config=WConfig)<EOL>def server_socket(self, config):<DEDENT>
raise NotImplementedError('<STR_LIT>')<EOL>
Return server socket. This socket is used for receiving requests and sending results. It is important, that the result can be polled by a IOLoop instance. :param config: server configuration :return: socket.socket
f9870:c0:m0
@abstractmethod<EOL><INDENT>@verify_type(config=WConfig, close_fd=bool)<EOL>def close_server_socket(self, config, close_fd=True):<DEDENT>
raise NotImplementedError('<STR_LIT>')<EOL>
Close previously opened server socket. If no socket is opened - do nothing. :param config: server configuration :param close_fd: should this function close socket fd, or will it close by an external function?. It \ is safer to pass True here. :return: None
f9870:c0:m1
@abstractmethod<EOL><INDENT>@verify_type(config=WConfig)<EOL>def client_socket(self, config):<DEDENT>
raise NotImplementedError('<STR_LIT>')<EOL>
Return client socket. This socket is used for sending request and receiving results. It is important, that the result can be polled by a IOLoop instance. :param config: client configuration :return: socket.socket
f9870:c0:m2
@abstractmethod<EOL><INDENT>@verify_type(config=WConfig, close_fd=bool)<EOL>def close_client_socket(self, config, close_fd=True):<DEDENT>
raise NotImplementedError('<STR_LIT>')<EOL>
Close previously opened client socket. If no socket is opened - do nothing. :param config: client configuration :param close_fd: should this function close socket fd, or will it close by an external function?. It \ is safer to pass True here. :return: None
f9870:c0:m3
@verify_type(config=WConfig)<EOL><INDENT>def target_socket(self, config):<DEDENT>
raise NotImplementedError('<STR_LIT>')<EOL>
Return socket information with server address. Mostly used for address validation. :param config: client configuration :return: WIPV4SocketInfo
f9870:c0:m4
@verify_type(config=WConfig)<EOL><INDENT>def bind_socket(self, config):<DEDENT>
raise NotImplementedError('<STR_LIT>')<EOL>
Return socket information with address that server binds to. :param config: server configuration :return: WIPV4SocketInfo
f9870:c0:m5
@verify_type(section=str, address_option=str, port_option=str)<EOL><INDENT>@verify_value(section=lambda x: len(x) > <NUM_LIT:0>, address_option=lambda x: len(x) > <NUM_LIT:0>, port_option=lambda x: len(x) > <NUM_LIT:0>)<EOL>def __init__(self, section, address_option, port_option):<DEDENT>
self.section = section<EOL>self.address_option = address_option<EOL>self.port_option = port_option<EOL>
Construct new configuration settings :param section: section name :param address_option: address option name :param port_option: port option name
f9870:c1:m0
@verify_type(target_socket_config=WNetworkNativeTransportSocketConfig)<EOL><INDENT>@verify_type(bind_socket_config=WNetworkNativeTransportSocketConfig)<EOL>def __init__(self, target_socket_config, bind_socket_config):<DEDENT>
self.__target_socket_config = target_socket_config<EOL>self.__bind_socket_config = bind_socket_config<EOL>self.__server_socket = None<EOL>self.__client_socket = None<EOL>
Create new transport :param target_socket_config: configuration for client socket :param bind_socket_config: configuration for server socket
f9870:c2:m0
@verify_type(config=WConfig)<EOL><INDENT>def target_socket(self, config):<DEDENT>
address = config[self.__target_socket_config.section][self.__target_socket_config.address_option]<EOL>port = config.getint(self.__target_socket_config.section, self.__target_socket_config.port_option)<EOL>target = WIPV4SocketInfo(address, port)<EOL>if target.address() is None or target.port() is None:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>return target<EOL>
:meth:`.WNetworkNativeTransportProto.server_socket` method implementation
f9870:c2:m1
@verify_type(config=WConfig)<EOL><INDENT>def bind_socket(self, config):<DEDENT>
address = config[self.__bind_socket_config.section][self.__bind_socket_config.address_option]<EOL>port = config.getint(self.__bind_socket_config.section, self.__bind_socket_config.port_option)<EOL>return WIPV4SocketInfo(address, port)<EOL>
:meth:`.WNetworkNativeTransportProto.bind_socket` method implementation
f9870:c2:m2
@abstractmethod<EOL><INDENT>def _create_socket(self):<DEDENT>
raise NotImplementedError('<STR_LIT>')<EOL>
Create general socket object, that can be used for client and/or server usage :return: socket.socket
f9870:c2:m3
@verify_type(config=WConfig)<EOL><INDENT>def create_server_socket(self, config):<DEDENT>
return self._create_socket()<EOL>
Create socket for server. (By default, same as WNetworkNativeTransport._create_socket) :param config: server configuration :return: socket.socket
f9870:c2:m4
@verify_type(config=WConfig)<EOL><INDENT>def create_client_socket(self, config):<DEDENT>
return self._create_socket()<EOL>
Create socket for client. (By default, same as WNetworkNativeTransport._create_socket) :param config: client configuration :return: socket.socket
f9870:c2:m5
@verify_type(config=WConfig)<EOL><INDENT>def server_socket(self, config):<DEDENT>
if self.__server_socket is None:<EOL><INDENT>self.__server_socket = self.create_server_socket(config)<EOL>self.__server_socket.bind(self.bind_socket(config).pair())<EOL><DEDENT>return self.__server_socket<EOL>
:meth:`.WNetworkNativeTransportProto.server_socket` method implementation
f9870:c2:m6
@verify_type(config=WConfig, close_fd=bool)<EOL><INDENT>def close_server_socket(self, config, close_fd=True):<DEDENT>
if close_fd is True:<EOL><INDENT>self.__server_socket.close()<EOL><DEDENT>self.__server_socket = None<EOL>
:meth:`.WNetworkNativeTransportProto.close_server_socket` method implementation
f9870:c2:m7
@verify_type(config=WConfig)<EOL><INDENT>def client_socket(self, config):<DEDENT>
if self.__client_socket is None:<EOL><INDENT>self.__client_socket = self.create_client_socket(config)<EOL><DEDENT>return self.__client_socket<EOL>
:meth:`.WNetworkNativeTransportProto.client_socket` method implementation
f9870:c2:m8