32
32
from astropy .table .table import Table
33
33
import tempfile
34
34
35
-
36
35
__all__ = ['Tap' , 'TapPlus' ]
37
36
38
37
VERSION = "20200428.1"
@@ -571,7 +570,7 @@ def __launchJobMultipart(self, query, uploadResource, uploadTableName,
571
570
"FORMAT" : str (outputFormat ),
572
571
"tapclient" : str (TAP_CLIENT_ID ),
573
572
"QUERY" : str (query ),
574
- "UPLOAD" : "" + str (uploadValue )}
573
+ "UPLOAD" : "" + str (uploadValue )}
575
574
if autorun is True :
576
575
args ['PHASE' ] = 'RUN'
577
576
if name is not None :
@@ -657,7 +656,7 @@ def __parseUrl(self, url, verbose=False):
657
656
if urlInfoPos < 0 :
658
657
raise ValueError ("Invalid URL format" )
659
658
660
- urlInfo = url [(urlInfoPos + 3 ):]
659
+ urlInfo = url [(urlInfoPos + 3 ):]
661
660
662
661
items = urlInfo .split ("/" )
663
662
@@ -672,7 +671,7 @@ def __parseUrl(self, url, verbose=False):
672
671
if portPos > 0 :
673
672
# port found
674
673
host = hostPort [0 :portPos ]
675
- port = int (hostPort [portPos + 1 :])
674
+ port = int (hostPort [portPos + 1 :])
676
675
else :
677
676
# no port found
678
677
host = hostPort
@@ -693,10 +692,10 @@ def __parseUrl(self, url, verbose=False):
693
692
tapContext = f"/{ items [2 ]} "
694
693
else :
695
694
data = []
696
- for i in range (1 , itemsSize - 1 ):
695
+ for i in range (1 , itemsSize - 1 ):
697
696
data .append (f"/{ items [i ]} " )
698
697
serverContext = utils .util_create_string_from_buffer (data )
699
- tapContext = f"/{ items [itemsSize - 1 ]} "
698
+ tapContext = f"/{ items [itemsSize - 1 ]} "
700
699
if verbose :
701
700
print (f"protocol: '{ protocol } '" )
702
701
print (f"host: '{ host } '" )
@@ -713,6 +712,7 @@ class TapPlus(Tap):
713
712
"""TAP plus class
714
713
Provides TAP and TAP+ capabilities
715
714
"""
715
+
716
716
def __init__ (self , url = None ,
717
717
host = None ,
718
718
server_context = None ,
@@ -1532,7 +1532,7 @@ def __uploadTableMultipart(self, resource, table_name=None,
1532
1532
chunk = f .read ()
1533
1533
files = [['FILE' , os .path .basename (resource ), chunk ]]
1534
1534
contentType , body = connHandler .encode_multipart (args , files )
1535
- else : # upload from URL
1535
+ else : # upload from URL
1536
1536
args = {
1537
1537
"TASKID" : str (- 1 ),
1538
1538
"TABLE_NAME" : str (table_name ),
@@ -1628,14 +1628,14 @@ def delete_user_table(self, table_name=None, force_removal=False,
1628
1628
raise ValueError ("Table name cannot be null" )
1629
1629
if force_removal is True :
1630
1630
args = {
1631
- "TABLE_NAME" : str (table_name ),
1632
- "DELETE" : "TRUE" ,
1633
- "FORCE_REMOVAL" : "TRUE" }
1631
+ "TABLE_NAME" : str (table_name ),
1632
+ "DELETE" : "TRUE" ,
1633
+ "FORCE_REMOVAL" : "TRUE" }
1634
1634
else :
1635
1635
args = {
1636
- "TABLE_NAME" : str (table_name ),
1637
- "DELETE" : "TRUE" ,
1638
- "FORCE_REMOVAL" : "FALSE" }
1636
+ "TABLE_NAME" : str (table_name ),
1637
+ "DELETE" : "TRUE" ,
1638
+ "FORCE_REMOVAL" : "FALSE" }
1639
1639
connHandler = self .__getconnhandler ()
1640
1640
data = connHandler .url_encode (args )
1641
1641
response = connHandler .execute_upload (data , verbose = verbose )
@@ -1648,6 +1648,145 @@ def delete_user_table(self, table_name=None, force_removal=False,
1648
1648
msg = f"Table '{ table_name } ' deleted."
1649
1649
print (msg )
1650
1650
1651
+ def rename_table (self , table_name = None , new_table_name = None , new_column_names_dict = {},
1652
+ verbose = False ):
1653
+ """
1654
+ This new method allows to update the column names of a user table.
1655
+ header example: rename_table(table_name=old_table_name, new_table_name=new_table_name -optional-
1656
+ , new_column_names_dict=[old_column1:new_column1, old_column2:new_colum2...])
1657
+ Parameters
1658
+ ----------
1659
+ table_name: str, required
1660
+ old name of the user's table
1661
+ new_table_name: str, required
1662
+ new name of the user's table
1663
+ new_column_names_dict: dict str:str, required
1664
+ dict with pairs "old_column1_name:new_column1_name"
1665
+ verbose : bool, optional, default 'False'
1666
+ flag to display information about the process
1667
+ """
1668
+ args = {}
1669
+
1670
+ if table_name is None :
1671
+ raise ValueError ("Table name cannot be null" )
1672
+ if (new_table_name is None or new_table_name == '' ) and \
1673
+ (new_column_names_dict is None or not new_column_names_dict ):
1674
+ raise ValueError ("Please introduce as minimum a new table tame or a new name for a column with format "
1675
+ "old_column1_name:new_column1_name, ... ,old_columnN_name:new_columnN_name" )
1676
+
1677
+ # Now we will check that the table exist
1678
+ table = self .load_table (table = table_name , verbose = verbose )
1679
+
1680
+ # Check now if the table exist and contains values
1681
+ if table is None :
1682
+ raise ValueError ("Table name not found" )
1683
+ columns = table .columns
1684
+ if len (columns ) == 0 :
1685
+ raise ValueError ("Table has no columns" )
1686
+
1687
+ if new_table_name is not None or new_table_name != '' :
1688
+ if new_column_names_dict is None or not new_column_names_dict :
1689
+ # case 1: We only need to rename the table
1690
+ args = self .get_args_4_rename_table_only_table_name (table_name , new_table_name )
1691
+ else :
1692
+ # case 2: We need to rename both, columns and column name
1693
+ args = self .get_args_4_rename_table_all (table_name , new_table_name , new_column_names_dict )
1694
+ # __end_if
1695
+ # __end_if
1696
+
1697
+ if new_table_name is None or new_table_name == '' :
1698
+ if new_column_names_dict is not None or new_column_names_dict :
1699
+ # case 3: We only need to rename the columns but same column name
1700
+ args = self .get_args_4_rename_table_only_columns (table_name , new_column_names_dict )
1701
+
1702
+ connHandler = self .__getconnhandler ()
1703
+ data = connHandler .url_encode (args )
1704
+ response = connHandler .execute_table_tool (data , verbose = verbose )
1705
+ if verbose :
1706
+ print (response .status , response .reason )
1707
+ print (response .getheaders ())
1708
+ connHandler .check_launch_response_status (response ,
1709
+ verbose ,
1710
+ 200 )
1711
+ msg = f"Table '{ table_name } ' updated."
1712
+ print (msg )
1713
+
1714
+ # __end_of_rename_table
1715
+
1716
+ def get_args_4_rename_table_only_table_name (self , table_name , new_table_name ):
1717
+
1718
+ args = {
1719
+ "action" : "rename" ,
1720
+ "new_table_name" : new_table_name ,
1721
+ "table_name" : table_name
1722
+ }
1723
+ return args
1724
+
1725
+ # __end_of_rename_table_only_table_name
1726
+
1727
+ def get_args_4_rename_table_all (self , table_name , new_table_name , new_column_names_dict ):
1728
+ count = 0
1729
+ new_column_names = ""
1730
+ # check if the changes proposed for the columns are correct.
1731
+ for key , value in new_column_names_dict .items ():
1732
+ if key == '' :
1733
+ raise ValueError (f" Old column name introduced " f"{ key } " f" cannot be empty" )
1734
+ if key == value :
1735
+ raise ValueError (f" Old column name introduced " f"{ key } " f" and new column name introduced "
1736
+ f" " f"{ key } " f" cannot be the same" )
1737
+ if value == "" :
1738
+ raise ValueError (f" New column name introduced " f"{ value } " f" cannot be empty" )
1739
+
1740
+ # Converting dict into a string with the format expected by the TAP server
1741
+ new_pair = key + ":" + value
1742
+ new_column_names = new_column_names + new_pair
1743
+ count = count + 1
1744
+ if count < len (new_column_names_dict ):
1745
+ new_column_names = new_column_names + ','
1746
+ # __end_for_loop
1747
+
1748
+ args = {
1749
+ "action" : "rename" ,
1750
+ "new_column_names" : new_column_names ,
1751
+ "new_table_name" : new_table_name ,
1752
+ "table_name" : table_name
1753
+ }
1754
+ return args
1755
+
1756
+ # __end_of_rename_table_all
1757
+
1758
+ def get_args_4_rename_table_only_columns (self , table_name , new_column_names_dict ):
1759
+ # check if the changes proposed for the columns are correct.
1760
+
1761
+ new_column_names = ""
1762
+
1763
+ count = 0
1764
+ for key , value in new_column_names_dict .items ():
1765
+ if key == '' :
1766
+ raise ValueError (f" Old column name introduced " f"{ key } " f" cannot be empty" )
1767
+ if key == value :
1768
+ raise ValueError (f" Old column name introduced " f"{ key } " f" and new column name introduced "
1769
+ f" " f"{ key } " f" cannot be the same" )
1770
+ if value == "" :
1771
+ raise ValueError (f" New column name introduced " f"{ value } " f" cannot be empty" )
1772
+
1773
+ # Converting dict into a string with the format expected by the TAP server
1774
+ new_pair = key + ":" + value
1775
+ new_column_names = new_column_names + new_pair
1776
+ count = count + 1
1777
+ if count < len (new_column_names_dict ):
1778
+ new_column_names = new_column_names + ','
1779
+ # __end_for_loop
1780
+
1781
+ args = {
1782
+ "action" : "rename" ,
1783
+ "new_column_names" : new_column_names ,
1784
+ "table_name" : table_name
1785
+ }
1786
+ return args
1787
+
1788
+ # __end_of_rename_table_only_columns
1789
+
1651
1790
def update_user_table (self , table_name = None , list_of_changes = [],
1652
1791
verbose = False ):
1653
1792
"""Updates a user table
@@ -1738,11 +1877,11 @@ def update_user_table(self, table_name=None, list_of_changes=[],
1738
1877
def get_table_update_arguments (table_name , columns , list_of_changes ):
1739
1878
num_cols = len (columns )
1740
1879
args = {
1741
- "ACTION" : "edit" ,
1742
- "NUMTABLES" : str (1 ),
1743
- "TABLE0_NUMCOLS" : str (num_cols ),
1744
- "TABLE0" : str (table_name ),
1745
- }
1880
+ "ACTION" : "edit" ,
1881
+ "NUMTABLES" : str (1 ),
1882
+ "TABLE0_NUMCOLS" : str (num_cols ),
1883
+ "TABLE0" : str (table_name ),
1884
+ }
1746
1885
index = 0
1747
1886
for column in columns :
1748
1887
found_in_changes = False
@@ -1890,11 +2029,11 @@ def set_ra_dec_columns(self, table_name=None,
1890
2029
raise ValueError ("Dec column name cannot be null" )
1891
2030
1892
2031
args = {
1893
- "ACTION" : "radec" ,
1894
- "TABLE_NAME" : str (table_name ),
1895
- "RA" : str (ra_column_name ),
1896
- "DEC" : str (dec_column_name ),
1897
- }
2032
+ "ACTION" : "radec" ,
2033
+ "TABLE_NAME" : str (table_name ),
2034
+ "RA" : str (ra_column_name ),
2035
+ "DEC" : str (dec_column_name ),
2036
+ }
1898
2037
connHandler = self .__getconnhandler ()
1899
2038
data = connHandler .url_encode (args )
1900
2039
response = connHandler .execute_table_edit (data , verbose = verbose )
0 commit comments