Services

Assemblies can only access some services explicitly exposed to them because the sandbox can not allow arbitrary calls to any library. The services integrated in Pinyto also try to be easy and pleasurable to use.

The database wrapper is already explained in the Database section.

Response Helper

Most requests in pinyto expect a response in the form of a HttpResponse object with type “application/json” and a JSON encoded string as payload. Because this is so common an easy to use helper function is used:

service.response.json_response(data)[source]

Returns the json as string with correct mimetype.

@param data: dict @return: HttpResponse

Factory

In assemblies it is not allowed to include python modules or classes. Service classes can be instantiated using the Factory class which is accessible.

class service.models.Factory[source]

Use this factory to create objects in outside the sandboxed process. Just pass the class name to the create method.

static create(class_name, *args)[source]

This method will create an object of the class of classname with the arguments supplied after that. If the class can not be created in the sandbox it throws an Exception. The Exception gets thrown even if this is not executed inside the sandbox because every code should be executable in the sandbox.

Parameters:
  • class_name (str) –
  • args – additional arguments
Returns:

Objects of the type specified in class_name

Return type:

Object

The factory can create the following classes:

  • Http
  • ParseHtml

Http

Http uses internally the requests library from Apache.

class service.http.Http[source]

Objects of this class can be used to connect to remote websites.

static get(url)[source]

This issues a http request to the supplied url and returns the response as a string. If the request fails an empty string is returned.

Parameters:url – Url with http:// or https:// at the beginning
Type:str
Return type:str
static post(url='', data=None)[source]

This issues a http request to the supplied url and returns the response as a string. If the request fails an empty string is returned.

For the params do not forget to add an @ to the beginning of each param name.

Parameters:
Return type:

str

ParseHtml

ParseHtml is based on BeautifulSoup version 4. The interface is quite different as the wrapper does not use a fluid interface.

class service.parsehtml.ParseHtml(html)[source]

Use this service to get information from html documents.

contains(descriptions)[source]

Use this function to check if the html contains the described tag. The descriptions must be a list of python dictionaries with {'tag': 'tagname', 'attrs': dict}

Parameters:descriptions (dict) –
Return type:boolean
find_element_and_collect_table_like_information(descriptions, searched_information)[source]

If you are retrieving data from websites you might need to get the contents of a table or a similar structure. This is the function to get that information. The descriptions must be a list of python dictionaries with {'tag': 'tag name', 'attrs': dict}. The last description in this list will be used for a findAll of that element. This should select all the rows of the table you want to read. specify all the information you are searching for in searched_information in the following format: {'name': {'search tag': 'td', 'search attrs': dict, 'captions': ['list', 'of', 'captions'], 'content tag': 'td', 'content attrs': dict}, 'next name': ...}

Parameters:
  • descriptions (dict) –
  • searched_information (dict) –
Return type:

dict

find_element_and_get_attribute_value(descriptions, attribute)[source]

Use this function to find the described tag and return the value from attribute if the tag is found. Returns empty string if the tag or the attribute is not found. The descriptions must be a list of python dictionaries with {'tag': 'tag name', 'attrs': dict}

Parameters:
  • descriptions (dict) –
  • attribute (str) –
Returns:

string or list if attribute is class

For this class the extract_content function from service.xml might become handy.

service.parsehtml.extract_content(tag)[source]

Takes a tag and returns the string content without markup.

Parameters:tag – BeautifulSoup Tag
Return type:str