Skip to content

Commit e1458ea

Browse files
author
Kevin D Smith
committed
Add path_to_caslib method
1 parent f305291 commit e1458ea

File tree

1 file changed

+93
-2
lines changed

1 file changed

+93
-2
lines changed

swat/cas/connection.py

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import copy
2929
import json
3030
import os
31+
import random
3132
import re
3233
import weakref
3334
import six
@@ -389,7 +390,7 @@ def _id_generator():
389390
num = num + 1
390391
self._id_generator = _id_generator()
391392

392-
self.server_version, self.server_features = self._get_server_features()
393+
self.server_type, self.server_version, self.server_features = self._get_server_features()
393394

394395
def _gen_id(self):
395396
''' Generate an ID unique to the session '''
@@ -410,11 +411,12 @@ def _get_server_features(self):
410411
info = self.retrieve('builtins.serverstatus', _messagelevel='error',
411412
_apptag='UI')
412413
version = tuple([int(x) for x in info['About']['Version'].split('.')][:2])
414+
stype = info['About']['System']['OS Name'].lower()
413415

414416
# if version >= (3, 4):
415417
# out.add('csv-ints')
416418

417-
return version, out
419+
return stype, version, out
418420

419421
def _detect_protocol(self, hostname, port, protocol=None):
420422
'''
@@ -3085,6 +3087,95 @@ def read_stata(self, filepath_or_buffer, casout=None, **kwargs):
30853087
'''
30863088
return self._read_any('read_stata', filepath_or_buffer, casout=casout, **kwargs)
30873089

3090+
def path_to_caslib(self, path, name=None, **kwargs):
3091+
'''
3092+
Return a caslib name for a given path
3093+
3094+
If a caslib does not exist for the current path or for a parent
3095+
path, a new caslib will be created.
3096+
3097+
Parameters
3098+
----------
3099+
path : string
3100+
The absolute path to the desired caslib directory
3101+
name : string, optional
3102+
The name to give to the caslib, if a new one is created
3103+
kwargs : keyword-parameter, optional
3104+
Additional parameters to use when creating a new caslib
3105+
3106+
Returns
3107+
-------
3108+
( caslib-name, relative-path )
3109+
The return value is a two-element tuple. The first element
3110+
is the name of the caslib. The second element is the relative
3111+
path to the requested directory from the caslib. The second
3112+
element will be blank if the given path matches a caslib,
3113+
or a new caslib is created.
3114+
3115+
'''
3116+
if not name:
3117+
name = 'Caslib_%x' % random.randint(0,1e9)
3118+
3119+
activeonadd_key = None
3120+
subdirectories_key = None
3121+
datasource_key = None
3122+
3123+
for key, value in kwargs.items():
3124+
if key.lower() == 'activeonadd':
3125+
activeonadd_key = key
3126+
elif key.lower() == 'subdirectories':
3127+
subdirectories_key = key
3128+
elif key.lower() == 'datasource':
3129+
datasource_key = key
3130+
3131+
if not activeonadd_key:
3132+
activeonadd_key = 'activeonadd'
3133+
kwargs[activeonadd_key] = False
3134+
if not subdirectories_key:
3135+
subdirectories_key = 'subdirectories'
3136+
kwargs[subdirectories_key] = True
3137+
if not datasource_key:
3138+
datasource_key = 'datasource'
3139+
kwargs[datasource_key] = dict(srctype='path')
3140+
3141+
is_windows = self.server_type.startswith('win')
3142+
3143+
if is_windows:
3144+
sep = '\\'
3145+
normpath = path.lower()
3146+
else:
3147+
sep = '/'
3148+
normpath = path
3149+
3150+
if normpath.endswith(sep):
3151+
normpath = normpath[:-1]
3152+
3153+
info = self.retrieve('table.caslibinfo',
3154+
_messagelevel='error')['CASLibInfo']
3155+
3156+
for libname, item, subdirs in zip(info['Name'], info['Path'],
3157+
info['Subdirs']):
3158+
if item.endswith(sep):
3159+
item = item[:-1]
3160+
if is_windows:
3161+
item = item.lower()
3162+
if item == normpath:
3163+
if bool(subdirs) != bool(kwargs[subdirectories_key]):
3164+
raise SWATError('caslib exists, but subdirectories flag differs')
3165+
return libname, ''
3166+
elif normpath.startswith(item):
3167+
if bool(subdirs) != bool(kwargs[subdirectories_key]):
3168+
raise SWATError('caslib exists, but subdirectories flag differs')
3169+
return libname, path[len(item)+1:]
3170+
3171+
out = self.retrieve('table.addcaslib', _messagelevel='error',
3172+
name=name, path=path, **kwargs)
3173+
3174+
if out.severity > 1:
3175+
raise SWATError(out.status)
3176+
3177+
return name, ''
3178+
30883179

30893180
def getone(connection, datamsghandler=None):
30903181
'''

0 commit comments

Comments
 (0)