/usr/lib/python2.7/dist-packages/sardana/sardanameta.py is in python-sardana 1.6.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 | #!/usr/bin/env python
##############################################################################
##
## This file is part of Sardana
##
## http://www.sardana-controls.org/
##
## Copyright 2011 CELLS / ALBA Synchrotron, Bellaterra, Spain
##
## Sardana is free software: you can redistribute it and/or modify
## it under the terms of the GNU Lesser General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## Sardana is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU Lesser General Public License for more details.
##
## You should have received a copy of the GNU Lesser General Public License
## along with Sardana. If not, see <http://www.gnu.org/licenses/>.
##
##############################################################################
"""This module is part of the Python Sardana libray. It defines the base
classes for MetaLibrary and MetaClass"""
from __future__ import absolute_import
__all__ = ["SardanaLibrary", "SardanaClass", "SardanaFunction"]
__docformat__ = 'restructuredtext'
import os
import inspect
import string
import weakref
import linecache
import traceback
from sardana.sardanabase import SardanaBaseObject
# ----------------------------------------------------------------------------
# Start patch around inspect issue http://bugs.python.org/issue993580
def findsource(obj):
"""Return the entire source file and starting line number for an object.
The argument may be a module, class, method, function, traceback, frame,
or code object. The source code is returned as a list of all the lines
in the file and the line number indexes a line in that list. An IOError
is raised if the source code cannot be retrieved."""
filename = inspect.getsourcefile(obj)
if filename:
linecache.checkcache(filename)
return inspect.findsource(obj)
def getsourcelines(object):
"""Return a list of source lines and starting line number for an object.
The argument may be a module, class, method, function, traceback, frame,
or code object. The source code is returned as a list of the lines
corresponding to the object and the line number indicates where in the
original source file the first line of code was found. An IOError is
raised if the source code cannot be retrieved."""
lines, lnum = findsource(object)
if inspect.ismodule(object): return lines, 0
else: return inspect.getblock(lines[lnum:]), lnum + 1
def getsource(object):
"""Return the text of the source code for an object.
The argument may be a module, class, method, function, traceback, frame,
or code object. The source code is returned as a single string. An
IOError is raised if the source code cannot be retrieved."""
lines, lnum = getsourcelines(object)
return string.join(lines, '')
# End patch around inspect issue http://bugs.python.org/issue993580
# ----------------------------------------------------------------------------
class SardanaLibrary(SardanaBaseObject):
"""Object representing a python module containing sardana classes.
Public members:
- module - reference to python module
- file_path - complete (absolute) path (with file name at the end)
- file_name - file name (including file extension)
- path - complete (absolute) path
- name - (=module name) module name (without file extension)
- meta_classes - dict<str, SardanMetaClass>
- exc_info - exception information if an error occurred when loading
the module"""
description = '<Undocumented>'
def __init__(self, **kwargs):
self.module = module = kwargs.pop('module', None)
self.file_path = file_path = kwargs.pop('file_path', None)
self.exc_info = kwargs.pop('exc_info', None)
if module is not None:
file_path = os.path.abspath(module.__file__)
self.file_path = file_path
if file_path is None:
self.path = kwargs.get('path', None)
self.file_name = kwargs.get('file_name', None)
name = kwargs.get('name', None)
else:
if self.file_path.endswith(".pyc"):
self.file_path = self.file_path[:-1]
self.path, self.file_name = os.path.split(self.file_path)
name, _ = os.path.splitext(self.file_name)
self.meta_classes = {}
self.meta_functions = {}
if module is not None and module.__doc__:
self.description = module.__doc__
self._code = getsourcelines(module)[0]
else:
self.description = name + " in error!"
self._code = None
kwargs['name'] = name
kwargs['full_name'] = file_path or name
SardanaBaseObject.__init__(self, **kwargs)
def __cmp__(self, o):
return cmp(self.full_name, o.full_name)
def __str__(self):
return self.name
@property
def module_name(self):
"""Returns the module name for this library.
:return: the module name
:rtype: str"""
return self.name
@property
def code(self):
"""Returns a sequence of sourcelines corresponding to the module code.
:return: list of source code lines
:rtype: list<str>"""
code = self._code
if code is None:
raise IOError('source code not available')
return code
def add_meta_class(self, meta_class):
"""Adds a new :class:~`sardana.sardanameta.SardanaClass` to this
library.
:param meta_class: the meta class to be added to this library
:type meta_class: :class:~`sardana.sardanameta.SardanaClass`"""
self.meta_classes[meta_class.name] = meta_class
def get_meta_class(self, meta_class_name):
"""Returns a :class:~`sardana.sardanameta.SardanaClass` for the
given meta class name or None if the meta class does not exist in this
library.
:param meta_class_name: the meta class name
:type meta_class_name: str
:return: a meta class or None
:rtype: :class:~`sardana.sardanameta.SardanaClass`"""
return self.meta_classes.get(meta_class_name)
def get_meta_classes(self):
"""Returns a sequence of the meta classes that belong to this library.
:return: a sequence of meta classes that belong to this library
:rtype: seq<:class:~`sardana.sardanameta.SardanaClass`>"""
return self.meta_classes.values()
def has_meta_class(self, meta_class_name):
"""Returns True if the given meta class name belongs to this library
or False otherwise.
:param meta_class_name: the meta class name
:type meta_class_name: str
:return: True if the given meta class name belongs to this library
or False otherwise
:rtype: bool"""
return meta_class_name in self.meta_classes
def add_meta_function(self, meta_function):
"""Adds a new :class:~`sardana.sardanameta.SardanaFunction` to this
library.
:param meta_function: the meta function to be added to this library
:type meta_function: :class:~`sardana.sardanameta.SardanaFunction`"""
self.meta_functions[meta_function.name] = meta_function
def get_meta_function(self, meta_function_name):
"""Returns a :class:~`sardana.sardanameta.SardanaFunction` for the
given meta function name or None if the meta function does not exist in
this library.
:param meta_function_name: the meta function name
:type meta_function_name: str
:return: a meta function or None
:rtype: :class:~`sardana.sardanameta.SardanaFunction`"""
return self.meta_functions.get(meta_function_name)
def get_meta_functions(self):
"""Returns a sequence of the meta functions that belong to this library.
:return: a sequence of meta functions that belong to this library
:rtype: seq<:class:~`sardana.sardanameta.SardanaFunction`>"""
return self.meta_functions.values()
def has_meta_function(self, meta_function_name):
"""Returns True if the given meta function name belongs to this library
or False otherwise.
:param meta_function_name: the meta function name
:type meta_function_name: str
:return: True if the given meta function name belongs to this library
or False otherwise
:rtype: bool"""
return meta_function_name in self.meta_functions
def get_meta(self, meta_name):
"""Returns a :class:~`sardana.sardanameta.SardanaCode` for the
given meta name or None if the meta does not exist in this library.
:param meta_name: the meta name (class, function)
:type meta_name: str
:return: a meta or None
:rtype: :class:~`sardana.sardanameta.SardanaCode`"""
ret = self.get_meta_class(meta_name)
if ret is None:
ret = self.get_meta_function(meta_name)
return ret
def has_meta(self, meta_name):
"""Returns True if the given meta name belongs to this library
or False otherwise.
:param meta_name: the meta name
:type meta_name: str
:return:
True if the given meta (class or function) name belongs to this
library or False otherwise
:rtype: bool"""
return self.has_meta_class(meta_name) or \
self.has_meta_function(meta_name)
def has_metas(self):
"""Returns True if any meta object exists in the library
or False otherwise.
:return:
True if any meta object (class or function) exists
in the library or False otherwise
:rtype: bool
"""
has_metas_bool = False
if (len(self.get_meta_classes()) > 0 or
len(self.get_meta_functions()) > 0):
has_metas_bool = True
return has_metas_bool
def get_metas(self):
"""Returns a sequence of the meta (class and functions) that belong to
this library.
:return:
a sequence of meta (class and functions) that belong to this library
:rtype: seq<:class:~`sardana.sardanameta.SardanaCode`>"""
return self.get_meta_classes() + self.get_meta_functions()
def get_name(self):
"""Returns the module name for this library (same as
:meth:~sardana.sardanameta.SardanaLibrary.get_module_name).
:return: the module name
:rtype: str"""
return self.name
def get_module_name(self):
"""Returns the module name for this library (same as
:meth:~sardana.sardanameta.SardanaLibrary.get_name).
:return: the module name
:rtype: str"""
return self.module_name
def get_module(self):
"""Returns the python module for this library.
:return: the python module
:rtype: object"""
return self.module
def get_description(self):
"""Returns the this library documentation or "<Undocumented>" if no
documentation exists.
:return: this library documentation or None
:rtype: str"""
return self.description
def get_code(self):
"""Returns a sequence of sourcelines corresponding to the module code.
:return: list of source code lines
:rtype: list<str>"""
return self.code
def get_file_path(self):
"""Returns the file path for this library. On posix systems is something
like: /abs/path/filename.py
:return: this library file path
:rtype: str"""
if self.file_path is None:
return None
if self.file_path.endswith('.pyc'):
return self.file_path[:-1]
return self.file_path
def get_file_name(self):
"""Returns the file name for this library. On posix systems is something
like: filename.py
:return: this library file name
:rtype: str"""
return self.file_name
def has_errors(self):
"""Returns True if this library has syntax errors or False otherwise.
:return: True if this library has syntax errors or False otherwise
:rtype: bool"""
return self.exc_info != None
def set_error(self, exc_info):
"""Sets the error information for this library
:param exc_info: error information. It must be an object similar to the
one returned by :func:`sys.exc_info`
:type exc_info: tuple<type, value, traceback>"""
self.exc_info = exc_info
if exc_info is None:
self.meta_classes = {}
self.meta_functions = {}
def get_error(self):
"""Gets the error information for this library or None if no error
exists
:return: error information. An object similar to the one returned by
:func:`sys.exc_info`
:rtype: tuple<type, value, traceback>"""
return self.exc_info
def serialize(self, *args, **kwargs):
"""Returns a serializable object describing this object.
:return: a serializable dict
:rtype: dict"""
kwargs = SardanaBaseObject.serialize(self, *args, **kwargs)
kwargs['id'] = 0
kwargs['module'] = self.name
kwargs['file_path'] = self.file_path
kwargs['file_name'] = self.file_name
kwargs['path'] = self.path
kwargs['description'] = self.description
kwargs['elements'] = self.meta_classes.keys() + self.meta_functions.keys()
if self.exc_info is None:
kwargs['exc_summary'] = None
kwargs['exc_info'] = None
else:
kwargs['exc_summary'] = "".join(traceback.format_exception_only(*self.exc_info[:2]))
kwargs['exc_info'] = "".join(traceback.format_exception(*self.exc_info))
return kwargs
class SardanaCode(SardanaBaseObject):
"""Object representing a python code (base for class and function)."""
description = '<Undocumented>'
def __init__(self, **kwargs):
lib = kwargs.pop('lib')
self._lib = weakref.ref(lib)
self._code_obj = kwargs.pop('code')
doc = self._code_obj.__doc__
if doc:
self.description = doc
self._code = getsourcelines(self._code_obj)
name = kwargs['name']
kwargs['full_name'] = "{0}.{1}".format(lib.name, name)
kwargs['parent'] = kwargs.pop('parent', self.lib)
SardanaBaseObject.__init__(self, **kwargs)
@property
def code_object(self):
return self._code_obj
@property
def lib(self):
"""Returns the library :class:~`sardana.sardanameta.SardanaLibrary`
for this class.
:return: a reference to the library where this class is located
:rtype: :class:~`sardana.sardanameta.SardanaLibrary`"""
return self._lib()
@property
def module(self):
"""Returns the python module for this class.
:return: the python module
:rtype: object"""
return self.lib.module
@property
def module_name(self):
"""Returns the module name for this class.
:return: the module name
:rtype: str"""
return self.lib.get_module_name()
@property
def file_path(self):
"""Returns the file path for for the library where this class is. On
posix systems is something like: /abs/path/filename.py
:return: the file path for for the library where this class is
:rtype: str"""
return self.lib.file_path
@property
def file_name(self):
"""Returns the file name for the library where this class is. On posix
systems is something like: filename.py
:return: the file name for the library where this class is
:rtype: str"""
return self.lib.file_name
@property
def path(self):
"""Returns the absolute path for the library where this class is. On
posix systems is something like: /abs/path
:return: the absolute path for the library where this class is
:rtype: str"""
return self.lib.path
@property
def code(self):
"""Returns a tuple (sourcelines, firstline) corresponding to the
definition of this code object. sourcelines is a list of source code
lines. firstline is the line number of the first source code line."""
code = self._code
if code is None:
raise IOError('source code not available')
return code
def get_code(self):
"""Returns a tuple (sourcelines, firstline) corresponding to the
definition of the controller class. sourcelines is a list of source code
lines. firstline is the line number of the first source code line."""
return self.code
def serialize(self, *args, **kwargs):
"""Returns a serializable object describing this object.
:return: a serializable dict
:rtype: dict"""
kwargs = SardanaBaseObject.serialize(self, *args, **kwargs)
kwargs['id'] = 0
kwargs['module'] = self.module_name
kwargs['file_name'] = self.file_name
kwargs['file_path'] = self.file_path
kwargs['path'] = self.path
kwargs['description'] = self.description
return kwargs
def get_brief_description(self, max_chars=60):
desc = self.description.replace('\n', ' ')
if len(desc) > (max_chars - 5):
desc = desc[:max_chars - 5] + '[...]'
return desc
class SardanaClass(SardanaCode):
"""Object representing a python class."""
def __init__(self, **kwargs):
klass = kwargs.pop('klass')
kwargs['code'] = klass
kwargs['name'] = kwargs.pop('name', klass.__name__)
SardanaCode.__init__(self, **kwargs)
@property
def klass(self):
return self.code_object
class SardanaFunction(SardanaCode):
"""Object representing a python function."""
def __init__(self, **kwargs):
function = kwargs.pop('function')
kwargs['code'] = function
kwargs['name'] = kwargs.pop('name', function.func_name)
SardanaCode.__init__(self, **kwargs)
@property
def function(self):
return self.code_object
|