Commit fed770d6 authored by Franziska Koehn's avatar Franziska Koehn
Browse files

documentation

parent 25aca6f8
"""
:Author: Franziska Koehn
:Created: 2015/01/13
This module houses functions relating to creating the chart.
"""
def count_substrings(substrings, strings):
"""
Returns a dictionary including the counts of its keys. This keys are the given list of substrings
**Parameters**
:substrings: list of substrings to be counted
:strings: strings in which the counts of substrings will be determined
"""
from collections import defaultdict
results = defaultdict(int)
......@@ -12,13 +28,24 @@ def count_substrings(substrings, strings):
results[sub] += 1
return list(results.items())
def create(file, data, size, y_max, x_max):
"""
creates diagram for given 'data' and max-values ('y_max', 'x_max') and renders it as a image, saved as 'file' in given 'size'
**Parameters**
:file: destination/filename where the rendered image will be saved
:data: dictionary, the key will be shown on the x-axis. the value on the y-axis. if a two-dimensional list will be pass, the values on the first dimension will drawn on the x-axis, the second on the y-axis
:size: size of rendered image
:y_max: max-value for y-axis
:x_max: max-value for x-axis (= count of keys)
"""
import matplotlib.pyplot as plt
GNOME_BLUE = '#3A81CC'
DPI = float(90)
BAR_WIDTH = 0.8 # width of bars
BAR_START = 0.1 # start of bars
GNOME_BLUE = '#3A81CC' # the proper gtk-widgets-colour
DPI = float(90) # dots per inch
BAR_WIDTH = 0.8 # width of bars
BAR_START = 0.1 # start of bars
w, h = size
labels = []
......@@ -44,7 +71,6 @@ def create(file, data, size, y_max, x_max):
bars = plt.bar(left=x_pos, height=values, width=BAR_WIDTH, color=GNOME_BLUE)
plt.xticks(label_pos, labels)
plt.ylabel('Count')
# plt.title('Counts of Substrings')
plt.gcf().set_size_inches(w/DPI,h/DPI)
for b in bars:
......
"""
:Author: Franziska Koehn
:Created: 2015/01/13
This module contains functions for reading the datatypes from json-files.
"""
from xsa.errors import *
DIR = 'datatypes/'
"""Directory, where all files are saved"""
def get_all(force=False):
"""
Reads all json-files in the given directory "DIR". Ignores corrupted files.
If cache is not None and force is not True, it will return the saved values.
**Parameters**
:force: if True, the functions reeds the types new from file and sets saves the new values in the cache.
"""
if get_all.cache and not force:
return get_all.cache
......@@ -9,7 +28,7 @@ def get_all(force=False):
from os.path import isfile, join
import json
get_all.cache = []
files = [ f for f in listdir(DIR) if isfile(join(DIR,f)) ]
files = [ f for f in listdir(DIR) if isfile(join(DIR,f)) ] #list of all files in the directory 'DIR'
for file in files:
with open(join(DIR,file)) as f:
try:
......@@ -20,7 +39,12 @@ def get_all(force=False):
return get_all()
get_all.cache = None
#TODO rename type to root_type
def get_rest(type):
"""
Returns REST-API-Interface of a given root-type, defined in its json-file. If no REST-API-Interface was defined it will raise a xsa.errors.NoRestApiError.
"""
for data in get_all():
if data['root-type'] == type:
if not 'REST-API' in data or not data['REST-API']:
......@@ -28,18 +52,39 @@ def get_rest(type):
else:
return data['REST-API']
# TODO rename data_types to root_types
def get_data_types():
"""
Returns all Root-Types.
"""
result = []
for data in get_all():
result.append(data['root-type'])
return result
# TODO rename type to root_types
def get_fields(type):
"""
Returns all fields of a given Root-Type.
**Parameters**
:type: Root-Type of returned fields
"""
for data in get_all():
if data['root-type'] == type:
return data['fields']
# TODO rename type to root_types
def get_labels(type):
"""
returns a list of labels of fields of a given root_type.
**Parameters**
:type: Root-Type of fields, their labels will be returned
"""
result = []
fields = get_fields(type)
if fields is not None:
......@@ -47,21 +92,47 @@ def get_labels(type):
result.append(f['label'])
return result
# TODO rename type to root_type
def get_field_label_by_key(type, key):
"""
Returns the label of the given key.
**Parameters**
:type: Root-Type of given key
:key: key of field, of which the label will be returned
"""
fields = get_fields(type)
for f in fields:
if "key" in f and f["key"] == key:
return f["label"]
return key
def get_field_label_by_field(root_type, field):
"""
Returns the label of the given field.
**Parameters**
:root_type: Root-Type of given field
:field: field, of which the label will be returned
"""
fields = get_fields(root_type)
for f in fields:
if f['field'] == field:
return f['label']
return field
# TODO rename type to field
# TODO a tuple or would be a dictionary better?
def get_fields_required(type):
"""
Returns a list, containing a tuple in which the first value is the label of a field and the second the boolean, if its required or not.
**Parameters**
:type: Root-Type of fields
"""
result = []
fields = get_fields(type)
if fields is not None:
......@@ -69,7 +140,16 @@ def get_fields_required(type):
result.append((f['label'],f['required']))
return result
# TODO rename type to root_types
def get_fields_from_labels(labels, type):
"""
Returns a list of fields of the labels in the given list labels.
**Parameters**
:labels:
:type:
"""
result=[]
fields = get_fields(type)
if fields is not None:
......
"""
:Author: Franziska Koehn
:Created: 2015/01/13
This module houses all classes of Exceptions of xsa. Some Exceptions do already exist in modules like httplib2 (...), the where here implemented for let some classes not know this libaries just for the exception.
"""
# TODO rename to xsa-exception ???
class Error(Exception):
"""A new class Error to discrimminate between xsa and non-xsa Exceptions"""
pass
class ServerNotFoundError(Error):
"""Should be raised when the server was not found."""
pass
class DatabaseError(Error):
"""Should be raised when a DatabaseError occured."""
pass
class UnauthorizedError(Error):
"""Should be raised when the user-name or -password was incorrect."""
pass
class ResponseNotReady(Error):
"""Should be raised when the response was not ready."""
pass
class EmptyResultset(Error):
"""Should be raised when the resultset of the given search is empty."""
pass
# TODO QueryError == CorruptedQueryError???
class QueryError(Error):
"""Should be raised when the query-definition is incompatible."""
pass
class NoRestApiError(Error):
"""Should be raised when no REST-API-Interface was defined in the json-file of a root-type"""
pass
# TODO QueryError == CorruptedQueryError???
class CorruptedQueryError(Error):
"""Should be raised when the query-definition is incompatible."""
pass
"""
:Author: Franziska Koehn
:Created: 2015/01/13
This module includes functions around sending and defining queries.
"""
from threading import Lock
requests_lock = Lock()
def get_query_methods():
"""Returns all applicable methods for creating a query."""
return ["AND", "OR"]
def get_operators():
"""Returns all applicable Operators for creating a query."""
return ["LIKE", ">", "<", "<=", ">=", "="]
#TODO download_async and download as one function???
def download_async(result, host, creds, rest, dest_folder='', cb=(lambda *_: None), cb_args=()):
"""
Downloads file by using threads (So the GUI will not be bocked while downloading).
**Parameters**
:result: resultset, from which will be downloaded
:host: address of host
:creds: credentials (including user-name and -password)
:rest: REST-API definition
:dest_folder: folder where the downloaded files will be saved
:cb: function for moving spinners
:cb_args: args for function cb
"""
from threading import Thread
download_thread = Thread(target=download, args=(result, host, creds, rest, dest_folder, cb, cb_args))
download_thread.start()
return download_thread
def download(result, host, creds, rest, dest_folder='', cb=(lambda *_: None), cb_args=()):
"""
Downloads a file.
**Parameters**
:result: resultset, from which will be downloaded
:host: address of host
:creds: credentials (including user-name and -password)
:rest: REST-API definition
:dest_folder: folder where the downloaded files will be saved
:cb: function for moving spinners
:cb_args: args for function cb
"""
requests_lock.acquire()
import re
......@@ -35,6 +71,15 @@ def download(result, host, creds, rest, dest_folder='', cb=(lambda *_: None), cb
cb(*cb_args)
def download_file(url, creds, path):
"""
Downloads a file.
**Parameters**
:url: host/REST-API with filled values
:creds: credentials (including user-name and -password)
:path: folder where the downloaded files will be saved
"""
import requests
from base64 import b64encode
from requests.auth import HTTPBasicAuth
......@@ -60,6 +105,17 @@ def download_file(url, creds, path):
def search_for(host, root_element, constraints, search_fields, user, passw):
"""
Does a search for given values. raises xsa -Exceptions
**Parameters**
:host: host-address
:root_element: root-element of search
:constraints: constraints of query
:search_fields: fields which will be returned from server
:user: user-name
:passw:user-password
"""
import xsa.errors as xsa_errors
from pyxnat.core import errors
......
......@@ -36,6 +36,7 @@ class ChartView(gtk.HPaned):
if not sub[0] == self.TreeViewChartValues.inital_value:
substrings.append(sub[0])
data = chart.count_substrings(substrings, strings)
#save chart as tmp-file without a name
with tempfile.NamedTemporaryFile(delete=True) as file:
chart.create(file,data,(allocation.width,allocation.height),len(self.results), len(self.TreeViewChartValues.store)-1)
file.flush()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment