LeenO computo metrico con LibreOffice  3.22.0
Il software libero per la gestione di computi metrici e contabilità lavori.
DocUtils.py
Vai alla documentazione di questo file.
1 '''
2 try to overcome limits on document user defined attributes
3 '''
4 from com.sun.star.beans import PropertyAttribute
5 from com.sun.star.beans import PropertyValue
6 from com.sun.star.beans import PropertyExistException, UnknownPropertyException
7 from com.sun.star.lang import IllegalArgumentException
8 import uno
9 import LeenoUtils
10 import PersistUtils
11 
12 def setDocUserDefinedAttribute(oDoc, name, value):
13  userProps = oDoc.DocumentProperties.UserDefinedProperties
14 
15  # stringize the property
16  sValue = PersistUtils.var2string(value)
17 
18  # try to add the property, exception if exists
19  try:
20  userProps.addProperty(name, PropertyAttribute.REMOVABLE, sValue)
21  except PropertyExistException:
22  pass
23  try:
24  userProps.setPropertyValue(name, sValue)
25  except IllegalArgumentException:
26  try:
27  userProps.removeProperty(name)
28  userProps.addProperty(name, PropertyAttribute.REMOVABLE, sValue)
29  userProps.setPropertyValue(name, sValue)
30  except Exception:
31  pass
32 
34  userProps = oDoc.DocumentProperties.UserDefinedProperties
35 
36  try:
37  sValue = userProps.getPropertyValue(name)
38  return PersistUtils.string2val(sValue)
39 
40  except Exception:
41  return None
42 
44  return getDocUserDefinedAttribute(oDoc, name) != None
45 
47  userProps = oDoc.DocumentProperties.UserDefinedProperties
48 
49  # try to add the property, exception if exists
50  try:
51  userProps.removeProperty(name)
52  except UnknownPropertyException:
53  pass
54 
55 
56 def storeDataBlock(oDoc, baseName, data):
57  '''
58  baseName : nome base per il blocco dati. Viene preposto ai dati prima di salvarli
59  data : un dizionario contenente una serie di chiave:valore. Le chiavi devono essere stringhe
60  '''
61  for key, value in data.items():
62  setDocUserDefinedAttribute(oDoc, baseName + '.' + key, value)
63 
64 
65 def loadDataBlock(oDoc, baseName):
66  '''
67  baseName : prefisso per il blocco di dati richiesto
68  Vengono lette TUTTE le proprietà inizianti con baseName
69  e restituite sotto forma di dizionario { key: value... }
70  '''
71  userProps = oDoc.DocumentProperties.UserDefinedProperties
72  props = userProps.PropertySetInfo.Properties
73  res = {}
74  for prop in props:
75  if prop.Name.startswith(baseName + '.'):
76  name = prop.Name[len(baseName) + 1:]
77  val = PersistUtils.string2var(userProps.getPropertyValue(prop.Name))
78  if val is not None:
79  res[name] = val
80  return res
81 
82 def loadDocument(filePath, Hidden=True):
83 
84  url = uno.systemPathToFileUrl(filePath)
85 
86  if Hidden:
87  # start hidden
88  p = PropertyValue()
89  p.Name = "Hidden"
90  p.Value = True
91  parms = (p, )
92  else:
93  parms = ()
94 
95  try:
96  return LeenoUtils.getDesktop().loadComponentFromURL(url, "_blank", 0, parms)
97  except Exception:
98  return None
99 
100 def createSheetDocument(Hidden=True):
101 
102  if Hidden:
103  # start hidden
104  p = PropertyValue()
105  p.Name = "Hidden"
106  p.Value = True
107  parms = (p, )
108  else:
109  parms = ()
110 
111  # create an empty document
112  desktop = LeenoUtils.getDesktop()
113  pth = 'private:factory/scalc'
114  try:
115  return desktop.loadComponentFromURL(pth, '_default', 0, parms)
116  except Exception:
117  return None
DocUtils.loadDocument
def loadDocument(filePath, Hidden=True)
Definition: DocUtils.py:82
PersistUtils.string2var
def string2var(s)
Definition: PersistUtils.py:24
PersistUtils.var2string
def var2string(var)
Definition: PersistUtils.py:41
DocUtils.storeDataBlock
def storeDataBlock(oDoc, baseName, data)
Definition: DocUtils.py:56
DocUtils.removeDocUserDefinedAttribute
def removeDocUserDefinedAttribute(oDoc, name)
Definition: DocUtils.py:46
DocUtils.hasDocUserDefinedAttribute
def hasDocUserDefinedAttribute(oDoc, name)
Definition: DocUtils.py:43
LeenoUtils.getDesktop
def getDesktop()
Definition: LeenoUtils.py:59
DocUtils.setDocUserDefinedAttribute
def setDocUserDefinedAttribute(oDoc, name, value)
Definition: DocUtils.py:12
DocUtils.loadDataBlock
def loadDataBlock(oDoc, baseName)
Definition: DocUtils.py:65
DocUtils.getDocUserDefinedAttribute
def getDocUserDefinedAttribute(oDoc, name)
Definition: DocUtils.py:33
DocUtils.createSheetDocument
def createSheetDocument(Hidden=True)
Definition: DocUtils.py:100