Beispiel #1
0
def scisheets(request, ncol, nrow):
    """
  Creates a new table with the specified number of columns and rows
  considering the number of rows with strings. 
  Renders a hierarchical table if ncol < 0.
  """
    ncol = int(ncol)
    nrow = int(nrow)
    ncolstr = int(ncol / 2)
    if ncol < 0:  # Test for hierarchical table
        num_nodes = 2 * abs(ncol)
        prob_child = 0.5
        table = DTTable.createRandomHierarchicalTable("HDemo",
                                                      nrow,
                                                      num_nodes,
                                                      prob_child,
                                                      prob_detach=0.5,
                                                      ncolstr=ncolstr,
                                                      table_cls=DTTable)
    else:
        table = DTTable.createRandomTable("Demo", nrow, ncol, ncolstr=ncolstr)
    _setTableFilepath(request,
                      table,
                      settings.SCISHEETS_DEFAULT_TABLEFILE,
                      verify=False)
    html = table.render()
    saveTable(request, table)
    return HttpResponse(html)
def createTable(name, column_name=None, is_subtable=False):
  """
  :param str name: str table name
  :param str or list column_name: column(s) to create
  :param bool is_subtable: true if table is a subtable
  :return: Table object
  """
  if column_name is None:
    colnms = []
  elif isinstance(column_name, list):
    colnms = column_name
  else:
    colnms = [column_name]
  table = DTTable(name)
  factor = 1
  for colnm in colnms:
    column = cl.Column(colnm)
    values = [factor*n for n in range(5)]
    factor += 1
    column.addCells(values, replace=True)
    table.addColumn(column)
  if not is_subtable:
    versioned_file = VersionedFile(TABLE_FILEPATH, TEST_DIR, MAX_VERSIONS)
    table.setVersionedFile(versioned_file)
    api_util.writeObjectToFile(table, TABLE_FILEPATH)
  return table
class TableFileHelper(object):
  """
  Creates and removes a dummy Table File
  """

  @classmethod
  def doesFilepathExist(cls, filepath):
    """
    Checks if the table file exists
    :param str filepath: filepath to check
    :return: boolean
    """
    return os.path.exists(filepath)

  @classmethod
  def doesTableFileExist(cls, table_filename, filedir):
    """
    Checks if the table file exists
    :param table_filename: table file name without extension
    :param table_filedir: directory for the table file
    :return: boolean
    """
    full_filename = "%s.%s" % (table_filename, settings.SCISHEETS_EXT)
    full_path = os.path.join(filedir, full_filename)
    return TableFileHelper.doesFilepathExist(full_path)

  def __init__(self, table_filename, table_filedir, table_name=None):
    """
    :param table_filename: name of the dummy table file
    :param table_filedir: directory for the table file
    """
    self.table = None
    self._table_filename = table_filename
    self._table_filedir = table_filedir
    self._full_path = os.path.join(table_filedir,
        "%s.%s" % (self._table_filename, settings.SCISHEETS_EXT))
    self._table_name = table_name
    if self._table_name is None:
      self._table_name = table_filename

  def create(self):
    """
    Creates Table file and the table
    """
    if os.path.exists(self._full_path):
      self.table = api_util.readObjectFromFile(self._full_path)
      self.table.setFilepath(self._full_path)  # Set before write
    else:
      self.table = DTTable(self._table_name)
      self.table.setFilepath(self._full_path)  # Set before write
      api_util.writeObjectToFile(self.table) 

  def destroy(self):
    """
    Destroys a Table file
    """
    if TableFileHelper.doesTableFileExist(self._table_filename,
        self._table_filedir):
      os.remove(self._full_path)
Beispiel #4
0
class TableFileHelper(object):
    """
  Creates and removes a dummy Table File
  """
    @classmethod
    def doesFilepathExist(cls, filepath):
        """
    Checks if the table file exists
    :param str filepath: filepath to check
    :return: boolean
    """
        return os.path.exists(filepath)

    @classmethod
    def doesTableFileExist(cls, table_filename, filedir):
        """
    Checks if the table file exists
    :param table_filename: table file name without extension
    :param table_filedir: directory for the table file
    :return: boolean
    """
        full_filename = "%s.%s" % (table_filename, settings.SCISHEETS_EXT)
        full_path = os.path.join(filedir, full_filename)
        return TableFileHelper.doesFilepathExist(full_path)

    def __init__(self, table_filename, table_filedir, table_name=None):
        """
    :param table_filename: name of the dummy table file
    :param table_filedir: directory for the table file
    """
        self.table = None
        self._table_filename = table_filename
        self._table_filedir = table_filedir
        self._full_path = os.path.join(
            table_filedir,
            "%s.%s" % (self._table_filename, settings.SCISHEETS_EXT))
        self._table_name = table_name
        if self._table_name is None:
            self._table_name = table_filename

    def create(self):
        """
    Creates Table file and the table
    """
        if os.path.exists(self._full_path):
            self.table = api_util.readObjectFromFile(self._full_path)
            self.table.setFilepath(self._full_path)  # Set before write
        else:
            self.table = DTTable(self._table_name)
            self.table.setFilepath(self._full_path)  # Set before write
            api_util.writeObjectToFile(self.table)

    def destroy(self):
        """
    Destroys a Table file
    """
        if TableFileHelper.doesTableFileExist(self._table_filename,
                                              self._table_filedir):
            os.remove(self._full_path)
Beispiel #5
0
 def create(self):
     """
 Creates Table file and the table
 """
     if os.path.exists(self._full_path):
         self.table = api_util.readObjectFromFile(self._full_path)
         self.table.setFilepath(self._full_path)  # Set before write
     else:
         self.table = DTTable(self._table_name)
         self.table.setFilepath(self._full_path)  # Set before write
         api_util.writeObjectToFile(self.table)
 def create(self):
   """
   Creates Table file and the table
   """
   if os.path.exists(self._full_path):
     self.table = api_util.readObjectFromFile(self._full_path)
     self.table.setFilepath(self._full_path)  # Set before write
   else:
     self.table = DTTable(self._table_name)
     self.table.setFilepath(self._full_path)  # Set before write
     api_util.writeObjectToFile(self.table) 
Beispiel #7
0
def createTable(name, column_name=None, is_subtable=False):
    """
  :param str name: str table name
  :param str or list column_name: column(s) to create
  :param bool is_subtable: true if table is a subtable
  :return: Table object
  """
    if column_name is None:
        colnms = []
    elif isinstance(column_name, list):
        colnms = column_name
    else:
        colnms = [column_name]
    table = DTTable(name)
    factor = 1
    for colnm in colnms:
        column = cl.Column(colnm)
        values = [factor * n for n in range(5)]
        factor += 1
        column.addCells(values, replace=True)
        table.addColumn(column)
    if not is_subtable:
        versioned_file = VersionedFile(TABLE_FILEPATH, TEST_DIR, MAX_VERSIONS)
        table.setVersionedFile(versioned_file)
        api_util.writeObjectToFile(table, TABLE_FILEPATH)
    return table
Beispiel #8
0
 def __init__(self, request):
     if request is None:
         return
     self['command'] = _extractDataFromRequest(request, 'command')
     self['target'] = _extractDataFromRequest(request, 'target')
     self['table_name'] = _extractDataFromRequest(request, 'table')
     self['args'] = _extractDataFromRequest(request, 'args[]', listvar=True)
     if self['args'] is not None:
         if not self['command'] in ['Prologue', 'Epilogue', 'Formula']:
             python_name = DTTable.fromHTMLToPythonName(self['args'][0])
             self['args'][0] = python_name
     html_name = _extractDataFromRequest(request, 'columnName')
     python_name = DTTable.fromHTMLToPythonName(html_name)
     self['column_name'] = DTTable.fromHTMLToPythonName(python_name)
     row_name = _extractDataFromRequest(request, 'row')
     if row_name is not None and len(str(row_name)) > 0:
         self['row_index'] = DTTable.rowIndexFromName(row_name)
     else:
         self['row_index'] = None  # Handles case where "row" is absent
     self['value'] = _extractDataFromRequest(request, 'value', convert=True)
     if self['row_index'] == -1:
         raise InternalError("Invalid row_index: %d" % self['row_index'])
def createCommandDict(request):
  """
  Creates a dictionary from the fields in the request
  that constitute the JSON structure sent in the command
  from AJAX.
  Input: request - HTML request object
  Output: cmd_dict - dictionary of the command
   TARGET  COMMAND   DESCRIPTION
    Table   Delete   Delete the table file and switch to the
                     using a random file
    Table   Epilogue Update the epilogue code for the table
    Table   Export   Export the table into python
    Table   ListTableFiles Returns a list of the table files
    Table   New      Opens a new blank table
    Table   OpenTableFile Change the current Table file to
                     what is specified in the args list
    Table   Prologue Update the prologue code for the table
    Table   Rename   Change the table name. Must be a valid python name
    Table   SaveAs   Save the table to the specified table file
    Table   Trim     Remove None rows from the end of the table
    Cell    Update   Update the specified cell
    Column  Append   Add a new column to the right of the current
    Column  Insert   Add a new column to the left of the current
    Column  Delete   Delete the column
    Column  Formula  Change the column's formula
    Column  Move     Move the column to another position
                       The name LAST is used for last column
    Column  Refactor Rename the column and change formulas to use
                     the new name
    Column  Rename   Rename the column
    Row     Append   Add a new row after the current row
    Row     Insert   Add a new row before the current row
    Row     Move     Move the row to the specified position
  """
  cmd_dict = {}
  cmd_dict['command'] = extractDataFromRequest(request, 'command')
  cmd_dict['target'] = extractDataFromRequest(request, 'target')
  cmd_dict['table_name'] = extractDataFromRequest(request, 'table')
  cmd_dict['args'] = extractDataFromRequest(request, 'args[]', listvar=True)
  cmd_dict['column_index'] = extractDataFromRequest(request,
      'column', convert=True)
  row_name = extractDataFromRequest(request, 'row')
  if row_name is not None and len(str(row_name)) > 0:
    cmd_dict['row_index'] = DTTable.rowIndexFromName(row_name)
  else:
    cmd_dict['row_index'] = None  # Handles case where "row" is absent
  cmd_dict['value'] = extractDataFromRequest(request, 'value',
      convert=True)
  if cmd_dict['row_index'] == -1:
    raise InternalError("Invalid row_index: %d" % cmd_dict['row_index'])
  return cmd_dict
def scisheets(request, ncol, nrow):
  """
  Creates a new table with the specified number of columns and rows
  considering the number of rows with strings
  """
  ncol = int(ncol)
  nrow = int(nrow)
  ncolstr = int(ncol/2)
  table = DTTable.createRandomTable("Demo", nrow, ncol,
      ncolstr=ncolstr)
  _setTableFilepath(request, table, 
      settings.SCISHEETS_DEFAULT_TABLEFILE,
      verify=False)
  html = table.render()
  saveTable(request, table)
  return HttpResponse(html)